#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
Include dependency graph for cmdopt.h:
Go to the source code of this file.
Functions | |
void | parse_optstr (char *Cmdoptstr) |
parse_optstr(Cmdoptstr): constructs and returns the hidden Option_ array from the string Cmdoptstr. More... | |
int | get_options (int argc, char *const *argv) |
get_options(argc, argv): processes the command line (argc,argv) to find options encoded in the hidden array Cmdopts (length Cmdoptno). More... | |
int | optval_bool (char Och) |
optval_{bool|int|dbl|str}(Och [, Val]): these functions query the hidden Cmdopts[] array after the command line has been processed. More... | |
int | optval_int (char Och, int *Val) |
int | optval_dbl (char Och, double *Val) |
int | optval_str (char Och, char **Val) |
int | opt_defval (char Och, void *Val, const void *Defval) |
opt_defval(Och, Val, Defval): this routine provides an alternative to the optval_[...] functions. More... | |
char* | opt_helpstr (void) |
opt_helpstr(void): generates a "help string" from the option list hidden in Cmdopts[]. More... |
Support switches (-x), switch groups (-xYz), or command-line parameters (-p xxx).
Definition in file cmdopt.h.
|
parse_optstr(Cmdoptstr): constructs and returns the hidden Option_ array from the string Cmdoptstr. The length of the array is also stored in a hidden location. The option string is composed of the following tokens separated by whitespaces:- "x": stands for the Boolean command line option -x "xYz" : the command line options -x -Y -z (may be grouped together) "xd<name>" : option -x expecting a mandatory integer argument which is described as "name" in the help string. "xf<name>" : as above, expecting a floating-point argument "xs<name>" : as above, expecting a string argument 'x' can be the characters 'a'..'z', 'A'..'Z', '0'..'9', '#'. Invalid options are ignored. If nothing was found then Cmdopts==NULL and Cmdoptno==0. To be called only once. |
|
get_options(argc, argv): processes the command line (argc,argv) to find options encoded in the hidden array Cmdopts (length Cmdoptno). If an option is found then the corresponding Cmdopts[] member is updated to show which argv[] member contained it and if it had an argument, then it is attempted to be stored as well. Options not found or having malformed arguments will be set as "absent". Return value: the argv[] index of the first non-option member. The index is multiplied by -1 if an error occurred. |
|
optval_{bool|int|dbl|str}(Och [, Val]): these functions query the hidden Cmdopts[] array after the command line has been processed. Och is the option char and the value is returned in *Val. If the option char is invalid then a warning is printed. If the option was not present on the command line then *Val is not updated and the functions return 0, otherwise the Idx field from the corresponding Cmdopts record is returned. Val may be set to NULL. |
|
|
|
|
|
|
|
opt_defval(Och, Val, Defval): this routine provides an alternative to the optval_[...] functions. Och is the option character, Val is a void pointer to the variable which should hold the option value, Defval is a void pointer to the default value. If the option is found, then *Val is set to its value if Val!=NULL. If not found or on error, then *Val will be set to *Defval (to the default) if both Val and Defval !=NULL. Be careful, there is NO POINTER TYPE CHECKING! Return value: 0 on error or if the option was not found, the option array index otherwise. |
|
opt_helpstr(void): generates a "help string" from the option list hidden in Cmdopts[]. Boolean options are collected together, the "argumented" options are listed separately. If -x and -y are switches, -i expects an integer option and -D a double, then the following string will be returned: "[-xy] [-i<name>] [-D<name>]" where "name" stands for the string stored in the Descr field. Space for the help string is allocated within. |