Browse Source Download (without any required ccan dependencies)

Module:

opt

Summary:

simple command line parsing

Author:

Rusty Russell <rusty@rustcorp.com.au>

Dependencies:

Description:

Simple but powerful command line parsing.

See Also:

Example:

#include <ccan/opt/opt.h>
#include <stdio.h>
#include <stdlib.h>

static bool someflag;
static int verbose;
static char *somestring;

static struct opt_table opts[] = {
     OPT_WITHOUT_ARG("--verbose|-v", opt_inc_intval, &verbose,
                     "Verbose mode (can be specified more than once)"),
     OPT_WITHOUT_ARG("--someflag", opt_set_bool, &someflag,
                     "Set someflag"),
     OPT_WITH_ARG("--somefile=<filename>", opt_set_charp, opt_show_charp,
                  &somestring, "Set somefile to <filename>"),
     OPT_WITHOUT_ARG("--usage|--help|-h", opt_usage_and_exit,
                     "args...\nA silly test program.",
                     "Print this message."),
     OPT_ENDTABLE
};

int main(int argc, char *argv[])
{
     int i;

     opt_register_table(opts, NULL);
     // For fun, register an extra one.
     opt_register_noarg("--no-someflag", opt_set_invbool, &someflag,
                        "Unset someflag");
     if (!opt_parse(&argc, argv, opt_log_stderr))
             exit(1);

     printf("someflag = %i, verbose = %i, somestring = %s\n",
            someflag, verbose, somestring);
     printf("%u args left over:", argc - 1);
     for (i = 1; i < argc; i++)
             printf(" %s", argv[i]);
     printf("\n");
     return 0;
}

License:

GPL (v2 or any later version)