Command Line Options

Command Line Options — Support for long options

Synopsis


#include <monitor/monitor.h>


            mon_opt;
enum        mon_opt_type;
enum        mon_opt_flags;
#define     MON_OPT_MASK
int         mon_getopt                      (int argc,
                                             char *argv[],
                                             const mon_opt options[],
                                             unsigned int num_options);
enum        mon_opt_error;
const char* mon_opt_errstr                  (mon_opt_error err);
void        mon_usage                       (const mon_opt options[],
                                             unsigned int num_options);

Description

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_NONEthe option takes no argument.
MON_OPT_FLAGthe option is a flag. The variable pointed by the valueptr field will be set to 1 if the option is found.
MON_OPT_STRINGthe option takes a string argument.
MON_OPT_INTthe option takes an integer argument.
MON_OPT_COUNTERthe option is a counter. The variable pointed by the valueptr field will be incremented by each occurance of the option.
MON_OPT_DOUBLEthe 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_OPTIONALthe option's argument is optional.
MON_OPT_DEFAULTthe option has a default value (the value of the variable pointed by the valueptr field before calling mon_getopt()).

MON_OPT_MASK

#define MON_OPT_MASK		0xff

Bitmask to separate mon_opt_type from mon_opt_flags.


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_EOFthere are no more options.
MON_OPT_MISSING_ARGthe argument is missing for an option that requires one.
MON_OPT_EXTRA_ARGan option that does not take an argument has one.
MON_OPT_UNKNOWN_LONGunknown long option.
MON_OPT_UNKNOWN_SHORTunknown short option.

mon_opt_errstr ()

const char* mon_opt_errstr                  (mon_opt_error err);

Returns a textual description for a mon_opt_error.

err :a mon_opt_error code.
Returns :the description of the error code.

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.