Command Line OptionsCommand Line Options — Support for long options | |
Details
mon_opt
typedef struct {
const char *long_opt;
int short_opt;
unsigned int type;
void *valueptr;
const char *desc;
const char *valdesc;
} mon_opt;
Descriptor of a command line option.
| const char *long_opt; | name of the long option.
|
| int short_opt; | name of the short option.
|
| unsigned int type; | the option's type. It must be a value of mon_opt_type, possibly combined with
one or more of mon_opt_flags.
|
| void *valueptr; | pointer to where to store the option's value.
|
| const char *desc; | the description of the option.
|
| const char *valdesc; | description of the option's argument.
|
enum mon_opt_type
typedef enum {
MON_OPT_NONE,
MON_OPT_FLAG,
MON_OPT_STRING,
MON_OPT_INT,
MON_OPT_COUNTER,
MON_OPT_DOUBLE
} mon_opt_type;
Option types.
| MON_OPT_NONE | the option takes no argument.
|
| MON_OPT_FLAG | the option is a flag. The variable pointed by the valueptr
field will be set to 1 if the option is found.
|
| MON_OPT_STRING | the option takes a string argument.
|
| MON_OPT_INT | the option takes an integer argument.
|
| MON_OPT_COUNTER | the option is a counter. The variable pointed by the
valueptr field will be incremented by each occurance of
the option.
|
| MON_OPT_DOUBLE | the option takes a double argument.
|
enum mon_opt_flags
typedef enum {
MON_OPT_OPTIONAL = (1 << 8),
MON_OPT_DEFAULT = (1 << 9)
} mon_opt_flags;
Option flags.
| MON_OPT_OPTIONAL | the option's argument is optional.
|
| MON_OPT_DEFAULT | the option has a default value (the value of the variable
pointed by the valueptr field before calling
mon_getopt()).
|
mon_getopt ()
int mon_getopt (int argc,
char *argv[],
const mon_opt options[],
unsigned int num_options);
Retrieves the next option. optind and optarg are
updated just like when using the standard getopt().
| argc : | the number of arguments.
|
| argv : | array holding the command-line arguments.
|
| options : | a mon_opt array holding the option descriptions.
|
| num_options : | number of elements in options.
|
| Returns : | the index in options if an option is recognized or one of the values of
mon_opt_error.
|
enum mon_opt_error
typedef enum {
MON_OPT_EOF = -1,
MON_OPT_MISSING_ARG = -2,
MON_OPT_EXTRA_ARG = -3,
MON_OPT_UNKNOWN_LONG = -4,
MON_OPT_UNKNOWN_SHORT = -5
} mon_opt_error;
Result values of mon_getopt() when something prevents processing the next option.
| MON_OPT_EOF | there are no more options.
|
| MON_OPT_MISSING_ARG | the argument is missing for an option that requires one.
|
| MON_OPT_EXTRA_ARG | an option that does not take an argument has one.
|
| MON_OPT_UNKNOWN_LONG | unknown long option.
|
| MON_OPT_UNKNOWN_SHORT | unknown short option.
|
mon_usage ()
void mon_usage (const mon_opt options[],
unsigned int num_options);
Prints the list of known options to the standard output.
| options : | a mon_opt array holding the option descriptions.
|
| num_options : | number of elements in options.
|