-
Notifications
You must be signed in to change notification settings - Fork 0
Adding Help
gershnik edited this page Mar 14, 2022
·
2 revisions
There isn't a special provision to add --help
option in Argum, unlike some other libraries. Adding one is easy, though, just like any other kind of option.
#include <cstdlib>
int main(int argc, char * argv[]) {
const char * progname = (argc ? argv[0] : "prog");
Parser parser;
parser.add(
Option("--help", "-h").
help("show this help message and exit").
handler([&]() {
std::cout << parser.formatHelp(progname);
std::exit(EXIT_SUCCESS);
}));
try {
parser.parse(argc, argv);
} catch (ParsingException & ex) {
std::cerr << ex.message() << '\n';
std::cerr << parser.formatUsage(progname) << '\n';
return EXIT_FAILURE;
}
}
Running this code produces
$ ./prog
$ ./prog --help
Usage: ./prog [--help]
options:
--help, -h show this help message and exit
$ ./prog -h
Usage: ./prog [--help]
options:
--help, -h show this help message and exit
$ ./prog --help=foo
extraneous argument for option: --help
Usage: ./prog [--help]
$ ./prog --help foo bar
Usage: ./prog [--help]
options:
--help, -h show this help message and exit
Note the last invocation. The parser seem to have ignored the invalid arguments following --help
. Why?
What is very important to understand is that argument handlers are invoked sequentially, as they are parsed - not after the whole
command line was validated like most other parsers do. Thus when the handler for --help
is invoked, the invalid arguments foo
and bar
haven't been handled yet. The --help
handler we specified prints the help message and exits the program so no error is ever discovered.
- Home
-
Usage Guide
Basics
Error Handling
Thread Safety
Adding Help
Defining options
Positional arguments
Reporting errors from handlers
Parsing common data types
Validation
Depending on order
Subcommands
Response files
Partial Parsing -
Advanced
Syntax Description
Customizing syntax
Customizing usage and help
Localization