libamxrt
0.4.2
Ambiorix Run Time Library
|
Functions | |
void | amxrt_cmd_line_options_reset (void) |
Removes all default options. More... | |
int | amxrt_cmd_line_add_option (int id, char short_option, const char *long_option, int has_args, const char *doc, const char *arg_doc) |
Adds a command line option definition. More... | |
void | amxrt_cmd_line_set_usage_doc (const char *usage) |
Set the overall usage documentation string. More... | |
int | amxrt_cmd_line_parse (int argc, char *argv[], amxrt_arg_fn_t fn) |
Starts parsing the command line options. More... | |
void | amxrt_cmd_line_parse_assignment (const char *option, bool force) |
Parses an command line option or an argument with an assignment. More... | |
void | amxrt_print_usage (void) |
Prints the usage information and all available options to stdout. More... | |
The command line option parser, reads the options passed on the command line and passes them to the configuration.
The command line option parser can handle short options (starting with a single '-') and long options (staring with a double '-'). Options must be put before any other arguments on the command line. The command line option parser stops at the first argument that is not an option (not starting with a '-').
The command line option parser uses getopt_long to parse the options.
The default options handled are:
If the default options doesn't fit your usage, they can be removed by calling amxrt_cmd_line_options_reset.
Adding new options (or re-adding some of the default options) can be done using: amxrt_cmd_line_add_option
Use amxrt_cmd_line_parse to start parsing the given command line options.
The syntax for parsing options at command line is:
int amxrt_cmd_line_add_option | ( | int | id, |
char | short_option, | ||
const char * | long_option, | ||
int | has_args, | ||
const char * | doc, | ||
const char * | arg_doc | ||
) |
Adds a command line option definition.
This function adds a command line option definition to the list of accepted options.
id | the id of the option, if 0 is given the short_option is used as id. |
short_option | a single character representing the short option/ |
long_option | a string literal containing the name of the long option without the double '-' |
has_args | must be one of no_argument (if the option does not take an argument), required_argument (if the option requires an argument) or optional_argument (if the takes an optional argument). |
doc | a string literal describing the option |
arg_doc | a string literal describing the argument or NULL if the option doesn't have an argument |
Definition at line 206 of file amxrt_args.c.
void amxrt_cmd_line_options_reset | ( | void | ) |
Removes all default options.
Calling this function will remove all defined default options. If some of them are needed they can be re-added using amxrt_cmd_line_add_option
Definition at line 200 of file amxrt_args.c.
int amxrt_cmd_line_parse | ( | int | argc, |
char * | argv[], | ||
amxrt_arg_fn_t | fn | ||
) |
Starts parsing the command line options.
Typically this function is called from within the applications main and the argc and argv arguments of the main are passed to this method.
Optionally a function pointer can be provided. This function will be called for each option encountered before the default option handler is used. If the argument handle function returns 0, it indicates that the option is handled and should not be passed to the default handler, when the callback function returns -1 the option parser will stop and the application should exit, when the callback function return -2, the default command line argument option handler is called. If the callback function returns anything else then 0,-1 or -2, it is considered an error and the command line option parser will stop.
The callback function must match this signature
argc | the number of arguments available |
argv | the vector containing the commandline arguments |
fn | (optional) a pointer to a callback function to handle the option or NULL |
Definition at line 236 of file amxrt_args.c.
void amxrt_cmd_line_parse_assignment | ( | const char * | option, |
bool | force | ||
) |
Parses an command line option or an argument with an assignment.
This function parses a command line option or command line argument with an assignment.
The key will be added to the config options. The config can be retrieved using amxrt_get_config.
Example:
The key can be any string without spaces. The value can a simple value or a JSON string.
If the key contains dots (.), is is used as a path to the config option.
option | the command line option value or the command line argument. |
force | when set to true, odl config sections can not overwrite this value. |
Definition at line 296 of file amxrt_args.c.
void amxrt_cmd_line_set_usage_doc | ( | const char * | usage | ) |
Set the overall usage documentation string.
When the function amxrt_print_usage the usage documentation is printed to stdout. On the first line the global usage is printed.
Example (default string)
The first part (until the first space) is the name of the application, this is added automatically by amxrt_print_usage (taken from argv[0] and should not be added to the provided string.
Example:
if after this line amxrt_print_usage is called (assuming that the name of the application is "amxrt") the output will look like this:
usage | A string literal containing the usage overview. |
Definition at line 230 of file amxrt_args.c.
void amxrt_print_usage | ( | void | ) |
Prints the usage information and all available options to stdout.
Using the usage doc string and the defined options the usage information is created and printed to stdout.
The usage doc string can be set using amxrt_cmd_line_set_usage_doc.
Options can be added using amxrt_cmd_line_add_option or all removed using amxrt_cmd_line_options_reset.
The default usage information is: Example:
Definition at line 137 of file amxrt_user_output.c.