Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

64 řádky
1.6 KiB

  1. unit uutlCommandLine;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. SysUtils, getopts;
  6. { Forward definitions so getopts doesn't have to be used }
  7. type
  8. TOption = getopts.TOption;
  9. const
  10. No_Argument = getopts.No_Argument;
  11. Required_Argument = getopts.Required_Argument;
  12. Optional_Argument = getopts.Optional_Argument;
  13. type
  14. TOptionHandler = procedure (const opt: string; const OptArg: string);
  15. TOptions = array[1..10] of TOption;
  16. POptions = ^TOptions;
  17. (*
  18. Process all arguments as defined in ShortOpts and LongOpts
  19. Arguments:
  20. ShortOpts - short option string (see getopts)
  21. LongOpts - pointer to the first element of an array of long options, terminated by an empty option record (see getopts)
  22. Handler - pointer to a function called to handle each option
  23. Result:
  24. The index of the first option not processed (this correctly handles the -- delimiter)
  25. *)
  26. function utlHandleAllOptions(ShortOpts : String; LongOpts : POptions; Handler: TOptionHandler): integer;
  27. // Get program binary name suitable for Usage statements
  28. function utlUsageProgramName: string;
  29. implementation
  30. function utlHandleAllOptions(ShortOpts : String; LongOpts : POptions; Handler: TOptionHandler): integer;
  31. var
  32. optIndex: integer;
  33. opt: string;
  34. begin
  35. optIndex:= 0;
  36. while True do begin
  37. opt:= GetLongOpts(ShortOpts, POption(LongOpts), optIndex);
  38. if opt = EndOfOptions then
  39. break;
  40. if opt = #0 then
  41. opt:= LongOpts^[optIndex].Name;
  42. Handler(opt, getopts.optArg);
  43. end;
  44. Result:= getopts.OptInd;
  45. end;
  46. function utlUsageProgramName: string;
  47. begin
  48. Result:= ChangeFileExt(ExtractFileName(ParamStr(0)), '');
  49. end;
  50. end.