Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an implementation of "getopt" for Delphi?

It doesn't get much easier than using getopt() to parse command line parameters in C/C++.

Is there anything similar for Delphi? Or ideally, with the same syntax? I know Delphi supports FindCmdLineSwitch and ParamStr(), but those still require some additional parsing.

I want something that works like getopt() in C. Something that easily allows basic toggle switches, as well as capturing a value after a switch. See below for some example C code to see what I'm talking about:

void print_help()
{
        printf("usage:\n") ;
        printf("\t\t-i set input file\n") ;
        printf("\t\t-o set output file\n") ;
        printf("\t\t-c set config file\n") ;
        printf("\t\t-h print this help information\n") ;
        printf("\t\t-v print version\n") ;
}
 char* input_file = NULL ;
        char *query=NULL;
          char opt_char=0;
        while ((opt_char = getopt(argc, argv, "i:q:vh")) != -1)
        {
                switch(opt_char)
                {
                        case 'h':
                                print_help();
                                exit(-1);
                                break;
                        case 'v':
                                print_version() ;
                                exit(-1) ;
                                break ;
                        case 'i':
                                input_file= optarg ;
                                break ;
                        case 'q':
                                query= optarg ;
                                break ;
                        default:
                                print_help();
                                exit(-1);
                                break;
                }
        }
like image 941
Mick Avatar asked May 07 '09 16:05

Mick


6 Answers

The getopts.pp file of the FPC RTL is self contained Delphi (Delphi2009 included) compatible unit that implements getopt :

Getopt implementation for Free Pascal, modeled after GNU getopt

The unit is available at the FPC SVN repository.

like image 113
Inoussa OUEDRAOGO Avatar answered Oct 16 '22 06:10

Inoussa OUEDRAOGO


There is an implementation TGetOpt, claiming to

implement a getopt variant for Delphi. It is nearly POSIX compatible, supporting long options, required, optional and no arguments

You can find it here.

like image 22
mghie Avatar answered Oct 16 '22 07:10

mghie


See TCommandParser, described in this EDN article http://edn.embarcadero.com/print/40404 and available for download on CodeCentral http://cc.embarcadero.com/item/27574. TCommandParser should also be included in the Delphi demos now. I made the code freely available to Embarcadero before I left, so hopefully they'll include it in the framework or something like it at some point.

like image 42
John Kaster Avatar answered Oct 16 '22 06:10

John Kaster


Nothing built in - but you can make one pretty easily. This should be close to what you are used to

TsoCommandLineParser = class
private
  fArguments:TStringList;
public
  constructor Create();
  destructor Destroy(); override;

  function GetOpt(const pArgument:string; const pDefaultValue:string = ''):string;
end;


constructor TsoCommandLineParser.Create();
var
  i:Integer;
begin
  inherited Create();
  fArguments := TStringList.Create();
  for i := 1 to ParamCount() do
  begin
    fArguments.Add(ParamStr(i));
  end;
end;

destructor TsoCommandLineParser.Destroy();
begin
  fArguments.Free();
  inherited Destroy();
end;

function TsoCommandLineParser.GetOpt(const pArgument:string; const pDefaultValue:string = ''):string;
var
  i:Integer;
begin
  i := fArguments.IndexOfName(pArgument);
  if i > -1 then
  begin
    Result := fArguments.ValueFromIndex[i];
  end
  else
  begin
    Result := pDefaultValue;
  end;
end;
like image 20
Darian Miller Avatar answered Oct 16 '22 07:10

Darian Miller


What is wrong about FindCmdLineSwitch in the SysUtils unit?

if FindCmdLineSwitch('h',['-'],false) then
  Print_Help();
if FindCmdLineSwitch('v',['-'],false) then
  print_Version();

you will have to loop thru the params to get the values, but thats fairly simple to do:

if FindCmdLineSwitch('f',['-'],false) then
  for ix := 1 to paramcount do
    if (paramStr(ix) = '-f') and (ix < paramcount) then
      begin
        if fileExists( ParamStr(ix+1) ) then
          filename := ParamStr(ix+1);
        break;
      end
like image 3
skamradt Avatar answered Oct 16 '22 07:10

skamradt


There is also a new parser called delphi-argparse that is relevant to this question.

like image 2
Merlin W. Avatar answered Oct 16 '22 06:10

Merlin W.