fpdoc.pp 9.4 KB

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