1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 12:13:52 +00:00

Removing set_ for failure_message, footer, name, and callback

This commit is contained in:
Henry Fredrick Schreiner 2018-06-16 15:44:15 +02:00 committed by Henry Schreiner
parent bf2bc39c30
commit b2e471ac4d
8 changed files with 27 additions and 25 deletions

View File

@ -56,7 +56,7 @@ matrix:
.ci/build_docs.sh .ci/build_docs.sh
fi fi
# GCC 6 and Coverage # GCC 7 and coverage (8 does not support lcov, wait till 9 and new lcov)
- compiler: gcc - compiler: gcc
env: env:
- GCC_VER=7 - GCC_VER=7

View File

@ -36,7 +36,9 @@ Validators are now much more powerful [#118], all built in validators upgraded t
Other changes: Other changes:
* Dropped `set_*` names on options, using `type_name` and `type_size` instead of `set_custom_option`. Methods return this. * Dropped `set_` on Option's `type_name`, `default_str`, and `default_val`
* Replaced `set_custom_option` with `type_name` and `type_size` instead of `set_custom_option`. Methods return `this`.
* Removed `set_` from App's `failure_message`, `footer`, `callback`, and `name`
* Added `->each()` to make adding custom callbacks easier [#126] * Added `->each()` to make adding custom callbacks easier [#126]
* Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering * Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering
* Added `get_groups()` to get groups * Added `get_groups()` to get groups

View File

@ -232,7 +232,7 @@ subcommand name from matching.
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` or `CLI::RuntimeError(return_value)`, 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 `.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.
You are allowed to throw `CLI::Success` in the callbacks. 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).
@ -254,14 +254,14 @@ There are several options that are supported on the main app and subcommands. Th
* `.formatter(fmt)`: Set a formatter, with signature `std::string(const App*, std::string, AppFormatMode)`. See Formatting for more details. * `.formatter(fmt)`: Set a formatter, with signature `std::string(const App*, std::string, AppFormatMode)`. See Formatting for more details.
* `.get_description()`: Access the description. * `.get_description()`: Access the description.
* `.parsed()`: True if this subcommand was given on the command line. * `.parsed()`: True if this subcommand was given on the command line.
* `.set_name(name)`: Add or change the name. * `.name(name)`: Add or change the name.
* `.set_callback(void() function)`: Set the callback that runs at the end of parsing. The options have already run at this point. * `.callback(void() function)`: Set the callback that runs at the end of parsing. The options have already run at this point.
* `.allow_extras()`: Do not throw an error if extra arguments are left over. * `.allow_extras()`: Do not throw an error if extra arguments are left over.
* `.prefix_command()`: Like `allow_extras`, but stop immediately on the first unrecognised item. It is ideal for allowing your app or subcommand to be a "prefix" to calling another app. * `.prefix_command()`: Like `allow_extras`, but stop immediately on the first unrecognised item. It is ideal for allowing your app or subcommand to be a "prefix" to calling another app.
* `.set_footer(message)`: Set text to appear at the bottom of the help string. * `.footer(message)`: Set text to appear at the bottom of the help string.
* `.set_help_flag(name, message)`: Set the help flag name and message, returns a pointer to the created option. * `.set_help_flag(name, message)`: Set the help flag name and message, returns a pointer to the created option.
* `.set_help_all_flag(name, message)`: Set the help all flag name and message, returns a pointer to the created option. Expands subcommands. * `.set_help_all_flag(name, message)`: Set the help all flag name and message, returns a pointer to the created option. Expands subcommands.
* `.set_failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default). * `.failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default).
* `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` will be hide the subcommand. * `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` will be hide the subcommand.
> Note: if you have a fixed number of required positional options, that will match before subcommand names. `{}` is an empty filter function. > Note: if you have a fixed number of required positional options, that will match before subcommand names. `{}` is an empty filter function.
@ -320,7 +320,7 @@ their own formatter since you can't access anything but the call operator once a
The App class was designed allow toolkits to subclass it, to provide preset default options (see above) and setup/teardown code. Subcommands remain an unsubclassed `App`, since those are not expected to need setup and teardown. The default `App` only adds a help flag, `-h,--help`, than can removed/replaced using `.set_help_flag(name, help_string)`. You can also set a help-all flag with `.set_help_all_flag(name, help_string)`; this will expand the subcommands (one level only). You can remove options if you have pointers to them using `.remove_option(opt)`. You can add a `pre_callback` override to customize the after parse The App class was designed allow toolkits to subclass it, to provide preset default options (see above) and setup/teardown code. Subcommands remain an unsubclassed `App`, since those are not expected to need setup and teardown. The default `App` only adds a help flag, `-h,--help`, than can removed/replaced using `.set_help_flag(name, help_string)`. You can also set a help-all flag with `.set_help_all_flag(name, help_string)`; this will expand the subcommands (one level only). You can remove options if you have pointers to them using `.remove_option(opt)`. You can add a `pre_callback` override to customize the after parse
but before run behavior, while but before run behavior, while
still giving the user freedom to `set_callback` on the main app. still giving the user freedom to `callback` on the main app.
The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector. The most important parse function is `parse(std::vector<std::string>)`, which takes a reversed list of arguments (so that `pop_back` processes the args in the correct order). `get_help_ptr` and `get_config_ptr` give you access to the help/config option pointers. The standard `parse` manually sets the name from the first argument, so it should not be in this vector.

