Commit a8f5b3283043b608ef067ab03dc9d486663f9b05

Authored by Henry Fredrick Schreiner
1 parent c02130b7

Updated names in Option, too

include/CLI/App.hpp
... ... @@ -109,7 +109,7 @@ protected:
109 109 /// Pointer to the config option
110 110 Option* config_ptr_ {nullptr};
111 111  
112   - yy///@}
  112 + ///@}
113 113  
114 114  
115 115 public:
... ... @@ -218,14 +218,14 @@ public:
218 218 return detail::lexical_cast(res[0][0], variable);
219 219 };
220 220  
221   - Option* retval = add_option(name, fun, description, defaulted);
222   - retval->typeval = detail::type_name<T>();
  221 + Option* opt = add_option(name, fun, description, defaulted);
  222 + opt->typeval_ = detail::type_name<T>();
223 223 if(defaulted) {
224 224 std::stringstream out;
225 225 out << variable;
226   - retval->defaultval = out.str();
  226 + opt->defaultval_ = out.str();
227 227 }
228   - return retval;
  228 + return opt;
229 229 }
230 230  
231 231 /// Add option for vectors
... ... @@ -248,13 +248,13 @@ public:
248 248 return variable.size() > 0 && retval;
249 249 };
250 250  
251   - Option* retval = add_option(name, fun, description, defaulted);
252   - retval->allow_vector = true;
253   - retval->_expected = -1;
254   - retval->typeval = detail::type_name<T>();
  251 + Option* opt = add_option(name, fun, description, defaulted);
  252 + opt->allow_vector_ = true;
  253 + opt->expected_ = -1;
  254 + opt->typeval_ = detail::type_name<T>();
255 255 if(defaulted)
256   - retval->defaultval = "[" + detail::join(variable) + "]";
257   - return retval;
  256 + opt->defaultval_ = "[" + detail::join(variable) + "]";
  257 + return opt;
258 258 }
259 259  
260 260 /// Add option for flag
... ... @@ -269,7 +269,7 @@ public:
269 269 Option* opt = add_option(name, fun, description, false);
270 270 if(opt->get_positional())
271 271 throw IncorrectConstruction("Flags cannot be positional");
272   - opt->_expected = 0;
  272 + opt->expected_ = 0;
273 273 return opt;
274 274 }
275 275  
... ... @@ -291,7 +291,7 @@ public:
291 291 Option* opt = add_option(name, fun, description, false);
292 292 if(opt->get_positional())
293 293 throw IncorrectConstruction("Flags cannot be positional");
294   - opt->_expected = 0;
  294 + opt->expected_ = 0;
295 295 return opt;
296 296 }
297 297  
... ... @@ -313,7 +313,7 @@ public:
313 313 Option* opt = add_option(name, fun, description, false);
314 314 if(opt->get_positional())
315 315 throw IncorrectConstruction("Flags cannot be positional");
316   - opt->_expected = 0;
  316 + opt->expected_ = 0;
