fpdoc.pp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. {
  2. FPDoc - Free Pascal Documentation Tool
  3. Copyright (C) 2000 - 2003 by
  4. Areca Systems GmbH / Sebastian Guenther, [email protected]
  5. See the file COPYING, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. }
  11. program FPDoc;
  12. uses
  13. SysUtils, Classes, Gettext, custapp,
  14. dGlobals, // GLobal definitions, constants.
  15. dwriter, // TFPDocWriter definition.
  16. dwlinear, // Linear (abstract) writer
  17. dw_LaTeX, // TLaTex writer
  18. dw_XML, // XML writer
  19. dw_dxml, // Delphi XML doc.
  20. dw_HTML, // HTML writer
  21. dw_ipflin, // IPF writer (new linear output)
  22. dw_man, // Man page writer
  23. dw_linrtf, // linear RTF writer
  24. dw_txt, fpdocproj, mkfpdoc; // TXT writer
  25. Type
  26. { TFPDocAplication }
  27. TFPDocAplication = Class(TCustomApplication)
  28. private
  29. FCreator : TFPDocCreator;
  30. FPackage : TFPDocPackage;
  31. FDryRun,
  32. FProjectFile : Boolean;
  33. FWriteProjectFile : String;
  34. Protected
  35. procedure OutputLog(Sender: TObject; const Msg: String);
  36. procedure ParseCommandLine;
  37. procedure Parseoption(const S: String);
  38. Procedure Usage(AnExitCode : Byte);
  39. Procedure DoRun; override;
  40. Public
  41. Constructor Create(AOwner : TComponent); override;
  42. Destructor Destroy; override;
  43. Function SelectedPackage : TFPDocPackage;
  44. end;
  45. Procedure TFPDocAplication.Usage(AnExitCode : Byte);
  46. Var
  47. I,P : Integer;
  48. S : String;
  49. L : TStringList;
  50. C : TFPDocWriterClass;
  51. Backend : String;
  52. begin
  53. Writeln(Format(SCmdLineHelp,[ExtractFileName(Paramstr(0))]));
  54. Writeln(SUsageOption010);
  55. Writeln(SUsageOption020);
  56. Writeln(SUsageOption030);
  57. Writeln(SUsageOption040);
  58. Writeln(SUsageOption050);
  59. Writeln(SUsageOption060);
  60. Writeln(SUsageOption070);
  61. Writeln(SUsageOption080);
  62. Writeln(SUsageOption090);
  63. Writeln(SUsageOption100);
  64. Writeln(SUsageOption110);
  65. Writeln(SUsageOption120);
  66. Writeln(SUsageOption130);
  67. Writeln(SUsageOption140);
  68. Writeln(SUsageOption150);
  69. Writeln(SUsageOption160);
  70. Writeln(SUsageOption170);
  71. Writeln(SUsageOption180);
  72. Writeln(SUsageOption190);
  73. Writeln(SUsageOption200);
  74. Writeln(SUsageOption210);
  75. Writeln(SUsageOption220);
  76. Writeln(SUsageOption230);
  77. Writeln(SUsageOption240);
  78. Writeln(SUsageOption250);
  79. L:=TStringList.Create;
  80. Try
  81. Backend:=FCreator.OPtions.Backend;
  82. If (Backend='') then
  83. begin
  84. Writeln;
  85. Writeln(SUsageFormats);
  86. EnumWriters(L);
  87. For I:=0 to L.Count-1 do
  88. begin
  89. S:=L[i];
  90. P:=Pos('=',S);
  91. Writeln(Format(' %s - %s',[Copy(S,1,P-1)+Space(10-p),Copy(S,P+1,Length(S))]));
  92. end;
  93. Writeln(SUsageBackendHelp);
  94. end
  95. else
  96. begin
  97. Writeln;
  98. Writeln(Format(SUsageFormatSpecific,[Lowercase(backend)]));
  99. C:=GetWriterClass(Backend);
  100. C.Usage(L);
  101. If L.Count>0 then
  102. For I:=0 to (L.Count-1) div 2 do
  103. begin
  104. S:=L[i*2];
  105. Writeln(Format('%s %s',[S+Space(30-Length(S)),L[(i*2)+1]]));
  106. end;
  107. end;
  108. Finally
  109. L.Free;
  110. end;
  111. Halt(AnExitCode);
  112. end;
  113. destructor TFPDocAplication.Destroy;
  114. begin
  115. FreeAndNil(FCreator);
  116. Inherited;
  117. end;
  118. function TFPDocAplication.SelectedPackage: TFPDocPackage;
  119. begin
  120. Result:=FPackage;
  121. if (FPackage=Nil) or (FPackage.Name='') then
  122. begin
  123. Writeln(SNeedPackageName);
  124. Usage(1);
  125. end;
  126. end;
  127. procedure TFPDocAplication.OutputLog(Sender: TObject; const Msg: String);
  128. begin
  129. Writeln(StdErr,Msg);
  130. end;
  131. procedure TFPDocAplication.ParseCommandLine;
  132. Function ProjectOpt(Const s : string) : boolean;
  133. begin
  134. Result:=(Copy(s,1,3)='-p=') or (Copy(s,1,10)='--project=');
  135. end;
  136. Function PackageOpt(Const s : string) : boolean;
  137. begin
  138. Result:=((Copy(s,1,3)='-a=') or (Copy(s,1,10)='--package='));
  139. end;
  140. var
  141. i : Integer;
  142. s : string;
  143. begin
  144. // Check project
  145. for i := 1 to ParamCount do
  146. begin
  147. s:=ParamStr(I);
  148. If ProjectOpt(S) then
  149. ParseOption(s);
  150. If (FCreator.Packages.Count=1) then
  151. FPackage:=FCreator.Packages[0]
  152. else if (FCreator.Options.DefaultPackageName<>'') then
  153. Fpackage:=FCreator.Packages.FindPackage(FCreator.Options.DefaultPackageName);
  154. end;
  155. If FCreator.Project.Packages.Count=0 then
  156. begin
  157. FPackage:=FCreator.Packages.Add as TFPDocPackage;
  158. end;
  159. // Check package
  160. for i := 1 to ParamCount do
  161. begin
  162. s:=ParamStr(I);
  163. If PackageOpt(S) then
  164. ParseOption(s);
  165. end;
  166. for i := 1 to ParamCount do
  167. begin
  168. s:=ParamStr(I);
  169. If Not (ProjectOpt(s) or PackageOpt(S)) then
  170. ParseOption(s);
  171. end;
  172. SelectedPackage; // Will print error if none available.
  173. end;
  174. procedure TFPDocAplication.Parseoption(Const S : String);
  175. procedure AddDirToFileList(List: TStrings; const ADirName, AMask: String);
  176. Var
  177. Info : TSearchRec;
  178. D : String;
  179. begin
  180. if (ADirName<>'') and not DirectoryExists(ADirName) then
  181. OutputLog(Self,'Directory '+ADirName+' does not exist')
  182. else
  183. begin
  184. if (ADirName='.') or (ADirName='') then
  185. D:=''
  186. else
  187. D:=IncludeTrailingPathDelimiter(ADirName);
  188. If (FindFirst(D+AMask,0,Info)=0) then
  189. try
  190. Repeat
  191. If (Info.Attr and faDirectory)=0 then
  192. List.Add(D+Info.name);
  193. Until FindNext(Info)<>0;
  194. finally
  195. FindClose(Info);
  196. end;
  197. end;
  198. end;
  199. procedure AddToFileList(List: TStrings; const FileName: String);
  200. var
  201. f: Text;
  202. s: String;
  203. begin
  204. if Copy(FileName, 1, 1) = '@' then
  205. begin
  206. AssignFile(f, Copy(FileName, 2, Length(FileName)));
  207. Reset(f);
  208. while not EOF(f) do
  209. begin
  210. ReadLn(f, s);
  211. List.Add(s);
  212. end;
  213. Close(f);
  214. end else
  215. List.Add(FileName);
  216. end;
  217. var
  218. i: Integer;
  219. Cmd, Arg: String;
  220. begin
  221. if (s = '-h') or (s = '--help') then
  222. Usage(0)
  223. else if s = '--hide-protected' then
  224. FCreator.Options.HideProtected := True
  225. else if s = '--warn-no-node' then
  226. FCreator.Options.WarnNoNode := True
  227. else if s = '--show-private' then
  228. FCreator.Options.ShowPrivate := False
  229. else if s = '--stop-on-parser-error' then
  230. FCreator.Options.StopOnParseError := True
  231. else if s = '--dont-trim' then
  232. FCreator.Options.donttrim := True
  233. else
  234. begin
  235. i := Pos('=', s);
  236. if i > 0 then
  237. begin
  238. Cmd := Copy(s, 1, i - 1);
  239. Arg := Copy(s, i + 1, Length(s));
  240. end
  241. else
  242. begin
  243. Cmd := s;
  244. SetLength(Arg, 0);
  245. end;
  246. if (Cmd = '--project') or (Cmd='-p') then
  247. begin
  248. FProjectFile:=True;
  249. FCreator.LoadProjectFile(Arg);
  250. end
  251. else if (Cmd = '--descr') then
  252. AddToFileList(SelectedPackage.Descriptions, Arg)
  253. else if (Cmd = '--descr-dir') then
  254. AddDirToFileList(SelectedPackage.Descriptions, Arg, '*.xml')
  255. else if (Cmd = '-f') or (Cmd = '--format') then
  256. begin
  257. Arg:=UpperCase(Arg);
  258. If FindWriterClass(Arg)=-1 then
  259. WriteLn(StdErr, Format(SCmdLineInvalidFormat, [Arg]))
  260. else
  261. FCreator.Options.BackEnd:=Arg;
  262. end
  263. else if (Cmd = '-l') or (Cmd = '--lang') then
  264. FCreator.Options.Language := Arg
  265. else if (Cmd = '-i') or (Cmd = '--input') then
  266. AddToFileList(SelectedPackage.Inputs, Arg)
  267. else if (Cmd = '--input-dir') then
  268. begin
  269. AddDirToFileList(SelectedPackage.Inputs, Arg,'*.pp');
  270. AddDirToFileList(SelectedPackage.Inputs, Arg,'*.pas');
  271. end
  272. else if (Cmd = '-o') or (Cmd = '--output') then
  273. SelectedPackage.Output := Arg
  274. else if (Cmd = '-v') or (Cmd = '--verbose') then
  275. FCreator.Verbose:=true
  276. else if (Cmd = '-n') or (Cmd = '--dry-run') then
  277. FDryRun:=True
  278. else if Cmd = '--content' then
  279. SelectedPackage.ContentFile := Arg
  280. else if Cmd = '--import' then
  281. SelectedPackage.Imports.Add(Arg)
  282. else if Cmd = '--package' then
  283. begin
  284. If FProjectFile then
  285. FPackage:=FCreator.Packages.FindPackage(Arg)
  286. else
  287. FPackage.Name:=Arg;
  288. end
  289. else if Cmd = '--ostarget' then
  290. FCreator.Options.OSTarget := Arg
  291. else if Cmd = '--cputarget' then
  292. FCreator.Options.CPUTarget := Arg
  293. else if Cmd = '--mo-dir' then
  294. FCreator.Options.modir := Arg
  295. else if Cmd = '--parse-impl' then
  296. FCreator.Options.InterfaceOnly:=false
  297. else if Cmd = '--write-project' then
  298. FWriteProjectFile:=Arg
  299. else
  300. begin
  301. FCreator.Options.BackendOptions.Add(Cmd);
  302. FCreator.Options.BackendOptions.Add(Arg);
  303. end;
  304. end;
  305. end;
  306. Procedure TFPDocAplication.DoRun;
  307. begin
  308. {$IFDEF Unix}
  309. gettext.TranslateResourceStrings('/usr/local/share/locale/%s/LC_MESSAGES/fpdoc.mo');
  310. {$ELSE}
  311. gettext.TranslateResourceStrings('intl/fpdoc.%s.mo');
  312. {$ENDIF}
  313. WriteLn(STitle);
  314. WriteLn(Format(SVersion, [DefFPCVersion, DefFPCDate]));
  315. WriteLn(SCopyright);
  316. WriteLn;
  317. ParseCommandLine;
  318. if (FWriteProjectFile<>'') then
  319. FCreator.CreateProjectFile(FWriteProjectFile)
  320. else
  321. FCreator.CreateDocumentation(FPackage,FDryRun);
  322. WriteLn(SDone);
  323. Terminate;
  324. end;
  325. constructor TFPDocAplication.Create(AOwner: TComponent);
  326. begin
  327. inherited Create(AOwner);
  328. StopOnException:=true;
  329. FCreator:=TFPDocCreator.Create(Self);
  330. FCreator.OnLog:=@OutputLog;
  331. end;
  332. begin
  333. With TFPDocAplication.Create(Nil) do
  334. try
  335. Run;
  336. finally
  337. Free;
  338. end;
  339. end.