![]() |
CAPS Universe documentation
1.0.4
All you need to know to be successful
|
Cascadeable command line argument parser. More...
Data Structures | |
| struct | caps_arg_parameter |
| struct | caps_arg_parser |
| struct | caps_arg_parser_list |
Enumerations | |
| enum | caps_arg_parameter_flag { CAPS_PARAM_UNKNOWN = 0 , CAPS_PARAM_OPTIONAL = 1 , CAPS_VALUE_OPTIONAL = 2 } |
Functions | |
| int | caps_arg_parser_process (const struct caps_arg_parser_list *plist, int argc, char *argv[argc]) |
| int | caps_arg_parser_help (const struct caps_arg_parser_list *plist) |
This command line argument parser is intended for library based command line processing.
With this library you can define your application's command line argument parser and cascade it with the command line argument parsers of external sources (libraries for example).
--version, --help and --usage. It returns with a -EAGAIN error code insteadSomething like the output of --help is generated at one single location based on the different parser definitions.
Parameters to understood are defined in caps_arg_parameter as a simple array. The difference between a mandatory parameter and an optional one is the flag CAPS_PARAM_OPTIONAL. Whenever a parameter is found in the command line arguments, its corresponding callback is called. It receives its own parser definition and the (possible) value of the command line argument. The parser definition contains an object, which can point to private data to be accessed when a parameter of this parser definition was found. The struct also contains the help texts for the parameter and its value. They are used, when the --help output is created.
A command line argument parser in caps_arg_parser defines an object pointer used by the parameter callbacks and a header and footer help text, used as the heading text of the parameters and the trailing text. It defines the NLS domain for all help texts of this parser as well. And it refers the array of parameters.
A command line argument parser list in caps_arg_parser_list defines the whole parser. It is intended to be used by the main program, so it defines the program version, bug reporting email address and the name of the program. These texts are used when creating the --help output. And it defines the streams for error output and regular output for special requirements. As the last, it refers an array of command line argument parsers.
The following example is a very simple one, with a single command line argument parser and a single command line argument (here --verbose).
At run-time this results into:
The --help output:
$ some_prog --help
Does something famous in the 'CAPS universe'. Usage:
some_prog [arguments]
Some header text:
-v, --verbose Increase verbosity
Some footer text
Usual options:
-V, --version Print program's version
--usage Print short usage help
-?, --help Print this help
Report bugs to projects@caps-universe.org
--version parameter is listed, if the caps_arg_parser_list::version member is defined.The --usage output:
$ some_prog --usage Usage: some_prog [--verbose] [--version] [--usage] [--help]
The --version output:
$ some_prog --version some_prog (caps universe) 1.0.1
The previous example can be cascaded with command line argument parsers from external sources. Thus, in the next example it uses three different command line argument parsers:
some_prog)We can keep the most code from the previous example, just enlarge the some_parser structure into an array of some_parsers, and add the program's command line argument parser as its first one.
some_parsers, e.g. some_parsers[1] and some_parsers[2].With this setup the output will now look different:
The --help output:
$ some_prog --help
Does something famous in the 'CAPS universe'. Usage:
some_prog [arguments] arguments
Some header text:
-v, --verbose Increase verbosity
Some footer text
libcapsdriver arguments:
--test-doc=FILE Direct file to print
--test-param=PARAMS IPP style printing parameters
--test-printer=FILE File/device node to print to
--test-exit Exit after print
These arguments are optional and for test and development only
Provider generic arguments:
-f FILE, --driver-ini=FILE Device Description INI file name
-d DIR, --parameter-dir=DIR Printers's dynamic parameter directory
These arguments are mandatory
Usual options:
-V, --version Print program's version
--usage Print short usage help
-?, --help Print this help
Report bugs to projects@caps-universe.org
The usage output --usage will differ as well:
$ testprog --usage
Usage: testprog [--verbose] [--test-doc=FILE] [--test-param=PARAMS]
[--test-printer=FILE] [--test-exit] --driver-ini=FILE --parameter-dir=DIR
[--version] [--usage] [--help]
But the --version output stays the same:
$ some_prog --version some_prog (caps universe) 1.0.1
argv[0] only).A command line argument callback is defined to:
The return value must be 0 if the callback function was successful.
Return a negative errno instead, -EINVAL for example, if the callback function wasn't able to process the command line argument successfully. A negative return value will terminate the command line argument processing and the call to caps_arg_parser_process() returns with this negative return value.
Parameter and value description flags for caps_arg_parameter::flags
| Enumerator | |
|---|---|
| CAPS_PARAM_UNKNOWN | |
| CAPS_PARAM_OPTIONAL | The marked parameter is optional |
| CAPS_VALUE_OPTIONAL | The marked option/parameter has an optional value, requires caps_arg_parameter::arg to be set |
| int caps_arg_parser_process | ( | const struct caps_arg_parser_list * | plist, |
| int | argc, | ||
| char * | argv[argc] | ||
| ) |
Process command line arguments with the given parser list
| [in] | plist | The full parser list |
| [in] | argc | Argument count, from main() |
| [in] | argv | Argument array, from main() |
| 0 | On success |
| value | A negative return value from a command line argument callback function |
| -EINVAL | bad or missing command line argument(s) or bad or missing argument values |
| -EAGAIN | Terminate the program by intention (e.g. --version, --help or --usage detected) |
A return value of -EAGAIN should terminate the program without an error code, since this is the user's expectation if one of the parameters --version, --help or --usage are given.
A return value of -EINVAL creates a corresponding error message and send it to caps_arg_parser_list::errorout. The program should terminate with an error code in this case.
| int caps_arg_parser_help | ( | const struct caps_arg_parser_list * | plist | ) |
Output a long help about possible arguments (same as --help)
| [in] | plist | The full parser list |
| -EAGAIN | Terminate the program by intention |
This function is intended to print the full help text in the case your program detects a bad usage by its own. For example if no command line arguments are given at all, but the program requires some of them. In this case it makes no sense to call caps_arg_parser_process() because it will fail, but its error message may not reflect the real reason.