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

add an optional help message string to the version_add flag #601

Merged
merged 1 commit into from
Jun 17, 2021
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ There are several options that are supported on the main app and subcommands and
- `.footer(message)`: Set text to appear at the bottom of the help string.
- `.footer(std::string())`: 🆕 Set a callback to generate a string that will appear at the end of the help string.
- `.set_help_flag(name, message)`: Set the help flag name and message, returns a pointer to the created option.
- `.set_version_flag(name, versionString or callback, help_message)`: 🆕 Set the version flag name and version string or callback and optional help 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.
- `.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.
Expand Down
17 changes: 8 additions & 9 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,9 @@ class App {
}

/// Set a version flag and version display string, replace the existing one if present
Option *set_version_flag(std::string flag_name = "", const std::string &versionString = "") {
Option *set_version_flag(std::string flag_name = "",
const std::string &versionString = "",
const std::string &version_help = "Display program version information and exit") {
// take flag_description by const reference otherwise add_flag tries to assign to version_description
if(version_ptr_ != nullptr) {
remove_option(version_ptr_);
Expand All @@ -742,17 +744,16 @@ class App {
// Empty name will simply remove the version flag
if(!flag_name.empty()) {
version_ptr_ = add_flag_callback(
flag_name,
[versionString]() { throw(CLI::CallForVersion(versionString, 0)); },
"Display program version information and exit");
flag_name, [versionString]() { throw(CLI::CallForVersion(versionString, 0)); }, version_help);
version_ptr_->configurable(false);
}

return version_ptr_;
}
/// Generate the version string through a callback function
Option *set_version_flag(std::string flag_name, std::function<std::string()> vfunc) {
// take flag_description by const reference otherwise add_flag tries to assign to version_description
Option *set_version_flag(std::string flag_name,
std::function<std::string()> vfunc,
const std::string &version_help = "Display program version information and exit") {
if(version_ptr_ != nullptr) {
remove_option(version_ptr_);
version_ptr_ = nullptr;
Expand All @@ -761,9 +762,7 @@ class App {
// Empty name will simply remove the version flag
if(!flag_name.empty()) {
version_ptr_ = add_flag_callback(
flag_name,
[vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); },
"Display program version information and exit");
flag_name, [vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); }, version_help);
version_ptr_->configurable(false);
}

Expand Down
15 changes: 15 additions & 0 deletions tests/HelpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,21 @@ TEST_CASE("TVersion: callback_flag", "[help]") {
CHECK_THAT(vers, Contains("VERSION"));
}

TEST_CASE("TVersion: help", "[help]") {

CLI::App app;

app.set_version_flag("-v,--version", "version_string", "help_for_version");

auto hvers = app.help();
CHECK_THAT(hvers, Contains("help_for_version"));

app.set_version_flag(
"-v", []() { return std::string("VERSION2 " CLI11_VERSION); }, "help_for_version2");
hvers = app.help();
CHECK_THAT(hvers, Contains("help_for_version2"));
}

TEST_CASE("TVersion: parse_throw", "[help]") {

CLI::App app;
Expand Down