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 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
|
{
Copyright 1998-2018 PasDoc developers.
This file is part of "PasDoc".
"PasDoc" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"PasDoc" 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "PasDoc"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
{ @abstract(The @name unit --- easing command line parsing)
@author(Johannes Berg <johannes@sipsolutions.de>)
To use this unit, create an object of @link(TOptionParser) and add options to
it, each option descends from @link(TOption).
Then, call your object's @link(TOptionParser.ParseOptions)
method and options are parsed.
After parsing, examine your option objects. }
unit PasDoc_OptionParser;
{$I pasdoc_defines.inc}
interface
uses
Classes
{$IFDEF USE_VARIANTS},
Variants
{$ENDIF};
const
{ default short option character used }
DefShortOptionChar = '-';
{ default long option string used }
DefLongOptionString = '--';
{ Marks "include config file" option }
OptionFileChar = '@';
{ Special substitution that, if found inside a config file, will be replaced
with actual path of the file }
CfgMacroCfgPath = '$CFG_PATH';
{ Indentation of option's name from the start of console line }
OptionIndent = ' ';
{ Separator between option's name and explanation }
OptionSep = ' ';
{ Width of console }
ConsoleWidth = 80;
type
TOptionParser = class;
{ @abstract(abstract base class for options)
This class implements all the basic functionality and provides
abstract methods for the @link(TOptionParser) class to call, which are
overridden by descendants.
It also provides function to write the explanation.
}
TOption = class
protected
FShort: char;
FLong: string;
FShortSens: boolean;
FLongSens: boolean;
FExplanation: string;
FWasSpecified: boolean;
FParser: TOptionParser;
function ParseOption(const AWords: TStrings): boolean; virtual; abstract;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; virtual; abstract;
procedure SetValue(const AValue: Variant); virtual; abstract;
{$ENDIF}
public
{ Create a new Option.
Set AShort to #0 in order to have no short option.
Technically you can set ALong to '' to have no long option,
but in practive *every* option should have long form.
Don't override this in descendants (this always simply calls
CreateEx). Override only CreateEx. }
constructor Create(const AShort:char; const ALong: string);
constructor CreateEx(const AShort:char; const ALong: string;
const AShortCaseSensitive, ALongCaseSensitive: boolean); virtual;
{ returns the width of the string "-s, @--long-option" where s is the short option.
Removes non-existant options (longoption = '' or shortoption = #0) }
function GetOptionWidth: Integer;
{ writes the wrapped explanation including option format,
AOptWidth determines how much it is indented & wrapped }
procedure WriteExplanation(const AOptWidth: Integer);
{ Short form of the option --- single character --- if #0 then not used }
property ShortForm: char read FShort write FShort;
{ long form of the option --- string --- if empty, then not used }
property LongForm: string read FLong write FLong;
{ specified whether the short form should be case sensitive or not }
property ShortCaseSensitive: boolean read FShortSens write FShortSens;
{ specifies whether the long form should be case sensitive or not }
property LongCaseSensitive: boolean read FLongSens write FLongSens;
{ signifies if the option was specified at least once }
property WasSpecified: boolean read FWasSpecified;
{ explanation for the option, see also @link(WriteExplanation) }
property Explanation: string read FExplanation write FExplanation;
{$IFDEF USE_VARIANTS}
{ Value as Variant --- for easier access through the @link(TOptionParser.ByName) property }
property Value: Variant read GetValue write SetValue;
{$ENDIF}
end;
{ @abstract(simple boolean option)
turned off when not specified,
turned on when specified. Cannot handle @--option=false et al. }
TBoolOption = class(TOption)
protected
function ParseOption(const AWords: TStrings): boolean; override;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
{$ENDIF}
public
property TurnedOn: boolean read FWasSpecified;
end;
{ @abstract(base class for all options that values)
base class for all options that take one or more values
of the form @--option=value or @--option value etc }
TValueOption = class(TOption)
protected
function CheckValue(const AString: String): boolean; virtual; abstract;
function ParseOption(const AWords: TStrings): boolean; override;
end;
{ @abstract(Integer option)
accepts only integers }
TIntegerOption = class(TValueOption)
protected
FValue: Integer;
function CheckValue(const AString: String): boolean; override;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
{$ENDIF}
public
property Value: Integer read FValue write FValue;
end;
{ @abstract(String option)
accepts a single string }
TStringOption = class(TValueOption)
protected
FValue: String;
function CheckValue(const AString: String): boolean; override;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
{$ENDIF}
public
property Value: String read FValue write FValue;
end;
{ @abstract(stringlist option)
accepts multiple strings and collates them
even if the option itself is specified more than one time }
TStringOptionList = class(TValueOption)
protected
FValues: TStringList;
function CheckValue(const AString: String): Boolean; override;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
{$ENDIF}
public
property Values: TStringList read FValues;
constructor CreateEx(const AShort: Char; const ALong: String;
const AShortCaseSensitive, ALongCaseSensitive: Boolean); override;
destructor Destroy; override;
end;
{ @abstract(pathlist option)
accepts multiple strings paths and collates them
even if the option itself is specified more than one time.
Paths in a single option can be separated by the
DirectorySeparator }
TPathListOption = class(TStringOptionList)
function CheckValue(const AString: String): Boolean; override;
end;
{ @abstract(useful for making a choice of things)
Values must not
have a + or - sign as the last character as that can be used
to add/remove items from the default set, specifying items without
+/- at the end clears the default and uses only specified items }
TSetOption = class(TValueOption)
protected
FPossibleValues,
FValues: TStringList;
function GetPossibleValues: string;
procedure SetPossibleValues(const Value: string);
function CheckValue(const AString: String): Boolean; override;
{$IFDEF USE_VARIANTS}
function GetValue: Variant; override;
procedure SetValue(const AValue: Variant); override;
{$ENDIF}
function GetValues: string;
procedure SetValues(const Value: string);
public
property PossibleValues: string read GetPossibleValues write SetPossibleValues;
constructor CreateEx(const AShort: Char; const ALong: String;
const AShortCaseSensitive, ALongCaseSensitive: Boolean); override;
destructor Destroy; override;
function HasValue(const AValue: string): boolean;
property Values: string read GetValues write SetValues;
end;
{ @abstract(OptionParser --- instantiate one of these for commandline parsing)
This class is the main parsing class, although a lot of parsing is handled
by @link(TOption) and its descendants instead. }
TOptionParser = class
protected
FParams: TStringList;
FOptions: TList;
FLeftList: TStringList;
FShortOptionChar: Char;
FLongOptionString: string;
FIncludeFileOptionName: string;
FIncludeFileOptionExpl: string;
function GetOption(const AIndex: Integer): TOption;
function GetOptionsCount: Integer;
function GetOptionByLongName(const AName: string): TOption;
function GetOptionByShortname(const AName: char): TOption;
public
{ Create without any options --- this will parse the current command line }
constructor Create; virtual;
{ Create with parameters to be used instead of command line }
constructor CreateParams(const AParams: TStrings); virtual;
{ destroy the option parser object and all associated @link(TOption) objects }
destructor Destroy; override;
{ Add a @link(TOption) descendant to be included in parsing the command line }
function AddOption(const AOption: TOption): TOption;
{ Parse the specified command line, see also @link(Create) }
procedure ParseOptions;
{ output explanations for all options to stdout, will nicely format the
output and wrap explanations }
procedure WriteExplanations;
{ This StringList contains all the items from the command line that could
not be parsed. Includes options that didn't accept their value and
non-options like filenames specified on the command line }
property LeftList: TStringList read FLeftList;
{ The number of option objects that were added to this parser }
property OptionsCount: Integer read GetOptionsCount;
{ retrieve an option by index --- you can use this and @link(OptionsCount)
to iterate through the options that this parser owns }
property Options[const AIndex: Integer]: TOption read GetOption;
{ retrieve an option by its long form. Case sensitivity of the options
is taken into account! }
property ByName[const AName: string]: TOption read GetOptionByLongName;
{ retrieve an option by its short form. Case sensitivity of the options
is taken into account! }
property ByShortName[const AName: char]: TOption read GetOptionByShortname;
{ introductory character to be used for short options }
property ShortOptionStart: Char read FShortOptionChar write FShortOptionChar default DefShortOptionChar;
{ introductory string to be used for long options }
property LongOptionStart: String read FLongOptionString write FLongOptionString;
{ name of an option to include config file }
property IncludeFileOptionName: string read FIncludeFileOptionName write FIncludeFileOptionName;
{ explanation of an option to include config file }
property IncludeFileOptionExpl: string read FIncludeFileOptionExpl write FIncludeFileOptionExpl;
end;
implementation
uses
SysUtils,
PasDoc_Utils, PasDoc_Types;
function TryStrToInt(const AString: string; var AValue: Integer): Boolean;
var
LError: Integer;
begin
Val(AString, AValue, LError);
Result := LError = 0;
end;
// Text wrapping done right. RTL versions in both Delphi and FPC wrap at first
// wordwrap symbol AFTER MaxCol (so most of sublines are longer than MaxCol) -
// and that is quite useless. This function ensures no subline is longer than
// MaxCol except for those which do not have a wordbreak inside.
function CorrectWrapText(const Line: string; MaxCol: Integer): string;
const
WordBreaks: TCharSet = [' ', '-', #9];
LineBreaks: TCharSet = [#13, #10];
LineBreak = LineEnding;
var
WBPos, SubLineStartPos, NextMaxPos, i: Integer;
begin
Result := ''; WBPos := 1; SubLineStartPos := 1; NextMaxPos := MaxCol;
for i := 1 to Length(Line) do
begin
if IsCharInSet(Line[i], WordBreaks) then
WBPos := i
else
if IsCharInSet(Line[i], LineBreaks) then // leave line breaks that are already there
begin
NextMaxPos := i + MaxCol;
Continue;
end;
if i <= NextMaxPos then Continue;
if WBPos = 0 then Continue; // no word breaks in the current subline - wait for the first one
Result := Result + Copy(Line, SubLineStartPos, WBPos - SubLineStartPos) + LineBreak;
SubLineStartPos := WBPos + 1; // do not include the word break we broke at
NextMaxPos := WBPos + MaxCol; // limit the next subline
WBPos := 0;
end;
Result := Result + Copy(Line, SubLineStartPos, MaxInt);
end;
// Write option name and explanation to console in two columns.
// NameColWidth is width of the first column not including indent and separator
procedure WriteOptionInfo(const Name, Explanation: string; NameColWidth: Integer);
var
i, LWritten, ExplWidth: Integer;
LLines: TStringList;
begin
Write(OptionIndent, Name);
LWritten := Length(OptionIndent) + Length(Name);
// 1 here comes from a glitch: if a line to be written finishes exactly at the
// right edge of console, an extra line feed is written after it. So we keep
// 1-char margin from the right edge.
ExplWidth := ConsoleWidth - Length(OptionIndent) - NameColWidth - Length(OptionSep) - 1;
LLines := TStringList.Create;
LLines.Text := CorrectWrapText(Explanation, ExplWidth);
for i := 0 to LLines.Count-1 do
begin
WriteLn(StringOfChar(' ', NameColWidth + Length(OptionIndent) - LWritten), OptionSep, LLines[i]);
LWritten := 0; // this value was needed for the 1st iteration only
end;
LLines.Free;
end;
{ TOptionParser }
constructor TOptionParser.Create;
begin
CreateParams(nil);
end;
function TOptionParser.AddOption(const AOption: TOption): TOption;
begin
FOptions.Add(AOption);
Result := AOption;
AOption.FParser := Self;
end;
constructor TOptionParser.CreateParams(const AParams: TStrings);
var
i: Integer;
begin
inherited Create;
FParams := TStringList.Create;
if Assigned(AParams) then begin
FParams.Assign(AParams);
end else begin
for i := 1 to ParamCount do begin
FParams.Add(ParamStr(i));
end;
end;
FLeftList := TStringList.Create;
FOptions := TList.Create;
FLongOptionString := DefLongOptionString;
FShortOptionChar := DefShortOptionChar;
end;
destructor TOptionParser.Destroy;
var
i: Integer;
begin
for i := FOptions.Count-1 downto 0 do begin
TOption(FOptions[i]).Free;
end;
FLeftList.Free;
FParams.Free;
FOptions.Free;
inherited;
end;
procedure TOptionParser.ParseOptions;
procedure ExpandCfgFileMacros(List: TStringList; const CfgFile: string);
var
i: Integer;
CfgFilePath: string;
begin
// Prepare macros data
CfgFilePath := ExtractFileDir(ExpandFileName(CfgFile));
// Do the replace. This might be optimized if the number of macros grows
for i := 0 to List.Count - 1 do
List[i] := StringReplace(List[i], CfgMacroCfgPath, CfgFilePath, [rfReplaceAll]);
end;
var
LCopyList, OptsFromFile: TStringList;
i, j: Integer;
CfgFile: string;
LFoundSomething: boolean;
begin
LCopyList := TStringList.Create;
LCopyList.Assign(FParams);
FLeftList.Clear;
try
// Pre-process config files ("@<path-to-file>" parameters)
for i := LCopyList.Count - 1 downto 0 do
if SCharIs(LCopyList[i], 1, OptionFileChar) then
begin
// read config file
OptsFromFile := TStringList.Create;
try
CfgFile := Copy(LCopyList[i], 2, MaxInt);
OptsFromFile.LoadFromFile(CfgFile);
ExpandCfgFileMacros(OptsFromFile, CfgFile);
// add to the list at current position
for j := OptsFromFile.Count - 1 downto 0 do
LCopyList.Insert(i + 1, LongOptionStart + OptsFromFile[j]);
finally
FreeAndNil(OptsFromFile);
// remove the option with file path
LCopyList.Delete(i);
end;
end;
while LCopyList.Count > 0 do begin
LFoundSomething := false;
for i := 0 to FOptions.Count-1 do begin
if TOption(FOptions[i]).ParseOption(LCopyList) then begin
LFoundSomething := true;
break;
end;
end;
if not LFoundSomething then begin
FLeftList.Add(LCopyList[0]);
LCopyList.Delete(0);
end;
end;
finally
LCopyList.Free;
end;
end;
function TOptionParser.GetOptionsCount: Integer;
begin
Result := FOptions.Count;
end;
function TOptionParser.GetOption(const AIndex: Integer): TOption;
begin
Result := TOption(FOptions[AIndex]);
end;
procedure TOptionParser.WriteExplanations;
function Max(const A,B: Integer): Integer;
begin
if A>B then Result := A else Result := B;
end;
var
i, LMaxWidth: Integer;
begin
LMaxWidth := 0;
for i := 0 to OptionsCount-1 do
LMaxWidth := Max(LMaxWidth, Options[i].GetOptionWidth);
// first write explanation for config file
WriteOptionInfo(FIncludeFileOptionName, FIncludeFileOptionExpl, LMaxWidth);
for i := 0 to OptionsCount-1 do begin
Options[i].WriteExplanation(LMaxWidth);
end;
end;
function TOptionParser.GetOptionByLongName(const AName: string): TOption;
var
i: Integer;
begin
Result := nil;
for i := GetOptionsCount-1 downto 0 do begin
if (Options[i].LongForm = AName)
OR (Options[i].LongCaseSensitive AND (LowerCase(Options[i].LongForm) = LowerCase(AName))) then begin
Result := Options[i];
break;
end;
end;
end;
function TOptionParser.GetOptionByShortname(const AName: char): TOption;
var
i: Integer;
begin
Result := nil;
for i := GetOptionsCount-1 downto 0 do begin
if (Options[i].ShortForm = AName)
OR (Options[i].LongCaseSensitive AND (LowerCase(Options[i].ShortForm) = LowerCase(AName))) then begin
Result := Options[i];
break;
end;
end;
end;
{ TOption }
constructor TOption.CreateEx(const AShort: char; const ALong: string;
const AShortCaseSensitive, ALongCaseSensitive: boolean);
begin
inherited Create;
FShort := AShort;
FLong := ALong;
FShortSens := AShortCaseSensitive;
FLongSens := ALongCaseSensitive;
end;
constructor TOption.Create(const AShort: char; const ALong: string);
begin
CreateEx(AShort, ALong, True, False);
end;
function TOption.GetOptionWidth: Integer;
begin
Result := 0;
if ShortForm<>#0 then begin
Inc(Result, 4); // "-x, "
end;
if Length(LongForm)>0 then begin
Inc(Result, Length(LongForm)+Length(FParser.LongOptionStart));
end else begin
Dec(Result, 2);
end;
end;
procedure TOption.WriteExplanation(const AOptWidth: Integer);
var
Name: string;
begin
Name := '';
if ShortForm <> #0 then begin
Name := FParser.ShortOptionStart + ShortForm;
if Length(LongForm) > 0 then
Name := Name + ', ';
end;
if Length(LongForm) > 0 then
Name := Name + FParser.LongOptionStart + LongForm;
WriteOptionInfo(Name, Explanation, AOptWidth);
end;
{ TBoolOption }
{$IFDEF USE_VARIANTS}
function TBoolOption.GetValue: Variant;
begin
Result := WasSpecified;
end;
{$ENDIF}
function TBoolOption.ParseOption(const AWords: TStrings): boolean;
begin
Result := False;
if ShortForm <> #0 then begin
if AWords[0] = FParser.ShortOptionStart+ShortForm then begin
Result := True;
AWords.Delete(0);
FWasSpecified := True;
end else begin
if (not ShortCaseSensitive) and (LowerCase(AWords[0]) = FParser.ShortOptionStart+LowerCase(ShortForm)) then begin
Result := True;
AWords.Delete(0);
FWasSpecified := True;
end;
end;
end;
if (not Result) and (Length(LongForm) > 0) then begin
if AWords[0] = FParser.LongOptionStart+LongForm then begin
Result := True;
AWords.Delete(0);
FWasSpecified := True;
end else begin
if (not LongCaseSensitive) and (LowerCase(AWords[0]) = FParser.LongOptionStart+LowerCase(LongForm)) then begin
Result := True;
AWords.Delete(0);
FWasSpecified := True;
end;
end;
end;
end;
{$IFDEF USE_VARIANTS}
procedure TBoolOption.SetValue(const AValue: Variant);
begin
// do nothing, this option can either be specified or not
end;
{$ENDIF}
{ TValueOption }
function TValueOption.ParseOption(const AWords: TStrings): boolean;
var
LValue: string;
begin
Result := False;
if ShortForm <> #0 then begin
if (Copy(AWords[0],1,Length(FParser.ShortOptionStart+ShortForm)) = FParser.ShortOptionStart+ShortForm)
OR ((not ShortCaseSensitive) and (LowerCase(Copy(AWords[0],1,Length(FParser.ShortOptionStart+ShortForm))) = FParser.ShortOptionStart+LowerCase(ShortForm))) then begin
LValue := Copy(AWords[0], Length(FParser.ShortOptionStart+ShortForm)+1, MaxInt);
if LValue = '' then begin
if AWords.Count>1 then begin
LValue := AWords[1];
if CheckValue(LValue) then begin
Result := True;
AWords.Delete(0);
AWords.Delete(0);
end else begin
Result := CheckValue('');
if Result then AWords.Delete(0);
end;
end else begin
Result := CheckValue(LValue);
if Result then AWords.Delete(0);
end;
end else begin
Result := CheckValue(LValue);
if Result then AWords.Delete(0);
end;
end;
end;
if Result then FWasSpecified := True;
if (not Result) and (Length(LongForm) > 0) then begin
if (Copy(AWords[0],1,Length(FParser.LongOptionStart+LongForm)) = FParser.LongOptionStart+LongForm)
OR ((not LongCaseSensitive) AND (LowerCase(Copy(AWords[0],1,Length(FParser.LongOptionStart+LongForm))) = FParser.LongOptionStart+LowerCase(LongForm))) then begin
if Length(AWords[0]) = Length(FParser.LongOptionStart+LongForm) then begin
if AWords.Count>1 then begin
LValue := AWords[1];
end else begin
LValue := '';
end;
Result := CheckValue(LValue);
if Result then begin
AWords.Delete(0);
if AWords.Count>0 then AWords.Delete(0);
end;
end else begin
if Copy(AWords[0], Length(FParser.LongOptionStart+LongForm)+1, 1) = '=' then begin
LValue := Copy(AWords[0], Length(FParser.LongOptionStart+LongForm)+2, MaxInt);
Result := CheckValue(LValue);
if Result then AWords.Delete(0);
end;
end;
end;
end;
if Result then FWasSpecified := True;
end;
{ TIntegerOption }
function TIntegerOption.CheckValue(const AString: String): boolean;
var
LValue: Integer;
begin
Result := TryStrToInt(AString, LValue);
if Result then FValue := LValue;
end;
{$IFDEF USE_VARIANTS}
function TIntegerOption.GetValue: Variant;
begin
Result := FValue;
end;
{$ENDIF}
{$IFDEF USE_VARIANTS}
procedure TIntegerOption.SetValue(const AValue: Variant);
begin
FValue := AValue;
end;
{$ENDIF}
{ TStringOption }
function TStringOption.CheckValue(const AString: String): boolean;
begin
FValue := AString;
Result := True;
end;
{$IFDEF USE_VARIANTS}
function TStringOption.GetValue: Variant;
begin
Result := FValue;
end;
procedure TStringOption.SetValue(const AValue: Variant);
begin
FValue := AValue;
end;
{$ENDIF}
{ TStringOptionList }
function TStringOptionList.CheckValue(const AString: String): Boolean;
begin
Result := True;
FValues.Add(AString);
end;
constructor TStringOptionList.CreateEx(const AShort: Char;
const ALong: String; const AShortCaseSensitive,
ALongCaseSensitive: Boolean);
begin
inherited;
FValues := TStringList.Create;
end;
destructor TStringOptionList.Destroy;
begin
FValues.Free;
inherited;
end;
{$IFDEF USE_VARIANTS}
function TStringOptionList.GetValue: Variant;
begin
Result := FValues.Text;
end;
procedure TStringOptionList.SetValue(const AValue: Variant);
begin
FValues.Text := AValue;
end;
{$ENDIF}
{ TSetOption }
function TSetOption.CheckValue(const AString: String): Boolean;
var
LList,
LResult: TStringList;
i: Integer;
s: string;
si: Integer;
LCleared: boolean;
begin
Result := True;
LCleared := false;
LList := TStringList.Create;
LResult := TStringList.Create;
try
LList.Duplicates := dupIgnore;
LList.CommaText := AString;
LList.Sorted := True;
LResult.Assign(FValues); // default values
LResult.Duplicates := dupIgnore;
LResult.Sorted := True;
i := 0;
while i < LList.Count do begin
s := LList[i];
if Length(s) = 0 then continue;
case s[length(s)] of
'-': begin
SetLength(s, Length(s)-1);
if FPossibleValues.IndexOf(s) >= 0 then begin
si := LResult.IndexOf(s);
if si>=0 then begin
LResult.Delete(si);
end;
end else begin
Result := false;
break;
end;
end;
'+': begin
SetLength(s, Length(s)-1);
if FPossibleValues.IndexOf(s) >= 0 then begin
LResult.Add(s);
end else begin
Result := false;
break;
end;
end;
else begin
if FPossibleValues.IndexOf(s) >= 0 then begin
LResult.Add(s);
end else begin
Result := false;
break;
end;
if not LCleared then begin
LCleared := True;
LResult.Clear;
i := -1; // restart from beginning
end;
end;
end;
Inc(i);
end;
finally
LList.Free;
FValues.Assign(LResult);
LResult.Free;
end;
end;
constructor TSetOption.CreateEx(const AShort: Char; const ALong: String;
const AShortCaseSensitive, ALongCaseSensitive: Boolean);
begin
inherited;
FPossibleValues := TStringList.Create;
FPossibleValues.Duplicates := dupIgnore;
FPossibleValues.Sorted := True;
FValues := TStringList.Create;
FValues.Duplicates := dupIgnore;
FValues.Sorted := True;
end;
destructor TSetOption.Destroy;
begin
FPossibleValues.Free;
FValues.Free;
inherited;
end;
function TSetOption.GetPossibleValues: string;
begin
Result := FPossibleValues.CommaText;
end;
{$IFDEF USE_VARIANTS}
function TSetOption.GetValue: Variant;
begin
Result := FValues.CommaText;
end;
{$ENDIF}
function TSetOption.GetValues: string;
begin
Result := FValues.CommaText;
end;
function TSetOption.HasValue(const AValue: string): boolean;
begin
Result := FValues.IndexOf(AValue)>=0;
end;
procedure TSetOption.SetPossibleValues(const Value: string);
begin
FPossibleValues.CommaText := Value;
end;
{$IFDEF USE_VARIANTS}
procedure TSetOption.SetValue(const AValue: Variant);
begin
FValues.CommaText := AValue;
end;
{$ENDIF}
procedure TSetOption.SetValues(const Value: string);
begin
FValues.CommaText := Value;
end;
{ TPathListOption }
{$IFNDEF DELPHI_6_UP}
{$IFDEF FPC}
const
sLineBreak = LineEnding;
PathSep = PathSeparator;
{$ELSE}
{$IFNDEF KYLIX}
const
sLineBreak = #13#10;
PathSep = ';';
{$ENDIF}
{$ENDIF}
{$ENDIF}
function TPathListOption.CheckValue(const AString: String): Boolean;
var
LValues: TStringList;
begin
Result := true;
LValues := TStringList.Create;
LValues.Text := StringReplace(AString, PathSep, sLineBreak, [rfReplaceAll]);
FValues.AddStrings(LValues);
LValues.Free;
end;
end.
|