R – n implementation of “getopt” for Delphi

comdelphiparsing

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;
                }
        }

Best Solution

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.

Related Question