| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestParsingParameters" -*- |
| 2 |
{ |
| 3 |
Copyright 2004-2023 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Test CastleParameters unit. } |
| 18 |
unit TestCastleParameters; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses Classes, SysUtils, |
| 23 |
CastleTester, CastleParameters, CastleUtils; |
| 24 |
|
| 25 |
type |
| 26 |
TParsedOption = record |
| 27 |
OptionNum: Integer; |
| 28 |
HasArgument: boolean; |
| 29 |
Argument: string; |
| 30 |
SeparateArgs: TSeparateArgs; |
| 31 |
end; |
| 32 |
PParsedOption = ^TParsedOption; |
| 33 |
TParsedOptionList = {$ifdef FPC}specialize{$endif} TStructList<TParsedOption>; |
| 34 |
|
| 35 |
TTestParsingParameters = class(TCastleTestCase) |
| 36 |
private |
| 37 |
procedure AssertParsEqual(const ParsValues: array of string; const ParsTestName: string); |
| 38 |
procedure AssertParsedParsEqual(const ParsedPars1: TParsedOptionList; |
| 39 |
const ParsedPars2: array of TParsedOption; const ParsTestName: string); |
| 40 |
published |
| 41 |
procedure TestParsingParameters; |
| 42 |
procedure TestOptionDescription; |
| 43 |
end; |
| 44 |
|
| 45 |
implementation |
| 46 |
|
| 47 |
uses CastleStringUtils, CastleLog; |
| 48 |
|
| 49 |
procedure ParseNextParam(OptionNum: Integer; HasArgument: boolean; |
| 50 |
const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer); |
| 51 |
var |
| 52 |
ParsedArray: TParsedOptionList absolute Data; |
| 53 |
LastItem: PParsedOption; |
| 54 |
begin |
| 55 |
LastItem := PParsedOption(ParsedArray.Add); |
| 56 |
LastItem^.OptionNum := OptionNum; |
| 57 |
LastItem^.HasArgument := HasArgument; |
| 58 |
LastItem^.Argument := Argument; |
| 59 |
LastItem^.SeparateArgs := SeparateArgs; |
| 60 |
end; |
| 61 |
|
| 62 |
{ Parse command-line parameters returning a list of parsed options. |
| 63 |
Works exactly like previous TParameters.Parse procedure, |
| 64 |
but instead of using a callback like OptionProc, this time |
| 65 |
it returns a list. |
| 66 |
@groupBegin } |
| 67 |
function ParseParameters( |
| 68 |
Options: POption_Array; OptionsCount: Integer; |
| 69 |
ParseOnlyKnownOptions: boolean = false): TParsedOptionList; overload; |
| 70 |
begin |
| 71 |
result := TParsedOptionList.Create; |
| 72 |
try |
| 73 |
Parameters.Parse(Options, OptionsCount, |
| 74 |
{$ifdef FPC}@{$endif}ParseNextParam, result, |
| 75 |
ParseOnlyKnownOptions); |
| 76 |
except result.Free; raise end; |
| 77 |
end; |
| 78 |
|
| 79 |
function ParseParameters( |
| 80 |
const Options: array of TOption; ParseOnlyKnownOptions: boolean = false): TParsedOptionList; overload; |
| 81 |
begin |
| 82 |
result := ParseParameters(@Options, High(Options) + 1, ParseOnlyKnownOptions); |
| 83 |
end; |
| 84 |
|
| 85 |
procedure TTestParsingParameters.AssertParsEqual(const ParsValues: array of string; |
| 86 |
const ParsTestName: string); |
| 87 |
var |
| 88 |
I: Integer; |
| 89 |
begin |
| 90 |
AssertEquals('Parameters Count at ' + ParsTestName, High(ParsValues), Parameters.High); |
| 91 |
for I := 0 to Parameters.High do |
| 92 |
AssertEquals('Parameters Values at ' + ParsTestName, Parameters[I], ParsValues[I]); |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TTestParsingParameters.AssertParsedParsEqual(const ParsedPars1: TParsedOptionList; |
| 96 |
const ParsedPars2: array of TParsedOption; const ParsTestName: string); |
| 97 |
var |
| 98 |
I, J: Integer; |
| 99 |
begin |
| 100 |
AssertEquals('Count at ' + ParsTestName, High(ParsedPars2), ParsedPars1.Count - 1); |
| 101 |
for I := 0 to ParsedPars1.Count - 1 do |
| 102 |
begin |
| 103 |
AssertEquals('OptionNum at ' + ParsTestName, ParsedPars1.L[I].OptionNum , ParsedPars2[I].OptionNum ); |
| 104 |
AssertEquals('HasArgument at ' + ParsTestName, ParsedPars1.L[I].HasArgument, ParsedPars2[I].HasArgument); |
| 105 |
AssertEquals('Argument at ' + ParsTestName, ParsedPars1.L[I].Argument , ParsedPars2[I].Argument ); |
| 106 |
for J := Low(TSeparateArgs) to High(TSeparateArgs) do |
| 107 |
AssertEquals('SeparateArgs at ' + ParsTestName, ParsedPars1.L[I].SeparateArgs[J], ParsedPars2[I].SeparateArgs[J]); |
| 108 |
end; |
| 109 |
end; |
| 110 |
|
| 111 |
procedure TTestParsingParameters.TestParsingParameters; |
| 112 |
|
| 113 |
function DynParsedOptionArrayToStr(const Name: string; |
| 114 |
V: TParsedOptionList): string; |
| 115 |
var |
| 116 |
I: Integer; |
| 117 |
begin |
| 118 |
Result := Name + NL; |
| 119 |
for I := 0 to v.Count - 1 do |
| 120 |
Result := Result + Format(' [%d] OptionNum %d, HasArg %s, Argument "%s"', [ |
| 121 |
I, |
| 122 |
v.L[I].OptionNum, |
| 123 |
BoolToStr(v.L[I].HasArgument, true), |
| 124 |
v.L[I].Argument |
| 125 |
]) + NL; |
| 126 |
end; |
| 127 |
|
| 128 |
function ParsToStr: String; |
| 129 |
var |
| 130 |
I: Integer; |
| 131 |
begin |
| 132 |
Result := 'Params now = ' + NL; |
| 133 |
for I := 0 to Parameters.High do |
| 134 |
Result := Result + Format(' ParStr(%d) = "%s"', [I, Parameters[I]]) + NL; |
| 135 |
end; |
| 136 |
|
| 137 |
procedure CheckPars(const TestName: string; const StartPars: array of string; |
| 138 |
const Options: array of TOption; |
| 139 |
const GoodAnswer: array of TParsedOption; const GoodRest: array of string; |
| 140 |
ParseOnlyKnownOptions: boolean); |
| 141 |
var |
| 142 |
Answer: TParsedOptionList; |
| 143 |
begin |
| 144 |
Parameters.AssignArray(StartPars); |
| 145 |
AssertParsEqual(StartPars, TestName); |
| 146 |
|
| 147 |
try |
| 148 |
Answer := ParseParameters(Options, ParseOnlyKnownOptions); |
| 149 |
except |
| 150 |
Fail('TestParsingParameters:' + TestName + ': Failed, unexpected exception'); |
| 151 |
raise; |
| 152 |
end; |
| 153 |
|
| 154 |
try |
| 155 |
try |
| 156 |
AssertParsedParsEqual(Answer, GoodAnswer, TestName); |
| 157 |
AssertParsEqual(GoodRest, TestName); |
| 158 |
except |
| 159 |
Fail('TestParsingParameters:' + TestName + ': Failed on testcase:' + NL + |
| 160 |
DynParsedOptionArrayToStr('Answer', Answer) + NL + |
| 161 |
ParsToStr |
| 162 |
); |
| 163 |
raise; |
| 164 |
end; |
| 165 |
finally Answer.Free end; |
| 166 |
end; |
| 167 |
|
| 168 |
procedure CheckParsFail(TestName: string; const StartPars: array of string; |
| 169 |
const Options: array of TOption; EClass: ExceptClass; const EMessage: string); |
| 170 |
var |
| 171 |
Answer: TParsedOptionList; |
| 172 |
begin |
| 173 |
Parameters.AssignArray(StartPars); |
| 174 |
AssertParsEqual(StartPars, TestName); |
| 175 |
|
| 176 |
try |
| 177 |
Answer := ParseParameters(Options); |
| 178 |
try |
| 179 |
Fail('TestParsingParameters:' + TestName + ': Should have raised an exception, but succeeded: ' + NL + |
| 180 |
DynParsedOptionArrayToStr('Answer', Answer) + NL + |
| 181 |
ParsToStr |
| 182 |
); |
| 183 |
finally Answer.Free end; |
| 184 |
except |
| 185 |
on E: Exception do |
| 186 |
begin |
| 187 |
if not (E is EClass) then |
| 188 |
raise { exception classes other than EClass are left to crash the test. } |
| 189 |
else |
| 190 |
if EMessage = '' then |
| 191 |
WriteLnLog('TestParsingParameters:' + TestName + ': OK, exception as expected. Any message allowed. Current message: ' + E.Message) |
| 192 |
else |
| 193 |
AssertEquals(EMessage, E.Message); |
| 194 |
//AssertEquals(EMessage, E.Message, 'TestParsingParameters:' + TestName + ': Exception as expected, but invalid message'); |
| 195 |
end; |
| 196 |
end; |
| 197 |
end; |
| 198 |
|
| 199 |
const |
| 200 |
{ Pars1_2 ------------------------------------------------------------ } |
| 201 |
|
| 202 |
Pars1_2Question: array[0..4]of string = |
| 203 |
('--zero-niewazne', 'ala', '--mama', 'teresa', '--kot-kocur=cygan'); |
| 204 |
|
| 205 |
Pars1: array[0..3]of TOption = ( |
| 206 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 207 |
(Short:#0; Long:'kot'; Argument: oaOptional), |
| 208 |
(Short:#0; Long:'kot-kocur'; Argument: oaOptional), |
| 209 |
(Short:'a'; Long:'ala'; Argument: oaNone) |
| 210 |
); |
| 211 |
Pars1Answer: array[0..1]of TParsedOption = ( |
| 212 |
(OptionNum:0; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 213 |
(OptionNum:2; HasArgument: true; Argument:'cygan'; SeparateArgs: ('','','', '','','', '','','')) |
| 214 |
); |
| 215 |
Pars1Rest: array[0..2]of string = ('--zero-niewazne', 'ala', 'teresa'); |
| 216 |
|
| 217 |
Pars2: array[0..2]of TOption = ( |
| 218 |
(Short:'m'; Long:'mama'; Argument: oaRequired), |
| 219 |
(Short:#0; Long:'kot-kocur'; Argument: oaOptional), |
| 220 |
(Short:'a'; Long:'ala'; Argument: oaNone) |
| 221 |
); |
| 222 |
Pars2Answer: array[0..1]of TParsedOption = ( |
| 223 |
(OptionNum:0; HasArgument: true; Argument:'teresa'; SeparateArgs: ('','','', '','','', '','','')), |
| 224 |
(OptionNum:1; HasArgument: true; Argument:'cygan'; SeparateArgs: ('','','', '','','', '','','')) |
| 225 |
); |
| 226 |
Pars2Rest: array[0..1]of string = ('--zero-niewazne', 'ala'); |
| 227 |
|
| 228 |
{ Pars3 ------------------------------------------------------------ } |
| 229 |
|
| 230 |
Pars3Question: array[0..8]of string = |
| 231 |
('-l=no', 'ala', '-l', '-l=foo nie=l', '--mama', '--', 'teresa', '--kot=cygan', '--'); |
| 232 |
|
| 233 |
Pars3: array[0..3]of TOption = ( |
| 234 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 235 |
(Short:#0; Long:'kot'; Argument: oaOptional), |
| 236 |
(Short:'a'; Long:'ala'; Argument: oaNone), |
| 237 |
(Short:'l'; Long:'lollobrygida'; Argument: oaOptional) |
| 238 |
); |
| 239 |
Pars3Answer: array[0..2]of TParsedOption = ( |
| 240 |
(OptionNum:3; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 241 |
(OptionNum:3; HasArgument: true; Argument:'foo nie=l'; SeparateArgs: ('','','', '','','', '','','')), |
| 242 |
(OptionNum:0; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')) |
| 243 |
); |
| 244 |
Pars3Rest: array[0..4]of string = ('-l=no', 'ala', 'teresa', '--kot=cygan', '--'); |
| 245 |
|
| 246 |
{ Pars4_5 ------------------------------------------------------------ } |
| 247 |
|
| 248 |
Pars4_5Question: array[0..2]of string = ('-nic', '--ala spacja', '--kot'); |
| 249 |
Pars45Rest: array[0..0]of string = ('-nic'); |
| 250 |
|
| 251 |
Pars4: array[0..2]of TOption = ( |
| 252 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 253 |
(Short:#0; Long:'kot'; Argument: oaOptional), |
| 254 |
(Short:'a'; Long:'ala spacja'; Argument: oaNone) |
| 255 |
); |
| 256 |
Pars4Answer: array[0..1]of TParsedOption = ( |
| 257 |
(OptionNum:2; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 258 |
(OptionNum:1; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')) |
| 259 |
); |
| 260 |
|
| 261 |
Pars5: array[0..2]of TOption = ( |
| 262 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 263 |
(Short:#0; Long:'kot'; Argument: oaOptional), |
| 264 |
(Short:'a'; Long:'ala spacja'; Argument: oaRequired) |
| 265 |
); |
| 266 |
Pars5Answer: array[0..0]of TParsedOption = ( |
| 267 |
(OptionNum:2; HasArgument: true; Argument:'--kot'; SeparateArgs: ('','','', '','','', '','','')) |
| 268 |
); |
| 269 |
|
| 270 |
{ Pars6 ------------------------------------------------------------ } |
| 271 |
|
| 272 |
Pars6Question: array[0..4]of string = ('--zero-niewazne', '--ala spacja', '-ma', '--kot', '-m'); |
| 273 |
Pars6: array[0..1]of TOption = ( |
| 274 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 275 |
(Short:'a'; Long:'ala spacja'; Argument: oaNone) |
| 276 |
); |
| 277 |
|
| 278 |
{ '--kot' powinien zostac zignorowany jesli uruchomimy z ParseOnlyKnownOptions. |
| 279 |
Podobnie '-ma' (mimo ze -m i -a są znane to są combined short option, |
| 280 |
not supported when ParseOnlyKnownOptions). |
| 281 |
Jednak '--ala spacja' powinno zostac sparsowane i usuniete, oraz '-m'. } |
| 282 |
Pars6_OnlyKnown_Answer: array[0..1]of TParsedOption = ( |
| 283 |
(OptionNum:1; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 284 |
(OptionNum:0; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')) |
| 285 |
); |
| 286 |
Pars6_OnlyKnown_Rest: array[0..2]of string = |
| 287 |
('--zero-niewazne', '-ma', '--kot'); |
| 288 |
|
| 289 |
{ Pars7 ------------------------------------------------------------ } |
| 290 |
|
| 291 |
Pars7Question: array[0..2]of string = ('zero', '-a', '-k'); |
| 292 |
Pars7: array[0..1]of TOption = ( |
| 293 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 294 |
(Short:'a'; Long:'ala spacja'; Argument: oaNone) |
| 295 |
); |
| 296 |
|
| 297 |
{ Pars8 ------------------------------------------------------------ } |
| 298 |
|
| 299 |
Pars8Question: array[0..2]of string = ('hehe', '--=ala spacja', '--kot'); |
| 300 |
Pars8: array[0..1]of TOption = ( |
| 301 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 302 |
(Short:'a'; Long:'ala spacja'; Argument: oaRequired) |
| 303 |
); |
| 304 |
|
| 305 |
{ Pars9 ------------------------------------------------------------ } |
| 306 |
|
| 307 |
Pars9Question: array[0..2]of string = ('foo', 'blah', '--kot'); |
| 308 |
Pars9: array[0..1]of TOption = ( |
| 309 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 310 |
(Short:'k'; Long:'kot'; Argument: oaRequired) |
| 311 |
); |
| 312 |
|
| 313 |
{ Pars10 ------------------------------------------------------------ } |
| 314 |
|
| 315 |
Pars10Question: array[0..0]of string = ('--kot=blah'); |
| 316 |
Pars10: array[0..1]of TOption = ( |
| 317 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 318 |
(Short:'k'; Long:'kot'; Argument: oaNone) |
| 319 |
); |
| 320 |
Pars10Rest: array[0..0]of string = ('--kot=blah'); |
| 321 |
|
| 322 |
{ Pars11 ------------------------------------------------------------ } |
| 323 |
|
| 324 |
Pars11Question: array[0..1]of string = ('ble', '--kot=blah'); |
| 325 |
Pars11: array[0..1]of TOption = ( |
| 326 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 327 |
(Short:'k'; Long:'kot'; Argument: oaNone) |
| 328 |
); |
| 329 |
|
| 330 |
{ Pars12 ------------------------------------------------------------ } |
| 331 |
|
| 332 |
Pars12Question: array[0..1]of string = ('ble', '-=blah'); |
| 333 |
Pars12: array[0..1]of TOption = ( |
| 334 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 335 |
(Short:'k'; Long:'kot'; Argument: oaNone) |
| 336 |
); |
| 337 |
|
| 338 |
{ Pars13 ------------------------------------------------------------ } |
| 339 |
|
| 340 |
Pars13Question: array[0..4]of string = ('--ble', '--ble', '1 ', '2', '3'); |
| 341 |
Pars13: array[0..2]of TOption = ( |
| 342 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 343 |
(Short:'k'; Long:'kot'; Argument: oaNone), |
| 344 |
(Short:'b'; Long:'ble'; Argument: oaRequired2Separate) |
| 345 |
); |
| 346 |
Pars13Answer: array[0..0]of TParsedOption = ( |
| 347 |
(OptionNum:2; HasArgument: false; Argument:''; SeparateArgs:('1 ','2','', '','','', '','','')) |
| 348 |
); |
| 349 |
Pars13Rest: array[0..1]of string = ('--ble', '3'); |
| 350 |
|
| 351 |
{ Pars14 ------------------------------------------------------------ } |
| 352 |
|
| 353 |
Pars14Question: array[0..4]of string = ('--ble', '--ble', '1 ', '2', '3'); |
| 354 |
Pars14: array[0..2]of TOption = ( |
| 355 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 356 |
(Short:'k'; Long:'kot'; Argument: oaNone), |
| 357 |
(Short:'b'; Long:'ble'; Argument: oaRequired3Separate) |
| 358 |
); |
| 359 |
Pars14Answer: array[0..0]of TParsedOption = ( |
| 360 |
(OptionNum:2; HasArgument: false; Argument:''; SeparateArgs:('1 ', '2', '3', '','','', '','','')) |
| 361 |
); |
| 362 |
Pars14Rest: array[0..0]of string = ('--ble'); |
| 363 |
|
| 364 |
{ Pars15 ------------------------------------------------------------ } |
| 365 |
|
| 366 |
Pars15Question: array[0..4]of string = ('--ble', '--ble', '1 ', '2', '3'); |
| 367 |
Pars15: array[0..2]of TOption = ( |
| 368 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 369 |
(Short:'k'; Long:'kot'; Argument: oaNone), |
| 370 |
(Short:'b'; Long:'ble'; Argument: oaRequired4Separate) |
| 371 |
); |
| 372 |
|
| 373 |
{ Pars16 ------------------------------------------------------------ } |
| 374 |
|
| 375 |
{ '-' is not errorneous "empty short option", it is treated as usual |
| 376 |
non-option parameter. '' is harmless too. } |
| 377 |
Pars16Question: array[0..2]of string = ('ble', '-', ''); |
| 378 |
Pars16: array[0..0]of TOption = ( |
| 379 |
(Short:'b'; Long:'ble'; Argument: oaRequired3Separate) |
| 380 |
); |
| 381 |
Pars16Rest: array[0..2]of string = ('ble', '-', ''); |
| 382 |
|
| 383 |
{ Pars17 ------------------------------------------------------------ } |
| 384 |
|
| 385 |
{ #0 is not allowed as short option (Short = #0 means "no short form for this |
| 386 |
option exists") } |
| 387 |
Pars17Question: array[0..1]of string = ('--ble', '-'#0); |
| 388 |
Pars17: array[0..0]of TOption = ( |
| 389 |
(Short:#0; Long:'mama'; Argument: oaOptional) |
| 390 |
); |
| 391 |
|
| 392 |
{ Pars18 ------------------------------------------------------------- } |
| 393 |
|
| 394 |
{ combining short options works. Arguments for last option are allowed. } |
| 395 |
Pars18Question: array[0..4]of string = ('-zero', '-abc', '--rere', 'blabla', '-abc=foo'); |
| 396 |
Pars18: array[0..2]of TOption = ( |
| 397 |
(Short:'a'; Long:'ania'; Argument: oaOptional), |
| 398 |
(Short:'b'; Long:'basia'; Argument: oaOptional), |
| 399 |
(Short:'c'; Long:'cycek'; Argument: oaRequired) |
| 400 |
); |
| 401 |
Pars18Answer: array[0..5]of TParsedOption = ( |
| 402 |
(OptionNum:0; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 403 |
(OptionNum:1; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 404 |
(OptionNum:2; HasArgument: true; Argument:'--rere'; SeparateArgs: ('','','', '','','', '','','')), |
| 405 |
(OptionNum:0; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 406 |
(OptionNum:1; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 407 |
(OptionNum:2; HasArgument: true; Argument:'foo'; SeparateArgs: ('','','', '','','', '','','')) |
| 408 |
); |
| 409 |
Pars18Rest: array[0..1]of string = ('-zero', 'blabla'); |
| 410 |
|
| 411 |
{ Pars19 -------------------------------------------------------------- } |
| 412 |
|
| 413 |
{ specyfying argument for paReuired?Separate is not allowed } |
| 414 |
Pars19Question: array[0..3]of string = ('', '--baba=kobita', '1', '2'); |
| 415 |
Pars19: array[0..0]of TOption = ( |
| 416 |
(Short:'a'; Long:'baba'; Argument: oaRequired2Separate) |
| 417 |
); |
| 418 |
|
| 419 |
{ Pars20 ----------------------------------------------------------------- } |
| 420 |
|
| 421 |
{ ParseOnlyKnownOptions parses short options, although not combined ones. |
| 422 |
Leaves the rest (unknown options (long and short), and combined short options |
| 423 |
(regardless if known or not)). |
| 424 |
Honors also -- and doesn't interpret things afterwards, although doesn't remove --. } |
| 425 |
Pars20Question: array[0..8]of string = ('--bar', '-sb=xyz', '--ble=xyz', '-s', '--1', '2', '--foo', '--', '--1'); |
| 426 |
Pars20: array[0..3]of TOption = ( |
| 427 |
(Short:'m'; Long:'mama'; Argument: oaOptional), |
| 428 |
(Short:'k'; Long:'1'; Argument: oaRequired), |
| 429 |
(Short:'b'; Long:'ble'; Argument: oaRequired), |
| 430 |
(Short:'s'; Long:'something'; Argument: oaNone) |
| 431 |
); |
| 432 |
Pars20_OnlyKnown_Answer: array[0..2]of TParsedOption = ( |
| 433 |
(OptionNum:2; HasArgument: true; Argument:'xyz'; SeparateArgs: ('','','', '','','', '','','')), |
| 434 |
(OptionNum:3; HasArgument: false; Argument:''; SeparateArgs: ('','','', '','','', '','','')), |
| 435 |
(OptionNum:1; HasArgument: true; Argument:'2'; SeparateArgs: ('','','', '','','', '','','')) |
| 436 |
); |
| 437 |
Pars20_OnlyKnown_Rest: array[0..4]of string = ('--bar', '-sb=xyz', '--foo', '--', '--1'); |
| 438 |
|
| 439 |
{ Pars21 ----------------------------------------------------------------- } |
| 440 |
|
| 441 |
Pars21Question: array[0..2]of string = |
| 442 |
('glplotter.exe', '--grid-custom', '12'); |
| 443 |
Pars21: array[0..3] of TOption = ( |
| 444 |
(Short:'h'; Long:'help'; Argument: oaNone), |
| 445 |
(Short:#0; Long:'grid-custom'; Argument: oaRequired), |
| 446 |
(Short:#0; Long:'light'; Argument: oaNone), |
| 447 |
(Short:#0; Long:'dark'; Argument: oaNone) |
| 448 |
); |
| 449 |
Pars21Answer: array[0..0]of TParsedOption = ( |
| 450 |
(OptionNum:1; HasArgument: true; Argument:'12'; SeparateArgs: ('','','', '','','', '','','')) |
| 451 |
); |
| 452 |
Pars21Rest: array[0..0]of string = ('glplotter.exe'); |
| 453 |
|
| 454 |
begin |
| 455 |
CheckPars('1', Pars1_2Question, Pars1, Pars1Answer, Pars1Rest, false); |
| 456 |
CheckPars('2', Pars1_2Question, Pars2, Pars2Answer, Pars2Rest, false); |
| 457 |
CheckPars('3', Pars3Question, Pars3, Pars3Answer, Pars3Rest, false); |
| 458 |
CheckPars('4', Pars4_5Question, Pars4, Pars4Answer, Pars45Rest, false); |
| 459 |
CheckPars('5', Pars4_5Question, Pars5, Pars5Answer, Pars45Rest, false); |
| 460 |
|
| 461 |
CheckParsFail('6', Pars6Question, Pars6, EInvalidLongOption, 'Invalid long option "--kot"'); |
| 462 |
CheckPars('6_OnlyKnown', Pars6Question, Pars6, Pars6_OnlyKnown_Answer, Pars6_OnlyKnown_Rest, true); |
| 463 |
CheckParsFail('7', Pars7Question, Pars7, EInvalidShortOption, 'Invalid short option character "k" in parameter "-k"'); |
| 464 |
CheckParsFail('8', Pars8Question, Pars8, EInvalidParams, 'Invalid empty parameter "--=ala spacja"'); |
| 465 |
CheckParsFail('9', Pars9Question, Pars9, EMissingOptionArgument, 'Missing argument for option --kot'); |
| 466 |
CheckPars('10', Pars10Question, Pars10, [], Pars10Rest, false); |
| 467 |
CheckParsFail('11', Pars11Question, Pars11, EExcessiveOptionArgument, 'Excessive argument for option --kot'); |
| 468 |
CheckParsFail('12', Pars12Question, Pars12, EInvalidParams, 'Invalid empty parameter "-=blah"'); |
| 469 |
|
| 470 |
CheckPars('13', Pars13Question, Pars13, Pars13Answer, Pars13Rest, false); |
| 471 |
CheckPars('14', Pars14Question, Pars14, Pars14Answer, Pars14Rest, false); |
| 472 |
CheckParsFail('15', Pars15Question, Pars15, EMissingOptionArgument, 'Not enough arguments for option --ble, this option needs 4 arguments but we have only 3'); |
| 473 |
CheckPars('16', Pars16Question, Pars16, [], Pars16Rest, false); |
| 474 |
CheckParsFail('17', Pars17Question, Pars17, EInvalidShortOption, 'Invalid short option character "#0 (null char)" in parameter "-'#0'"'); |
| 475 |
CheckPars('18', Pars18Question, Pars18, Pars18Answer, Pars18Rest, false); |
| 476 |
CheckParsFail('19', Pars19Question, Pars19, EExcessiveOptionArgument, 'Option --baba requires 2 arguments, you cannot give them using the form --option=argument, you must give all the arguments as separate parameters'); |
| 477 |
CheckPars('20_OnlyKnown', Pars20Question, Pars20, Pars20_OnlyKnown_Answer, Pars20_OnlyKnown_Rest, true); |
| 478 |
CheckPars('21', Pars21Question, Pars21, Pars21Answer, Pars21Rest, false); |
| 479 |
end; |
| 480 |
|
| 481 |
procedure TTestParsingParameters.TestOptionDescription; |
| 482 |
begin |
| 483 |
AssertEquals( |
| 484 |
' --fast Do not "clean"' + NL + |
| 485 |
' before "package". Recompile only what changed. This' + NL + |
| 486 |
' is faster for development, but cannot guarantee' + NL + |
| 487 |
' that everything is recompiled in a release mode.', |
| 488 |
OptionDescription('--fast', |
| 489 |
'Do not "clean"' + NL + 'before "package". Recompile only what changed. This is faster for development, but cannot guarantee that everything is recompiled in a release mode.') |
| 490 |
); |
| 491 |
|
| 492 |
AssertEquals( |
| 493 |
' --fast Do not "clean" before "package". Recompile only' + NL + |
| 494 |
' what changed. This is faster for development, but' + NL + |
| 495 |
' cannot guarantee that everything is recompiled in a' + NL + |
| 496 |
' release mode.', |
| 497 |
OptionDescription('--fast', |
| 498 |
'Do not "clean" before "package". Recompile only what changed. This is faster for development, but cannot guarantee that everything is recompiled in a release mode.') |
| 499 |
); |
| 500 |
end; |
| 501 |
|
| 502 |
initialization |
| 503 |
RegisterTest(TTestParsingParameters); |
| 504 |
end. |