mkfpdocproj.pp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. program mkfpdocproj;
  2. {$mode objfpc}{$H+}
  3. uses
  4. Classes, SysUtils, CustApp, mgrfpdocproj;
  5. type
  6. { TManageFPDocProjectApplication }
  7. TManageFPDocProjectApplication = class(TCustomApplication)
  8. private
  9. FMGR : TFPDocProjectManager;
  10. FPackageName,
  11. FInputFileName,
  12. FOutputFileName,
  13. FCmd : String;
  14. FCmdArgs,
  15. FCmdOptions: TStrings;
  16. procedure AddDescrFiles;
  17. procedure AddDescriptionDirs;
  18. procedure AddInputDirs;
  19. procedure AddInputFiles;
  20. procedure RemoveInputFiles;
  21. procedure RemoveDescrFiles;
  22. function CheckCmdOption(C: Char; S: String): Boolean;
  23. function GetCmdOption(C: Char; S: String): String;
  24. procedure SetOptions(Enable: Boolean);
  25. protected
  26. procedure ParseOptions;
  27. Procedure Error(Const Msg : String);
  28. procedure Usage(AExitCode: Integer);
  29. procedure DoRun; override;
  30. public
  31. constructor Create(TheOwner: TComponent); override;
  32. Destructor Destroy; override;
  33. end;
  34. Resourcestring
  35. SErrNeedArgument = 'Option at position %d needs an argument: %s';
  36. { TManageFPDocProjectApplication }
  37. procedure TManageFPDocProjectApplication.Usage(AExitCode : Integer);
  38. begin
  39. // to be filled
  40. Halt(AExitCode);
  41. end;
  42. Function CheckOptionStr(O : String;Short : Char;Long : String): Boolean;
  43. begin
  44. Result:=(O='-'+short) or (O='--'+long) or (copy(O,1,Length(Long)+3)=('--'+long+'='));
  45. end;
  46. procedure TManageFPDocProjectApplication.ParseOptions;
  47. Function CheckOption(Index : Integer;Short : char;Long : String): Boolean;
  48. begin
  49. Result:=CheckOptionStr(ParamStr(Index),Short,Long);
  50. end;
  51. Function OptionArg(Var Index : Integer) : String;
  52. Var
  53. P : Integer;
  54. begin
  55. if (Length(ParamStr(Index))>1) and (Paramstr(Index)[2]<>'-') then
  56. begin
  57. If Index<ParamCount then
  58. begin
  59. Inc(Index);
  60. Result:=Paramstr(Index);
  61. end
  62. else
  63. Error(Format(SErrNeedArgument,[Index,ParamStr(Index)]));
  64. end
  65. else If length(ParamStr(Index))>2 then
  66. begin
  67. P:=Pos('=',Paramstr(Index));
  68. If (P=0) then
  69. Error(Format(SErrNeedArgument,[Index,ParamStr(Index)]))
  70. else
  71. begin
  72. Result:=Paramstr(Index);
  73. Delete(Result,1,P);
  74. end;
  75. end;
  76. end;
  77. Var
  78. I : Integer;
  79. S : String;
  80. begin
  81. I:=0;
  82. // We can't use the TCustomApplication option handling,
  83. // because they cannot handle [general opts] [command] [cmd-opts] [args]
  84. While (I<ParamCount) do
  85. begin
  86. Inc(I);
  87. if Checkoption(I,'i','input') then
  88. FInputFileName:=OptionArg(i)
  89. else if Checkoption(I,'o','output') then
  90. FOutputFileName:=OptionArg(i)
  91. else if CheckOption(I,'p','package') then
  92. FPackageName:=OptionArg(i)
  93. else if CheckOption(I,'h','help') then
  94. begin
  95. Usage(0);
  96. end
  97. else
  98. begin
  99. S:=ParamStr(I);
  100. If (S<>'') then
  101. begin
  102. if (S[1]<>'-') then
  103. begin
  104. if (FCmd='') then
  105. FCmd:=lowercase(S)
  106. else
  107. FCmdArgs.Add(S)
  108. end
  109. end
  110. else
  111. FCmdOptions.Add(S);
  112. end;
  113. Inc(I);
  114. end;
  115. if (FOutputFileName='') then
  116. FOutputFileName:=FInputFileName;
  117. If (FOutputFileName='') then
  118. Error('Need an output filename');
  119. if (FPackageName='') then
  120. Error('Need a package name');
  121. if (FCmd='') then
  122. Error('Need a command');
  123. end;
  124. procedure TManageFPDocProjectApplication.Error(Const Msg: String);
  125. begin
  126. Writeln('Error : ',Msg);
  127. Usage(1);
  128. end;
  129. Function TManageFPDocProjectApplication.CheckCmdOption(C : Char; S : String) : Boolean;
  130. Var
  131. I : integer;
  132. begin
  133. I:=0;
  134. Result:=False;
  135. While (Not Result) and (I<FCmdOptions.Count) do
  136. begin
  137. Result:=CheckOptionStr(FCmdOptions[i],C,S);
  138. Inc(I);
  139. end;
  140. end;
  141. Function TManageFPDocProjectApplication.GetCmdOption(C : Char; S : String) : String;
  142. Var
  143. I,P : integer;
  144. B : Boolean;
  145. begin
  146. I:=0;
  147. B:=False;
  148. While (Not B) and (I<FCmdOptions.Count) do
  149. begin
  150. B:=CheckOptionStr(FCmdOptions[i],C,S);
  151. if B then
  152. begin
  153. Result:=FCmdArgs[I];
  154. if (Length(S)>1) and (S[2]<>'-') then
  155. begin
  156. If I<FCmdArgs.Count-1 then
  157. begin
  158. Inc(I);
  159. Result:=FCmdArgs[I];
  160. end
  161. else
  162. Error(Format(SErrNeedArgument,[I,Result]));
  163. end
  164. else If length(Result)>2 then
  165. begin
  166. P:=Pos('=',Result);
  167. If (P=0) then
  168. Error(Format(SErrNeedArgument,[I,Result]))
  169. else
  170. Delete(Result,1,P);
  171. end;
  172. end;
  173. Inc(I);
  174. end;
  175. end;
  176. procedure TManageFPDocProjectApplication.AddDescriptionDirs;
  177. Var
  178. Recursive: Boolean;
  179. Mask : String;
  180. I : Integer;
  181. begin
  182. Recursive:=CheckCmdOption('r','recursive');
  183. Mask:=GetCmdOption('m','mask');
  184. For I:=0 to FCmdArgs.Count-1 do
  185. FMGr.AddDescrFilesFromDirectory(FCmdArgs[i],Mask,Recursive);
  186. end;
  187. procedure TManageFPDocProjectApplication.AddInputDirs;
  188. Var
  189. Recursive: Boolean;
  190. Options,Mask : String;
  191. I : Integer;
  192. begin
  193. Recursive:=CheckCmdOption('r','recursive');
  194. Mask:=GetCmdOption('m','mask');
  195. Options:=GetCmdOption('o','options');
  196. For I:=0 to FCmdArgs.Count-1 do
  197. FMGr.AddInputFilesFromDirectory(FCmdArgs[i],Mask,Options,Recursive);
  198. end;
  199. procedure TManageFPDocProjectApplication.AddInputFiles;
  200. Var
  201. Options : String;
  202. I : Integer;
  203. begin
  204. Options:=GetCmdOption('o','options');
  205. For I:=0 to FCmdArgs.Count-1 do
  206. FMGr.AddInputFile(FCmdArgs[i],Options);
  207. end;
  208. procedure TManageFPDocProjectApplication.RemoveInputFiles;
  209. Var
  210. I : Integer;
  211. begin
  212. For I:=0 to FCmdArgs.Count-1 do
  213. FMGr.RemoveInputFile(FCmdArgs[i]);
  214. end;
  215. procedure TManageFPDocProjectApplication.RemoveDescrFiles;
  216. Var
  217. I : Integer;
  218. begin
  219. For I:=0 to FCmdArgs.Count-1 do
  220. FMGr.RemoveDescrFile(FCmdArgs[i]);
  221. end;
  222. procedure TManageFPDocProjectApplication.AddDescrFiles;
  223. Var
  224. I : Integer;
  225. begin
  226. For I:=0 to FCmdArgs.Count-1 do
  227. FMGr.AddDescrFile(FCmdArgs[i]);
  228. end;
  229. procedure TManageFPDocProjectApplication.SetOptions(Enable : Boolean);
  230. Var
  231. I : Integer;
  232. begin
  233. For I:=0 to FCmdArgs.Count-1 do
  234. FMgr.SetOption(FCmdArgs[i],Enable);
  235. end;
  236. procedure TManageFPDocProjectApplication.DoRun;
  237. begin
  238. ParseOptions;
  239. if (FInputFileName='') then
  240. FMGR.AddPackage(FPackageName)
  241. else
  242. begin
  243. if (FCmd='expand-macros') then
  244. FMGR.ReadOptionFile(FInputFileName)
  245. else
  246. FMGR.ReadOptionFile(FInputFileName,FCMdArgs);
  247. FMGR.SelectPackage(FPackageName);
  248. end;
  249. if (FCmd='add-description-dirs') then
  250. AddDescriptionDirs
  251. else if (FCmd='add-input-dirs') then
  252. AddInputDirs
  253. else if (FCmd='add-input-files') then
  254. AddInputFiles
  255. else if (FCmd='add-descr-files') then
  256. AddDescrFiles
  257. else if (FCmd='remove-input-files') then
  258. RemoveInputFiles
  259. else if (FCmd='remove-descr-files') then
  260. RemoveDescrFiles
  261. else if (FCmd='set-options') then
  262. SetOptions(True)
  263. else if (FCmd='unset-options') then
  264. SetOptions(False)
  265. else if (FCMd<>'expand-macros') then
  266. Error(Format('Unknown command : "%s"',[FCmd]));
  267. FMgr.WriteOptionFile(FOutputFileName);
  268. Terminate;
  269. end;
  270. constructor TManageFPDocProjectApplication.Create(TheOwner: TComponent);
  271. begin
  272. inherited Create(TheOwner);
  273. StopOnException:=True;
  274. FCmdArgs:=TStringList.Create;
  275. FCmdOptions:=TStringList.Create;
  276. FMGR:=TFPDocProjectManager.Create(Self);
  277. end;
  278. destructor TManageFPDocProjectApplication.Destroy;
  279. begin
  280. FreeAndNil(FMGR);
  281. FreeAndNil(FCmdArgs);
  282. FreeAndNil(FCmdOptions);
  283. inherited Destroy;
  284. end;
  285. var
  286. Application: TManageFPDocProjectApplication;
  287. begin
  288. Application:=TManageFPDocProjectApplication.Create(nil);
  289. Application.Title:='Program to manipulate FPDoc project files';
  290. Application.Run;
  291. Application.Free;
  292. end.