makestub.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. {
  2. makestub - pas2js stub generator
  3. Copyright (C) 2017 - 2020 by Michael Van Canneyt [email protected]
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. }
  10. program makestub;
  11. {$mode objfpc}
  12. {$H+}
  13. uses SysUtils, Classes, custapp, stubcreator;
  14. Type
  15. { TStubCreatorApplication }
  16. TStubCreatorApplication = Class(TCustomApplication)
  17. Private
  18. FCreator : TStubCreator;
  19. procedure PrintUsage(S: String);
  20. Protected
  21. function ParseOptions : Boolean;
  22. Procedure DoRun; override;
  23. Public
  24. Constructor Create(AOwner : TComponent); override;
  25. Destructor Destroy; override;
  26. end;
  27. { TStubCreatorApplication }
  28. procedure TStubCreatorApplication.PrintUsage(S : String);
  29. begin
  30. {AllowWriteln}
  31. if S<>'' then
  32. Writeln('Error : ',S);
  33. writeln('usage: stubcreator options');
  34. writeln;
  35. writeln('Where options is one or more of');
  36. Writeln('-h --help This text');
  37. writeln('-i --input=file Is the file to be read by the parser');
  38. writeln('-I --include=dir Add dir to include path');
  39. writeln('-o --output=file Output file name. If not specified, standard output is assumed ');
  40. Writeln('-c --config=filename Read ini file with configuration');
  41. Writeln('-H --header=filename Add file header using contents of file "filename"');
  42. Writeln('-f --forwardclasses[=list]');
  43. Writeln(' Generate forward definitions for list of classes. If empty, for all classes.');
  44. {AllowWriteln-}
  45. ExitCode:=Ord(S<>'');
  46. end;
  47. function TStubCreatorApplication.ParseOptions : Boolean;
  48. Var
  49. S : String;
  50. begin
  51. Result:=False;
  52. S:=CheckOptions('d:i:o:c:h:f:H:I:',['help','input:','output:','forwardclasses::',
  53. 'config:','linenumberwidth:','define:','header:',
  54. 'include:']);
  55. if (S<>'') or HasOption('h','help') then
  56. begin
  57. PrintUsage(S);
  58. Exit;
  59. end;
  60. FCreator.InputFileName:=GetOptionValue('i','input');
  61. FCreator.OutputFileName:=GetOptionValue('o','output');
  62. FCreator.HeaderFileName:=GetOptionValue('H','header');
  63. If HasOption('d','define') then
  64. for S in GetOptionValues('d','define') do
  65. FCreator.Defines.Add(S);
  66. If HasOption('I','include') then
  67. for S in GetOptionValues('i','include') do
  68. FCreator.IncludePaths.Add(S);
  69. if Hasoption('f','forwardclasses') then
  70. FCreator.ForwardClasses:=GetOptionValue('f','forwardclasses');
  71. if (FCreator.HeaderFileName<>'') and Not FileExists(FCreator.HeaderFileName) then
  72. begin
  73. PrintUsage(Format('Header file "%s"does not exist',[FCreator.HeaderFileName]));
  74. Exit;
  75. end;
  76. Result:=True;
  77. end;
  78. { TStubCreatorApplication }
  79. procedure TStubCreatorApplication.DoRun;
  80. begin
  81. Terminate;
  82. If not ParseOptions then
  83. exit;
  84. FCreator.Execute;
  85. end;
  86. constructor TStubCreatorApplication.Create(AOwner: TComponent);
  87. begin
  88. inherited Create(AOwner);
  89. FCreator:=TStubCreator.Create(Self);
  90. StopOnException:=True;
  91. end;
  92. destructor TStubCreatorApplication.Destroy;
  93. begin
  94. FreeAndNil(FCreator);
  95. inherited Destroy;
  96. end;
  97. Var
  98. Application : TStubCreatorApplication;
  99. begin
  100. Application:=TStubCreatorApplication.Create(Nil);
  101. Application.Initialize;
  102. Application.Run;
  103. Application.Free;
  104. end.