/usr/src/castle-game-engine-5.2.0/base/castleparameters.pas is in castle-game-engine-src 5.2.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | {
Copyright 2002-2014 Michalis Kamburelis.
This file is part of "Castle Game Engine".
"Castle Game Engine" is free software; see the file COPYING.txt,
included in this distribution, for details about the copyright.
"Castle Game Engine" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
----------------------------------------------------------------------------
}
{ Processing command-line parameters and options (TParameters,
single instance Parameters). }
unit CastleParameters;
{$I castleconf.inc}
interface
uses SysUtils, CastleVectors, CastleUtils, CastleStringUtils;
type
EInvalidParams = class(EWithHiddenClassName);
type
EInvalidShortOption = class(EInvalidParams);
EInvalidLongOption = class(EInvalidParams);
EWrongOptionArgument = class(EInvalidParams);
EExcessiveOptionArgument = class(EWrongOptionArgument);
EMissingOptionArgument = class(EWrongOptionArgument);
TOptionArgument = (
{ No arguments allowed. }
oaNone,
{ An optional argument. It must be given as @--option=argument
or (short form) -o=argument.
If you use a short form and you combine many short option names
into one parameter, then only the last option may have an argument.
For example @code(-abc=blah) is equivalent to @code(-a -b -c=blah). }
oaOptional,
{ A required argument. It must be given as for oaOptional,
but this time the equal sign is not needed (we know anyway that
following parameter must be an argument). So the following versions
are possible:
@preformatted(
--option=argument # long form, as one parameter
--option argument # long form, as two parameters
-o=argument # short form, as one parameter
-o argument # short form, as two parameters
) }
oaRequired,
{ We require a specified (more than one) argument.
All of the arguments must be specified as separate parameters, like
@preformatted(
--option Argument1 Argument2
-o Argument1 Argument2
) }
oaRequired2Separate,
oaRequired3Separate,
oaRequired4Separate,
oaRequired5Separate,
oaRequired6Separate,
oaRequired7Separate,
oaRequired8Separate,
oaRequired9Separate
);
TOptionArguments = set of TOptionArgument;
const
oaRequiredSeparateFirst = oaRequired2Separate;
oaRequiredSeparateLast = oaRequired9Separate;
RequiredSeparateFirstCount = 2;
RequiredSeparateLastCount = RequiredSeparateFirstCount
+ Ord(oaRequiredSeparateLast) - Ord(oaRequiredSeparateFirst);
OptionArgumentsRequiredSeparate: TOptionArguments =
[oaRequiredSeparateFirst .. oaRequiredSeparateLast];
type
TOptionSeparateArgument = oaRequiredSeparateFirst .. oaRequiredSeparateLast;
TSeparateArgs = array[1..RequiredSeparateLastCount]of string;
const
EmptySeparateArgs: TSeparateArgs = ('','','', '','','', '','','');
type
{ Callback used by TParameters.Parse to notify about new option.
@param(OptionNum The option number in the Options table (zero-based).)
@param(HasArgument Says if you have a single argument in the Argument
parameter. Always @false when your option has oaNone or
oaRequiredXSeparate. Always @true when your option has oaRequired.
For oaOptional, this is how you know if the optional argument was used.)
@param(Argument A single argument for oaRequired or oaOptional,
only if HasArgument. Otherwise empty string.)
@param(SeparateArgs For options using oaRequiredXSeparate,
your arguments are here. You get exactly as many argument
as your oaRequiredXSeparate requested, the rest of SeparateArgs
is empty strings.)
@param(Data This is the OptionProcData value you passed to TParameters.Parse,
use this to pass some pointer to your callback.)
}
TOptionProc = procedure (OptionNum: Integer; HasArgument: boolean;
const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer);
{ Command-line option specification, for TParameters.Parse.
Both Short and Long option names are case-sensitive.
The convention is to make Long option names using only lower-case letters,
separates by dashes, like @code(my-option-name).
Note that spaces are allowed (as Short option name, or within Long
option nam), but in practice should not be used as they are a pain
to pass for the users (you'd have to quote option names under most shells). }
TOption = record
{ Short option name. Use #0 if none. Cannot be '-' or '=' (these would
cause ambiguity when parsing options). }
Short: Char;
{ Long option name. Use '' if none. Cannot contain '=' (this would
cause ambiguity when parsing options). }
Long: string;
Argument: TOptionArgument;
end;
POption = ^TOption;
TOption_Array = array [0..MaxInt div SizeOf(TOption) - 1] of TOption;
POption_Array = ^TOption_Array;
{ Storing and processing command-line parameters and options.
For simple processing, you can just read values of this list,
checking count with comfortable methods like CheckHigh.
For more involved processing, use the Parse function,
that does a whole job for you based on a simple specification
of allowed options.
Some terminology:
@definitionList(
@itemLabel @italic(Parameter)
@item(Command-line parameters list is given directly by the OS to our
program. These are the contents of this list,
initialized from the standard Pascal ParamStr/ParamCount.
They can be modified to remove the already-handled parameters.)
@itemLabel @italic(Option)
@item(Options are things encoded by the user in the parameters.
Examples:
@unorderedList(
@item(Command-line
@preformatted( view3dscene --navigation Walk)
passes two parameters (@code(@--navigation) and @code(Walk))
for view3dscene, and these two parameters form one option:
@code(@--navigation=Walk).)
@item(Command-line
@preformatted( view3dscene -hv)
passes one parameter (@code(-hv)) for view3dscene,
and inside this parameter two options are encoded:
@code(-h) and @code(-v).)
)
The very idea of this unit is to decode "options" from the "parameters".
)
@itemLabel @italic(Argument)
@item(Argument is a part of the option, that clarifies what this option does.
For example in @code(@--navigation=Walk), "@code(Walk)" is the argument
and "@code(@--navigation)" is the option long name.
Some options don't take any arguments, some take optional argument,
some take required argument, some have a couple of arguments.
TOptionArgument type allows you to specify all this.)
)
For simple programs, you can directly parse command-line by looking at
our parameters strings. For more involved cases,
using @link(Parse) method has a lot of advantages:
@unorderedList(
@item(Less error-prone, and your program's code stays simple.)
@item(We automatically handle special parameter @-- that is a standard
way to mark the end of the options. (Useful for users that have filenames
that start with "-" character.))
@item(We automatically detect and make exceptions with nice messages
on various errors. For example unrecognized options are clearly
reported (so they will not mistaken for e.g. missing filenames
by your program).)
@item(We automatically allow combining of short options,
so user can use @code(-abc) instead of @code(-a -b -c).)
@item(We have a simple interface, where you simply specify what
options you want, long and short option names, option arguments
and such.)
)
See [http://castle-engine.sourceforge.net/common_options.php]
for a user description how short and long options are expected to be given
on the command-line. }
TParameters = class(TCastleStringList)
public
function High: Integer;
{ Does the number of parameters (High) satisfy given condition.
@raises EInvalidParams When High is wrong.
@groupBegin }
procedure CheckHigh(ParamValue: integer);
procedure CheckHighAtLeast(ParamValue: integer);
procedure CheckHighAtMost(ParamValue: integer);
{ @groupEnd }
{ Is one of given strings present on the parameters list.
Looks inside Strings[1..High], case sensitive. }
function IsPresent(const A: array of string): boolean;
{ Parse command-line parameters. Given a specification of your command-line
options (in Options), we will find and pass these options to your
OptionProc callback. The handled options will be removed from
the @link(Parameters) list.
After running this, you should treat the remaining @link(Parameters)
as "normal" parameters, usually a filenames to open by your program or such.
See also TOption for a specification of an option,
and see TOptionArgument for a specification of an option argument,
and see TOptionProc for a specification what your OptionProc callback gets.
@raises EInvalidShortOption On invalid (unknown) short option name.
@raises EInvalidLongOption On invalid long option name.
@raises(EExcessiveOptionArgument When an option gets too many arguments,
this may happen for options with oaNone or oaRequiredXSeparate
that are specified with @code(--option=argument) form.)
@raises(EMissingOptionArgument When an option gets too few arguments,
this may happen when argument for oaRequired option is missing,
or when too few arguments are given for oaRequiredXSeparate option.)
@raises(EInvalidParams On invalid parameter without an option,
like @code(-=argument) or @code(--=argument).)
Note that a single dash parameter is left alone, without making any
exceptions, as this is a standard way of telling "standard input"
or "standard output" for some programs.
Note that a double dash parameter @-- is handled and removed from
the @link(Parameters) list, and signals an end of options.
You should not modify @link(Parameters) list when this function
is running, in particular do not modify it from your OptionProc callback.
Also, do not depend on when the handled options are exactly removed
from the @link(Parameters) list (before or after OptionProc callback).
We never touch here the Strings[0] value, we look
only at the Strings[1] to Strings[High].
ParseOnlyKnownLongOptions = @true makes this procedure work a little
differently, it's designed to allow you to process @italic(some) long options
and leave the rest options not handled (without making any error):
@orderedList(
@item(All short options are ignored then.)
@item(All unknown long options are also ignored, without making any error.)
@item(The special @-- is handled (signals the end of options),
but it's not removed from the @link(Parameters).)
)
The ParseOnlyKnownLongOptions = @true is useful if you want to handle
some command-line options, but you still want to leave final options
parsing to a later code. For example TCastleWindowCustom.ParseParameters parses
some window parameters (like --geometry), leaving your program-specific
stuff in peace.
Note that ParseOnlyKnownLongOptions = @true isn't an absolutely
fool-proof solution, for example the command-line
@code(view3dscene --navigation --geometry 800x600 Walk) is actually invalid.
But we will handle it, by first detecting and removing @code(--geometry 800x600)
from TCastleWindowCustom.ParseParameters, and then detecting and removing
@code(--navigation Walk) from view3dscene code.
Basically, processing by Parse many times is not fool-proof
in some weird situations.
@groupBegin }
procedure Parse(Options: POption_Array; OptionsCount: Integer;
OptionProc: TOptionProc; OptionProcData: Pointer;
ParseOnlyKnownLongOptions: boolean = false); overload;
procedure Parse(const Options: array of TOption;
OptionProc: TOptionProc; OptionProcData: Pointer;
ParseOnlyKnownLongOptions: boolean = false); overload;
{ @groupEnd }
end;
function SeparateArgsToVector3Single(const v: TSeparateArgs): TVector3Single;
const
OnlyHelpOptions: array[0..0]of TOption = (
(Short: 'h'; Long: 'help'; Argument: oaNone)
);
HelpOptionHelp =
' -h / --help Print this help message and exit.';
VersionOptionHelp =
' -v / --version Print the version number and exit.';
var
{ Command-line parameters. Initialized from standard
ParamStr(0) ... ParamStr(ParamCount). Can be later modified,
which is good --- you can remove handled parameters.
You also have all the methods of TCastleStringList class
(e.g. you can assign to another TCastleStringList instance). }
Parameters: TParameters;
implementation
function OptionSeparateArgumentToCount(const v: TOptionSeparateArgument): Integer; forward;
{ TParameters ---------------------------------------------------------------- }
function ParametersCountString(Count: Integer; const MiddleStr: string): string; overload;
begin
result := IntToStr(Count);
if Count = 1 then
result := result +MiddleStr +' parameter' else
result := result +MiddleStr +' parameters';
end;
function ParametersCountString(Count: Integer): string; overload;
begin
result := ParametersCountString(Count, '');
end;
{ Since we can modify Parameters, we can't really output
in CheckHigh* for user how many parameters were excepted (because you maybe
ate some). Output only how many params are missing/too much. }
function TParameters.High: Integer;
begin
Result := Count - 1;
end;
procedure TParameters.CheckHigh(ParamValue: integer);
begin
if ParamValue <> High then
begin
if ParamValue < High then
raise EInvalidParams.Create('Expected ' +
ParametersCountString(High-ParamValue, ' less')) else
raise EInvalidParams.Create('Expected ' +
ParametersCountString(ParamValue-High, ' more'));
end;
end;
procedure TParameters.CheckHighAtLeast(ParamValue: integer);
begin
if ParamValue > High then
raise EInvalidParams.Create('Expected ' +
ParametersCountString(ParamValue-High, ' more'));
end;
procedure TParameters.CheckHighAtMost(ParamValue: integer);
begin
if ParamValue < High then
raise EInvalidParams.Create('Expected ' +
ParametersCountString(High-ParamValue, ' less'));
end;
function TParameters.IsPresent(const A: array of string): boolean;
var
I, J: Integer;
begin
for I := 1 to High do
for J := 0 to System.High(A) do
if Strings[I] = A[J] then
Exit(true);
Result := false;
end;
procedure TParameters.Parse(const Options: array of TOption; OptionProc: TOptionProc;
OptionProcData: Pointer; ParseOnlyKnownLongOptions: boolean);
begin
Parse(@Options, System.High(Options)+1, OptionProc, OptionProcData,
ParseOnlyKnownLongOptions);
end;
procedure SplitLongParameter(const s: string; out ParamLong: string;
out HasArgument: boolean; out Argument: string; PrefixLength: Integer);
{ zadany s musi sie zaczynac od PrefixLength znakow ktore sa ignorowane
(dla "prawdziwej" long option z definicji TParameters.Parse PrefixLength musi byc
2 i musza one byc rowne '--').
Rozbija parametr na nazwe parametru (nie zawierajaca znaku '=', rozna od '',
bedzie wyjatek EInvalidParams w tym rzadkim przypadku gdy
s[PrefixLength+1] = '=' lub gdy string sie konczy po PrefixLength znakach)
i Argument, tzn. jezeli s nie zawieral znaku '=' zwraca HasArgument
=false i Argument = '', wpp. ParamLong to czesc zawarta pomiedzy '--' a '=',
HasArgument = true, Argument to czesc za pierwszyn znakiem '='
(w ten sposob sam Argument moze bez problemu zawierac znak '=').
Przyklady:
s = '--long-option' ->
ParamLong = 'long-option', HasArgument = false, Argument = ''
s = '--long-option=arg' ->
ParamLong = 'long-option', HasArgument = true, Argument = 'arg'
s = '--' ->
EInvalidParams
s = '--=arg' ->
EInvalidParams
}
var p: Integer;
begin
p := Pos('=', s);
HasArgument := p <> 0;
if HasArgument then
begin
ParamLong := CopyPos(s, PrefixLength+1, p-1);
Argument := SEnding(s, p+1);
end else
begin
ParamLong := SEnding(s, PrefixLength+1);
Argument := '';
end;
if ParamLong = '' then
raise EInvalidParams.Create('Invalid empty parameter "'+s+'"');
end;
procedure TParameters.Parse(
Options: POption_Array; OptionsCount: Integer; OptionProc: TOptionProc;
OptionProcData: Pointer; ParseOnlyKnownLongOptions: boolean);
function ParseLongParameter(const s: string; out HasArgument: boolean;
out Argument: string): Integer;
{ s jest jakims parametrem ktory zaczyna sie od '--' i nie jest rowny '--'.
Wyciaga z s-a opcje jaka reprezentuje (i zwraca jej numer w Params,
zero-based), wyciaga tez zapisany razem z nia parametr i zwraca
HasArgument i Argument (pamietaj ze wyciaga tylko argumenty dolaczone
do opcji przy pomocy znaku "="; nie sprawdza tez w ogole czy HasArgument
w jakis sposob zgadza sie z Options[result].Argument.).
Jezeli ParseOnlyKnownLongOptions to moze zwrocic -1 aby zaznaczyc ze
ten parametr nie reprezentuje zadnej znanej opcji (chociaz ciagle
nieprawidlowe postacie w rodzaju --=argument czy --non-arg-option=argument
beda oczywiscie powodowaly wyjatek.) }
var ParamLong: string;
i: Integer;
begin
SplitLongParameter(s, ParamLong, HasArgument, Argument, 2);
for i := 0 to OptionsCount-1 do
if Options^[i].Long = ParamLong then
begin result := i; Exit; end;
if ParseOnlyKnownLongOptions then
result := -1 else
raise EInvalidLongOption.Create('Invalid long option "'+s+'"');
end;
function FindShortOption(c: char; const Parameter: string): Integer;
{ znajdz takie i ze Options[i].Short = c (i c <> #0).
Jesli sie nie uda - wyjatek EInvalidshortOption.
Parametr "Parameter" jest nam potrzebny
_tylko_ zeby skomponowac ladniejszy (wiecej mowiacy) Message wyjatku,
podany Parameter powinien byc parametrem w ktorym znalezlismy literke c. }
const
SInvalidShortOpt = 'Invalid short option character "%s" in parameter "%s"';
begin
if c = #0 then
raise EInvalidShortOption.CreateFmt(SInvalidShortOpt, ['#0 (null char)', Parameter]);
for result := 0 to OptionsCount-1 do
if Options^[result].Short = c then Exit;
raise EInvalidShortOption.CreateFmt(SInvalidShortOpt, [c, Parameter]);
end;
function ParseShortParameter(const s: string; var HasArgument: boolean;
var Argument: string; SimpleShortOptions: TIntegerList): Integer;
{ s jest jakims parametrem zaczynajacym sie od '-' i nie bedacym '-'.
Dziala tak jak ParseLongParameter tyle ze nigdy nie zwraca -1
(podany s MUSI zawierac znany parametr).
Ponadto do SimpleShortOptions dopisze ciag prostych opcji ktore zostaly
podane razem z ostatnia opcja (czyli z opcja zwracana pod nazwa).
Te proste opcje zostaly "skombinowane" razem z ostatnia opcja w
jednym parametrze. W rezultacie nazywam je "prostymi" bo one nie moga
miec argumentu - Options^[].Argument tych opcji moze byc tylko oaNone
lub oaOptional. Ta procedura NIE sprawdza ze to sie zgadza
tak jak w ogole nie sprawdza zadnego Options^[].Argument, takze
dla ostatniej (zwracanej pod nazwa) opcji nie sprawdza - moze wiec
zwrocic opcje oaNone z HasArgument albo oaRequired[*Separate] z
not HasArgument.
Ten kto uzywa tej opcji musi sprawdzic czy HasArgument ma sens ze
zwrocona opcja. W przypadku oaRequired[*Separate] moze/musi odczytac
dalsze parametry zeby postac argument/argumenty opcji.
Zasada jest taka ze ta procedura zajmuje sie TYLKO parametrem s.
Ona nie wchodzi na inne Strings[], zreszta w ogole nie wie dla jakiego
I zachodzi Strings[I] = s.
}
var ParamShortStr: string;
i: Integer;
begin
{ calculate ParamShortStr, HasArgument, Argument }
SplitLongParameter(s, ParamShortStr, HasArgument, Argument, 1);
{ add to SimpleShortOptions }
for i := 1 to Length(ParamShortStr)-1 do
SimpleShortOptions.Add( FindShortOption(ParamShortStr[i], s) );
{ calculate result }
result := FindShortOption(ParamShortStr[Length(ParamShortStr)], s);
end;
var i, j, k, OptionNum: Integer;
HasArgument: boolean;
Argument, OptionName: string;
SeparateArgs: TSeparateArgs;
SimpleShortOptions: TIntegerList;
begin
i := 1;
SimpleShortOptions := TIntegerList.Create;
try
while i <= High do
begin
if Strings[i] = '--' then
begin
if not ParseOnlyKnownLongOptions then Delete(I);
Break
end;
Assert(SimpleShortOptions.Count = 0);
{ calculate OptionNum; Ustaw je na numer w Params jezeli Strings[i] to opcja
(w tym przypadku musisz tez ustalic OptionName), wpp. (jesli to nie opcja
i mozemy ja pominac) ustal OptionNum na -1.
Warunek Length(Strings[i]) > 1 w linijce ponizej gwarantuje nam
ze parametr '-' uznamy za nie-opcje (zamiast np. powodowac wyjatek
"empty option") }
OptionNum := -1;
if SCharIs(Strings[i], 1, '-') and (Length(Strings[i]) > 1) then
begin
if SCharIs(Strings[i], 2, '-') then
begin
OptionNum := ParseLongParameter(Strings[i], HasArgument, Argument);
if OptionNum <> -1 then OptionName := '--'+Options^[OptionNum].Long;
end else
if not ParseOnlyKnownLongOptions then
begin
OptionNum := ParseShortParameter(Strings[i], HasArgument, Argument, SimpleShortOptions);
OptionName := '-'+Options^[OptionNum].Short;
end;
end;
{ OptionNum = -1 oznacza ze z jakiegos powodu Strings[i] jednak NIE przedstawia
soba zadnej opcji i powinnismy postepowac dalej jakby Strings[i] byl
normalnym parametrem, nie-opcja. W praktyce bylo nam to potrzebne
bo gdy ParseOnlyKnownLongOptions = true to fakt ze chcemy dany Strings[i]
mozemy czasem odkryc dosc pozno, np. bedac w wywolaniu ParseLongParameter. }
if OptionNum <> -1 then
begin
{ najpierw zajmij sie SimpleShortOptions }
for k := 0 to SimpleShortOptions.Count-1 do
begin
if not (Options^[SimpleShortOptions[k]].Argument in [oaNone, oaOptional]) then
raise EMissingOptionArgument.Create('Missing argument for short option -'+
Options^[SimpleShortOptions[k]].Short +'; when combining short options only the last '+
'option can have an argument');
OptionProc(SimpleShortOptions[k], false, '', EmptySeparateArgs, OptionProcData);
end;
SimpleShortOptions.Count := 0;
{ teraz zajmij sie opcja OptionNum o nazwie OptionName }
Delete(i);
SeparateArgs := EmptySeparateArgs;
{ upewnij sie ze HasArgument ma dopuszczalna wartosc. Odczytaj argumenty
podane jako osobne paranetry dla oaRequired i oaRequired?Separate. }
if (Options^[OptionNum].Argument = oaRequired) and (not HasArgument) then
begin
if i > High then
raise EMissingOptionArgument.Create('Missing argument for option '+OptionName);
HasArgument := true;
Argument := Strings[i];
Delete(i);
end else
if (Options^[OptionNum].Argument = oaNone) and HasArgument then
raise EExcessiveOptionArgument.Create('Excessive argument for option '+OptionName) else
if Options^[OptionNum].Argument in OptionArgumentsRequiredSeparate then
begin
if HasArgument then
raise EExcessiveOptionArgument.CreateFmt('Option %s requires %d arguments, '+
'you cannot give them using the form --option=argument, you must give '+
'all the arguments as separate parameters', [OptionName,
OptionSeparateArgumentToCount(Options^[OptionNum].Argument) ]);
for j := 1 to OptionSeparateArgumentToCount(Options^[OptionNum].Argument) do
begin
if i > High then
raise EMissingOptionArgument.CreateFmt('Not enough arguments for option %s, '+
'this option needs %d arguments but we have only %d', [OptionName,
OptionSeparateArgumentToCount(Options^[OptionNum].Argument), j-1]);
SeparateArgs[j] := Strings[i];
Delete(i);
end;
end;
OptionProc(OptionNum, HasArgument, Argument, SeparateArgs, OptionProcData);
end else
Inc(i);
end;
finally SimpleShortOptions.Free end;
end;
{ some simple helper utilities ---------------------------------------------- }
function OptionSeparateArgumentToCount(const v: TOptionSeparateArgument): Integer;
begin
result := RequiredSeparateFirstCount + Ord(v) - Ord(oaRequiredSeparateFirst)
end;
function SeparateArgsToVector3Single(const v: TSeparateArgs): TVector3Single;
begin
result[0] := StrToFloat(v[1]);
result[1] := StrToFloat(v[2]);
result[2] := StrToFloat(v[3]);
end;
procedure InitializationParams;
var
I: Integer;
begin
Parameters := TParameters.Create;
for I := 0 to ParamCount do
Parameters.Add(ParamStr(i));
end;
procedure FinalizationParams;
begin
FreeAndNil(Parameters);
end;
initialization
InitializationParams;
finalization
FinalizationParams;
end.
|