Skip to content

Commit

Permalink
Removing set_ for failure_message, footer, name, and callback
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Jun 18, 2018
1 parent bf2bc39 commit b2e471a
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ matrix:
.ci/build_docs.sh
fi
# GCC 6 and Coverage
# GCC 7 and coverage (8 does not support lcov, wait till 9 and new lcov)
- compiler: gcc
env:
- GCC_VER=7
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ Validators are now much more powerful [#118], all built in validators upgraded t

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 filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering
* Added `get_groups()` to get groups
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
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.
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).
Expand All @@ -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.
* `.get_description()`: Access the description.
* `.parsed()`: True if this subcommand was given on the command line.
* `.set_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.
* `.name(name)`: Add or change the name.
* `.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.
* `.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_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.

> Note: if you have a fixed number of required positional options, that will match before subcommand names. `{}` is an empty filter function.
Expand Down Expand Up @@ -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
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.

Expand Down
2 changes: 1 addition & 1 deletion examples/subcom_in_files/subcommand_a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void setup_subcommand_a(CLI::App &app) {
sub->add_flag("--with-foo", opt->with_foo, "Counter");

// 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.
Expand Down
8 changes: 4 additions & 4 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,13 @@ class App {
/// 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
/// to get a pointer to App if needed.
App *set_callback(std::function<void()> callback) {
App *callback(std::function<void()> callback) {
callback_ = callback;
return this;
}

/// 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;
return this;
}
Expand Down Expand Up @@ -901,7 +901,7 @@ class App {
}

/// 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;
}

Expand Down Expand Up @@ -1012,7 +1012,7 @@ class App {
///@{

/// Set footer.
App *set_footer(std::string footer) {
App *footer(std::string footer) {
footer_ = footer;
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CreationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ TEST_F(TApp, SubcommandDefaults) {
app.prefix_command();
app.ignore_case();
app.fallthrough();
app.set_footer("footy");
app.footer("footy");
app.group("Stuff");
app.require_subcommand(2, 3);

Expand Down
6 changes: 3 additions & 3 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST(THelp, Basic) {

TEST(THelp, Footer) {
CLI::App app{"My prog"};
app.set_footer("Report bugs to [email protected]");
app.footer("Report bugs to [email protected]");

std::string help = app.help();

Expand Down Expand Up @@ -128,7 +128,7 @@ TEST(THelp, VectorOpts) {

TEST(THelp, MultiPosOpts) {
CLI::App app{"My prog"};
app.set_name("program");
app.name("program");
std::vector<int> x, y;
app.add_option("quick", x, "Disc")->expected(2);
app.add_option("vals", y, "Other");
Expand Down Expand Up @@ -535,7 +535,7 @@ TEST_F(CapturedHelp, NormalError) {
}

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(out.str(), "");
Expand Down
16 changes: 8 additions & 8 deletions tests/SubcommandTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ TEST_F(TApp, FooFooProblem) {

TEST_F(TApp, Callbacks) {
auto sub1 = app.add_subcommand("sub1");
sub1->set_callback([]() { throw CLI::Success(); });
sub1->callback([]() { throw CLI::Success(); });
auto sub2 = app.add_subcommand("sub2");
bool val = false;
sub2->set_callback([&val]() { val = true; });
sub2->callback([&val]() { val = true; });

args = {"sub2"};
EXPECT_FALSE(val);
Expand All @@ -191,9 +191,9 @@ TEST_F(TApp, Callbacks) {

TEST_F(TApp, RuntimeErrorInCallback) {
auto sub1 = app.add_subcommand("sub1");
sub1->set_callback([]() { throw CLI::RuntimeError(); });
sub1->callback([]() { throw CLI::RuntimeError(); });
auto sub2 = app.add_subcommand("sub2");
sub2->set_callback([]() { throw CLI::RuntimeError(2); });
sub2->callback([]() { throw CLI::RuntimeError(2); });

args = {"sub1"};
EXPECT_THROW(run(), CLI::RuntimeError);
Expand Down Expand Up @@ -309,7 +309,7 @@ TEST_F(TApp, CallbackOrdering) {
app.add_option("--val", val);

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"};
run();
Expand Down Expand Up @@ -573,7 +573,7 @@ TEST_F(SubcommandProgram, HelpOrder) {

TEST_F(SubcommandProgram, Callbacks) {

start->set_callback([]() { throw CLI::Success(); });
start->callback([]() { throw CLI::Success(); });

run();

Expand Down Expand Up @@ -668,8 +668,8 @@ TEST_F(SubcommandProgram, MixedOrderExtras) {

TEST_F(SubcommandProgram, CallbackOrder) {
std::vector<int> callback_order;
start->set_callback([&callback_order]() { callback_order.push_back(1); });
stop->set_callback([&callback_order]() { callback_order.push_back(2); });
start->callback([&callback_order]() { callback_order.push_back(1); });
stop->callback([&callback_order]() { callback_order.push_back(2); });

args = {"start", "stop"};
run();
Expand Down

0 comments on commit b2e471a

Please sign in to comment.