prefixunits.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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('All other options are passed as-is to the parser');
  50. Halt(Ord(Err<>''));
  51. end;
  52. procedure TApplication.DoRun;
  53. Const
  54. ShortOpts = 'bhf:k:n:o:d:';
  55. LongOpts : Array of string = ('backup','filename:','known-namespaces:','namespace:','option:','dest-filename:');
  56. Var
  57. S : String;
  58. Opts : Array of string;
  59. begin
  60. Terminate;
  61. S:=CheckOptions(Shortopts,LongOpts);
  62. if (S<>'') or HasOption('h','help') then
  63. Usage(S);
  64. FPrefixer.NameSpace:=GetOptionValue('n','namespace');
  65. FPrefixer.FileName:=GetOptionValue('f','filename');
  66. FPrefixer.DestFileName:=GetOptionValue('d','dest-filename');
  67. FPrefixer.CreateBackups:=HasOption('b','backup');
  68. if FPrefixer.FileName='' then
  69. begin
  70. Opts:=GetNonOptions(ShortOpts,LongOpts);
  71. if (Length(Opts)>0) then
  72. FPrefixer.FileName:=Opts[0];
  73. end;
  74. Opts:=GetOptionValues('o','option');
  75. For S in Opts do
  76. FPrefixer.Params.Add(S);
  77. if (FPrefixer.NameSpace='') then
  78. Usage('Namespace is required');
  79. if (FPrefixer.FileName='') then
  80. Usage('Filename is required');
  81. S:=GetOptionValue('k','known-namespaces');
  82. if S='' then
  83. S:=ExtractFilePath(ParamStr(0))+'knownprefixes.txt';
  84. if FileExists(S) then
  85. FPrefixer.KnownNameSpaces.LoadFromFile(S);
  86. FPrefixer.Execute;
  87. end;
  88. begin
  89. With TApplication.Create(Nil) do
  90. try
  91. Initialize;
  92. Run;
  93. finally
  94. Free;
  95. end;
  96. end.