Commit 63bd186760156d622c2c92daabb8f1ba1ecc3be3

Authored by Henry Fredrick Schreiner
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 41 friend Option;
42 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 49 std::vector<Option_p> options;
47 50  
48 51 /// Pair of classifer, string for missing options. (extra detail is removed on returning from parse)
... ... @@ -52,8 +55,10 @@ protected:
52 55 std::vector<App_p> subcommands;
53 56 bool parsed {false};
54 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 62 Option* help_flag {nullptr};
58 63  
59 64 std::function<void()> app_callback;
... ... @@ -68,8 +73,8 @@ protected:
68 73  
69 74 public:
70 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 79 if(help)
75 80 help_flag = add_flag("-h,--help", "Print this help message and exit");
... ... @@ -121,8 +126,8 @@ public:
121 126 }
122 127  
123 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 131 subcommands.back()->name = name_;
127 132 subcommands.back()->allow_extras();
128 133 subcommands.back()->parent = this;
... ... @@ -150,16 +155,16 @@ public:
150 155 Option* add_option(
151 156 std::string name_,
152 157 callback_t callback,
153   - std::string description="",
  158 + std::string description_="",
154 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 163 if(std::find_if(std::begin(options), std::end(options),
159 164 [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) {
160 165 options.emplace_back();
161 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 168 return option.get();
164 169 } else
165 170 throw OptionAlreadyAdded(myopt.get_name());
... ... @@ -171,7 +176,7 @@ public:
171 176 Option* add_option(
172 177 std::string name_,
173 178 T &variable, ///< The variable to set
174   - std::string description="",
  179 + std::string description_="",
175 180 bool defaulted=false
176 181 ) {
177 182  
... ... @@ -186,7 +191,7 @@ public:
186 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 195 retval->typeval = detail::type_name<T>();
191 196 if(defaulted) {
192 197 std::stringstream out;
... ... @@ -201,7 +206,7 @@ public:
201 206 Option* add_option(
202 207 std::string name_,
203 208 std::vector<T> &variable, ///< The variable vector to set
204   - std::string description="",
  209 + std::string description_="",
205 210 bool defaulted=false
206 211 ) {
207 212  
... ... @@ -216,7 +221,7 @@ public:
216 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 225 retval->allow_vector = true;
221 226 retval->_expected = -1;
222 227 retval->typeval = detail::type_name<T>();
... ... @@ -229,13 +234,13 @@ public:
229 234 /// Add option for flag
230 235 Option* add_flag(
231 236 std::string name_,
232   - std::string description=""
  237 + std::string description_=""
233 238 ) {
234 239 CLI::callback_t fun = [](CLI::results_t){
235 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 244 if(opt->get_positional())
240 245 throw IncorrectConstruction("Flags cannot be positional");
241 246 opt->_expected = 0;
... ... @@ -248,7 +253,7 @@ public:
248 253 Option* add_flag(
249 254 std::string name_,
250 255 T &count, ///< A varaible holding the count
251   - std::string description=""
  256 + std::string description_=""
252 257 ) {
253 258  
254 259 count = 0;
... ... @@ -257,7 +262,7 @@ public:
257 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 266 if(opt->get_positional())
262 267 throw IncorrectConstruction("Flags cannot be positional");
263 268 opt->_expected = 0;
... ... @@ -270,7 +275,7 @@ public:
270 275 Option* add_flag(
271 276 std::string name_,
272 277 T &count, ///< A varaible holding true if passed
273   - std::string description=""
  278 + std::string description_=""
274 279 ) {
275 280  
276 281 count = false;
... ... @@ -279,7 +284,7 @@ public:
279 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 288 if(opt->get_positional())
284 289 throw IncorrectConstruction("Flags cannot be positional");
285 290 opt->_expected = 0;
... ... @@ -293,7 +298,7 @@ public:
293 298 std::string name_,
294 299 T &member, ///< The selected member of the set
295 300 std::set<T> _options, ///< The set of posibilities
296   - std::string description="",
  301 + std::string description_="",
297 302 bool defaulted=false
298 303 ) {
299 304  
... ... @@ -310,7 +315,7 @@ public:
310 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 319 retval->typeval = detail::type_name<T>();
315 320 retval->typeval += " in {" + detail::join(_options) + "}";
316 321 if(defaulted) {
... ... @@ -326,7 +331,7 @@ public:
326 331 std::string name_,
327 332 std::string &member, ///< The selected member of the set
328 333 std::set<std::string> _options, ///< The set of posibilities
329   - std::string description="",
  334 + std::string description_="",
330 335 bool defaulted=false
331 336 ) {
332 337  
... ... @@ -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 357 retval->typeval = detail::type_name<std::string>();
353 358 retval->typeval += " in {" + detail::join(_options) + "}";
354 359 if(defaulted) {
... ... @@ -391,7 +396,7 @@ public:
391 396 /// Parses the command line - throws errors
392 397 /// This must be called after the options are in but before the rest of the program.
393 398 std::vector<std::string> parse(int argc, char **argv) {
394   - progname = argv[0];
  399 + name = argv[0];
395 400 std::vector<std::string> args;
396 401 for(int i=argc-1; i>0; i--)
397 402 args.push_back(argv[i]);
... ... @@ -439,7 +444,7 @@ public:
439 444 std::string help(size_t wid=30, std::string prev="") const {
440 445 // Delegate to subcommand if needed
441 446 if(prev == "")
442   - prev = progname;
  447 + prev = name;
443 448 else
444 449 prev += " " + name;
445 450  
... ... @@ -447,7 +452,7 @@ public:
447 452 return selected_subcommands.at(0)->help(wid, prev);
448 453  
449 454 std::stringstream out;
450   - out << prog_description << std::endl;
  455 + out << description << std::endl;
451 456 out << "Usage: " << prev;
452 457  
453 458 // Check for options
... ... @@ -517,7 +522,7 @@ public:
517 522 if(subcommands.size()> 0) {
518 523 out << "Subcommands:" << std::endl;
519 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 527 return out.str();
523 528 }
... ...