cr8r_opt Struct Reference

Description of a command line option A list of these, terminated with a certain sentinel value, must be passed to cr8r_opt_parse. More...

Data Fields

void * dest
 POINTER to variable/memory to store result in. More...
 
bool found
 Flag indicating whether or not the option was found. More...
 
int arg_mode
 0 indicates this option does not take an argument, 1 indicates this option has a required argument, and 2 indicates this option has an optional argument. More...
 
const char * short_name
 The short name for this option. More...
 
const char * long_name
 The long name for this option. More...
 
const char * description
 Description of this option, for printing in the command help.
 
bool(* on_opt )(cr8r_opt *self, char *opt)
 Function to call when this option is encountered. More...
 
bool(* on_missing )(cr8r_opt *self)
 Function to call after reading command line arguments if this option was not found. More...
 

Detailed Description

Description of a command line option A list of these, terminated with a certain sentinel value, must be passed to cr8r_opt_parse.

See CR8R_OPT_GENERIC_OPTIONAL and CR8R_OPT_GENERIC_REQUIRED for macros that can automatically generate these descriptions

Definition at line 20 of file opts.h.

Field Documentation

◆ dest

void* cr8r_opt::dest

POINTER to variable/memory to store result in.

on_opt and on_missing are responsible for setting this. To have a default value, the variable pointed by dest may be initialized or on_missing may be used. May be NULL. Can also be used to provide CUSTOM callbacks with extra data as well as a pointers to memory to initialize.

Definition at line 26 of file opts.h.

◆ found

bool cr8r_opt::found

Flag indicating whether or not the option was found.

Should not be set, managed by cr8r_opt_parse. Automatically set to false before parsing starts and true when this argument is found (but after calling on_opt if applicable). Can be checked after parsing is done to find which arguments were/weren't found, as an alternative to on_missing. Can also be checked in on_opt to determine if this option has been encountered before (ie is a duplicate).

Definition at line 33 of file opts.h.

◆ arg_mode

int cr8r_opt::arg_mode

0 indicates this option does not take an argument, 1 indicates this option has a required argument, and 2 indicates this option has an optional argument.

For long form options, --long-name=argument (argument in same command line argument after an '=') or --long-name argument (argument in next command line argument) are permitted. If either of these occurs, the argument will be passed to on_opt. Otherwise (if there is no '=' and the next command line argument starts with '-'), the option is considered to not havean argument. If arg_required is true, this is an error and on_opt is not called. If arg_required is false, on_opt is called with NULL. For short form options, note that -ab=argument and -ab argument are both valid ways of providing a short form option a with no argument and b with argument "argument", but -bargument is not a valid way of doing this. This would be interpreted as 9 short form options with no arguments, or possibly an argument to t depending on what follows.

Definition at line 45 of file opts.h.

◆ short_name

const char* cr8r_opt::short_name

The short name for this option.

Must not contain '-', '=', ' ', or more than one character. However, multibyte characters are allowed. May be NULL, but if both short_name and long_name are NULL then this indicates the end of the option list. Any character in a short form option group (command line argument starting with a '-') is checked using this name.

Definition at line 51 of file opts.h.

◆ long_name

const char* cr8r_opt::long_name

The long name for this option.

Must not contain '=' or ' ', but may contain any number of characters including multibyte characters and '-'. May be NULL, but if both short_name and long_name are NULL then this indicates the end of the option list. Any long form option (part of a command line argument which starts with "--" and stops if an '=' is encountered) is checked using this name.

Definition at line 58 of file opts.h.

◆ on_opt

bool(* cr8r_opt::on_opt) (cr8r_opt *self, char *opt)

Function to call when this option is encountered.

If NULL, any argument is ignored and the only effect of encountering this option is setting found (and hence not calling on_missing if applicable). If this function returns false, the argument to this option is considered invalid. The argument to the option, opt, can be supplied in the command line either in the same command line argument as the option but separated by an '=', or in the next command line argument but not preceded by a '-'. If no such argument is found, opt is NULL, so if arg_required is not set, on_opt must be prepared to deal with that. found is set after calling this function, so it can be used to check if this option is provided more than once.

Definition at line 69 of file opts.h.

◆ on_missing

bool(* cr8r_opt::on_missing) (cr8r_opt *self)

Function to call after reading command line arguments if this option was not found.

If the option is not found and this function is NULL or returns false, this option is considered missing. This function should return true if it is ok for this option to be missing ( cr8r_opt_missing_optional can be used to always return true, also see CR8R_OPT_GENERIC_OPTIONAL ). This function can also be used to initialize a variable in a default way if it is not provided as an option, eg by randomizing it, offering more flexibility than simply giving the variable a default value.

Definition at line 76 of file opts.h.


The documentation for this struct was generated from the following file: