Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename #136

Merged
merged 3 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Validators are now much more powerful [#118], all built in validators upgraded t

Other changes:

* 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
if(MSVC)
add_definitions("/W4")
else()
add_definitions(-Wall -Wextra -pedantic -Wno-deprecated-declarations)
add_definitions(-Wall -Wextra -pedantic)
endif()

if(CMAKE_VERSION VERSION_GREATER 3.6)
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ The add commands return a pointer to an internally stored `Option`. If you set t

* `->required()`: The program will quit if this option is not present. This is `mandatory` in Plumbum, but required options seems to be a more standard term. For compatibility, `->mandatory()` also works.
* `->expected(N)`: Take `N` values instead of as many as possible, only for vector args. If negative, require at least `-N`; end with `--` or another recognized option.
* `->set_custom_option(typename, N)`: Set the name and (optional) intrinsic size of an option. The parser will require multiples of this number if negative.
* `->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead)
* `->type_size(N)`: Set the intrinsic size of an option. The parser will require multiples of this number if negative.
* `->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer.
* `->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer.
* `->envname(name)`: Gets the value from the environment if present and not passed on the command line.
Expand Down Expand Up @@ -231,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 @@ -253,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 @@ -319,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/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int main(int argc, char **argv) {

Level level;
app.add_set("-l,--level", level, {Level::High, Level::Medium, Level::Low}, "Level settings")
->set_type_name("enum/Level in {High=0, Medium=1, Low=2}");
->type_name("enum/Level in {High=0, Medium=1, Low=2}");

CLI11_PARSE(app, argc, argv);

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
68 changes: 40 additions & 28 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 @@ -331,7 +331,7 @@ class App {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(name, fun, description, false);
opt->set_custom_option(detail::type_name<T>());
opt->type_name(detail::type_name<T>());
return opt;
}

Expand All @@ -345,11 +345,11 @@ class App {
CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); };

Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>());
opt->type_name(detail::type_name<T>());
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
Expand All @@ -371,7 +371,7 @@ class App {
};

Option *opt = add_option(name, fun, description, false);
opt->set_custom_option(detail::type_name<T>(), -1);
opt->type_name(detail::type_name<T>())->type_size(-1);
return opt;
}

Expand All @@ -393,9 +393,9 @@ class App {
};

Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(detail::type_name<T>(), -1);
opt->type_name(detail::type_name<T>())->type_size(-1);
if(defaulted)
opt->set_default_str("[" + detail::join(variable) + "]");
opt->default_str("[" + detail::join(variable) + "]");
return opt;
}

Expand Down Expand Up @@ -440,7 +440,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}

Expand All @@ -460,7 +460,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}

Expand All @@ -480,7 +480,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
opt->multi_option_policy(CLI::MultiOptionPolicy::TakeLast);
return opt;
}
Expand All @@ -499,7 +499,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
if(opt->get_positional())
throw IncorrectConstruction::PositionalFlag(name);
opt->set_custom_option("", 0);
opt->type_size(0);
return opt;
}

Expand Down Expand Up @@ -530,7 +530,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
std::string typeval = detail::type_name<T>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
return opt;
}

Expand All @@ -550,7 +550,7 @@ class App {
};

Option *opt = add_option(name, fun, description, false);
opt->set_type_name_fn(
opt->type_name_fn(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });

return opt;
Expand All @@ -575,11 +575,11 @@ class App {
Option *opt = add_option(name, fun, description, defaulted);
std::string typeval = detail::type_name<T>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
if(defaulted) {
std::stringstream out;
out << member;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
Expand All @@ -601,12 +601,12 @@ class App {
};

Option *opt = add_option(name, fun, description, defaulted);
opt->set_type_name_fn(
opt->type_name_fn(
[&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; });
if(defaulted) {
std::stringstream out;
out << member;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
Expand Down Expand Up @@ -634,7 +634,7 @@ class App {
Option *opt = add_option(name, fun, description, false);
std::string typeval = detail::type_name<std::string>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);

return opt;
}
Expand All @@ -660,7 +660,7 @@ class App {
};

Option *opt = add_option(name, fun, description, false);
opt->set_type_name_fn([&options]() {
opt->type_name_fn([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});

Expand Down Expand Up @@ -691,9 +691,9 @@ class App {
Option *opt = add_option(name, fun, description, defaulted);
std::string typeval = detail::type_name<std::string>();
typeval += " in {" + detail::join(options) + "}";
opt->set_custom_option(typeval);
opt->type_name(typeval);
if(defaulted) {
opt->set_default_str(member);
opt->default_str(member);
}
return opt;
}
Expand All @@ -720,11 +720,11 @@ class App {
};

Option *opt = add_option(name, fun, description, defaulted);
opt->set_type_name_fn([&options]() {
opt->type_name_fn([&options]() {
return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}";
});
if(defaulted) {
opt->set_default_str(member);
opt->default_str(member);
}
return opt;
}
Expand All @@ -749,11 +749,11 @@ class App {
};

CLI::Option *opt = add_option(name, fun, description, defaulted);
opt->set_custom_option(label, 2);
opt->type_name(label)->type_size(2);
if(defaulted) {
std::stringstream out;
out << variable;
opt->set_default_str(out.str());
opt->default_str(out.str());
}
return opt;
}
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 All @@ -1039,6 +1039,18 @@ class App {
return formatter_->make_help(this, prev, mode);
}

/// Provided for backwards compatibility \deprecated
CLI11_DEPRECATED("Please use footer instead")
App *set_footer(std::string msg) { return footer(msg); }

/// Provided for backwards compatibility \deprecated
CLI11_DEPRECATED("Please use name instead")
App *set_name(std::string msg) { return name(msg); }

/// Provided for backwards compatibility \deprecated
CLI11_DEPRECATED("Please use callback instead")
App *set_callback(std::function<void()> fn) { return callback(fn); }

///@}
/// @name Getters
///@{
Expand Down
2 changes: 1 addition & 1 deletion include/CLI/Macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif
#endif

#if defined(PYBIND11_CPP14)
#if defined(CLI11_CPP14)
#define CLI11_DEPRECATED(reason) [[deprecated(reason)]]
#elif defined(_MSC_VER)
#define CLI11_DEPRECATED(reason) __declspec(deprecated(reason))
Expand Down
Loading