Commit 2fa8cae9e86d1ceaaebece5811c78ea96ccbc25f
Committed by
GitHub
1 parent
15bb724e
feat: add changeable help message string to version (#601)
add an optional help message string to the version_add flag if desired to override
Showing
3 changed files
with
24 additions
and
9 deletions
README.md
| ... | ... | @@ -599,6 +599,7 @@ There are several options that are supported on the main app and subcommands and |
| 599 | 599 | - `.footer(message)`: Set text to appear at the bottom of the help string. |
| 600 | 600 | - `.footer(std::string())`: ๐ Set a callback to generate a string that will appear at the end of the help string. |
| 601 | 601 | - `.set_help_flag(name, message)`: Set the help flag name and message, returns a pointer to the created option. |
| 602 | +- `.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. | |
| 602 | 603 | - `.set_help_all_flag(name, message)`: Set the help all flag name and message, returns a pointer to the created option. Expands subcommands. |
| 603 | 604 | - `.failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default). |
| 604 | 605 | - `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` will be hide the subcommand. | ... | ... |
include/CLI/App.hpp
| ... | ... | @@ -732,7 +732,9 @@ class App { |
| 732 | 732 | } |
| 733 | 733 | |
| 734 | 734 | /// Set a version flag and version display string, replace the existing one if present |
| 735 | - Option *set_version_flag(std::string flag_name = "", const std::string &versionString = "") { | |
| 735 | + Option *set_version_flag(std::string flag_name = "", | |
| 736 | + const std::string &versionString = "", | |
| 737 | + const std::string &version_help = "Display program version information and exit") { | |
| 736 | 738 | // take flag_description by const reference otherwise add_flag tries to assign to version_description |
| 737 | 739 | if(version_ptr_ != nullptr) { |
| 738 | 740 | remove_option(version_ptr_); |
| ... | ... | @@ -742,17 +744,16 @@ class App { |
| 742 | 744 | // Empty name will simply remove the version flag |
| 743 | 745 | if(!flag_name.empty()) { |
| 744 | 746 | version_ptr_ = add_flag_callback( |
| 745 | - flag_name, | |
| 746 | - [versionString]() { throw(CLI::CallForVersion(versionString, 0)); }, | |
| 747 | - "Display program version information and exit"); | |
| 747 | + flag_name, [versionString]() { throw(CLI::CallForVersion(versionString, 0)); }, version_help); | |
| 748 | 748 | version_ptr_->configurable(false); |
| 749 | 749 | } |
| 750 | 750 | |
| 751 | 751 | return version_ptr_; |
| 752 | 752 | } |
| 753 | 753 | /// Generate the version string through a callback function |
| 754 | - Option *set_version_flag(std::string flag_name, std::function<std::string()> vfunc) { | |
| 755 | - // take flag_description by const reference otherwise add_flag tries to assign to version_description | |
| 754 | + Option *set_version_flag(std::string flag_name, | |
| 755 | + std::function<std::string()> vfunc, | |
| 756 | + const std::string &version_help = "Display program version information and exit") { | |
| 756 | 757 | if(version_ptr_ != nullptr) { |
| 757 | 758 | remove_option(version_ptr_); |
| 758 | 759 | version_ptr_ = nullptr; |
| ... | ... | @@ -761,9 +762,7 @@ class App { |
| 761 | 762 | // Empty name will simply remove the version flag |
| 762 | 763 | if(!flag_name.empty()) { |
| 763 | 764 | version_ptr_ = add_flag_callback( |
| 764 | - flag_name, | |
| 765 | - [vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); }, | |
| 766 | - "Display program version information and exit"); | |
| 765 | + flag_name, [vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); }, version_help); | |
| 767 | 766 | version_ptr_->configurable(false); |
| 768 | 767 | } |
| 769 | 768 | ... | ... |
tests/HelpTest.cpp
| ... | ... | @@ -1220,6 +1220,21 @@ TEST_CASE("TVersion: callback_flag", "[help]") { |
| 1220 | 1220 | CHECK_THAT(vers, Contains("VERSION")); |
| 1221 | 1221 | } |
| 1222 | 1222 | |
| 1223 | +TEST_CASE("TVersion: help", "[help]") { | |
| 1224 | + | |
| 1225 | + CLI::App app; | |
| 1226 | + | |
| 1227 | + app.set_version_flag("-v,--version", "version_string", "help_for_version"); | |
| 1228 | + | |
| 1229 | + auto hvers = app.help(); | |
| 1230 | + CHECK_THAT(hvers, Contains("help_for_version")); | |
| 1231 | + | |
| 1232 | + app.set_version_flag( | |
| 1233 | + "-v", []() { return std::string("VERSION2 " CLI11_VERSION); }, "help_for_version2"); | |
| 1234 | + hvers = app.help(); | |
| 1235 | + CHECK_THAT(hvers, Contains("help_for_version2")); | |
| 1236 | +} | |
| 1237 | + | |
| 1223 | 1238 | TEST_CASE("TVersion: parse_throw", "[help]") { |
| 1224 | 1239 | |
| 1225 | 1240 | CLI::App app; | ... | ... |