View File

@ -18,7 +18,7 @@ void setup_subcommand_a(CLI::App &app) {
sub->add_flag("--with-foo", opt->with_foo, "Counter"); sub->add_flag("--with-foo", opt->with_foo, "Counter");
// Set the run function as callback to be called when this subcommand is issued. // Set the run function as callback to be called when this subcommand is issued.
sub->set_callback([opt]() { run_subcommand_a(*opt); }); sub->callback([opt]() { run_subcommand_a(*opt); });
} }
/// The function that runs our code. /// The function that runs our code.

View File

@ -222,13 +222,13 @@ class App {
/// it is not possible to overload on std::function (fixed in c++14 /// it is not possible to overload on std::function (fixed in c++14
/// and backported to c++11 on newer compilers). Use capture by reference /// and backported to c++11 on newer compilers). Use capture by reference
/// to get a pointer to App if needed. /// to get a pointer to App if needed.
App *set_callback(std::function<void()> callback) { App *callback(std::function<void()> callback) {
callback_ = callback; callback_ = callback;
return this; return this;
} }
/// Set a name for the app (empty will use parser to set the name) /// Set a name for the app (empty will use parser to set the name)
App *set_name(std::string name = "") { App *name(std::string name = "") {
name_ = name; name_ = name;
return this; return this;
} }
@ -901,7 +901,7 @@ class App {
} }
/// Provide a function to print a help message. The function gets access to the App pointer and error. /// Provide a function to print a help message. The function gets access to the App pointer and error.
void set_failure_message(std::function<std::string(const App *, const Error &e)> function) { void failure_message(std::function<std::string(const App *, const Error &e)> function) {
failure_message_ = function; failure_message_ = function;
} }
@ -1012,7 +1012,7 @@ class App {
///@{ ///@{
/// Set footer. /// Set footer.
App *set_footer(std::string footer) { App *footer(std::string footer) {
footer_ = footer; footer_ = footer;
return this; return this;
} }

View File

@ -362,7 +362,7 @@ TEST_F(TApp, SubcommandDefaults) {
app.prefix_command(); app.prefix_command();
app.ignore_case(); app.ignore_case();
app.fallthrough(); app.fallthrough();
app.set_footer("footy"); app.footer("footy");
app.group("Stuff"); app.group("Stuff");
app.require_subcommand(2, 3); app.require_subcommand(2, 3);

View File

@ -24,7 +24,7 @@ TEST(THelp, Basic) {
TEST(THelp, Footer) { TEST(THelp, Footer) {
CLI::App app{"My prog"}; CLI::App app{"My prog"};
app.set_footer("Report bugs to bugs@example.com"); app.footer("Report bugs to bugs@example.com");
std::string help = app.help(); std::string help = app.help();
@ -128,7 +128,7 @@ TEST(THelp, VectorOpts) {
TEST(THelp, MultiPosOpts) { TEST(THelp, MultiPosOpts) {
CLI::App app{"My prog"}; CLI::App app{"My prog"};
app.set_name("program"); app.name("program");
std::vector<int> x, y; std::vector<int> x, y;
app.add_option("quick", x, "Disc")->expected(2); app.add_option("quick", x, "Disc")->expected(2);
app.add_option("vals", y, "Other"); app.add_option("vals", y, "Other");
@ -535,7 +535,7 @@ TEST_F(CapturedHelp, NormalError) {
} }
TEST_F(CapturedHelp, RepacedError) { TEST_F(CapturedHelp, RepacedError) {
app.set_failure_message(CLI::FailureMessage::help); app.failure_message(CLI::FailureMessage::help);
EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError)); EXPECT_EQ(run(CLI::ExtrasError({"Thing"})), static_cast<int>(CLI::ExitCodes::ExtrasError));
EXPECT_EQ(out.str(), ""); EXPECT_EQ(out.str(), "");

View File

@ -178,10 +178,10 @@ TEST_F(TApp, FooFooProblem) {
TEST_F(TApp, Callbacks) { TEST_F(TApp, Callbacks) {
auto sub1 = app.add_subcommand("sub1"); auto sub1 = app.add_subcommand("sub1");
sub1->set_callback([]() { throw CLI::Success(); }); sub1->callback([]() { throw CLI::Success(); });
auto sub2 = app.add_subcommand("sub2"); auto sub2 = app.add_subcommand("sub2");
bool val = false; bool val = false;
sub2->set_callback([&val]() { val = true; }); sub2->callback([&val]() { val = true; });
args = {"sub2"}; args = {"sub2"};
EXPECT_FALSE(val); EXPECT_FALSE(val);
@ -191,9 +191,9 @@ TEST_F(TApp, Callbacks) {
TEST_F(TApp, RuntimeErrorInCallback) { TEST_F(TApp, RuntimeErrorInCallback) {
auto sub1 = app.add_subcommand("sub1"); auto sub1 = app.add_subcommand("sub1");
sub1->set_callback([]() { throw CLI::RuntimeError(); }); sub1->callback([]() { throw CLI::RuntimeError(); });
auto sub2 = app.add_subcommand("sub2"); auto sub2 = app.add_subcommand("sub2");
sub2->set_callback([]() { throw CLI::RuntimeError(2); }); sub2->callback([]() { throw CLI::RuntimeError(2); });
args = {"sub1"}; args = {"sub1"};
EXPECT_THROW(run(), CLI::RuntimeError); EXPECT_THROW(run(), CLI::RuntimeError);
@ -309,7 +309,7 @@ TEST_F(TApp, CallbackOrdering) {
app.add_option("--val", val); app.add_option("--val", val);
auto sub = app.add_subcommand("sub"); auto sub = app.add_subcommand("sub");
sub->set_callback([&val, &sub_val]() { sub_val = val; }); sub->callback([&val, &sub_val]() { sub_val = val; });
args = {"sub", "--val=2"}; args = {"sub", "--val=2"};
run(); run();
@ -573,7 +573,7 @@ TEST_F(SubcommandProgram, HelpOrder) {
TEST_F(SubcommandProgram, Callbacks) { TEST_F(SubcommandProgram, Callbacks) {
start->set_callback([]() { throw CLI::Success(); }); start->callback([]() { throw CLI::Success(); });
run(); run();
@ -668,8 +668,8 @@ TEST_F(SubcommandProgram, MixedOrderExtras) {
TEST_F(SubcommandProgram, CallbackOrder) { TEST_F(SubcommandProgram, CallbackOrder) {
std::vector<int> callback_order; std::vector<int> callback_order;
start->set_callback([&callback_order]() { callback_order.push_back(1); }); start->callback([&callback_order]() { callback_order.push_back(1); });
stop->set_callback([&callback_order]() { callback_order.push_back(2); }); stop->callback([&callback_order]() { callback_order.push_back(2); });
args = {"start", "stop"}; args = {"start", "stop"};
run(); run();