fpdoc.pp 9.3 KB

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