prefixunits.pp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. {
  2. This file is part of the Free Component Library
  3. Copyright (c) 2022 by Michael Van Canneyt, [email protected]
  4. Prefix units in uses clause of a single program or unit.
  5. See the file COPYING.FPC, 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 prefixunits;
  12. {$mode objfpc}
  13. {$H+}
  14. uses cwstring, SysUtils, Classes, custapp, prefixer;
  15. type
  16. { TApplication }
  17. TApplication = class(TCustomApplication)
  18. Private
  19. FPrefixer : TPrefixer;
  20. Public
  21. Constructor create(aOwner : TComponent); override;
  22. Destructor Destroy; override;
  23. Procedure Usage(Err : string);
  24. Procedure DoRun; override;
  25. end;
  26. constructor TApplication.create(aOwner: TComponent);
  27. begin
  28. inherited create(aOwner);
  29. FPrefixer:=TPrefixer.Create(Self);
  30. end;
  31. destructor TApplication.Destroy;
  32. begin
  33. FreeAndNil(FPrefixer);
  34. inherited Destroy;
  35. end;
  36. procedure TApplication.Usage(Err: string);
  37. begin
  38. if Err<>'' then
  39. Writeln('Error: ',Err);
  40. Writeln('Usage: ',ExtractFileName(Paramstr(0)),' [OPTIONS] filename');
  41. Writeln('Where options is exactly one of');
  42. Writeln('-h or --help shows this help');
  43. Writeln('-f or --filename=FILE Filename to process, can be specifid simply');
  44. Writeln('-d or --dest-filename=FILE Destination filename to produce. Default is namespace.filename');
  45. Writeln('-k or --known-namespaces=FILE Name of file with known namespaces.');
  46. Writeln('-n or --namespace=NS Namespace to apply to file.');
  47. Writeln('-o or --option=option Option to pass to compiler.');
  48. Writeln('-b or --backup Create backups of existing files when overwriting.');
  49. writeln('-p --program Assume the sources are a program. This will not write a dotted file. Namespace is not needed');
  50. Writeln('-r or --replace Default is to create an include and use a define to separate dotted from non-dotted');
  51. Writeln(' Use this to replace the units clause as-is.');
  52. Writeln('All other options are passed as-is to the parser');
  53. Halt(Ord(Err<>''));
  54. end;
  55. procedure TApplication.DoRun;
  56. Const
  57. ShortOpts = 'bhf:k:n:o:d:rp';
  58. LongOpts : Array of string = ('backup','filename:','known-namespaces:','namespace:','option:','dest-filename:','replace','program');
  59. Var
  60. S : String;
  61. Opts : Array of string;
  62. Prog : Boolean;
  63. begin
  64. Terminate;
  65. S:=CheckOptions(Shortopts,LongOpts);
  66. if (S<>'') or HasOption('h','help') then
  67. Usage(S);
  68. FPrefixer.NameSpace:=GetOptionValue('n','namespace');
  69. if HasOption('r','replace') then
  70. FPrefixer.UnitFileMode :=fmReplace;
  71. FPrefixer.FileName:=GetOptionValue('f','filename');
  72. FPrefixer.DestFileName:=GetOptionValue('d','dest-filename');
  73. FPrefixer.CreateBackups:=HasOption('b','backup');
  74. if FPrefixer.FileName='' then
  75. begin
  76. Opts:=GetNonOptions(ShortOpts,LongOpts);
  77. if (Length(Opts)>0) then
  78. FPrefixer.FileName:=Opts[0];
  79. end;
  80. Opts:=GetOptionValues('o','option');
  81. Prog:=HasOption('p','program');
  82. For S in Opts do
  83. FPrefixer.Params.Add(S);
  84. if Not Prog and (FPrefixer.NameSpace='') then
  85. Usage('Namespace is required');
  86. FPrefixer.SkipDestFileName:=Prog;
  87. if (FPrefixer.FileName='') then
  88. Usage('Filename is required');
  89. S:=GetOptionValue('k','known-namespaces');
  90. if S='' then
  91. S:=ExtractFilePath(ParamStr(0))+'knownprefixes.txt';
  92. if FileExists(S) then
  93. FPrefixer.KnownNameSpaces.LoadFromFile(S);
  94. FPrefixer.Execute;
  95. end;
  96. begin
  97. With TApplication.Create(Nil) do
  98. try
  99. Initialize;
  100. Run;
  101. finally
  102. Free;
  103. end;
  104. end.