diff --git a/CHANGELOG.md b/CHANGELOG.md index 0262b0e0c..9b08573f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ ## In progress * Make unlimited positionals vs. unlimited options more intuitive [#102] +* Add missing getters `get_options` and `get_description` to App [#105] +* The app name now can be set, and will override the auto name if present [#105] [#102]: https://github.com/CLIUtils/CLI11/issues/102 +[#105]: https://github.com/CLIUtils/CLI11/issues/105 ## Version 1.5: Optionals diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index e2c239194..4319c25c9 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -64,8 +64,8 @@ class App { /// @name Basics ///@{ - /// Subcommand name or program name (from parser) - std::string name_{"program"}; + /// Subcommand name or program name (from parser if name is empty) + std::string name_; /// Description of the current program/subcommand std::string description_; @@ -193,7 +193,8 @@ class App { ///@{ /// Create a new program. Pass in the same arguments as main(), along with a help string. - App(std::string description_ = "") : App(description_, nullptr) { + App(std::string description_ = "", std::string name = "") : App(description_, nullptr) { + name_ = name; set_help_flag("-h,--help", "Print this help message and exit"); } @@ -211,6 +212,12 @@ class App { return this; } + /// Set a name for the app (empty will use parser to set the name) + App *set_name(std::string name = "") { + name_ = name; + return this; + } + /// Remove the error when extras are left over on the command line. App *allow_extras(bool allow = true) { allow_extras_ = allow; @@ -723,7 +730,10 @@ class App { /// Parses the command line - throws errors /// This must be called after the options are in but before the rest of the program. void parse(int argc, char **argv) { - name_ = argv[0]; + // If the name is not set, read from command line + if(name_.empty()) + name_ = argv[0]; + std::vector args; for(int i = argc - 1; i > 0; i--) args.emplace_back(argv[i]); @@ -890,7 +900,7 @@ class App { std::stringstream out; out << description_ << std::endl; - out << "Usage: " << prev; + out << "Usage:" << (prev.empty() ? "" : " ") << prev; // Check for options_ bool npos = false; @@ -977,6 +987,18 @@ class App { /// @name Getters ///@{ + /// Get the app or subcommand description + std::string get_description() const { return description_; } + + /// Get the list of options (user facing function, so returns raw pointers) + std::vector