Handling subcommands, options, option-arguments and operands
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
etc
examples
src
tests
.gitignore
Doxyfile
LICENSE
Makefile.am
README.rst
TODO.rst
autogen.sh
configure.ac

README.rst

libargparse is a C library to handle subcommands, options, option-arguments and operands conforming to the XBD Utility Conventions included in POSIX.1-2008.

For usage check the examples in the examples/ directory and the header file argparse.h.

Using this code:

 1 #include <argparse.h>
 2 
 3 int main(int argc, char **argv) {
 4     args *args = args_new();
 5 
 6     args_add_option(args, option_new("f", "feature", "description of feature"));
 7     args_parse(args, argc--, argv++);
 8 
 9     args_free(args);
10 }

The following arguments will be recognized and parsed:

  • -f
  • +f
  • --feature
  • --enable-feature
  • --disable-feature
  • --no-feature
  • --with-feature
  • --without-feature

Given opt->accepts_arguments = true or opt->requires_arguments = true option-arguments will be parsed in this manner:

  • a positive prefix sets a positive opt->present, a negative sets negative
  • a negative option-argument inverts opt->present

This behaviour is similar to GNU autoconf's parameter handling.

Examples:

  • -f -> positive present
  • +f -> negative present
  • -f=no -> negative present
  • +f=no -> positive present
  • --feature -> positive present
  • --enable-feature -> positive present
  • --with-feature=disable -> negative present
  • --without-feature=true -> negative present
  • --disable-feature true -> negative present

A passend option-argument like "disable", "true", etc. pp. are still saved as the first option-argument.

Argparse does not provide a way to link options as mutually exclusive or to set the same value. It is encouraged to make use of the functionality above, otherwise this logic has to be implemented separately.

Using the _help() functions it is possible to print help output generated from the description, short_description, name members to a stream.

#include <argparse.h>
#include <stdio.h>

int main(int argc, char **argv) {
    args *args = args_new();

    args_add_option(args, option_new("f", "feature", "description of feature"));
    args_help(args, stdout);

    args_free(args);
}

Results in:

-f --feature description of feature