Commit 63bd186760156d622c2c92daabb8f1ba1ecc3be3
1 parent
81f58c13
Refactor name/desc
Showing
1 changed file
with
34 additions
and
29 deletions
include/CLI/App.hpp
| @@ -41,8 +41,11 @@ class App { | @@ -41,8 +41,11 @@ class App { | ||
| 41 | friend Option; | 41 | friend Option; |
| 42 | protected: | 42 | protected: |
| 43 | 43 | ||
| 44 | - std::string name; | ||
| 45 | - std::string prog_description; | 44 | + /// Subcommand name or program name (from parser) |
| 45 | + std::string name {"program"}; | ||
| 46 | + | ||
| 47 | + /// Description of the current program/subcommand | ||
| 48 | + std::string description; | ||
| 46 | std::vector<Option_p> options; | 49 | std::vector<Option_p> options; |
| 47 | 50 | ||
| 48 | /// Pair of classifer, string for missing options. (extra detail is removed on returning from parse) | 51 | /// Pair of classifer, string for missing options. (extra detail is removed on returning from parse) |
| @@ -52,8 +55,10 @@ protected: | @@ -52,8 +55,10 @@ protected: | ||
| 52 | std::vector<App_p> subcommands; | 55 | std::vector<App_p> subcommands; |
| 53 | bool parsed {false}; | 56 | bool parsed {false}; |
| 54 | std::vector<App*> selected_subcommands; | 57 | std::vector<App*> selected_subcommands; |
| 55 | - int required_subcommand = 0; ///< -1 for 1 or more, 0 for not required, # for exact number required | ||
| 56 | - std::string progname {"program"}; | 58 | + |
| 59 | + /// -1 for 1 or more, 0 for not required, # for exact number required | ||
| 60 | + int required_subcommand = 0; | ||
| 61 | + | ||
| 57 | Option* help_flag {nullptr}; | 62 | Option* help_flag {nullptr}; |
| 58 | 63 | ||
| 59 | std::function<void()> app_callback; | 64 | std::function<void()> app_callback; |
| @@ -68,8 +73,8 @@ protected: | @@ -68,8 +73,8 @@ protected: | ||
| 68 | 73 | ||
| 69 | public: | 74 | public: |
| 70 | /// Create a new program. Pass in the same arguments as main(), along with a help string. | 75 | /// Create a new program. Pass in the same arguments as main(), along with a help string. |
| 71 | - App(std::string prog_description="", bool help=true) | ||
| 72 | - : prog_description(prog_description) { | 76 | + App(std::string description="", bool help=true) |
| 77 | + : description(description) { | ||
| 73 | 78 | ||
| 74 | if(help) | 79 | if(help) |
| 75 | help_flag = add_flag("-h,--help", "Print this help message and exit"); | 80 | help_flag = add_flag("-h,--help", "Print this help message and exit"); |
| @@ -121,8 +126,8 @@ public: | @@ -121,8 +126,8 @@ public: | ||
| 121 | } | 126 | } |
| 122 | 127 | ||
| 123 | /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false | 128 | /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false |
| 124 | - App* add_subcommand(std::string name_, std::string description="", bool help=true) { | ||
| 125 | - subcommands.emplace_back(new App(description, help)); | 129 | + App* add_subcommand(std::string name_, std::string description_="", bool help=true) { |
| 130 | + subcommands.emplace_back(new App(description_, help)); | ||
| 126 | subcommands.back()->name = name_; | 131 | subcommands.back()->name = name_; |
| 127 | subcommands.back()->allow_extras(); | 132 | subcommands.back()->allow_extras(); |
| 128 | subcommands.back()->parent = this; | 133 | subcommands.back()->parent = this; |
| @@ -150,16 +155,16 @@ public: | @@ -150,16 +155,16 @@ public: | ||
| 150 | Option* add_option( | 155 | Option* add_option( |
| 151 | std::string name_, | 156 | std::string name_, |
| 152 | callback_t callback, | 157 | callback_t callback, |
| 153 | - std::string description="", | 158 | + std::string description_="", |
| 154 | bool defaulted=false | 159 | bool defaulted=false |
| 155 | ) { | 160 | ) { |
| 156 | - Option myopt{name_, description, callback, defaulted, this}; | 161 | + Option myopt{name_, description_, callback, defaulted, this}; |
| 157 | 162 | ||
| 158 | if(std::find_if(std::begin(options), std::end(options), | 163 | if(std::find_if(std::begin(options), std::end(options), |
| 159 | [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) { | 164 | [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) { |
| 160 | options.emplace_back(); | 165 | options.emplace_back(); |
| 161 | Option_p& option = options.back(); | 166 | Option_p& option = options.back(); |
| 162 | - option.reset(new Option(name_, description, callback, defaulted, this)); | 167 | + option.reset(new Option(name_, description_, callback, defaulted, this)); |
| 163 | return option.get(); | 168 | return option.get(); |
| 164 | } else | 169 | } else |
| 165 | throw OptionAlreadyAdded(myopt.get_name()); | 170 | throw OptionAlreadyAdded(myopt.get_name()); |
| @@ -171,7 +176,7 @@ public: | @@ -171,7 +176,7 @@ public: | ||
| 171 | Option* add_option( | 176 | Option* add_option( |
| 172 | std::string name_, | 177 | std::string name_, |
| 173 | T &variable, ///< The variable to set | 178 | T &variable, ///< The variable to set |
| 174 | - std::string description="", | 179 | + std::string description_="", |
| 175 | bool defaulted=false | 180 | bool defaulted=false |
| 176 | ) { | 181 | ) { |
| 177 | 182 | ||
| @@ -186,7 +191,7 @@ public: | @@ -186,7 +191,7 @@ public: | ||
| 186 | return detail::lexical_cast(res[0][0], variable); | 191 | return detail::lexical_cast(res[0][0], variable); |
| 187 | }; | 192 | }; |
| 188 | 193 | ||
| 189 | - Option* retval = add_option(name_, fun, description, defaulted); | 194 | + Option* retval = add_option(name_, fun, description_, defaulted); |
| 190 | retval->typeval = detail::type_name<T>(); | 195 | retval->typeval = detail::type_name<T>(); |
| 191 | if(defaulted) { | 196 | if(defaulted) { |
| 192 | std::stringstream out; | 197 | std::stringstream out; |
| @@ -201,7 +206,7 @@ public: | @@ -201,7 +206,7 @@ public: | ||
| 201 | Option* add_option( | 206 | Option* add_option( |
| 202 | std::string name_, | 207 | std::string name_, |
| 203 | std::vector<T> &variable, ///< The variable vector to set | 208 | std::vector<T> &variable, ///< The variable vector to set |
| 204 | - std::string description="", | 209 | + std::string description_="", |
| 205 | bool defaulted=false | 210 | bool defaulted=false |
| 206 | ) { | 211 | ) { |
| 207 | 212 | ||
| @@ -216,7 +221,7 @@ public: | @@ -216,7 +221,7 @@ public: | ||
| 216 | return variable.size() > 0 && retval; | 221 | return variable.size() > 0 && retval; |
| 217 | }; | 222 | }; |
| 218 | 223 | ||
| 219 | - Option* retval = add_option(name_, fun, description, defaulted); | 224 | + Option* retval = add_option(name_, fun, description_, defaulted); |
| 220 | retval->allow_vector = true; | 225 | retval->allow_vector = true; |
| 221 | retval->_expected = -1; | 226 | retval->_expected = -1; |
| 222 | retval->typeval = detail::type_name<T>(); | 227 | retval->typeval = detail::type_name<T>(); |
| @@ -229,13 +234,13 @@ public: | @@ -229,13 +234,13 @@ public: | ||
| 229 | /// Add option for flag | 234 | /// Add option for flag |
| 230 | Option* add_flag( | 235 | Option* add_flag( |
| 231 | std::string name_, | 236 | std::string name_, |
| 232 | - std::string description="" | 237 | + std::string description_="" |
| 233 | ) { | 238 | ) { |
| 234 | CLI::callback_t fun = [](CLI::results_t){ | 239 | CLI::callback_t fun = [](CLI::results_t){ |
| 235 | return true; | 240 | return true; |
| 236 | }; | 241 | }; |
| 237 | 242 | ||
| 238 | - Option* opt = add_option(name_, fun, description, false); | 243 | + Option* opt = add_option(name_, fun, description_, false); |
| 239 | if(opt->get_positional()) | 244 | if(opt->get_positional()) |
| 240 | throw IncorrectConstruction("Flags cannot be positional"); | 245 | throw IncorrectConstruction("Flags cannot be positional"); |
| 241 | opt->_expected = 0; | 246 | opt->_expected = 0; |
| @@ -248,7 +253,7 @@ public: | @@ -248,7 +253,7 @@ public: | ||
| 248 | Option* add_flag( | 253 | Option* add_flag( |
| 249 | std::string name_, | 254 | std::string name_, |
| 250 | T &count, ///< A varaible holding the count | 255 | T &count, ///< A varaible holding the count |
| 251 | - std::string description="" | 256 | + std::string description_="" |
| 252 | ) { | 257 | ) { |
| 253 | 258 | ||
| 254 | count = 0; | 259 | count = 0; |
| @@ -257,7 +262,7 @@ public: | @@ -257,7 +262,7 @@ public: | ||
| 257 | return true; | 262 | return true; |
| 258 | }; | 263 | }; |
| 259 | 264 | ||
| 260 | - Option* opt = add_option(name_, fun, description, false); | 265 | + Option* opt = add_option(name_, fun, description_, false); |
| 261 | if(opt->get_positional()) | 266 | if(opt->get_positional()) |
| 262 | throw IncorrectConstruction("Flags cannot be positional"); | 267 | throw IncorrectConstruction("Flags cannot be positional"); |
| 263 | opt->_expected = 0; | 268 | opt->_expected = 0; |
| @@ -270,7 +275,7 @@ public: | @@ -270,7 +275,7 @@ public: | ||
| 270 | Option* add_flag( | 275 | Option* add_flag( |
| 271 | std::string name_, | 276 | std::string name_, |
| 272 | T &count, ///< A varaible holding true if passed | 277 | T &count, ///< A varaible holding true if passed |
| 273 | - std::string description="" | 278 | + std::string description_="" |
| 274 | ) { | 279 | ) { |
| 275 | 280 | ||
| 276 | count = false; | 281 | count = false; |
| @@ -279,7 +284,7 @@ public: | @@ -279,7 +284,7 @@ public: | ||
| 279 | return res.size() == 1; | 284 | return res.size() == 1; |
| 280 | }; | 285 | }; |
| 281 | 286 | ||
| 282 | - Option* opt = add_option(name_, fun, description, false); | 287 | + Option* opt = add_option(name_, fun, description_, false); |
| 283 | if(opt->get_positional()) | 288 | if(opt->get_positional()) |
| 284 | throw IncorrectConstruction("Flags cannot be positional"); | 289 | throw IncorrectConstruction("Flags cannot be positional"); |
| 285 | opt->_expected = 0; | 290 | opt->_expected = 0; |
| @@ -293,7 +298,7 @@ public: | @@ -293,7 +298,7 @@ public: | ||
| 293 | std::string name_, | 298 | std::string name_, |
| 294 | T &member, ///< The selected member of the set | 299 | T &member, ///< The selected member of the set |
| 295 | std::set<T> _options, ///< The set of posibilities | 300 | std::set<T> _options, ///< The set of posibilities |
| 296 | - std::string description="", | 301 | + std::string description_="", |
| 297 | bool defaulted=false | 302 | bool defaulted=false |
| 298 | ) { | 303 | ) { |
| 299 | 304 | ||
| @@ -310,7 +315,7 @@ public: | @@ -310,7 +315,7 @@ public: | ||
| 310 | return std::find(std::begin(_options), std::end(_options), member) != std::end(_options); | 315 | return std::find(std::begin(_options), std::end(_options), member) != std::end(_options); |
| 311 | }; | 316 | }; |
| 312 | 317 | ||
| 313 | - Option* retval = add_option(name_, fun, description, defaulted); | 318 | + Option* retval = add_option(name_, fun, description_, defaulted); |
| 314 | retval->typeval = detail::type_name<T>(); | 319 | retval->typeval = detail::type_name<T>(); |
| 315 | retval->typeval += " in {" + detail::join(_options) + "}"; | 320 | retval->typeval += " in {" + detail::join(_options) + "}"; |
| 316 | if(defaulted) { | 321 | if(defaulted) { |
| @@ -326,7 +331,7 @@ public: | @@ -326,7 +331,7 @@ public: | ||
| 326 | std::string name_, | 331 | std::string name_, |
| 327 | std::string &member, ///< The selected member of the set | 332 | std::string &member, ///< The selected member of the set |
| 328 | std::set<std::string> _options, ///< The set of posibilities | 333 | std::set<std::string> _options, ///< The set of posibilities |
| 329 | - std::string description="", | 334 | + std::string description_="", |
| 330 | bool defaulted=false | 335 | bool defaulted=false |
| 331 | ) { | 336 | ) { |
| 332 | 337 | ||
| @@ -348,7 +353,7 @@ public: | @@ -348,7 +353,7 @@ public: | ||
| 348 | } | 353 | } |
| 349 | }; | 354 | }; |
| 350 | 355 | ||
| 351 | - Option* retval = add_option(name_, fun, description, defaulted); | 356 | + Option* retval = add_option(name_, fun, description_, defaulted); |
| 352 | retval->typeval = detail::type_name<std::string>(); | 357 | retval->typeval = detail::type_name<std::string>(); |
| 353 | retval->typeval += " in {" + detail::join(_options) + "}"; | 358 | retval->typeval += " in {" + detail::join(_options) + "}"; |
| 354 | if(defaulted) { | 359 | if(defaulted) { |
| @@ -391,7 +396,7 @@ public: | @@ -391,7 +396,7 @@ public: | ||
| 391 | /// Parses the command line - throws errors | 396 | /// Parses the command line - throws errors |
| 392 | /// This must be called after the options are in but before the rest of the program. | 397 | /// This must be called after the options are in but before the rest of the program. |
| 393 | std::vector<std::string> parse(int argc, char **argv) { | 398 | std::vector<std::string> parse(int argc, char **argv) { |
| 394 | - progname = argv[0]; | 399 | + name = argv[0]; |
| 395 | std::vector<std::string> args; | 400 | std::vector<std::string> args; |
| 396 | for(int i=argc-1; i>0; i--) | 401 | for(int i=argc-1; i>0; i--) |
| 397 | args.push_back(argv[i]); | 402 | args.push_back(argv[i]); |
| @@ -439,7 +444,7 @@ public: | @@ -439,7 +444,7 @@ public: | ||
| 439 | std::string help(size_t wid=30, std::string prev="") const { | 444 | std::string help(size_t wid=30, std::string prev="") const { |
| 440 | // Delegate to subcommand if needed | 445 | // Delegate to subcommand if needed |
| 441 | if(prev == "") | 446 | if(prev == "") |
| 442 | - prev = progname; | 447 | + prev = name; |
| 443 | else | 448 | else |
| 444 | prev += " " + name; | 449 | prev += " " + name; |
| 445 | 450 | ||
| @@ -447,7 +452,7 @@ public: | @@ -447,7 +452,7 @@ public: | ||
| 447 | return selected_subcommands.at(0)->help(wid, prev); | 452 | return selected_subcommands.at(0)->help(wid, prev); |
| 448 | 453 | ||
| 449 | std::stringstream out; | 454 | std::stringstream out; |
| 450 | - out << prog_description << std::endl; | 455 | + out << description << std::endl; |
| 451 | out << "Usage: " << prev; | 456 | out << "Usage: " << prev; |
| 452 | 457 | ||
| 453 | // Check for options | 458 | // Check for options |
| @@ -517,7 +522,7 @@ public: | @@ -517,7 +522,7 @@ public: | ||
| 517 | if(subcommands.size()> 0) { | 522 | if(subcommands.size()> 0) { |
| 518 | out << "Subcommands:" << std::endl; | 523 | out << "Subcommands:" << std::endl; |
| 519 | for(const App_p &com : subcommands) | 524 | for(const App_p &com : subcommands) |
| 520 | - detail::format_help(out, com->get_name(), com->prog_description, wid); | 525 | + detail::format_help(out, com->get_name(), com->description, wid); |
| 521 | } | 526 | } |
| 522 | return out.str(); | 527 | return out.str(); |
| 523 | } | 528 | } |