A simple library to parse command-line options in C. This module is inspired by command-line-args.
Before using the library (cli_args.a), you need to create it with:
$ make
You can set options using the standards. All the commands below are equivalent, setting the same values:
$ ./example --help --timeout 1000 --file one.js two.js
$ ./example -ht 1000 --file one.js two.js
$ ./example -h -t 1000 -f one.js two.js
To access the values, first initialize and create a list of option definitions describing the options your application accepts (name
, alias
, type
and description
):
size = 3;
option_defs = init_option_defs(size);
option_defs[0] = add_def("help", "h", OPT_BOOLEAN, NULL);
option_defs[1] = add_def("file", "f", OPT_STRING, NULL);
option_defs[1].multiple = 1;
option_defs[2] = add_def("timeout", "t", OPT_INTEGER, NULL);
Next, parse the options by initializing and by using command_line_args:
args = init_args(argv, option_defs, size);
options = command_line_args(args);
options
now looks like this by using:
print_options(&options);
{ help: true, timeout: 1000, file: [ 'one.js', 'two.js' ] }
Finally don't forget to free the options variable when you don't need it anymore:
free_options(&options);
Parsing is strict by default - an exception is thrown if the user sets a singular option more than once or sets an unknown value or option (one without a valid definition). To be more permissive, enabling partial mode will return known options in the usual manner while collecting unknown arguments in a separate _unknown
property.
args = init_args(argv, option_defs, size);
args->partial = 1;
options = command_line_args(args);
A usage guide (typically printed when --help
is set) can be generated using command-line-usage. See the examples below and read the documentation for instructions how to create them.
A typical usage guide example.
This project is licensed under the MIT License - see the LICENSE.md file for details