@@ -139,6 +139,9 @@ class App {
139139 // / A pointer to the help all flag if there is one INHERITABLE
140140 Option *help_all_ptr_{nullptr };
141141
142+ // / A pointer to a version flag if there is one
143+ Option *version_ptr_{nullptr };
144+
142145 // / This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
143146 std::shared_ptr<FormatterBase> formatter_{new Formatter ()};
144147
@@ -703,6 +706,45 @@ class App {
703706 return help_all_ptr_;
704707 }
705708
709+ // / Set a version flag and version display string, replace the existing one if present
710+ Option *set_version_flag (std::string flag_name = " " , const std::string &versionString = " " ) {
711+ // take flag_description by const reference otherwise add_flag tries to assign to version_description
712+ if (version_ptr_ != nullptr ) {
713+ remove_option (version_ptr_);
714+ version_ptr_ = nullptr ;
715+ }
716+
717+ // Empty name will simply remove the version flag
718+ if (!flag_name.empty ()) {
719+ version_ptr_ = add_flag_callback (
720+ flag_name,
721+ [versionString]() { throw (CLI::CallForVersion (versionString, 0 )); },
722+ " display program version information and exit" );
723+ version_ptr_->configurable (false );
724+ }
725+
726+ return version_ptr_;
727+ }
728+ // / Generate the version string through a callback function
729+ Option *set_version_flag (std::string flag_name, std::function<std::string()> vfunc) {
730+ // take flag_description by const reference otherwise add_flag tries to assign to version_description
731+ if (version_ptr_ != nullptr ) {
732+ remove_option (version_ptr_);
733+ version_ptr_ = nullptr ;
734+ }
735+
736+ // Empty name will simply remove the version flag
737+ if (!flag_name.empty ()) {
738+ version_ptr_ = add_flag_callback (
739+ flag_name,
740+ [vfunc]() { throw (CLI::CallForVersion (vfunc (), 0 )); },
741+ " display program version information and exit" );
742+ version_ptr_->configurable (false );
743+ }
744+
745+ return version_ptr_;
746+ }
747+
706748 private:
707749 // / Internal function for adding a flag
708750 Option *_add_flag_internal (std::string flag_name, CLI::callback_t fun, std::string flag_description) {
@@ -1345,6 +1387,11 @@ class App {
13451387 return e.get_exit_code ();
13461388 }
13471389
1390+ if (dynamic_cast <const CLI::CallForVersion *>(&e) != nullptr ) {
1391+ out << e.what () << std::endl;
1392+ return e.get_exit_code ();
1393+ }
1394+
13481395 if (e.get_exit_code () != static_cast <int >(ExitCodes::Success)) {
13491396 if (failure_message_)
13501397 err << failure_message_ (this , e) << std::flush;
@@ -1530,6 +1577,23 @@ class App {
15301577 return formatter_->make_help (this , prev, mode);
15311578 }
15321579
1580+ // / Displays a version string
1581+ std::string version () const {
1582+ std::string val;
1583+ if (version_ptr_ != nullptr ) {
1584+ auto rv = version_ptr_->results ();
1585+ version_ptr_->clear ();
1586+ version_ptr_->add_result (" true" );
1587+ try {
1588+ version_ptr_->run_callback ();
1589+ } catch (const CLI::CallForVersion &cfv) {
1590+ val = cfv.what ();
1591+ }
1592+ version_ptr_->clear ();
1593+ version_ptr_->add_result (rv);
1594+ }
1595+ return val;
1596+ }
15331597 // /@}
15341598 // / @name Getters
15351599 // /@{
@@ -1726,6 +1790,12 @@ class App {
17261790 // / Get a pointer to the config option. (const)
17271791 const Option *get_config_ptr () const { return config_ptr_; }
17281792
1793+ // / Get a pointer to the version option.
1794+ Option *get_version_ptr () { return version_ptr_; }
1795+
1796+ // / Get a pointer to the version option. (const)
1797+ const Option *get_version_ptr () const { return version_ptr_; }
1798+
17291799 // / Get the parent of this subcommand (or nullptr if master app)
17301800 App *get_parent () { return parent_; }
17311801
0 commit comments