CLI11  1.9.0
Option.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 // Distributed under the 3-Clause BSD License. See accompanying
4 // file LICENSE or https://github.com/CLIUtils/CLI11 for details.
5 
6 #include <algorithm>
7 #include <functional>
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <tuple>
12 #include <utility>
13 #include <vector>
14 
15 #include "CLI/Error.hpp"
16 #include "CLI/Macros.hpp"
17 #include "CLI/Split.hpp"
18 #include "CLI/StringTools.hpp"
19 #include "CLI/Validators.hpp"
20 
21 namespace CLI {
22 
23 using results_t = std::vector<std::string>;
25 using callback_t = std::function<bool(const results_t &)>;
26 
27 class Option;
28 class App;
29 
30 using Option_p = std::unique_ptr<Option>;
32 enum class MultiOptionPolicy : char {
33  Throw,
34  TakeLast,
35  TakeFirst,
36  Join,
37  TakeAll
38 };
39 
42 template <typename CRTP> class OptionBase {
43  friend App;
44 
45  protected:
47  std::string group_ = std::string("Options");
48 
50  bool required_{false};
51 
53  bool ignore_case_{false};
54 
56  bool ignore_underscore_{false};
57 
59  bool configurable_{true};
60 
63 
65  char delimiter_{'\0'};
66 
69 
72 
74  template <typename T> void copy_to(T *other) const {
75  other->group(group_);
76  other->required(required_);
77  other->ignore_case(ignore_case_);
78  other->ignore_underscore(ignore_underscore_);
79  other->configurable(configurable_);
80  other->disable_flag_override(disable_flag_override_);
81  other->delimiter(delimiter_);
82  other->always_capture_default(always_capture_default_);
83  other->multi_option_policy(multi_option_policy_);
84  }
85 
86  public:
87  // setters
88 
90  CRTP *group(const std::string &name) {
91  group_ = name;
92  return static_cast<CRTP *>(this);
93  }
94 
96  CRTP *required(bool value = true) {
97  required_ = value;
98  return static_cast<CRTP *>(this);
99  }
100 
102  CRTP *mandatory(bool value = true) { return required(value); }
103 
104  CRTP *always_capture_default(bool value = true) {
105  always_capture_default_ = value;
106  return static_cast<CRTP *>(this);
107  }
108 
109  // Getters
110 
112  const std::string &get_group() const { return group_; }
113 
115  bool get_required() const { return required_; }
116 
118  bool get_ignore_case() const { return ignore_case_; }
119 
121  bool get_ignore_underscore() const { return ignore_underscore_; }
122 
124  bool get_configurable() const { return configurable_; }
125 
128 
130  char get_delimiter() const { return delimiter_; }
131 
134 
137 
138  // Shortcuts for multi option policy
139 
141  CRTP *take_last() {
142  auto self = static_cast<CRTP *>(this);
144  return self;
145  }
146 
148  CRTP *take_first() {
149  auto self = static_cast<CRTP *>(this);
151  return self;
152  }
153 
155  CRTP *take_all() {
156  auto self = static_cast<CRTP *>(this);
158  return self;
159  }
160 
162  CRTP *join() {
163  auto self = static_cast<CRTP *>(this);
165  return self;
166  }
167 
169  CRTP *join(char delim) {
170  auto self = static_cast<CRTP *>(this);
171  self->delimiter_ = delim;
172  self->multi_option_policy(MultiOptionPolicy::Join);
173  return self;
174  }
175 
177  CRTP *configurable(bool value = true) {
178  configurable_ = value;
179  return static_cast<CRTP *>(this);
180  }
181 
183  CRTP *delimiter(char value = '\0') {
184  delimiter_ = value;
185  return static_cast<CRTP *>(this);
186  }
187 };
188 
191 class OptionDefaults : public OptionBase<OptionDefaults> {
192  public:
193  OptionDefaults() = default;
194 
195  // Methods here need a different implementation if they are Option vs. OptionDefault
196 
199  multi_option_policy_ = value;
200  return this;
201  }
202 
204  OptionDefaults *ignore_case(bool value = true) {
205  ignore_case_ = value;
206  return this;
207  }
208 
210  OptionDefaults *ignore_underscore(bool value = true) {
211  ignore_underscore_ = value;
212  return this;
213  }
214 
216  OptionDefaults *disable_flag_override(bool value = true) {
217  disable_flag_override_ = value;
218  return this;
219  }
220 
222  OptionDefaults *delimiter(char value = '\0') {
223  delimiter_ = value;
224  return this;
225  }
226 };
227 
228 class Option : public OptionBase<Option> {
229  friend App;
230 
231  protected:
234 
236  std::vector<std::string> snames_{};
237 
239  std::vector<std::string> lnames_{};
240 
243  std::vector<std::pair<std::string, std::string>> default_flag_values_{};
244 
246  std::vector<std::string> fnames_{};
247 
249  std::string pname_{};
250 
252  std::string envname_{};
253 
257 
259  std::string description_{};
260 
262  std::string default_str_{};
263 
267  std::function<std::string()> type_name_{[]() { return std::string(); }};
268 
270  std::function<std::string()> default_function_{};
271 
275 
281 
286 
288  std::vector<Validator> validators_{};
289 
291  std::set<Option *> needs_{};
292 
294  std::set<Option *> excludes_{};
295 
299 
301  App *parent_{nullptr};
302 
305 
309 
315  enum class option_state {
316  parsing = 0,
317  validated = 2,
318  reduced = 4,
319  callback_run = 6,
320  };
324  bool allow_extra_args_{false};
326  bool flag_like_{false};
330 
332  Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
333  : description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
334  std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name));
335  }
336 
337  public:
340 
341  Option(const Option &) = delete;
342  Option &operator=(const Option &) = delete;
343 
345  std::size_t count() const { return results_.size(); }
346 
348  bool empty() const { return results_.empty(); }
349 
351  explicit operator bool() const { return !empty(); }
352 
354  void clear() {
355  results_.clear();
357  }
358 
362 
364  Option *expected(int value) {
365  if(value < 0) {
366  expected_min_ = -value;
369  }
370  allow_extra_args_ = true;
371  flag_like_ = false;
372  } else if(value == detail::expected_max_vector_size) {
373  expected_min_ = 1;
375  allow_extra_args_ = true;
376  flag_like_ = false;
377  } else {
378  expected_min_ = value;
379  expected_max_ = value;
380  flag_like_ = (expected_min_ == 0);
381  }
382  return this;
383  }
384 
386  Option *expected(int value_min, int value_max) {
387  if(value_min < 0) {
388  value_min = -value_min;
389  }
390 
391  if(value_max < 0) {
393  }
394  if(value_max < value_min) {
395  expected_min_ = value_max;
396  expected_max_ = value_min;
397  } else {
398  expected_max_ = value_max;
399  expected_min_ = value_min;
400  }
401 
402  return this;
403  }
406  Option *allow_extra_args(bool value = true) {
407  allow_extra_args_ = value;
408  return this;
409  }
411  bool get_allow_extra_args() const { return allow_extra_args_; }
412 
415  Option *run_callback_for_default(bool value = true) {
417  return this;
418  }
421 
423  Option *check(Validator validator, const std::string &validator_name = "") {
424  validator.non_modifying();
425  validators_.push_back(std::move(validator));
426  if(!validator_name.empty())
427  validators_.back().name(validator_name);
428  return this;
429  }
430 
432  Option *check(std::function<std::string(const std::string &)> Validator,
433  std::string Validator_description = "",
434  std::string Validator_name = "") {
435  validators_.emplace_back(Validator, std::move(Validator_description), std::move(Validator_name));
436  validators_.back().non_modifying();
437  return this;
438  }
439 
441  Option *transform(Validator Validator, const std::string &Validator_name = "") {
442  validators_.insert(validators_.begin(), std::move(Validator));
443  if(!Validator_name.empty())
444  validators_.front().name(Validator_name);
445  return this;
446  }
447 
449  Option *transform(const std::function<std::string(std::string)> &func,
450  std::string transform_description = "",
451  std::string transform_name = "") {
452  validators_.insert(validators_.begin(),
453  Validator(
454  [func](std::string &val) {
455  val = func(val);
456  return std::string{};
457  },
458  std::move(transform_description),
459  std::move(transform_name)));
460 
461  return this;
462  }
463 
465  Option *each(const std::function<void(std::string)> &func) {
466  validators_.emplace_back(
467  [func](std::string &inout) {
468  func(inout);
469  return std::string{};
470  },
471  std::string{});
472  return this;
473  }
475  Validator *get_validator(const std::string &Validator_name = "") {
476  for(auto &Validator : validators_) {
477  if(Validator_name == Validator.get_name()) {
478  return &Validator;
479  }
480  }
481  if((Validator_name.empty()) && (!validators_.empty())) {
482  return &(validators_.front());
483  }
484  throw OptionNotFound(std::string{"Validator "} + Validator_name + " Not Found");
485  }
486 
488  Validator *get_validator(int index) {
489  // This is an signed int so that it is not equivalent to a pointer.
490  if(index >= 0 && index < static_cast<int>(validators_.size())) {
491  return &(validators_[static_cast<decltype(validators_)::size_type>(index)]);
492  }
493  throw OptionNotFound("Validator index is not valid");
494  }
495 
497  Option *needs(Option *opt) {
498  if(opt != this) {
499  needs_.insert(opt);
500  }
501  return this;
502  }
503 
505  template <typename T = App> Option *needs(std::string opt_name) {
506  auto opt = dynamic_cast<T *>(parent_)->get_option_no_throw(opt_name);
507  if(opt == nullptr) {
508  throw IncorrectConstruction::MissingOption(opt_name);
509  }
510  return needs(opt);
511  }
512 
514  template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
515  needs(opt);
516  return needs(opt1, args...);
517  }
518 
520  bool remove_needs(Option *opt) {
521  auto iterator = std::find(std::begin(needs_), std::end(needs_), opt);
522 
523  if(iterator == std::end(needs_)) {
524  return false;
525  }
526  needs_.erase(iterator);
527  return true;
528  }
529 
532  if(opt == this) {
533  throw(IncorrectConstruction("and option cannot exclude itself"));
534  }
535  excludes_.insert(opt);
536 
537  // Help text should be symmetric - excluding a should exclude b
538  opt->excludes_.insert(this);
539 
540  // Ignoring the insert return value, excluding twice is now allowed.
541  // (Mostly to allow both directions to be excluded by user, even though the library does it for you.)
542 
543  return this;
544  }
545 
547  template <typename T = App> Option *excludes(std::string opt_name) {
548  auto opt = dynamic_cast<T *>(parent_)->get_option_no_throw(opt_name);
549  if(opt == nullptr) {
550  throw IncorrectConstruction::MissingOption(opt_name);
551  }
552  return excludes(opt);
553  }
554 
556  template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
557  excludes(opt);
558  return excludes(opt1, args...);
559  }
560 
562  bool remove_excludes(Option *opt) {
563  auto iterator = std::find(std::begin(excludes_), std::end(excludes_), opt);
564 
565  if(iterator == std::end(excludes_)) {
566  return false;
567  }
568  excludes_.erase(iterator);
569  return true;
570  }
571 
573  Option *envname(std::string name) {
574  envname_ = std::move(name);
575  return this;
576  }
577 
582  template <typename T = App> Option *ignore_case(bool value = true) {
583  if(!ignore_case_ && value) {
584  ignore_case_ = value;
585  auto *parent = dynamic_cast<T *>(parent_);
586  for(const Option_p &opt : parent->options_) {
587  if(opt.get() == this) {
588  continue;
589  }
590  auto &omatch = opt->matching_name(*this);
591  if(!omatch.empty()) {
592  ignore_case_ = false;
593  throw OptionAlreadyAdded("adding ignore case caused a name conflict with " + omatch);
594  }
595  }
596  } else {
597  ignore_case_ = value;
598  }
599  return this;
600  }
601 
606  template <typename T = App> Option *ignore_underscore(bool value = true) {
607 
608  if(!ignore_underscore_ && value) {
609  ignore_underscore_ = value;
610  auto *parent = dynamic_cast<T *>(parent_);
611  for(const Option_p &opt : parent->options_) {
612  if(opt.get() == this) {
613  continue;
614  }
615  auto &omatch = opt->matching_name(*this);
616  if(!omatch.empty()) {
617  ignore_underscore_ = false;
618  throw OptionAlreadyAdded("adding ignore underscore caused a name conflict with " + omatch);
619  }
620  }
621  } else {
622  ignore_underscore_ = value;
623  }
624  return this;
625  }
626 
629  if(value != multi_option_policy_) {
631  expected_min_ > 1) { // this bizarre condition is to maintain backwards compatibility
632  // with the previous behavior of expected_ with vectors
634  }
635  multi_option_policy_ = value;
637  }
638  return this;
639  }
640 
642  Option *disable_flag_override(bool value = true) {
643  disable_flag_override_ = value;
644  return this;
645  }
649 
651  int get_type_size() const { return type_size_min_; }
652 
654  int get_type_size_min() const { return type_size_min_; }
656  int get_type_size_max() const { return type_size_max_; }
657 
659  std::string get_envname() const { return envname_; }
660 
662  std::set<Option *> get_needs() const { return needs_; }
663 
665  std::set<Option *> get_excludes() const { return excludes_; }
666 
668  std::string get_default_str() const { return default_str_; }
669 
671  callback_t get_callback() const { return callback_; }
672 
674  const std::vector<std::string> &get_lnames() const { return lnames_; }
675 
677  const std::vector<std::string> &get_snames() const { return snames_; }
678 
680  const std::vector<std::string> &get_fnames() const { return fnames_; }
681 
683  int get_expected() const { return expected_min_; }
684 
686  int get_expected_min() const { return expected_min_; }
688  int get_expected_max() const { return expected_max_; }
689 
692 
695  int t = type_size_max_;
697  }
699  int get_items_expected() const { return get_items_expected_min(); }
700 
702  bool get_positional() const { return pname_.length() > 0; }
703 
705  bool nonpositional() const { return (snames_.size() + lnames_.size()) > 0; }
706 
708  bool has_description() const { return description_.length() > 0; }
709 
711  const std::string &get_description() const { return description_; }
712 
714  Option *description(std::string option_description) {
715  description_ = std::move(option_description);
716  return this;
717  }
718 
722 
727  std::string get_name(bool positional = false, //<[input] Show the positional name
728  bool all_options = false //<[input] Show every option
729  ) const {
730  if(get_group().empty())
731  return {}; // Hidden
732 
733  if(all_options) {
734 
735  std::vector<std::string> name_list;
736 
738  if((positional && (!pname_.empty())) || (snames_.empty() && lnames_.empty())) {
739  name_list.push_back(pname_);
740  }
741  if((get_items_expected() == 0) && (!fnames_.empty())) {
742  for(const std::string &sname : snames_) {
743  name_list.push_back("-" + sname);
744  if(check_fname(sname)) {
745  name_list.back() += "{" + get_flag_value(sname, "") + "}";
746  }
747  }
748 
749  for(const std::string &lname : lnames_) {
750  name_list.push_back("--" + lname);
751  if(check_fname(lname)) {
752  name_list.back() += "{" + get_flag_value(lname, "") + "}";
753  }
754  }
755  } else {
756  for(const std::string &sname : snames_)
757  name_list.push_back("-" + sname);
758 
759  for(const std::string &lname : lnames_)
760  name_list.push_back("--" + lname);
761  }
762 
763  return detail::join(name_list);
764  }
765 
766  // This returns the positional name no matter what
767  if(positional)
768  return pname_;
769 
770  // Prefer long name
771  if(!lnames_.empty())
772  return std::string(2, '-') + lnames_[0];
773 
774  // Or short name if no long name
775  if(!snames_.empty())
776  return std::string(1, '-') + snames_[0];
777 
778  // If positional is the only name, it's okay to use that
779  return pname_;
780  }
781 
785 
787  void run_callback() {
788 
790  _validate_results(results_);
792  }
793 
795  _reduce_results(proc_results_, results_);
797  }
800  if(!(callback_)) {
801  return;
802  }
803  const results_t &send_results = proc_results_.empty() ? results_ : proc_results_;
804  bool local_result = callback_(send_results);
805 
806  if(!local_result)
808  }
809  }
810 
812  const std::string &matching_name(const Option &other) const {
813  static const std::string estring;
814  for(const std::string &sname : snames_)
815  if(other.check_sname(sname))
816  return sname;
817  for(const std::string &lname : lnames_)
818  if(other.check_lname(lname))
819  return lname;
820 
821  if(ignore_case_ ||
822  ignore_underscore_) { // We need to do the inverse, in case we are ignore_case or ignore underscore
823  for(const std::string &sname : other.snames_)
824  if(check_sname(sname))
825  return sname;
826  for(const std::string &lname : other.lnames_)
827  if(check_lname(lname))
828  return lname;
829  }
830  return estring;
831  }
833  bool operator==(const Option &other) const { return !matching_name(other).empty(); }
834 
836  bool check_name(std::string name) const {
837 
838  if(name.length() > 2 && name[0] == '-' && name[1] == '-')
839  return check_lname(name.substr(2));
840  if(name.length() > 1 && name.front() == '-')
841  return check_sname(name.substr(1));
842 
843  std::string local_pname = pname_;
844  if(ignore_underscore_) {
845  local_pname = detail::remove_underscore(local_pname);
846  name = detail::remove_underscore(name);
847  }
848  if(ignore_case_) {
849  local_pname = detail::to_lower(local_pname);
850  name = detail::to_lower(name);
851  }
852  return name == local_pname;
853  }
854 
856  bool check_sname(std::string name) const {
857  return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
858  }
859 
861  bool check_lname(std::string name) const {
862  return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
863  }
864 
866  bool check_fname(std::string name) const {
867  if(fnames_.empty()) {
868  return false;
869  }
870  return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
871  }
872 
875  std::string get_flag_value(const std::string &name, std::string input_value) const {
876  static const std::string trueString{"true"};
877  static const std::string falseString{"false"};
878  static const std::string emptyString{"{}"};
879  // check for disable flag override_
881  if(!((input_value.empty()) || (input_value == emptyString))) {
882  auto default_ind = detail::find_member(name, fnames_, ignore_case_, ignore_underscore_);
883  if(default_ind >= 0) {
884  // We can static cast this to std::size_t because it is more than 0 in this block
885  if(default_flag_values_[static_cast<std::size_t>(default_ind)].second != input_value) {
886  throw(ArgumentMismatch::FlagOverride(name));
887  }
888  } else {
889  if(input_value != trueString) {
890  throw(ArgumentMismatch::FlagOverride(name));
891  }
892  }
893  }
894  }
896  if((input_value.empty()) || (input_value == emptyString)) {
897  if(flag_like_) {
898  return (ind < 0) ? trueString : default_flag_values_[static_cast<std::size_t>(ind)].second;
899  } else {
900  return (ind < 0) ? default_str_ : default_flag_values_[static_cast<std::size_t>(ind)].second;
901  }
902  }
903  if(ind < 0) {
904  return input_value;
905  }
906  if(default_flag_values_[static_cast<std::size_t>(ind)].second == falseString) {
907  try {
908  auto val = detail::to_flag_value(input_value);
909  return (val == 1) ? falseString : (val == (-1) ? trueString : std::to_string(-val));
910  } catch(const std::invalid_argument &) {
911  return input_value;
912  }
913  } else {
914  return input_value;
915  }
916  }
917 
919  Option *add_result(std::string s) {
920  _add_result(std::move(s), results_);
922  return this;
923  }
924 
926  Option *add_result(std::string s, int &results_added) {
927  results_added = _add_result(std::move(s), results_);
929  return this;
930  }
931 
933  Option *add_result(std::vector<std::string> s) {
934  for(auto &str : s) {
935  _add_result(std::move(str), results_);
936  }
938  return this;
939  }
940 
942  results_t results() const { return results_; }
943 
946  results_t res = proc_results_.empty() ? results_ : proc_results_;
949  res = results_;
950  _validate_results(res);
951  }
952  if(!res.empty()) {
953  results_t extra;
954  _reduce_results(extra, res);
955  if(!extra.empty()) {
956  res = std::move(extra);
957  }
958  }
959  }
960  return res;
961  }
962 
964  template <typename T, enable_if_t<!std::is_const<T>::value, detail::enabler> = detail::dummy>
965  void results(T &output) const {
966  bool retval;
967  if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
968  const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
969  retval = detail::lexical_conversion<T, T>(res, output);
970  } else {
971  results_t res;
972  if(results_.empty()) {
973  if(!default_str_.empty()) {
974  //_add_results takes an rvalue only
975  _add_result(std::string(default_str_), res);
976  _validate_results(res);
977  results_t extra;
978  _reduce_results(extra, res);
979  if(!extra.empty()) {
980  res = std::move(extra);
981  }
982  } else {
983  res.emplace_back();
984  }
985  } else {
986  res = reduced_results();
987  }
988  retval = detail::lexical_conversion<T, T>(res, output);
989  }
990  if(!retval) {
992  }
993  }
994 
996  template <typename T> T as() const {
997  T output;
998  results(output);
999  return output;
1000  }
1001 
1004 
1008 
1010  Option *type_name_fn(std::function<std::string()> typefun) {
1011  type_name_ = std::move(typefun);
1012  return this;
1013  }
1014 
1016  Option *type_name(std::string typeval) {
1017  type_name_fn([typeval]() { return typeval; });
1018  return this;
1019  }
1020 
1022  Option *type_size(int option_type_size) {
1023  if(option_type_size < 0) {
1024  // this section is included for backwards compatibility
1025  type_size_max_ = -option_type_size;
1026  type_size_min_ = -option_type_size;
1028  } else {
1029  type_size_max_ = option_type_size;
1031  type_size_min_ = option_type_size;
1032  }
1033  if(type_size_max_ == 0)
1034  required_ = false;
1035  }
1036  return this;
1037  }
1039  Option *type_size(int option_type_size_min, int option_type_size_max) {
1040  if(option_type_size_min < 0 || option_type_size_max < 0) {
1041  // this section is included for backwards compatibility
1043  option_type_size_min = (std::abs)(option_type_size_min);
1044  option_type_size_max = (std::abs)(option_type_size_max);
1045  }
1046 
1047  if(option_type_size_min > option_type_size_max) {
1048  type_size_max_ = option_type_size_min;
1049  type_size_min_ = option_type_size_max;
1050  } else {
1051  type_size_min_ = option_type_size_min;
1052  type_size_max_ = option_type_size_max;
1053  }
1054  if(type_size_max_ == 0) {
1055  required_ = false;
1056  }
1057  return this;
1058  }
1059 
1061  Option *default_function(const std::function<std::string()> &func) {
1062  default_function_ = func;
1063  return this;
1064  }
1065 
1068  if(default_function_) {
1070  }
1071  return this;
1072  }
1073 
1075  Option *default_str(std::string val) {
1076  default_str_ = std::move(val);
1077  return this;
1078  }
1079 
1082  template <typename X> Option *default_val(const X &val) {
1083  std::string val_str = detail::to_string(val);
1084  auto old_option_state = current_option_state_;
1085  results_t old_results{std::move(results_)};
1086  results_.clear();
1087  try {
1088  add_result(val_str);
1090  run_callback(); // run callback sets the state we need to reset it again
1092  } else {
1093  _validate_results(results_);
1094  current_option_state_ = old_option_state;
1095  }
1096  } catch(const CLI::Error &) {
1097  // this should be done
1098  results_ = std::move(old_results);
1099  current_option_state_ = old_option_state;
1100  throw;
1101  }
1102  results_ = std::move(old_results);
1103  default_str_ = std::move(val_str);
1104  return this;
1105  }
1106 
1108  std::string get_type_name() const {
1109  std::string full_type_name = type_name_();
1110  if(!validators_.empty()) {
1111  for(auto &Validator : validators_) {
1112  std::string vtype = Validator.get_description();
1113  if(!vtype.empty()) {
1114  full_type_name += ":" + vtype;
1115  }
1116  }
1117  }
1118  return full_type_name;
1119  }
1120 
1121  private:
1123  void _validate_results(results_t &res) const {
1124  // Run the Validators (can change the string)
1125  if(!validators_.empty()) {
1126  if(type_size_max_ > 1) { // in this context index refers to the index in the type
1127  int index = 0;
1128  if(get_items_expected_max() < static_cast<int>(res.size()) &&
1130  // create a negative index for the earliest ones
1131  index = get_items_expected_max() - static_cast<int>(res.size());
1132  }
1133 
1134  for(std::string &result : res) {
1135  if(result.empty() && type_size_max_ != type_size_min_ && index >= 0) {
1136  index = 0; // reset index for variable size chunks
1137  continue;
1138  }
1139  auto err_msg = _validate(result, (index >= 0) ? (index % type_size_max_) : index);
1140  if(!err_msg.empty())
1141  throw ValidationError(get_name(), err_msg);
1142  ++index;
1143  }
1144  } else {
1145  int index = 0;
1146  if(expected_max_ < static_cast<int>(res.size()) &&
1148  // create a negative index for the earliest ones
1149  index = expected_max_ - static_cast<int>(res.size());
1150  }
1151  for(std::string &result : res) {
1152  auto err_msg = _validate(result, index);
1153  ++index;
1154  if(!err_msg.empty())
1155  throw ValidationError(get_name(), err_msg);
1156  }
1157  }
1158  }
1159  }
1160 
1164  void _reduce_results(results_t &res, const results_t &original) const {
1165 
1166  // max num items expected or length of vector, always at least 1
1167  // Only valid for a trimming policy
1168 
1169  res.clear();
1170  // Operation depends on the policy setting
1171  switch(multi_option_policy_) {
1173  break;
1175  // Allow multi-option sizes (including 0)
1176  std::size_t trim_size = std::min<std::size_t>(
1177  static_cast<std::size_t>(std::max<int>(get_items_expected_max(), 1)), original.size());
1178  if(original.size() != trim_size) {
1179  res.assign(original.end() - static_cast<results_t::difference_type>(trim_size), original.end());
1180  }
1181  } break;
1183  std::size_t trim_size = std::min<std::size_t>(
1184  static_cast<std::size_t>(std::max<int>(get_items_expected_max(), 1)), original.size());
1185  if(original.size() != trim_size) {
1186  res.assign(original.begin(), original.begin() + static_cast<results_t::difference_type>(trim_size));
1187  }
1188  } break;
1190  if(results_.size() > 1) {
1191  res.push_back(detail::join(original, std::string(1, (delimiter_ == '\0') ? '\n' : delimiter_)));
1192  }
1193  break;
1195  default: {
1196  auto num_min = static_cast<std::size_t>(get_items_expected_min());
1197  auto num_max = static_cast<std::size_t>(get_items_expected_max());
1198  if(num_min == 0) {
1199  num_min = 1;
1200  }
1201  if(num_max == 0) {
1202  num_max = 1;
1203  }
1204  if(original.size() < num_min) {
1205  throw ArgumentMismatch::AtLeast(get_name(), static_cast<int>(num_min), original.size());
1206  }
1207  if(original.size() > num_max) {
1208  throw ArgumentMismatch::AtMost(get_name(), static_cast<int>(num_max), original.size());
1209  }
1210  break;
1211  }
1212  }
1213  }
1214 
1215  // Run a result through the Validators
1216  std::string _validate(std::string &result, int index) const {
1217  std::string err_msg;
1218  if(result.empty() && expected_min_ == 0) {
1219  // an empty with nothing expected is allowed
1220  return err_msg;
1221  }
1222  for(const auto &vali : validators_) {
1223  auto v = vali.get_application_index();
1224  if(v == -1 || v == index) {
1225  try {
1226  err_msg = vali(result);
1227  } catch(const ValidationError &err) {
1228  err_msg = err.what();
1229  }
1230  if(!err_msg.empty())
1231  break;
1232  }
1233  }
1234 
1235  return err_msg;
1236  }
1237 
1239  int _add_result(std::string &&result, std::vector<std::string> &res) const {
1240  int result_count = 0;
1241  if(allow_extra_args_ && !result.empty() && result.front() == '[' &&
1242  result.back() == ']') { // this is now a vector string likely from the default or user entry
1243  result.pop_back();
1244 
1245  for(auto &var : CLI::detail::split(result.substr(1), ',')) {
1246  if(!var.empty()) {
1247  result_count += _add_result(std::move(var), res);
1248  }
1249  }
1250  return result_count;
1251  }
1252  if(delimiter_ == '\0') {
1253  res.push_back(std::move(result));
1254  ++result_count;
1255  } else {
1256  if((result.find_first_of(delimiter_) != std::string::npos)) {
1257  for(const auto &var : CLI::detail::split(result, delimiter_)) {
1258  if(!var.empty()) {
1259  res.push_back(var);
1260  ++result_count;
1261  }
1262  }
1263  } else {
1264  res.push_back(std::move(result));
1265  ++result_count;
1266  }
1267  }
1268  return result_count;
1269  }
1270 }; // namespace CLI
1271 
1272 } // namespace CLI
CLI::OptionBase::get_ignore_case
bool get_ignore_case() const
The status of ignore case.
Definition: Option.hpp:118
CLI::Option::operator==
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition: Option.hpp:833
CLI::Option::transform
Option * transform(Validator Validator, const std::string &Validator_name="")
Adds a transforming Validator with a built in type name.
Definition: Option.hpp:441
CLI::OptionBase::get_always_capture_default
bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition: Option.hpp:133
CLI::OptionBase::group
CRTP * group(const std::string &name)
Changes the group membership.
Definition: Option.hpp:90
CLI::Option::default_str_
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition: Option.hpp:262
CLI::Option::reduced_results
results_t reduced_results() const
Get a copy of the results.
Definition: Option.hpp:945
CLI::Option::check_name
bool check_name(std::string name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
Definition: Option.hpp:836
CLI::Option::ignore_case
Option * ignore_case(bool value=true)
Definition: Option.hpp:582
CLI::Validator::get_description
std::string get_description() const
Generate type description information for the Validator.
Definition: Validators.hpp:125
CLI::Option::get_envname
std::string get_envname() const
The environment variable associated to this value.
Definition: Option.hpp:659
CLI::Option::disable_flag_override
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition: Option.hpp:642
CLI::Option::get_default_str
std::string get_default_str() const
The default value (for help printing)
Definition: Option.hpp:668
CLI::OptionBase::get_required
bool get_required() const
True if this is a required option.
Definition: Option.hpp:115
CLI::OptionBase::get_disable_flag_override
bool get_disable_flag_override() const
The status of configurable.
Definition: Option.hpp:127
CLI::OptionBase::take_all
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition: Option.hpp:155
CLI::detail::enabler
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:22
CLI::OptionBase::delimiter_
char delimiter_
Specify a delimiter character for vector arguments.
Definition: Option.hpp:65
CLI::detail::expected_max_vector_size
constexpr int expected_max_vector_size
Definition: StringTools.hpp:36
CLI::Option::get_needs
std::set< Option * > get_needs() const
The set of options needed.
Definition: Option.hpp:662
CLI::Option::add_result
Option * add_result(std::vector< std::string > s)
Puts a result at the end.
Definition: Option.hpp:933
CLI::Option::default_function
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition: Option.hpp:1061
CLI::OptionBase::join
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition: Option.hpp:169
CLI::Option::pname_
std::string pname_
A positional name.
Definition: Option.hpp:249
CLI::Validator
Some validators that are provided.
Definition: Validators.hpp:61
CLI::OptionDefaults::ignore_underscore
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition: Option.hpp:210
Error.hpp
CLI::Option::get_lnames
const std::vector< std::string > & get_lnames() const
Get the long names.
Definition: Option.hpp:674
CLI::Option::option_state::validated
@ validated
the results have been validated
CLI::Option::default_flag_values_
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition: Option.hpp:243
CLI::detail::split
std::vector< std::string > split(const std::string &s, char delim)
Split a string by a delim.
Definition: StringTools.hpp:39
CLI::Option::expected_min_
int expected_min_
The minimum number of expected values.
Definition: Option.hpp:283
CLI::Option::transform
Option * transform(const std::function< std::string(std::string)> &func, std::string transform_description="", std::string transform_name="")
Adds a Validator-like function that can change result.
Definition: Option.hpp:449
CLI::Option::description
Option * description(std::string option_description)
Set the description.
Definition: Option.hpp:714
CLI::Option::flag_like_
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition: Option.hpp:326
CLI::Option::fnames_
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition: Option.hpp:246
CLI::Option::get_type_size_max
int get_type_size_max() const
The maximum number of arguments the option expects.
Definition: Option.hpp:656
CLI::Option::snames_
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition: Option.hpp:236
CLI::Option::results
results_t results() const
Get a copy of the results.
Definition: Option.hpp:942
CLI::Option::get_type_size_min
int get_type_size_min() const
The minimum number of arguments the option expects.
Definition: Option.hpp:654
CLI::Option::each
Option * each(const std::function< void(std::string)> &func)
Adds a user supplied function to run on each item passed in (communicate though lambda capture)
Definition: Option.hpp:465
CLI::OptionBase::multi_option_policy_
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition: Option.hpp:71
CLI::Option::get_positional
bool get_positional() const
True if the argument can be given directly.
Definition: Option.hpp:702
CLI::OptionBase::get_configurable
bool get_configurable() const
The status of configurable.
Definition: Option.hpp:124
CLI::Option::run_callback_for_default_
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition: Option.hpp:328
CLI::Option::get_items_expected_max
int get_items_expected_max() const
Get the maximum number of items expected to be returned and used for the callback.
Definition: Option.hpp:694
CLI::MultiOptionPolicy::TakeLast
@ TakeLast
take only the last Expected number of arguments
CLI::Option::count
std::size_t count() const
Count the total number of times an option was passed.
Definition: Option.hpp:345
CLI::Option::option_state::reduced
@ reduced
a subset of results has been generated
CLI::Option::results_
results_t results_
complete Results of parsing
Definition: Option.hpp:311
CLI::Option::parent_
App * parent_
Remember the parent app.
Definition: Option.hpp:301
CLI::Option::needs_
std::set< Option * > needs_
A list of options that are required with this option.
Definition: Option.hpp:291
CLI::Option::type_size
Option * type_size(int option_type_size)
Set a custom option size.
Definition: Option.hpp:1022
CLI::Option::get_name
std::string get_name(bool positional=false, bool all_options=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition: Option.hpp:727
CLI::Option::expected
Option * expected(int value)
Set the number of expected arguments.
Definition: Option.hpp:364
CLI::MultiOptionPolicy::Join
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
CLI::Option::has_description
bool has_description() const
True if option has description.
Definition: Option.hpp:708
CLI::Option::get_expected_max
int get_expected_max() const
The max number of times the option expects to be included.
Definition: Option.hpp:688
CLI::ConversionError
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition: Error.hpp:182
CLI::detail::join
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:56
CLI::Option::type_size_min_
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition: Option.hpp:280
CLI::Option::check_fname
bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition: Option.hpp:866
CLI::Option::envname
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition: Option.hpp:573
CLI::OptionBase::take_first
CRTP * take_first()
Set the multi option policy to take last.
Definition: Option.hpp:148
CLI::Validator::get_name
const std::string & get_name() const
Get the name of the Validator.
Definition: Validators.hpp:143
CLI::Option::option_state::parsing
@ parsing
The option is currently collecting parsed results.
CLI::Option::excludes_
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition: Option.hpp:294
CLI::Option::excludes
Option * excludes(Option *opt)
Sets excluded options.
Definition: Option.hpp:531
CLI::MultiOptionPolicy
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition: Option.hpp:32
CLI::OptionBase::always_capture_default_
bool always_capture_default_
Automatically capture default value.
Definition: Option.hpp:68
CLI::Option::callback_
callback_t callback_
Options store a callback to do all the work.
Definition: Option.hpp:304
CLI::OptionDefaults::OptionDefaults
OptionDefaults()=default
CLI::detail::to_string
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:222
CLI::Option::envname_
std::string envname_
If given, check the environment for this option.
Definition: Option.hpp:252
CLI::Option::get_validator
Validator * get_validator(const std::string &Validator_name="")
Get a named Validator.
Definition: Option.hpp:475
CLI::MultiOptionPolicy::TakeAll
@ TakeAll
just get all the passed argument regardless
CLI
Definition: App.hpp:27
CLI::Option::nonpositional
bool nonpositional() const
True if option has at least one non-positional name.
Definition: Option.hpp:705
CLI::Option::needs
Option * needs(Option *opt)
Sets required options.
Definition: Option.hpp:497
CLI::OptionBase::copy_to
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition: Option.hpp:74
CLI::detail::dummy
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:25
CLI::Option::needs
Option * needs(std::string opt_name)
Can find a string if needed.
Definition: Option.hpp:505
CLI::Option::type_name_
std::function< std::string()> type_name_
Definition: Option.hpp:267
CLI::Option::add_result
Option * add_result(std::string s, int &results_added)
Puts a result at the end and get a count of the number of arguments actually added.
Definition: Option.hpp:926
CLI::Option::get_snames
const std::vector< std::string > & get_snames() const
Get the short names.
Definition: Option.hpp:677
CLI::OptionBase::delimiter
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition: Option.hpp:183
CLI::Option::check
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
Definition: Option.hpp:423
CLI::detail::find_member
std::ptrdiff_t find_member(std::string name, const std::vector< std::string > names, bool ignore_case=false, bool ignore_underscore=false)
Check if a string is a member of a list of strings and optionally ignore case or ignore underscores.
Definition: StringTools.hpp:241
CLI::Option::needs
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition: Option.hpp:514
CLI::Option::allow_extra_args_
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition: Option.hpp:324
CLI::Option::remove_excludes
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition: Option.hpp:562
CLI::OptionBase
Definition: Option.hpp:42
CLI::Option::expected_max_
int expected_max_
The maximum number of expected values.
Definition: Option.hpp:285
Validators.hpp
CLI::Error
All errors derive from this one.
Definition: Error.hpp:63
CLI::OptionBase::configurable_
bool configurable_
Allow this option to be given in a configuration file.
Definition: Option.hpp:59
CLI::OptionDefaults::multi_option_policy
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition: Option.hpp:198
CLI::OptionAlreadyAdded
Thrown when an option already exists.
Definition: Error.hpp:128
CLI::Option::default_val
Option * default_val(const X &val)
Definition: Option.hpp:1082
CLI::Option::type_size
Option * type_size(int option_type_size_min, int option_type_size_max)
Set a custom option type size range.
Definition: Option.hpp:1039
CLI::Option::get_flag_value
std::string get_flag_value(const std::string &name, std::string input_value) const
Definition: Option.hpp:875
CLI::OptionBase::join
CRTP * join()
Set the multi option policy to join.
Definition: Option.hpp:162
CLI::Option::type_name_fn
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition: Option.hpp:1010
CLI::detail::get_names
std::tuple< std::vector< std::string >, std::vector< std::string >, std::string > get_names(const std::vector< std::string > &input)
Get a vector of short names, one of long names, and a single name.
Definition: Split.hpp:99
CLI::callback_t
std::function< bool(const results_t &)> callback_t
callback function definition
Definition: Option.hpp:25
CLI::Option
Definition: Option.hpp:228
CLI::detail::to_lower
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:196
CLI::Option::validators_
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition: Option.hpp:288
CLI::Option::type_size_max_
int type_size_max_
Definition: Option.hpp:278
CLI::Option::excludes
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition: Option.hpp:547
CLI::Option::type_name
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition: Option.hpp:1016
CLI::MultiOptionPolicy::TakeFirst
@ TakeFirst
take only the first Expected number of arguments
CLI::Option::multi_option_policy
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition: Option.hpp:628
CLI::MultiOptionPolicy::Throw
@ Throw
Throw an error if any extra arguments were given.
CLI::Option::get_items_expected
int get_items_expected() const
The total min number of expected string values to be used.
Definition: Option.hpp:699
CLI::detail::remove_underscore
std::string remove_underscore(std::string str)
remove underscores from a string
Definition: StringTools.hpp:204
CLI::Option::excludes
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition: Option.hpp:556
CLI::Option::run_callback_for_default
Option * run_callback_for_default(bool value=true)
Definition: Option.hpp:415
CLI::Option::proc_results_
results_t proc_results_
results after reduction
Definition: Option.hpp:313
CLI::Option::expected
Option * expected(int value_min, int value_max)
Set the range of expected arguments.
Definition: Option.hpp:386
CLI::OptionBase::get_multi_option_policy
MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition: Option.hpp:136
CLI::OptionBase::group_
std::string group_
The group membership.
Definition: Option.hpp:47
CLI::Option::get_description
const std::string & get_description() const
Get the description.
Definition: Option.hpp:711
StringTools.hpp
CLI::Option::get_callback_run
bool get_callback_run() const
See if the callback has been run already.
Definition: Option.hpp:1003
CLI::OptionBase::take_last
CRTP * take_last()
Set the multi option policy to take last.
Definition: Option.hpp:141
CLI::Option::results
void results(T &output) const
Get the results as a specified type.
Definition: Option.hpp:965
CLI::OptionDefaults::ignore_case
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition: Option.hpp:204
Macros.hpp
CLI::results_t
std::vector< std::string > results_t
Definition: Option.hpp:23
CLI::Option::Option
Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
Making an option by hand is not defined, it must be made by the App class.
Definition: Option.hpp:332
CLI::App
Creates a command line program, with very few defaults.
Definition: App.hpp:61
CLI::Option::get_type_size
int get_type_size() const
The number of arguments the option expects.
Definition: Option.hpp:651
CLI::Option::matching_name
const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
Definition: Option.hpp:812
CLI::Option::check_sname
bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition: Option.hpp:856
CLI::OptionBase::required_
bool required_
True if this is a required option.
Definition: Option.hpp:50
CLI::Option::capture_default_str
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition: Option.hpp:1067
CLI::Option::as
T as() const
Return the results as the specified type.
Definition: Option.hpp:996
CLI::Option::default_function_
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition: Option.hpp:270
CLI::OptionDefaults
Definition: Option.hpp:191
Split.hpp
CLI::OptionBase::disable_flag_override_
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition: Option.hpp:62
CLI::OptionBase::get_group
const std::string & get_group() const
Get the group of this option.
Definition: Option.hpp:112
CLI::Option::get_fnames
const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition: Option.hpp:680
CLI::OptionBase::configurable
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition: Option.hpp:177
CLI::Option::ignore_underscore
Option * ignore_underscore(bool value=true)
Definition: Option.hpp:606
CLI::Option::option_state
option_state
enumeration for the option state machine
Definition: Option.hpp:315
CLI::Option::default_str
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition: Option.hpp:1075
CLI::Option_p
std::unique_ptr< Option > Option_p
Definition: Option.hpp:30
CLI::Option::get_type_name
std::string get_type_name() const
Get the full typename for this option.
Definition: Option.hpp:1108
CLI::detail::to_flag_value
int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition: TypeTools.hpp:551
CLI::OptionBase::always_capture_default
CRTP * always_capture_default(bool value=true)
Definition: Option.hpp:104
CLI::Option::get_validator
Validator * get_validator(int index)
Get a Validator by index NOTE: this may not be the order of definition.
Definition: Option.hpp:488
CLI::Option::empty
bool empty() const
True if the option was not passed.
Definition: Option.hpp:348
CLI::Option::get_items_expected_min
int get_items_expected_min() const
The total min number of expected string values to be used.
Definition: Option.hpp:691
CLI::Option::get_expected
int get_expected() const
The number of times the option expects to be included.
Definition: Option.hpp:683
CLI::Validator::non_modifying
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition: Validators.hpp:157
CLI::Option::get_excludes
std::set< Option * > get_excludes() const
The set of options excluded.
Definition: Option.hpp:665
CLI::detail::checked_multiply
std::enable_if< std::is_integral< T >::value, bool >::type checked_multiply(T &a, T b)
Performs a *= b; if it doesn't cause integer overflow. Returns false otherwise.
Definition: Validators.hpp:656
CLI::OptionDefaults::delimiter
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition: Option.hpp:222
CLI::Option::get_allow_extra_args
bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition: Option.hpp:411
CLI::Option::option_state::callback_run
@ callback_run
the callback has been executed
CLI::Option::current_option_state_
option_state current_option_state_
Whether the callback has run (needed for INI parsing)
Definition: Option.hpp:322
CLI::Option::check
Option * check(std::function< std::string(const std::string &)> Validator, std::string Validator_description="", std::string Validator_name="")
Adds a Validator. Takes a const string& and returns an error message (empty if conversion/check is ok...
Definition: Option.hpp:432
CLI::OptionBase::get_delimiter
char get_delimiter() const
Get the current delimiter char.
Definition: Option.hpp:130
CLI::Option::description_
std::string description_
The description for help strings.
Definition: Option.hpp:259
CLI::OptionNotFound
Thrown when counting a non-existent option.
Definition: Error.hpp:326
CLI::OptionBase::mandatory
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition: Option.hpp:102
CLI::Option::get_expected_min
int get_expected_min() const
The number of times the option expects to be included.
Definition: Option.hpp:686
CLI::OptionBase::required
CRTP * required(bool value=true)
Set the option as required.
Definition: Option.hpp:96
CLI::Option::clear
void clear()
Clear the parsed results (mostly for testing)
Definition: Option.hpp:354
CLI::detail::split_names
std::vector< std::string > split_names(std::string current)
Definition: Split.hpp:59
CLI::ExitCodes::ValidationError
@ ValidationError
CLI::Option::operator=
Option & operator=(const Option &)=delete
CLI::Option::add_result
Option * add_result(std::string s)
Puts a result at the end.
Definition: Option.hpp:919
CLI::OptionDefaults::disable_flag_override
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition: Option.hpp:216
CLI::Option::lnames_
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition: Option.hpp:239
CLI::Option::check_lname
bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition: Option.hpp:861
CLI::OptionBase::ignore_underscore_
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition: Option.hpp:56
CLI::OptionBase::ignore_case_
bool ignore_case_
Ignore the case when matching (option, not value)
Definition: Option.hpp:53
CLI::Option::run_callback
void run_callback()
Process the callback.
Definition: Option.hpp:787
CLI::Option::get_run_callback_for_default
bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition: Option.hpp:420
CLI::Option::remove_needs
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition: Option.hpp:520
CLI::Option::get_callback
callback_t get_callback() const
Get the callback function.
Definition: Option.hpp:671
CLI::IncorrectConstruction
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition: Error.hpp:86
CLI::Option::allow_extra_args
Option * allow_extra_args(bool value=true)
Definition: Option.hpp:406
CLI::OptionBase::get_ignore_underscore
bool get_ignore_underscore() const
The status of ignore_underscore.
Definition: Option.hpp:121