cmpoptions.pas 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. unit cmpoptions;
  2. {$mode objfpc}
  3. interface
  4. type
  5. { TOptions }
  6. TOptions = class(TObject)
  7. private
  8. FHelp: boolean;
  9. FNoDestFileExtension: boolean;
  10. FNoSourceFileExtension: boolean;
  11. FSilent: boolean;
  12. FSourceFileExtension: String;
  13. FSourceMask: String;
  14. FDestFileExtension: String;
  15. FDestPath: String;
  16. public
  17. constructor Create;
  18. procedure LoadParams;
  19. property Help : boolean read FHelp write FHelp;
  20. property SourceMask : String read FSourceMask write FSourceMask;
  21. property SourceFileExtension : String read FSourceFileExtension write FSourceFileExtension;
  22. property NoSourceFileExtension: boolean read FNoSourceFileExtension;
  23. property DestPath : String read FDestPath write FDestPath;
  24. property DestFileExtension : String read FDestFileExtension write FDestFileExtension;
  25. property NoDestFileExtension : boolean read FNoDestFileExtension;
  26. property Silent : boolean read FSilent;
  27. end;
  28. implementation
  29. uses SysUtils;
  30. { TOptions }
  31. constructor TOptions.Create;
  32. begin
  33. FHelp := false;
  34. FSourceMask := '';
  35. FNoSourceFileExtension := false;
  36. FDestFileExtension := '';
  37. FDestPath := '';
  38. FSilent := false;
  39. end;
  40. procedure TOptions.LoadParams;
  41. var
  42. i: integer;
  43. sParam : Char;
  44. sValue : String;
  45. IsInvalidParam: boolean;
  46. begin
  47. if ParamCount = 0 then FHelp := true
  48. else FHelp := false;
  49. FSourceMask := IncludeTrailingBackslash(GetCurrentDir) + '*.*';
  50. FSourceFileExtension := '';
  51. FNoSourceFileExtension := false;
  52. FDestPath := IncludeTrailingBackslash(GetCurrentDir);
  53. FDestFileExtension := '';
  54. FNoDestFileExtension := false;
  55. FSilent := false;
  56. for i := 1 to ParamCount do
  57. begin
  58. if copy(ParamStr(i), 1, 1) = '-' then
  59. begin
  60. sParam := copy(ParamStr(i) + ' ', 2, 1)[1];
  61. sValue := copy(ParamStr(i), 3, length(ParamStr(i)));
  62. IsInvalidParam := false;
  63. case sParam of
  64. 'h': FHelp := true;
  65. 'm': begin
  66. FSourceMask := sValue;
  67. if copy(FSourceMask, 1, 2) = '\\' then FSourceMask := ExpandUNCFileName(FSourceMask)
  68. else FSourceMask := ExpandFileName(FSourceMask);
  69. if ExtractFileName(FSourceMask) = '' then FSourceMask := FSourceMask + '*';
  70. end;
  71. 'n': begin
  72. FSourceFileExtension := sValue;
  73. FNoSourceFileExtension := FSourceFileExtension = '';
  74. end;
  75. 'd': begin
  76. FDestPath := sValue;
  77. if copy(FDestPath, 1, 2) = '\\' then FDestPath := ExpandUNCFileName(FDestPath)
  78. else FDestPath := ExpandFileName(FDestPath);
  79. end;
  80. 'e': begin
  81. FDestFileExtension := sValue;
  82. FNoDestFileExtension := FDestFileExtension = '';
  83. end;
  84. 's': FSilent := true;
  85. else begin
  86. FHelp := true;
  87. writeln(format('invalid param "%s"', [ParamStr(i)]));
  88. end;
  89. end;
  90. end
  91. else IsInvalidParam := true;
  92. if (FNoSourceFileExtension = false) and
  93. (FSourceFileExtension = '') then
  94. begin
  95. FSourceFileExtension := ExtractFileExt(FSourceMask);
  96. end
  97. else if (ExtractFileExt(FSourceMask) <> '') and
  98. (FSourceFileExtension <> '') and
  99. (ExtractFileExt(FSourceMask) <> FSourceFileExtension) then
  100. begin
  101. writeln(format('parameter conflict: different sourcefile extension "%s" and "%s"',
  102. [ExtractFileExt(FSourceMask), FSourceFileExtension]));
  103. FHelp := true;
  104. end;
  105. if IsInvalidParam then
  106. begin
  107. FHelp := true;
  108. writeln(format('invalid param "%s"', [ParamStr(i)]));
  109. end;
  110. end;
  111. end;
  112. end.