Skip to content

Commit 2fa8cae

Browse files
authored
feat: add changeable help message string to version (#601)
add an optional help message string to the version_add flag if desired to override
1 parent 15bb724 commit 2fa8cae

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ There are several options that are supported on the main app and subcommands and
599599
- `.footer(message)`: Set text to appear at the bottom of the help string.
600600
- `.footer(std::string())`: 🆕 Set a callback to generate a string that will appear at the end of the help string.
601601
- `.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.
602603
- `.set_help_all_flag(name, message)`: Set the help all flag name and message, returns a pointer to the created option. Expands subcommands.
603604
- `.failure_message(func)`: Set the failure message function. Two provided: `CLI::FailureMessage::help` and `CLI::FailureMessage::simple` (the default).
604605
- `.group(name)`: Set a group name, defaults to `"Subcommands"`. Setting `""` will be hide the subcommand.

include/CLI/App.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,9 @@ class App {
732732
}
733733

734734
/// 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") {
736738
// take flag_description by const reference otherwise add_flag tries to assign to version_description
737739
if(version_ptr_ != nullptr) {
738740
remove_option(version_ptr_);
@@ -742,17 +744,16 @@ class App {
742744
// Empty name will simply remove the version flag
743745
if(!flag_name.empty()) {
744746
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);
748748
version_ptr_->configurable(false);
749749
}
750750

751751
return version_ptr_;
752752
}
753753
/// 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") {
756757
if(version_ptr_ != nullptr) {
757758
remove_option(version_ptr_);
758759
version_ptr_ = nullptr;
@@ -761,9 +762,7 @@ class App {
761762
// Empty name will simply remove the version flag
762763
if(!flag_name.empty()) {
763764
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);
767766
version_ptr_->configurable(false);
768767
}
769768

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)