This is an automated email from the ASF dual-hosted git repository. astitcher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-proton.git
commit 344e0bd6772fcaca5c3f1b7c2463898ee7de27ea Author: Andrew Stitcher <[email protected]> AuthorDate: Thu Mar 12 16:54:05 2026 -0400 PROTON-1442: [C++ examples] Allow string cli options to contain spaces This code was written with the assistance of Cursor. --- cpp/examples/options.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cpp/examples/options.hpp b/cpp/examples/options.hpp index dab1bc20d..053f1ae53 100644 --- a/cpp/examples/options.hpp +++ b/cpp/examples/options.hpp @@ -53,6 +53,11 @@ class options { opts_.push_back(new option_value<T>(value, short_name, long_name, description, var)); } + /** Overload for std::string: takes the full argument (including spaces). */ + void add_value(std::string& value, char short_name, const std::string& long_name, const std::string& description, const std::string var) { + opts_.push_back(new option_string(value, short_name, long_name, description, var)); + } + /** Sets flag when parse() is called if option is present. */ void add_flag(bool& flag, char short_name, const std::string& long_name, const std::string& description) { opts_.push_back(new option_flag(flag, short_name, long_name, description)); @@ -143,6 +148,34 @@ class options { T& value_; }; + class option_string : public option { + public: + option_string(std::string& value, char s, const std::string& l, const std::string& d, const std::string& v) : + option(s, l, d, v), value_(value) {} + + bool parse(int argc, char const * const * argv, int &i) { + std::string arg(argv[i]); + if (arg == short_ || arg == long_) { + if (i < argc-1) { + value_ = argv[++i]; + return true; + } else { + throw bad_option("missing value for " + arg); + } + } + if (arg.compare(0, long_.size(), long_) == 0 && arg[long_.size()] == '=') { + value_ = arg.substr(long_.size() + 1); + return true; + } + return false; + } + + virtual void print_default(std::ostream& os) const { if (!value_.empty()) os << " (default \"" << value_ << "\")"; } + + private: + std::string& value_; + }; + class option_flag: public option { public: option_flag(bool& flag, const char s, const std::string& l, const std::string& d) : --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
