Skip to content

Commit 51f4100

Browse files
committed
add an optional help message string to the version_add flag if desired to override
1 parent e2e3cb2 commit 51f4100

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

include/CLI/App.hpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,9 @@ class App {
719719
}
720720

721721
/// Set a version flag and version display string, replace the existing one if present
722-
Option *set_version_flag(std::string flag_name = "", const std::string &versionString = "") {
722+
Option *set_version_flag(std::string flag_name = "",
723+
const std::string &versionString = "",
724+
const std::string &version_help = "Display program version information and exit") {
723725
// take flag_description by const reference otherwise add_flag tries to assign to version_description
724726
if(version_ptr_ != nullptr) {
725727
remove_option(version_ptr_);
@@ -731,15 +733,16 @@ class App {
731733
version_ptr_ = add_flag_callback(
732734
flag_name,
733735
[versionString]() { throw(CLI::CallForVersion(versionString, 0)); },
734-
"Display program version information and exit");
736+
version_help);
735737
version_ptr_->configurable(false);
736738
}
737739

738740
return version_ptr_;
739741
}
740742
/// Generate the version string through a callback function
741-
Option *set_version_flag(std::string flag_name, std::function<std::string()> vfunc) {
742-
// take flag_description by const reference otherwise add_flag tries to assign to version_description
743+
Option *set_version_flag(std::string flag_name,
744+
std::function<std::string()> vfunc,
745+
const std::string &version_help = "Display program version information and exit") {
743746
if(version_ptr_ != nullptr) {
744747
remove_option(version_ptr_);
745748
version_ptr_ = nullptr;
@@ -750,7 +753,7 @@ class App {
750753
version_ptr_ = add_flag_callback(
751754
flag_name,
752755
[vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); },
753-
"Display program version information and exit");
756+
version_help);
754757
version_ptr_->configurable(false);
755758
}
756759

tests/HelpTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,21 @@ TEST_CASE("TVersion: callback_flag", "[help]") {
12201220
CHECK_THAT(vers, Contains("VERSION"));
12211221
}
12221222

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+
12231238
TEST_CASE("TVersion: parse_throw", "[help]") {
12241239

12251240
CLI::App app;

0 commit comments

Comments
 (0)