1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-03 14:03:52 +00:00

Adding docs, fixing examples for usage

This commit is contained in:
Henry Fredrick Schreiner 2017-11-20 10:10:37 -05:00 committed by Henry Schreiner
parent 8955375eb2
commit 0bca8fdd30
5 changed files with 9 additions and 14 deletions

View File

@ -96,7 +96,7 @@ app.add_option("-f,--file", filename, "A help string");
try { try {
app.parse(argc, argv); app.parse(argc, argv);
} catch (const CLI::ParseError &e) { } catch (const CLI::Error &e) {
return app.exit(e); return app.exit(e);
} }
``` ```
@ -137,7 +137,7 @@ App* subcom = app.add_subcommand(name, discription);
An option name must start with a alphabetic character or underscore. For long options, anything but an equals sign or a comma is valid after that. Names are given as a comma separated string, with the dash or dashes. An option or flag can have as many names as you want, and afterward, using `count`, you can use any of the names, with dashes as needed, to count the options. One of the names is allowed to be given without proceeding dash(es); if present the option is a positional option, and that name will be used on help line for its positional form. If you want the default value to print in the help description, pass in `true` for the final parameter for `add_option` or `add_set`. The set options allow your users to pick from a set of predefined options. An option name must start with a alphabetic character or underscore. For long options, anything but an equals sign or a comma is valid after that. Names are given as a comma separated string, with the dash or dashes. An option or flag can have as many names as you want, and afterward, using `count`, you can use any of the names, with dashes as needed, to count the options. One of the names is allowed to be given without proceeding dash(es); if present the option is a positional option, and that name will be used on help line for its positional form. If you want the default value to print in the help description, pass in `true` for the final parameter for `add_option` or `add_set`. The set options allow your users to pick from a set of predefined options.
On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a `CLI::ParseError` to signal a failure. On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a relevant `CLI::Error` to signal a failure.
### Example ### Example
@ -193,7 +193,7 @@ If you want to require at least one subcommand is given, use `.require_subcomman
If an `App` (main or subcommand) has been parsed on the command line, `->parsed` will be true (or convert directly to bool). If an `App` (main or subcommand) has been parsed on the command line, `->parsed` will be true (or convert directly to bool).
All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommands passed on the command line. A `got_subcommand(App_or_name)` method is also provided that will check to see if an `App` pointer or a string name was collected on the command line. All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommands passed on the command line. A `got_subcommand(App_or_name)` method is also provided that will check to see if an `App` pointer or a string name was collected on the command line.
For many cases, however, using an app's callback may be easier. Every app executes a callback function after it parses; just use a lambda function (with capture to get parsed values) to `.set_callback`. If you throw `CLI::Success`, you can For many cases, however, using an app's callback may be easier. Every app executes a callback function after it parses; just use a lambda function (with capture to get parsed values) to `.set_callback`. If you throw `CLI::Success` or `CLI::RuntimeError(return_value)`, you can
even exit the program through the callback. The main `App` has a callback slot, as well, but it is generally not as useful. even exit the program through the callback. The main `App` has a callback slot, as well, but it is generally not as useful.
If you want only one, use `app.require_subcommand(1)`. You are allowed to throw `CLI::Success` in the callbacks. If you want only one, use `app.require_subcommand(1)`. You are allowed to throw `CLI::Success` in the callbacks.
Multiple subcommands are allowed, to allow [`Click`][Click] like series of commands (order is preserved). Multiple subcommands are allowed, to allow [`Click`][Click] like series of commands (order is preserved).
@ -289,7 +289,7 @@ If you use the excellent [Rang] library to add color to your terminal in a safe,
std::atexit([](){std::cout << rang::style::reset;}); std::atexit([](){std::cout << rang::style::reset;});
try { try {
app.parse(argc, argv); app.parse(argc, argv);
} catch (const CLI::ParseError &e) { } catch (const CLI::Error &e) {
std::cout << (e.get_exit_code()==0 ? rang::fg::blue : rang::fg::red); std::cout << (e.get_exit_code()==0 ? rang::fg::blue : rang::fg::red);
return app.exit(e); return app.exit(e);
} }

View File

@ -18,7 +18,7 @@ int main(int argc, char **argv) {
// Standard parsing lines (copy and paste in) // Standard parsing lines (copy and paste in)
try { try {
app.parse(argc, argv); app.parse(argc, argv);
} catch(const CLI::ParseError &e) { } catch(const CLI::Error &e) {
return app.exit(e); return app.exit(e);
} }

View File

@ -8,11 +8,7 @@ int main(int argc, char **argv) {
std::vector<int> vals; std::vector<int> vals;
app.add_option("--vals,-v", vals)->expected(1); app.add_option("--vals,-v", vals)->expected(1);
try { CLI11_PARSE(app, argc, argv);
app.parse(argc, argv);
} catch(const CLI::ParseError &e) {
return app.exit(e);
}
std::vector<std::string> more_comms = app.remaining(); std::vector<std::string> more_comms = app.remaining();

View File

@ -29,7 +29,7 @@ namespace CLI {
#define CLI11_PARSE(app, argc, argv) \ #define CLI11_PARSE(app, argc, argv) \
try { \ try { \
(app).parse((argc), (argv)); \ (app).parse((argc), (argv)); \
} catch(const CLI::Error &e) { \ } catch(const CLI::Error &e) { \
return (app).exit(e); \ return (app).exit(e); \
} }
#endif #endif
@ -682,7 +682,7 @@ class App {
int exit(const Error &e) const { int exit(const Error &e) const {
/// Avoid printing anything if this is a CLI::RuntimeError /// Avoid printing anything if this is a CLI::RuntimeError
if(dynamic_cast<const CLI::RuntimeError*>(&e) != nullptr) if(dynamic_cast<const CLI::RuntimeError *>(&e) != nullptr)
return e.get_exit_code(); return e.get_exit_code();
if(e.exit_code != static_cast<int>(ExitCodes::Success)) { if(e.exit_code != static_cast<int>(ExitCodes::Success)) {

View File

@ -83,8 +83,7 @@ struct OptionAlreadyAdded : public ConstructionError {
/// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code. /// Does not output a diagnostic in CLI11_PARSE, but allows to return from main() with a specific error code.
struct RuntimeError : public Error { struct RuntimeError : public Error {
RuntimeError(int exit_code = 1) RuntimeError(int exit_code = 1) : Error("RuntimeError", "runtime error", exit_code, false) {}
: Error("RuntimeError", "runtime error", exit_code, false) {}
}; };
// Parsing errors // Parsing errors