paramparser.pp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. {
  2. FPCResLipo - Free Pascal External Resource Thinner
  3. Part of the Free Pascal distribution
  4. Copyright (C) 2008 by Giulio Bernardi
  5. Handles the parsing of parameters
  6. See the file COPYING, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. }
  12. unit paramparser;
  13. {$MODE OBJFPC} {$H+}
  14. interface
  15. uses
  16. Classes, SysUtils, externaltypes;
  17. type
  18. EParametersException = class(Exception);
  19. EOutputFileAlreadySetException = class(EParametersException);
  20. EUnknownParameterException = class(EParametersException);
  21. EArgumentMissingException = class(EParametersException);
  22. EUnknownEndianessException = class(EParametersException);
  23. type
  24. { TParameters }
  25. TParameters = class
  26. private
  27. fHelp : boolean;
  28. fVersion : boolean;
  29. fVerbose : boolean;
  30. fInputFiles : TStringList;
  31. fOutputFile : string;
  32. fEndianess : byte;
  33. procedure ParseOutputFile(aList : TStringList; var index : integer; const parname : string);
  34. procedure ParseEndianess(aList : TStringList; var index : integer; const parname : string);
  35. function DoOptionalArgument(aList : TStringList; const i : integer) : string;
  36. function DoMandatoryArgument(aList : TStringList; const i : integer) : string;
  37. function IsParameter(const s : string) : boolean;
  38. function ParamsToStrList : TStringList;
  39. protected
  40. public
  41. constructor Create;
  42. destructor Destroy; override;
  43. procedure Parse;
  44. property Help : boolean read fHelp;
  45. property Version : boolean read fVersion;
  46. property Verbose : boolean read fVerbose;
  47. property InputFiles : TStringList read fInputFiles;
  48. property OutputFile : string read fOutputFile write fOutputFile;
  49. property Endianess : byte read fEndianess write fEndianess;
  50. end;
  51. implementation
  52. uses
  53. msghandler;
  54. { TParameters }
  55. procedure TParameters.ParseOutputFile(aList: TStringList; var index: integer;
  56. const parname : string);
  57. begin
  58. if fOutputFile<>'' then
  59. raise EOutputFileAlreadySetException.Create('');
  60. inc(index);
  61. fOutputFile:=DoMandatoryArgument(aList,index);
  62. if fOutputFile='' then
  63. raise EArgumentMissingException.Create(parname);
  64. end;
  65. procedure TParameters.ParseEndianess(aList: TStringList; var index: integer;
  66. const parname: string);
  67. var tmp : string;
  68. begin
  69. inc(index);
  70. tmp:=LowerCase(DoMandatoryArgument(aList,index));
  71. if tmp='' then
  72. raise EArgumentMissingException.Create(parname);
  73. if tmp='big' then fEndianess:=EXT_ENDIAN_BIG
  74. else if tmp='little' then fEndianess:=EXT_ENDIAN_LITTLE
  75. else raise EUnknownEndianessException.Create(tmp);
  76. end;
  77. function TParameters.DoOptionalArgument(aList: TStringList; const i: integer
  78. ): string;
  79. begin
  80. Result:='';
  81. if aList.Count>i then
  82. begin
  83. if not IsParameter(aList[i]) then
  84. Result:=aList[i];
  85. end;
  86. end;
  87. function TParameters.DoMandatoryArgument(aList: TStringList; const i: integer
  88. ): string;
  89. begin
  90. Result:='';
  91. if aList.count>i then
  92. Result:=aList[i];
  93. end;
  94. function TParameters.IsParameter(const s: string): boolean;
  95. begin
  96. Result:=false;
  97. if length(s)<=1 then exit;
  98. if copy(s,1,1)='-' then Result:=true;
  99. end;
  100. function TParameters.ParamsToStrList: TStringList;
  101. var i : integer;
  102. begin
  103. Result:=TStringList.Create;
  104. try
  105. for i:=1 to ParamCount do
  106. Result.Add(ParamStr(i));
  107. except
  108. Result.Free;
  109. raise;
  110. end;
  111. end;
  112. procedure TParameters.Parse;
  113. var fList : TStringList;
  114. tmp : string;
  115. i : integer;
  116. begin
  117. fList:=ParamsToStrList;
  118. try
  119. i:=0;
  120. while i<fList.Count do
  121. begin
  122. tmp:=fList[i];
  123. Messages.DoVerbose(Format('parsing parameter ''%s''',[tmp]));
  124. if IsParameter(tmp) then
  125. begin
  126. if ((tmp='--help') or (tmp='-h') or (tmp='-?')) then
  127. fHelp:=true
  128. else if ((tmp='--version') or (tmp='-V')) then
  129. fVersion:=true
  130. else if ((tmp='--verbose') or (tmp='-v')) then
  131. fVerbose:=true
  132. else if ((tmp='-o') or (tmp='--output')) then
  133. ParseOutputFile(fList,i,tmp)
  134. else if ((tmp='-e') or (tmp='--endian')) then
  135. ParseEndianess(fList,i,tmp)
  136. else
  137. raise EUnknownParameterException.Create(tmp);
  138. end
  139. else
  140. fInputFiles.Add(tmp); //assume it is an input file
  141. inc(i);
  142. end;
  143. finally
  144. fList.Free;
  145. end;
  146. end;
  147. constructor TParameters.Create;
  148. begin
  149. fHelp:=false;
  150. fVersion:=false;
  151. fVerbose:=false;
  152. fInputFiles:=TStringList.Create;
  153. fOutputFile:='';
  154. fEndianess:=EXT_ENDIAN_BIG;
  155. end;
  156. destructor TParameters.Destroy;
  157. begin
  158. fInputFiles.Free;
  159. end;
  160. end.