Commit 2fa8cae9e86d1ceaaebece5811c78ea96ccbc25f

Authored by Philip Top
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
README.md
@@ -599,6 +599,7 @@ There are several options that are supported on the main app and subcommands and @@ -599,6 +599,7 @@ There are several options that are supported on the main app and subcommands and
599 - `.footer(message)`: Set text to appear at the bottom of the help string. 599 - `.footer(message)`: Set text to appear at the bottom of the help string.
600 - `.footer(std::string())`: ๐Ÿ†• Set a callback to generate a string that will appear at the end of the help string. 600 - `.footer(std::string())`: ๐Ÿ†• Set a callback to generate a string that will appear at the end of the help string.
601 - `.set_help_flag(name, message)`: Set the help flag name and message, returns a pointer to the created option. 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 - `.set_help_all_flag(name, message)`: Set the help all flag name and message, returns a pointer to the created option. Expands subcommands. 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 - `.failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default). 604 - `.failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default).
604 - `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` will be hide the subcommand. 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,7 +732,9 @@ class App {
732 } 732 }
733 733
734 /// Set a version flag and version display string, replace the existing one if present 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 // take flag_description by const reference otherwise add_flag tries to assign to version_description 738 // take flag_description by const reference otherwise add_flag tries to assign to version_description
737 if(version_ptr_ != nullptr) { 739 if(version_ptr_ != nullptr) {
738 remove_option(version_ptr_); 740 remove_option(version_ptr_);
@@ -742,17 +744,16 @@ class App { @@ -742,17 +744,16 @@ class App {
742 // Empty name will simply remove the version flag 744 // Empty name will simply remove the version flag
743 if(!flag_name.empty()) { 745 if(!flag_name.empty()) {
744 version_ptr_ = add_flag_callback( 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 version_ptr_->configurable(false); 748 version_ptr_->configurable(false);
749 } 749 }
750 750
751 return version_ptr_; 751 return version_ptr_;
752 } 752 }
753 /// Generate the version string through a callback function 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 if(version_ptr_ != nullptr) { 757 if(version_ptr_ != nullptr) {
757 remove_option(version_ptr_); 758 remove_option(version_ptr_);
758 version_ptr_ = nullptr; 759 version_ptr_ = nullptr;
@@ -761,9 +762,7 @@ class App { @@ -761,9 +762,7 @@ class App {
761 // Empty name will simply remove the version flag 762 // Empty name will simply remove the version flag
762 if(!flag_name.empty()) { 763 if(!flag_name.empty()) {
763 version_ptr_ = add_flag_callback( 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 version_ptr_->configurable(false); 766 version_ptr_->configurable(false);
768 } 767 }
769 768
tests/HelpTest.cpp
@@ -1220,6 +1220,21 @@ TEST_CASE(&quot;TVersion: callback_flag&quot;, &quot;[help]&quot;) { @@ -1220,6 +1220,21 @@ TEST_CASE(&quot;TVersion: callback_flag&quot;, &quot;[help]&quot;) {
1220 CHECK_THAT(vers, Contains("VERSION")); 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 TEST_CASE("TVersion: parse_throw", "[help]") { 1238 TEST_CASE("TVersion: parse_throw", "[help]") {
1224 1239
1225 CLI::App app; 1240 CLI::App app;