Quick.Parameters.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. { ***************************************************************************
  2. Copyright (c) 2016-2020 Kike Pérez
  3. Unit : Quick.Parameters
  4. Description : Map comandline to class
  5. Author : Kike Pérez
  6. Version : 1.4
  7. Created : 12/07/2020
  8. Modified : 29/07/2020
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Parameters;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Classes,
  26. SysUtils,
  27. StrUtils,
  28. Generics.Collections,
  29. Quick.Commons,
  30. {$IFDEF CONSOLE}
  31. Quick.Console,
  32. {$ENDIF}
  33. rtti,
  34. TypInfo,
  35. Quick.RTTI.Utils;
  36. type
  37. CommandDescription = class(TCustomAttribute)
  38. private
  39. fDescription : string;
  40. public
  41. constructor Create(const aDescription : string);
  42. property Description : string read fDescription;
  43. end;
  44. ParamCommand = class(TCustomAttribute)
  45. private
  46. fPosition : Integer;
  47. public
  48. constructor Create(aPosition : Integer);
  49. property Position : Integer read fPosition;
  50. end;
  51. ParamName = class(TCustomAttribute)
  52. private
  53. fName : string;
  54. fAlias : string;
  55. public
  56. constructor Create(const aName: string; const aAlias : string = '');
  57. property Name : string read fName;
  58. property Alias : string read fAlias;
  59. end;
  60. ParamValueIsNextParam = class(TCustomAttribute);
  61. ParamHelp = class(TCustomAttribute)
  62. private
  63. fHelp : string;
  64. fValueName : string;
  65. public
  66. constructor Create(const aHelp : string; const aValueName : string = '');
  67. property Help : string read fHelp;
  68. property ValueName : string read fValueName;
  69. end;
  70. ParamSwitchChar = class(TCustomAttribute)
  71. private
  72. fSwithChar : string;
  73. public
  74. constructor Create(const aSwitchChar : string);
  75. property SwitchChar : string read fSwithChar write fSwithChar;
  76. end;
  77. ParamValueSeparator = class(TCustomAttribute)
  78. private
  79. fValueSeparator : string;
  80. public
  81. constructor Create(const aValueSeparator : string);
  82. property ValueSeparator : string read fValueSeparator write fValueSeparator;
  83. end;
  84. ParamRequired = class(TCustomAttribute);
  85. {$IFDEF CONSOLE}
  86. TColorizeHelp = class
  87. private
  88. fCommandName : TConsoleColor;
  89. fCommandDescription : TConsoleColor;
  90. fCommandUsage : TConsoleColor;
  91. fSections : TConsoleColor;
  92. fArgumentName : TConsoleColor;
  93. fArgumentDescription : TConsoleColor;
  94. public
  95. property CommandName : TConsoleColor read fCommandName write fCommandName;
  96. property CommandDescription : TConsoleColor read fCommandDescription write fCommandDescription;
  97. property CommandUsage : TConsoleColor read fCommandUsage write fCommandUsage;
  98. property Sections : TConsoleColor read fSections write fSections;
  99. property ArgumentName : TConsoleColor read fArgumentName write fArgumentName;
  100. property ArgumentDescription : TConsoleColor read fArgumentDescription write fArgumentDescription;
  101. end;
  102. {$ENDIF}
  103. TParameters = class
  104. type
  105. TValueType = (vtString, vtInteger, vtFloat, vtBoolean, vtEnumeration);
  106. TParam = class
  107. private
  108. fName : string;
  109. fAlias : string;
  110. fValue : string;
  111. fPrecisePosition : Integer;
  112. fParamType: TValueType;
  113. fRequired : Boolean;
  114. fHelp : string;
  115. fValueName : string;
  116. fValueIsNextParam: Boolean;
  117. fSwitchChar: string;
  118. fValueSeparator: string;
  119. fIsPresent: Boolean;
  120. public
  121. constructor Create;
  122. property Name : string read fName write fName;
  123. property Alias : string read fAlias write fAlias;
  124. property Value : string read fValue write fValue;
  125. property PrecisePosition : Integer read fPrecisePosition write fPrecisePosition;
  126. property ParamType : TValueType read fParamType write fParamType;
  127. property Required : Boolean read fRequired write fRequired;
  128. property SwitchChar : string read fSwitchChar write fSwitchChar;
  129. property ValueSeparator : string read fValueSeparator write fValueSeparator;
  130. property Help : string read fHelp write fHelp;
  131. property HepValueName : string read fValueName write fValueName;
  132. property ValueIsNextParam : Boolean read fValueIsNextParam write fValueIsNextParam;
  133. property IsPresent : Boolean read fIsPresent write fIsPresent;
  134. function IsSwitch : Boolean;
  135. function IsCommand : Boolean;
  136. end;
  137. private
  138. fParams : TObjectList<TParam>;
  139. fDescription : string;
  140. fHelp: Boolean;
  141. {$IFDEF CONSOLE}
  142. fColorizeHelp: TColorizeHelp;
  143. {$ENDIF}
  144. function ExistParam(aParameter : TParam; const aParam : string) : Boolean;
  145. function GetParamName(aParameter : TParam; const aParam : string) : string;
  146. function GetParamValue(aParameter : TParam; const aParam : string) : string;
  147. function ValueType(const aProp : TRttiProperty) : TValueType;
  148. procedure ParseParams;
  149. function CheckHelpSwitch : Boolean;
  150. protected
  151. {$IFDEF CONSOLE}
  152. procedure GetColors; virtual;
  153. {$ENDIF}
  154. procedure Validate; virtual;
  155. public
  156. constructor Create; virtual;
  157. destructor Destroy; override;
  158. property Description : string read fDescription write fDescription;
  159. {$IFDEF CONSOLE}
  160. property ColorizeHelp : TColorizeHelp read fColorizeHelp write fColorizeHelp;
  161. procedure ShowHelp; virtual;
  162. {$ENDIF}
  163. function GetHelp : TStringList;
  164. property Help : Boolean read fHelp write fHelp;
  165. end;
  166. TServiceParameters = class(TParameters)
  167. private
  168. fInstance : string;
  169. fInstall : Boolean;
  170. fRemove : Boolean;
  171. fConsole : Boolean;
  172. published
  173. [ParamHelp('Install service with a custom name','Service name')]
  174. property Instance : string read fInstance write fInstance;
  175. [ParamHelp('Install as a service')]
  176. property Install : Boolean read fInstall write fInstall;
  177. [ParamHelp('Remove service')]
  178. property &Remove : Boolean read fRemove write fRemove;
  179. [ParamHelp('Force run as a console application (when runned from another service)')]
  180. property Console : Boolean read fConsole write fConsole;
  181. end;
  182. ENotValidCommandlineParameter = class(Exception);
  183. ERequiredParameterNotFound = class(Exception);
  184. EParameterValueNotFound = class(Exception);
  185. EParamValueNotSupported = class(Exception);
  186. implementation
  187. { TParameter }
  188. constructor TParameters.Create;
  189. begin
  190. {$IFDEF CONSOLE}
  191. fColorizeHelp := TColorizeHelp.Create;
  192. GetColors;
  193. {$ENDIF}
  194. fParams := TObjectList<TParam>.Create(True);
  195. ParseParams;
  196. {$IFDEF CONSOLE}
  197. if fHelp then
  198. begin
  199. ShowHelp;
  200. Halt;
  201. end;
  202. {$ENDIF}
  203. Validate;
  204. end;
  205. destructor TParameters.Destroy;
  206. begin
  207. fParams.Free;
  208. {$IFDEF CONSOLE}
  209. fColorizeHelp.Free;
  210. {$ENDIF}
  211. inherited;
  212. end;
  213. function TParameters.ExistParam(aParameter : TParam; const aParam : string) : Boolean;
  214. var
  215. i : Integer;
  216. parName : string;
  217. begin
  218. Result := False;
  219. if aParam.IsEmpty then Exit;
  220. for i := 1 to ParamCount do
  221. begin
  222. parName := ParamStr(i);
  223. if parName = aParameter.ValueSeparator then raise ENotValidCommandlineParameter.CreateFmt('Not valid commandline "%s"', [parName]);
  224. parName := GetParamName(aParameter,ParamStr(i));
  225. if CompareText(parName,aParam) = 0 then Exit(True);
  226. end;
  227. end;
  228. function TParameters.GetParamName(aParameter : TParam; const aParam : string) : string;
  229. var
  230. switch : string;
  231. begin
  232. if CompareText(aParam,'-' + aParameter.Alias) = 0 then switch := '-'
  233. else switch := aParameter.SwitchChar;
  234. if aParam.StartsWith(switch) then Result := aParam.Substring(switch.Length);
  235. if Result.Contains(aParameter.ValueSeparator) then Result := Result.Substring(0,Result.IndexOf(aParameter.ValueSeparator));
  236. end;
  237. function TParameters.GetParamValue(aParameter : TParam; const aParam : string) : string;
  238. var
  239. i : Integer;
  240. parName : string;
  241. param : string;
  242. begin
  243. Result := '';
  244. for i := 1 to ParamCount do
  245. begin
  246. param := ParamStr(i);
  247. parName := GetParamName(aParameter,param);
  248. if CompareText(parName,aParam) = 0 then
  249. begin
  250. if aParameter.ValueIsNextParam then
  251. begin
  252. if i < ParamCount then Result := ParamStr(i+1);
  253. end
  254. else
  255. begin
  256. if param.Contains(aParameter.ValueSeparator) then Result := param.Substring(param.IndexOf(aParameter.ValueSeparator)+(aParameter.ValueSeparator.Length));
  257. end;
  258. Exit;
  259. end;
  260. end;
  261. end;
  262. function TParameters.CheckHelpSwitch: Boolean;
  263. var
  264. param : TParam;
  265. begin
  266. param := TParam.Create;
  267. param.Name := 'help';
  268. param.Alias := 'h';
  269. try
  270. Result := ExistParam(param,param.Name);
  271. finally
  272. param.Free;
  273. end;
  274. end;
  275. procedure TParameters.ParseParams;
  276. var
  277. param : TParam;
  278. value : TValue;
  279. valueint : Int64;
  280. valuefloat : Extended;
  281. rType : TRttiType;
  282. rProp : TRttiProperty;
  283. attr : TCustomAttribute;
  284. pinfo : PTypeInfo;
  285. found : Boolean;
  286. begin
  287. fHelp := CheckHelpSwitch;
  288. rType := TRTTI.GetType(Self.ClassInfo);
  289. //get main info
  290. for attr in rType.GetAttributes do
  291. begin
  292. if attr is CommandDescription then Self.Description := CommandDescription(attr).Description;
  293. end;
  294. //get parameters
  295. for rProp in TRTTI.GetProperties(rType,TRttiPropertyOrder.roFirstBase) do
  296. begin
  297. if rProp.Visibility <> TMemberVisibility.mvPublished then continue;
  298. param := TParam.Create;
  299. fParams.Add(param);
  300. param.Name := rProp.Name;
  301. for attr in rProp.GetAttributes do
  302. begin
  303. if attr is ParamHelp then
  304. begin
  305. param.Help := ParamHelp(attr).Help;
  306. param.HepValueName := ParamHelp(attr).ValueName;
  307. end;
  308. if attr is ParamName then
  309. begin
  310. param.Name := ParamName(attr).Name;
  311. param.Alias := ParamName(attr).Alias;
  312. end;
  313. if attr is ParamCommand then param.PrecisePosition := ParamCommand(attr).Position;
  314. if attr is ParamRequired then param.Required := True;
  315. if attr is ParamSwitchChar then param.SwitchChar := ParamSwitchChar(attr).SwitchChar;
  316. if attr is ParamValueSeparator then param.ValueSeparator := ParamValueSeparator(attr).ValueSeparator;
  317. if attr is ParamValueIsNextParam then param.ValueIsNextParam := True;
  318. end;
  319. param.ParamType := ValueType(rProp);
  320. if param.IsCommand then
  321. begin
  322. found := ParamCount >= param.PrecisePosition;
  323. param.SwitchChar := ' ';
  324. end
  325. else found := (ExistParam(param,param.Name)) or (ExistParam(param,param.Alias));
  326. value := nil;
  327. if found then
  328. begin
  329. if param.IsSwitch then
  330. begin
  331. value := True;
  332. end
  333. else
  334. begin
  335. if param.IsCommand then param.Value := ParamStr(param.PrecisePosition)
  336. else param.Value := GetParamValue(param,param.Name);
  337. if (param.Value.IsEmpty) and (not param.Alias.IsEmpty) then param.Value := GetParamValue(param,param.Alias);
  338. if (not param.Value.IsEmpty) and (not fHelp) then
  339. case param.ParamType of
  340. TValueType.vtString :
  341. begin
  342. value := param.Value;
  343. end;
  344. TValueType.vtInteger :
  345. begin
  346. if not TryStrToInt64(param.Value,valueint) then raise EParamValueNotSupported.CreateFmt('Parameter "%s" needs a numeric value',[param.Name]);
  347. value := valueint;
  348. end;
  349. TValueType.vtFloat :
  350. begin
  351. if not TryStrToFloat(param.Value,valuefloat) then raise EParamValueNotSupported.CreateFmt('Parameter "%s" needs a float value',[param.Name]);
  352. value := valuefloat;
  353. end;
  354. TValueType.vtEnumeration :
  355. begin
  356. pinfo := TRTTI.GetPropertyValue(Self,param.Name).TypeInfo;
  357. if not IsInteger(param.Value) then TValue.Make(GetEnumValue(pinfo,param.Value),pinfo,value)
  358. else TValue.Make(StrToInt(param.Value),pinfo,value);
  359. end;
  360. end;
  361. end;
  362. param.IsPresent := True;
  363. if not value.IsEmpty then rProp.SetValue(Self,value);
  364. end;
  365. end;
  366. //add help
  367. param := TParam.Create;
  368. param.Name := 'Help';
  369. param.Alias := 'h';
  370. param.ParamType := TValueType.vtBoolean;
  371. param.Help := 'Show this documentation';
  372. fParams.Add(param);
  373. end;
  374. procedure TParameters.Validate;
  375. var
  376. param : TParam;
  377. begin
  378. if help then Exit;
  379. for param in fParams do
  380. begin
  381. if param.IsPresent then
  382. begin
  383. if (not param.IsSwitch) and (param.Value.IsEmpty) then raise EParamValueNotSupported.CreateFmt('Value for parameter "%s" not specified',[param.Name]);
  384. end
  385. else
  386. begin
  387. if param.Required then raise ERequiredParameterNotFound.CreateFmt('Required parameter "%s" not found',[param.Name]);
  388. end;
  389. end;
  390. end;
  391. function TParameters.ValueType(const aProp: TRttiProperty): TValueType;
  392. var
  393. rType : TRttiType;
  394. begin
  395. rType := aProp.PropertyType;
  396. case rType.TypeKind of
  397. tkString, tkWideString, tkChar, tkUnicodeString : Result := TValueType.vtString;
  398. tkInteger, tkInt64 : Result := TValueType.vtInteger;
  399. tkFloat : Result := TValueType.vtFloat;
  400. tkEnumeration :
  401. begin
  402. if TRTTI.GetPropertyValue(Self,aProp.Name).TypeInfo = System.TypeInfo(Boolean) then Result := TValueType.vtBoolean
  403. else Result := TValueType.vtEnumeration;
  404. end;
  405. else raise EParamValueNotSupported.CreateFmt('Parameter "%s": Value not supported',[aProp.Name]);
  406. end;
  407. end;
  408. {$IFDEF CONSOLE}
  409. procedure TParameters.ShowHelp;
  410. var
  411. version : string;
  412. arg : string;
  413. value : string;
  414. usage : string;
  415. commands : string;
  416. param : TParam;
  417. maxlen : Integer;
  418. arglen : Integer;
  419. begin
  420. //show app and version
  421. version := GetAppVersionStr;
  422. if version.IsEmpty then cout(GetAppName,fColorizeHelp.CommandName)
  423. else cout(Format('%s v.%s',[GetAppName,GetAppVersionStr]),fColorizeHelp.CommandName);
  424. usage := '';
  425. maxlen := 0;
  426. commands := '';
  427. //show usage
  428. arglen := 0;
  429. for param in fParams do
  430. begin
  431. if (param.Name.Length + param.Alias.Length) > maxlen then maxlen := param.Name.Length + param.Alias.Length;
  432. if param.Required then arg := '<' + param.SwitchChar + param.Name +'%s>'
  433. else arg := '[' + param.SwitchChar + param.Name + '%s]';
  434. if param.IsSwitch then
  435. begin
  436. arg := Format(arg,['']);
  437. end
  438. else if param.IsCommand then
  439. begin
  440. if param.HepValueName.IsEmpty then value := param.Name
  441. else value := param.HepValueName;
  442. if param.Required then commands := commands + Format('<%s> ',[value])
  443. else commands := commands + Format('[%s] ',[value]);
  444. Continue;
  445. end
  446. else
  447. begin
  448. if param.ValueIsNextParam then value := ' <value>'
  449. else value := param.ValueSeparator + '<value>';
  450. if not param.HepValueName.IsEmpty then value := StringReplace(value,'value',param.HepValueName,[rfIgnoreCase,rfReplaceAll]);
  451. arg := Format(arg,[value]);
  452. end;
  453. //fit usage line
  454. arglen := arglen + arg.Length;
  455. if arglen > 80 then
  456. begin
  457. usage := usage + #10 + FillStr(' ',8 + (GetAppName.Length));
  458. arglen := arg.Length;
  459. end;
  460. usage := usage + arg + ' ';
  461. end;
  462. maxlen := maxlen + 5;
  463. coutSL('Usage: ',fColorizeHelp.Sections);
  464. coutSL(Format('%s %s%s',[GetAppName,commands,usage]),fColorizeHelp.CommandUsage);
  465. cout('',ccWhite);
  466. cout('',ccWhite);
  467. //show description
  468. cout(Description,fColorizeHelp.CommandDescription);
  469. cout('',ccWhite);
  470. //show arguments
  471. cout('Arguments:',fColorizeHelp.Sections);
  472. cout('',ccWhite);
  473. for param in fParams do
  474. begin
  475. //if param.IsCommand then Continue;
  476. if param.Alias.IsEmpty then
  477. begin
  478. coutSL(Format(' %s%s%s',[param.SwitchChar,param.Name,FillStr(' ',maxlen - param.Name.Length)]),fColorizeHelp.ArgumentName);
  479. end
  480. else
  481. begin
  482. coutSL(Format(' %s%s, -%s%s',[param.SwitchChar,param.Name,param.Alias,FillStr(' ',maxlen - (param.Name.Length + param.Alias.Length + 3))]),fColorizeHelp.ArgumentName);
  483. end;
  484. coutSL(param.Help,fColorizeHelp.ArgumentDescription);
  485. cout('',ccWhite);
  486. end;
  487. cout('',ccWhite);
  488. end;
  489. procedure TParameters.GetColors;
  490. begin
  491. fColorizeHelp.CommandName := ccLightCyan;
  492. fColorizeHelp.CommandDescription := ccDarkGray;
  493. fColorizeHelp.CommandUsage := ccLightGray;
  494. fColorizeHelp.fSections := ccWhite;
  495. fColorizeHelp.ArgumentName := ccWhite;
  496. fColorizeHelp.ArgumentDescription := ccLightGray;
  497. end;
  498. {$ENDIF}
  499. function TParameters.GetHelp : TStringList;
  500. var
  501. line : string;
  502. version : string;
  503. arg : string;
  504. value : string;
  505. usage : string;
  506. commands : string;
  507. param : TParam;
  508. maxlen : Integer;
  509. arglen : Integer;
  510. begin
  511. Result := TStringList.Create;
  512. line := '';
  513. //show app and version
  514. version := GetAppVersionStr;
  515. if version.IsEmpty then Result.Add(GetAppName)
  516. else Result.Add(Format('%s v.%s',[GetAppName,GetAppVersionStr]));
  517. usage := '';
  518. maxlen := 0;
  519. commands := '';
  520. //show usage
  521. arglen := 0;
  522. for param in fParams do
  523. begin
  524. if (param.Name.Length + param.Alias.Length) > maxlen then maxlen := param.Name.Length + param.Alias.Length;
  525. if param.Required then arg := '<' + param.SwitchChar + param.Name +'%s>'
  526. else arg := '[' + param.SwitchChar + param.Name + '%s]';
  527. if param.IsSwitch then
  528. begin
  529. arg := Format(arg,['']);
  530. end
  531. else if param.IsCommand then
  532. begin
  533. if param.HepValueName.IsEmpty then value := param.Name
  534. else value := param.HepValueName;
  535. if param.Required then commands := commands + Format('<%s> ',[value])
  536. else commands := commands + Format('[%s] ',[value]);
  537. Continue;
  538. end
  539. else
  540. begin
  541. if param.ValueIsNextParam then value := ' <value>'
  542. else value := param.ValueSeparator + '<value>';
  543. if not param.HepValueName.IsEmpty then value := StringReplace(value,'value',param.HepValueName,[rfIgnoreCase,rfReplaceAll]);
  544. arg := Format(arg,[value]);
  545. end;
  546. //fit usage line
  547. arglen := arglen + arg.Length;
  548. if arglen > 80 then
  549. begin
  550. usage := usage + #10 + FillStr(' ',8 + (GetAppName.Length));
  551. arglen := arg.Length;
  552. end;
  553. usage := usage + arg + ' ';
  554. end;
  555. maxlen := maxlen + 5;
  556. Result.Add(Format('Usage: %s %s%s',[GetAppName,commands,usage]));
  557. Result.Add('');
  558. Result.Add('');
  559. //show description
  560. Result.Add(Description);
  561. Result.Add('');
  562. //show arguments
  563. Result.Add('Arguments:');
  564. Result.Add('');
  565. for param in fParams do
  566. begin
  567. //if param.IsCommand then Continue;
  568. line := '';
  569. if param.Alias.IsEmpty then
  570. begin
  571. line := line + Format(' %s%s%s',[param.SwitchChar,param.Name,FillStr(' ',maxlen - param.Name.Length)]);
  572. end
  573. else
  574. begin
  575. line := line + Format(' %s%s, -%s%s',[param.SwitchChar,param.Name,param.Alias,FillStr(' ',maxlen - (param.Name.Length + param.Alias.Length + 3))]);
  576. end;
  577. line := line + param.Help;
  578. Result.Add(line);
  579. Result.Add('');
  580. end;
  581. end;
  582. { CommandDescription }
  583. constructor CommandDescription.Create(const aDescription: string);
  584. begin
  585. fDescription := aDescription;
  586. end;
  587. { ParamName }
  588. constructor ParamName.Create(const aName: string; const aAlias : string = '');
  589. begin
  590. fName := aName;
  591. fAlias := aAlias;
  592. end;
  593. { ParamHelp }
  594. constructor ParamHelp.Create(const aHelp : string; const aValueName : string = '');
  595. begin
  596. fHelp := aHelp;
  597. if not aValueName.IsEmpty then fValueName := aValueName
  598. else fValueName := 'value';
  599. end;
  600. { TParameters.TParam }
  601. constructor TParameters.TParam.Create;
  602. begin
  603. IsPresent := False;
  604. fSwitchChar := '--';
  605. fValueSeparator := '=';
  606. fPrecisePosition := 0;
  607. end;
  608. function TParameters.TParam.IsCommand: Boolean;
  609. begin
  610. Result := fPrecisePosition > 0;
  611. end;
  612. function TParameters.TParam.IsSwitch: Boolean;
  613. begin
  614. Result := fParamType = TValueType.vtBoolean;
  615. end;
  616. { ParamSwitchChar }
  617. constructor ParamSwitchChar.Create(const aSwitchChar: string);
  618. begin
  619. fSwithChar := aSwitchChar;
  620. end;
  621. { ParamValueSeparator }
  622. constructor ParamValueSeparator.Create(const aValueSeparator: string);
  623. begin
  624. fValueSeparator := aValueSeparator;
  625. end;
  626. { ParamCommand }
  627. constructor ParamCommand.Create(aPosition: Integer);
  628. begin
  629. fPosition := aPosition;
  630. end;
  631. end.