Commit e8c195b683dbc5b4d0f99fef55ac3379d5b58e4d

Authored by Henry Fredrick Schreiner
1 parent 4d61a453

Making it easier to add custom options

include/CLI/App.hpp
... ... @@ -237,11 +237,11 @@ public:
237 237 };
238 238  
239 239 Option* opt = add_option(name, fun, description, defaulted);
240   - opt->typeval_ = detail::type_name<T>();
  240 + opt->set_custom_option(detail::type_name<T>());
241 241 if(defaulted) {
242 242 std::stringstream out;
243 243 out << variable;
244   - opt->defaultval_ = out.str();
  244 + opt->set_default_val(out.str());
245 245 }
246 246 return opt;
247 247 }
... ... @@ -266,11 +266,9 @@ public:
266 266 };
267 267  
268 268 Option* opt = add_option(name, fun, description, defaulted);
269   - opt->allow_vector_ = true;
270   - opt->expected_ = -1;
271   - opt->typeval_ = detail::type_name<T>();
  269 + opt->set_custom_option(detail::type_name<T>(), -1, true);
272 270 if(defaulted)
273   - opt->defaultval_ = "[" + detail::join(variable) + "]";
  271 + opt->set_default_val("[" + detail::join(variable) + "]");
274 272 return opt;
275 273 }
276 274  
... ... @@ -286,7 +284,7 @@ public:
286 284 Option* opt = add_option(name, fun, description, false);
287 285 if(opt->get_positional())
288 286 throw IncorrectConstruction("Flags cannot be positional");
289   - opt->expected_ = 0;
  287 + opt->set_custom_option("", 0);
290 288 return opt;
291 289 }
292 290  
... ... @@ -308,7 +306,7 @@ public:
308 306 Option* opt = add_option(name, fun, description, false);
309 307 if(opt->get_positional())
310 308 throw IncorrectConstruction("Flags cannot be positional");
311   - opt->expected_ = 0;
  309 + opt->set_custom_option("", 0);
312 310 return opt;
313 311 }
314 312  
... ... @@ -330,7 +328,7 @@ public:
330 328 Option* opt = add_option(name, fun, description, false);
331 329 if(opt->get_positional())
332 330 throw IncorrectConstruction("Flags cannot be positional");
333   - opt->expected_ = 0;
  331 + opt->set_custom_option("", 0);
334 332 return opt;
335 333 }
336 334  
... ... @@ -356,12 +354,13 @@ public:
356 354 };
357 355  
358 356 Option* opt = add_option(name, fun, description, defaulted);
359   - opt->typeval_ = detail::type_name<T>();
360   - opt->typeval_ += " in {" + detail::join(options) + "}";
  357 + std::string typeval = detail::type_name<T>();
  358 + typeval += " in {" + detail::join(options) + "}";
  359 + opt->set_custom_option(typeval);
361 360 if(defaulted) {
362 361 std::stringstream out;
363 362 out << member;
364   - opt->defaultval_ = out.str();
  363 + opt->set_default_val(out.str());
365 364 }
366 365 return opt;
367 366 }
... ... @@ -391,10 +390,11 @@ public:
391 390 };
392 391  
393 392 Option* opt = add_option(name, fun, description, defaulted);
394   - opt->typeval_ = detail::type_name<std::string>();
395   - opt->typeval_ += " in {" + detail::join(options) + "}";
  393 + std::string typeval = detail::type_name<std::string>();
  394 + typeval += " in {" + detail::join(options) + "}";
  395 + opt->set_custom_option(typeval);
396 396 if(defaulted) {
397   - opt->defaultval_ = member;
  397 + opt->set_default_val(member);
398 398 }
399 399 return opt;
400 400 }
... ...
include/CLI/Option.hpp
... ... @@ -73,8 +73,8 @@ protected:
73 73 /// The number of expected values, 0 for flag, -1 for unlimited vector
74 74 int expected_ {1};
75 75  
76   - /// A private setting to allow non-vector args to not be able to accept incorrect expected values
77   - bool allow_vector_ {false};
  76 + /// A private setting to allow args to not be able to accept incorrect expected values
  77 + bool changeable_ {false};
78 78  
79 79 /// Ignore the case when matching (option, not value)
80 80 bool ignore_case_ {false};
... ... @@ -155,12 +155,12 @@ public:
155 155  
156 156 /// Set the number of expected arguments (Flags bypass this)
157 157 Option* expected(int value) {
158   - if(value == 0)
  158 + if(!changeable_)
  159 + throw IncorrectConstruction("You can only change the expected arguments for vectors");
  160 + else if(value == 0)
159 161 throw IncorrectConstruction("Cannot set 0 expected, use a flag instead");
160 162 else if(expected_ == 0)
161 163 throw IncorrectConstruction("Cannot make a flag take arguments!");
162   - else if(!allow_vector_ && value != 1)
163   - throw IncorrectConstruction("You can only change the Expected arguments for vectors");
164 164 expected_ = value;
165 165 return this;
166 166 }
... ... @@ -460,6 +460,23 @@ public:
460 460 }
461 461  
462 462 ///@}
  463 + /// @name Custom options
  464 + ///@{
  465 +
  466 + /// Set a custom option, typestring, expected, and changeable
  467 + void set_custom_option(std::string typeval, int expected=1, bool changeable = false) {
  468 + typeval_ = typeval;
  469 + expected_ = expected;
  470 + changeable_ = changeable;
  471 + }
  472 +
  473 + /// Set the default value string representation
  474 + void set_default_val(std::string val) {
  475 + defaultval_ = val;
  476 + }
  477 +
  478 + ///@}
  479 +
463 480  
464 481 protected:
465 482  
... ...