317 317 return opt;
318 318 }
319 319  
... ... @@ -323,12 +323,12 @@ public:
323 323 Option* add_set(
324 324 std::string name,
325 325 T &member, ///< The selected member of the set
326   - std::set<T> _options, ///< The set of posibilities
  326 + std::set<T> options, ///< The set of posibilities
327 327 std::string description="",
328 328 bool defaulted=false
329 329 ) {
330 330  
331   - CLI::callback_t fun = [&member, _options](CLI::results_t res){
  331 + CLI::callback_t fun = [&member, options](CLI::results_t res){
332 332 if(res.size()!=1) {
333 333 return false;
334 334 }
... ... @@ -338,30 +338,30 @@ public:
338 338 bool retval = detail::lexical_cast(res[0][0], member);
339 339 if(!retval)
340 340 return false;
341   - return std::find(std::begin(_options), std::end(_options), member) != std::end(_options);
  341 + return std::find(std::begin(options), std::end(options), member) != std::end(options);
342 342 };
343 343  
344   - Option* retval = add_option(name, fun, description, defaulted);
345   - retval->typeval = detail::type_name<T>();
346   - retval->typeval += " in {" + detail::join(_options) + "}";
  344 + Option* opt = add_option(name, fun, description, defaulted);
  345 + opt->typeval_ = detail::type_name<T>();
  346 + opt->typeval_ += " in {" + detail::join(options) + "}";
347 347 if(defaulted) {
348 348 std::stringstream out;
349 349 out << member;
350   - retval->defaultval = out.str();
  350 + opt->defaultval_ = out.str();
351 351 }
352   - return retval;
  352 + return opt;
353 353 }
354 354  
355 355 /// Add set of options, string only, ignore case
356 356 Option* add_set_ignore_case(
357 357 std::string name,
358 358 std::string &member, ///< The selected member of the set
359   - std::set<std::string> _options, ///< The set of posibilities
  359 + std::set<std::string> options, ///< The set of posibilities
360 360 std::string description="",
361 361 bool defaulted=false
362 362 ) {
363 363  
364   - CLI::callback_t fun = [&member, _options](CLI::results_t res){
  364 + CLI::callback_t fun = [&member, options](CLI::results_t res){
365 365 if(res.size()!=1) {
366 366 return false;
367 367 }
... ... @@ -369,9 +369,9 @@ public:
369 369 return false;
370 370 }
371 371 member = detail::to_lower(res.at(0).at(0));
372   - auto iter = std::find_if(std::begin(_options), std::end(_options),
  372 + auto iter = std::find_if(std::begin(options), std::end(options),
373 373 [&member](std::string val){return detail::to_lower(val) == member;});
374   - if(iter == std::end(_options))
  374 + if(iter == std::end(options))
375 375 return false;
376 376 else {
377 377 member = *iter;
... ... @@ -379,13 +379,13 @@ public:
379 379 }
380 380 };
381 381  
382   - Option* retval = add_option(name, fun, description, defaulted);
383   - retval->typeval = detail::type_name<std::string>();
384   - retval->typeval += " in {" + detail::join(_options) + "}";
  382 + Option* opt = add_option(name, fun, description, defaulted);
  383 + opt->typeval_ = detail::type_name<std::string>();
  384 + opt->typeval_ += " in {" + detail::join(options) + "}";
385 385 if(defaulted) {
386   - retval->defaultval = detail::to_lower(member);
  386 + opt->defaultval_ = detail::to_lower(member);
387 387 }
388   - return retval;
  388 + return opt;
389 389 }
390 390  
391 391  
... ... @@ -535,8 +535,8 @@ public:
535 535 std::string config_to_str() const {
536 536 std::stringstream out;
537 537 for(const Option_p &opt : options_) {
538   - if(opt->lnames.size() > 0 && opt->count() > 0 && opt->get_expected() > 0)
539   - out << opt->lnames[0] << "=" << detail::join(opt->flatten_results()) << std::endl;
  538 + if(opt->lnames_.size() > 0 && opt->count() > 0 && opt->get_expected() > 0)
  539 + out << opt->lnames_[0] << "=" << detail::join(opt->flatten_results()) << std::endl;
540 540 }
541 541 return out.str();
542 542 }
... ... @@ -781,8 +781,8 @@ protected:
781 781  
782 782 // Get envname options if not yet passed
783 783 for(const Option_p& opt : options_) {
784   - if (opt->count() == 0 && opt->_envname != "") {
785   - char *ename = std::getenv(opt->_envname.c_str());
  784 + if (opt->count() == 0 && opt->envname_ != "") {
  785 + char *ename = std::getenv(opt->envname_.c_str());
786 786 if(ename != nullptr) {
787 787 opt->get_new();
788 788 opt->add_result(0, std::string(ename));
... ... @@ -804,11 +804,11 @@ protected:
804 804 && (opt->count() < opt->get_expected() || opt->count() == 0))
805 805 throw RequiredError(opt->get_name());
806 806 // Requires
807   - for (const Option* opt_req : opt->_requires)
  807 + for (const Option* opt_req : opt->requires_)
808 808 if (opt->count() > 0 && opt_req->count() == 0)
809 809 throw RequiresError(opt->get_name(), opt_req->get_name());
810 810 // Excludes
811   - for (const Option* opt_ex : opt->_excludes)
  811 + for (const Option* opt_ex : opt->excludes_)
812 812 if (opt->count() > 0 && opt_ex->count() != 0)
813 813 throw ExcludesError(opt->get_name(), opt_ex->get_name());
814 814 }
... ...
include/CLI/Option.hpp
... ... @@ -32,71 +32,71 @@ protected:
32 32 /// @name Names
33 33 ///@{
34 34  
35   - /// A list of the short names (-a) without the leading dashes
36   - std::vector<std::string> snames;
  35 + /// A list of the short names (`-a`) without the leading dashes
  36 + std::vector<std::string> snames_;
37 37  
38   - /// A list of the long names (--a) without the leading dashes
39   - std::vector<std::string> lnames;
  38 + /// A list of the long names (`--a`) without the leading dashes
  39 + std::vector<std::string> lnames_;
40 40  
41 41 /// A positional name
42   - std::string pname;
  42 + std::string pname_;
43 43  
44 44 /// If given, check the environment for this option
45   - std::string _envname;
  45 + std::string envname_;
46 46  
47 47 ///@}
48 48 /// @name Help
49 49 ///@{
50 50  
51 51 /// The description for help strings
52   - std::string description;
  52 + std::string description_;
53 53  
54 54 /// A human readable default value, usually only set if default is true in creation
55   - std::string defaultval;
  55 + std::string defaultval_;
56 56  
57 57 /// A human readable type value, set when App creates this
58   - std::string typeval;
  58 + std::string typeval_;
59 59  
60 60 /// The group membership
61   - std::string _group {"Options"};
  61 + std::string group_ {"Options"};
62 62  
63 63 /// True if this option has a default
64   - bool _default {false};
  64 + bool default_ {false};
65 65  
66 66 ///@}
67 67 /// @name Configuration
68 68 ///@{
69 69  
70 70 /// True if this is a required option
71   - bool _required {false};
  71 + bool required_ {false};
72 72  
73 73 /// The number of expected values, 0 for flag, -1 for unlimited vector
74   - int _expected {1};
  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 non-vector args to not be able to accept incorrect expected values
  77 + bool allow_vector_ {false};
78 78  
79 79 /// Ignore the case when matching (option, not value)
80   - bool case_insensitive {false};
  80 + bool ignore_case_ {false};
81 81  
82 82 /// A list of validators to run on each value parsed
83   - std::vector<std::function<bool(std::string)>> _validators;
  83 + std::vector<std::function<bool(std::string)>> validators_;
84 84  
85 85 /// A list of options that are required with this option
86   - std::set<Option*> _requires;
  86 + std::set<Option*> requires_;
87 87  
88 88 /// A list of options that are excluded with this option
89   - std::set<Option*> _excludes;
  89 + std::set<Option*> excludes_;
90 90  
91 91 ///@}
92 92 /// @name Other
93 93 ///@{
94 94  
95 95 /// Remember the parent app
96   - App* parent;
  96 + App* parent_;
97 97  
98 98 /// Options store a callback to do all the work
99   - callback_t callback;
  99 + callback_t callback_;
100 100  
101 101  
102 102 ///@}
... ... @@ -104,31 +104,31 @@ protected:
104 104 ///@{
105 105  
106 106 /// Results of parsing
107   - results_t results;
  107 + results_t results_;
108 108  
109 109 ///@}
110 110  
111 111 /// Making an option by hand is not defined, it must be made by the App class
112   - Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool _default=true, App* parent = nullptr) :
113   - description(description), callback(callback), _default(_default), parent(parent) {
114   - std::tie(snames, lnames, pname) = detail::get_names(detail::split_names(name));
  112 + Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) :
  113 + description_(description), callback_(callback), default_(default_), parent_(parent) {
  114 + std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name));
115 115 }
116 116  
117 117 public:
118 118  
119   - // This class is "true" if optio passed.
  119 + /// This class is true if option is passed.
120 120 operator bool() const {
121   - return results.size() > 0;
  121 + return results_.size() > 0;
122 122 }
123 123  
124 124 /// Clear the parsed results (mostly for testing)
125 125 void clear() {
126   - results.clear();
  126 + results_.clear();
127 127 }
128 128  
129 129 /// Set the option as required
130 130 Option* required(bool value = true) {
131   - _required = value;
  131 + required_ = value;
132 132 return this;
133 133 }
134 134  
... ... @@ -139,70 +139,70 @@ public:
139 139  
140 140 /// True if this is a required option
141 141 bool get_required() const {
142   - return _required;
  142 + return required_;
143 143 }
144 144  
145 145 /// Set the number of expected arguments (Flags bypass this)
146 146 Option* expected(int value) {
147 147 if(value == 0)
148 148 throw IncorrectConstruction("Cannot set 0 expected, use a flag instead");
149   - if(!allow_vector && value != 1)
  149 + if(!allow_vector_ && value != 1)
150 150 throw IncorrectConstruction("You can only change the Expected arguments for vectors");
151   - _expected = value;
  151 + expected_ = value;
152 152 return this;
153 153 }
154 154  
155 155 /// The number of arguments the option expects
156 156 int get_expected() const {
157   - return _expected;
  157 + return expected_;
158 158 }
159 159  
160 160 /// True if this has a default value
161 161 int get_default() const {
162   - return _default;
  162 + return default_;
163 163 }
164 164  
165 165 /// True if the argument can be given directly
166 166 bool get_positional() const {
167   - return pname.length() > 0;
  167 + return pname_.length() > 0;
168 168 }
169 169  
170 170 /// True if option has at least one non-positional name
171 171 bool nonpositional() const {
172   - return (snames.size() + lnames.size()) > 0;
  172 + return (snames_.size() + lnames_.size()) > 0;
173 173 }
174 174  
175 175 /// True if option has description
176 176 bool has_description() const {
177   - return description.length() > 0;
  177 + return description_.length() > 0;
178 178 }
179 179  
180 180 /// Adds a validator
181 181 Option* check(std::function<bool(std::string)> validator) {
182 182  
183   - _validators.push_back(validator);
  183 + validators_.push_back(validator);
184 184 return this;
185 185 }
186 186  
187 187 /// Changes the group membership
188 188 Option* group(std::string name) {
189   - _group = name;
  189 + group_ = name;
190 190 return this;
191 191 }
192 192  
193 193 /// Get the group of this option
194 194 const std::string& get_group() const {
195   - return _group;
  195 + return group_;
196 196 }
197 197  
198 198 /// Get the description
199 199 const std::string& get_description() const {
200   - return description;
  200 + return description_;
201 201 }
202 202  
203 203 /// Sets required options
204 204 Option* requires(Option* opt) {
205   - auto tup = _requires.insert(opt);
  205 + auto tup = requires_.insert(opt);
206 206 if(!tup.second)
207 207 throw OptionAlreadyAdded(get_name() + " requires " + opt->get_name());
208 208 return this;
... ... @@ -217,7 +217,7 @@ public:
217 217  
218 218 /// Sets excluded options
219 219 Option* excludes(Option* opt) {
220   - auto tup = _excludes.insert(opt);
  220 + auto tup = excludes_.insert(opt);
221 221 if(!tup.second)
222 222 throw OptionAlreadyAdded(get_name() + " excludes " + opt->get_name());
223 223 return this;
... ... @@ -232,13 +232,13 @@ public:
232 232  
233 233 /// Sets environment variable to read if no option given
234 234 Option* envname(std::string name) {
235   - _envname = name;
  235 + envname_ = name;
236 236 return this;
237 237 }
238 238  
239 239 /// The name and any extras needed for positionals
240 240 std::string help_positional() const {
241   - std::string out = pname;
  241 + std::string out = pname_;
242 242 if(get_expected()<1)
243 243 out = out + "x" + std::to_string(get_expected());
244 244 else if(get_expected()==-1)
... ... @@ -249,17 +249,17 @@ public:
249 249  
250 250 // Just the pname
251 251 std::string get_pname() const {
252   - return pname;
  252 + return pname_;
253 253 }
254 254  
255 255  
256 256 /// Process the callback
257 257 void run_callback() const {
258   - if(!callback(results))
  258 + if(!callback_(results_))
259 259 throw ConversionError(get_name() + "=" + detail::join(flatten_results()));
260   - if(_validators.size()>0) {
  260 + if(validators_.size()>0) {
261 261 for(const std::string & result : flatten_results())
262   - for(const std::function<bool(std::string)> &vali : _validators)
  262 + for(const std::function<bool(std::string)> &vali : validators_)
263 263 if(!vali(result))
264 264 throw ValidationError(get_name() + "=" + result);
265 265 }
... ... @@ -267,17 +267,17 @@ public:
267 267  
268 268 /// If options share any of the same names, they are equal (not counting positional)
269 269 bool operator== (const Option& other) const {
270   - for(const std::string &sname : snames)
  270 + for(const std::string &sname : snames_)
271 271 if(other.check_sname(sname))
272 272 return true;
273   - for(const std::string &lname : lnames)
  273 + for(const std::string &lname : lnames_)
274 274 if(other.check_lname(lname))
275 275 return true;
276 276 // We need to do the inverse, just in case we are ignore_case
277   - for(const std::string &sname : other.snames)
  277 + for(const std::string &sname : other.snames_)
278 278 if(check_sname(sname))
279 279 return true;
280   - for(const std::string &lname : other.lnames)
  280 + for(const std::string &lname : other.lnames_)
281 281 if(check_lname(lname))
282 282 return true;
283 283 return false;
... ... @@ -286,11 +286,11 @@ public:
286 286 /// Gets a , sep list of names. Does not include the positional name if opt_only=true.
287 287 std::string get_name(bool opt_only=false) const {
288 288 std::vector<std::string> name_list;
289   - if(!opt_only && pname.length() > 0)
290   - name_list.push_back(pname);
291   - for(const std::string& sname : snames)
  289 + if(!opt_only && pname_.length() > 0)
  290 + name_list.push_back(pname_);
  291 + for(const std::string& sname : snames_)
292 292 name_list.push_back("-"+sname);
293   - for(const std::string& lname : lnames)
  293 + for(const std::string& lname : lnames_)
294 294 name_list.push_back("--"+lname);
295 295 return detail::join(name_list);
296 296 }
... ... @@ -301,8 +301,8 @@ public:
301 301 /// You are never expected to add an argument to the template here.
302 302 template<typename T=App>
303 303 Option* ignore_case(bool value = true) {
304   - case_insensitive = value;
305   - for(const Option_p& opt : dynamic_cast<T*>(parent)->options_)
  304 + ignore_case_ = value;
  305 + for(const Option_p& opt : dynamic_cast<T*>(parent_)->options_)
306 306 if(opt.get() != this && *opt == *this)
307 307 throw OptionAlreadyAdded(opt->get_name());
308 308 return this;
... ... @@ -316,8 +316,8 @@ public:
316 316 else if (name.length()>1 && name.substr(0,1) == "-")
317 317 return check_sname(name.substr(1));
318 318 else {
319   - std::string local_pname = pname;
320   - if(case_insensitive) {
  319 + std::string local_pname = pname_;
  320 + if(ignore_case_) {
321 321 local_pname = detail::to_lower(local_pname);
322 322 name = detail::to_lower(name);
323 323 }
... ... @@ -327,42 +327,42 @@ public:
327 327  
328 328 /// Requires "-" to be removed from string
329 329 bool check_sname(std::string name) const {
330   - if(case_insensitive) {
  330 + if(ignore_case_) {
331 331 name = detail::to_lower(name);
332   - return std::find_if(std::begin(snames), std::end(snames),
  332 + return std::find_if(std::begin(snames_), std::end(snames_),
333 333 [&name](std::string local_sname){return detail::to_lower(local_sname) == name;})
334   - != std::end(snames);
  334 + != std::end(snames_);
335 335 } else
336   - return std::find(std::begin(snames), std::end(snames), name) != std::end(snames);
  336 + return std::find(std::begin(snames_), std::end(snames_), name) != std::end(snames_);
337 337 }
338 338  
339 339 /// Requires "--" to be removed from string
340 340 bool check_lname(std::string name) const {
341   - if(case_insensitive) {
  341 + if(ignore_case_) {
342 342 name = detail::to_lower(name);
343   - return std::find_if(std::begin(lnames), std::end(lnames),
  343 + return std::find_if(std::begin(lnames_), std::end(lnames_),
344 344 [&name](std::string local_sname){return detail::to_lower(local_sname) == name;})
345   - != std::end(lnames);
  345 + != std::end(lnames_);
346 346 } else
347   - return std::find(std::begin(lnames), std::end(lnames), name) != std::end(lnames);
  347 + return std::find(std::begin(lnames_), std::end(lnames_), name) != std::end(lnames_);
348 348 }
349 349  
350 350  
351 351 /// Puts a result at position r
352 352 void add_result(int r, std::string s) {
353   - results.at(r).push_back(s);
  353 + results_.at(r).push_back(s);
354 354 }
355 355  
356 356 /// Starts a new results vector (used for r in add_result)
357 357 int get_new() {
358   - results.emplace_back();
359   - return results.size() - 1;
  358 + results_.emplace_back();
  359 + return results_.size() - 1;
360 360 }
361 361  
362 362 /// Count the total number of times an option was passed
363 363 int count() const {
364 364 int out = 0;
365   - for(const std::vector<std::string>& vec : results)
  365 + for(const std::vector<std::string>& vec : results_)
366 366 out += vec.size();
367 367 return out;
368 368 }
... ... @@ -386,25 +386,25 @@ public:
386 386 std::stringstream out;
387 387  
388 388 if(get_expected() != 0) {
389   - if(typeval != "")
390   - out << " " << typeval;
391   - if(defaultval != "")
392   - out << "=" << defaultval;
  389 + if(typeval_ != "")
  390 + out << " " << typeval_;
  391 + if(defaultval_ != "")
  392 + out << "=" << defaultval_;
393 393 if(get_expected() > 1)
394 394 out << " x " << get_expected();
395 395 if(get_expected() == -1)
396 396 out << " ...";
397 397 }
398   - if(_envname != "")
399   - out << " (env:" << _envname << ")";
400   - if(_requires.size() > 0) {
  398 + if(envname_ != "")
  399 + out << " (env:" << envname_ << ")";
  400 + if(requires_.size() > 0) {
401 401 out << " Requires:";
402   - for(const Option* opt : _requires)
  402 + for(const Option* opt : requires_)
403 403 out << " " << opt->get_name();
404 404 }
405   - if(_excludes.size() > 0) {
  405 + if(excludes_.size() > 0) {
406 406 out << " Excludes:";
407   - for(const Option* opt : _excludes)
  407 + for(const Option* opt : excludes_)
408 408 out << " " << opt->get_name();
409 409 }
410 410 return out.str();
... ... @@ -414,7 +414,7 @@ public:
414 414 /// Produce a flattened vector of results, vs. a vector of vectors.
415 415 std::vector<std::string> flatten_results() const {
416 416 std::vector<std::string> output;
417   - for(const std::vector<std::string> result : results)
  417 + for(const std::vector<std::string> result : results_)
418 418 output.insert(std::end(output), std::begin(result), std::end(result));
419 419 return output;
420 420 }
... ...