CLI11  2.6.2
App.hpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2026, University of Cincinnati, developed by Henry Schreiner
2 // under NSF AWARD 1414736 and by the respective contributors.
3 // All rights reserved.
4 //
5 // SPDX-License-Identifier: BSD-3-Clause
6 
7 #pragma once
8 
9 // IWYU pragma: private, include "CLI/CLI.hpp"
10 
11 // [CLI11:public_includes:set]
12 #include <algorithm>
13 #include <cstdint>
14 #include <functional>
15 #include <iostream>
16 #include <iterator>
17 #include <memory>
18 #include <numeric>
19 #include <set>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 // [CLI11:public_includes:end]
25 
26 // CLI Library includes
27 #include "ConfigFwd.hpp"
28 #include "Error.hpp"
29 #include "FormatterFwd.hpp"
30 #include "Macros.hpp"
31 #include "Option.hpp"
32 #include "Split.hpp"
33 #include "StringTools.hpp"
34 #include "TypeTools.hpp"
35 
36 namespace CLI {
37 // [CLI11:app_hpp:verbatim]
38 
39 #ifndef CLI11_PARSE
40 #define CLI11_PARSE(app, ...) \
41  try { \
42  (app).parse(__VA_ARGS__); \
43  } catch(const CLI::ParseError &e) { \
44  return (app).exit(e); \
45  }
46 #endif
47 
48 namespace detail {
49 enum class Classifier : std::uint8_t {
50  NONE,
52  SHORT,
53  LONG,
55  SUBCOMMAND,
57 };
58 struct AppFriend;
59 } // namespace detail
60 
61 namespace FailureMessage {
63 CLI11_INLINE std::string simple(const App *app, const Error &e);
64 
66 CLI11_INLINE std::string help(const App *app, const Error &e);
67 } // namespace FailureMessage
68 
70 enum class ExtrasMode : std::uint8_t {
71  Error = 0,
73  Ignore,
76  Capture
77 };
78 
80 enum class ConfigExtrasMode : std::uint8_t { Error = 0, Ignore, IgnoreAll, Capture };
81 
83 enum class config_extras_mode : std::uint8_t { error = 0, ignore, ignore_all, capture };
84 
88 enum class PrefixCommandMode : std::uint8_t { Off = 0, SeparatorOnly = 1, On = 2 };
89 
90 class App;
91 
92 using App_p = std::shared_ptr<App>;
93 
94 namespace detail {
96 
97 template <typename T, enable_if_t<!std::is_integral<T>::value || (sizeof(T) <= 1U), detail::enabler> = detail::dummy>
99  return opt->always_capture_default();
100 }
101 
103 template <typename T, enable_if_t<std::is_integral<T>::value && (sizeof(T) > 1U), detail::enabler> = detail::dummy>
106 }
107 
108 } // namespace detail
109 
110 class Option_group;
112 
115 class App {
116  friend Option;
117  friend detail::AppFriend;
118 
119  protected:
120  // This library follows the Google style guide for member names ending in underscores
121 
124 
126  std::string name_{};
127 
129  std::string description_{};
130 
133 
137 
140 
142  bool has_automatic_name_{false};
143 
145  bool required_{false};
146 
148  bool disabled_{false};
149 
151  bool pre_parse_called_{false};
152 
155  bool immediate_callback_{false};
156 
158  std::function<void(std::size_t)> pre_parse_callback_{};
159 
161  std::function<void()> parse_complete_callback_{};
162 
164  std::function<void()> final_callback_{};
165 
169 
172 
174  std::vector<Option_p> options_{};
175 
179 
181  std::string usage_{};
182 
184  std::function<std::string()> usage_callback_{};
185 
187  std::string footer_{};
188 
190  std::function<std::string()> footer_callback_{};
191 
193  Option *help_ptr_{nullptr};
194 
197 
199  Option *version_ptr_{nullptr};
200 
202  std::shared_ptr<FormatterBase> formatter_{new Formatter()};
203 
205  std::function<std::string(const App *, const Error &e)> failure_message_{FailureMessage::simple};
206 
210 
211  using missing_t = std::vector<std::pair<detail::Classifier, std::string>>;
212 
217 
219  std::vector<Option *> parse_order_{};
220 
222  std::vector<App *> parsed_subcommands_{};
223 
225  std::set<App *> exclude_subcommands_{};
226 
229  std::set<Option *> exclude_options_{};
230 
233  std::set<App *> need_subcommands_{};
234 
237  std::set<Option *> need_options_{};
238 
242 
244  std::vector<App_p> subcommands_{};
245 
247  bool ignore_case_{false};
248 
250  bool ignore_underscore_{false};
251 
254  bool fallthrough_{false};
255 
258 
261 #ifdef _WIN32
262  true
263 #else
264  false
265 #endif
266  };
268  bool positionals_at_end_{false};
269 
270  enum class startup_mode : std::uint8_t { stable, enabled, disabled };
274 
276  bool configurable_{false};
277 
280 
283 
286  bool silent_{false};
287 
290 
293 
295  std::uint32_t parsed_{0U};
296 
298  std::size_t require_subcommand_min_{0};
299 
301  std::size_t require_subcommand_max_{0};
302 
304  std::size_t require_option_min_{0};
305 
307  std::size_t require_option_max_{0};
308 
310  App *parent_{nullptr};
311 
313  std::string group_{"SUBCOMMANDS"};
314 
316  std::vector<std::string> aliases_{};
317 
321 
323  Option *config_ptr_{nullptr};
324 
326  std::shared_ptr<Config> config_formatter_{new ConfigTOML()};
327 
329 
330 #ifdef _WIN32
331  std::vector<std::string> normalized_argv_{};
333 
335  std::vector<char *> normalized_argv_view_{};
336 #endif
337 
339  App(std::string app_description, std::string app_name, App *parent);
340 
341  public:
344 
346  explicit App(std::string app_description = "", std::string app_name = "")
347  : App(app_description, app_name, nullptr) {
348  set_help_flag("-h,--help", "Print this help message and exit");
349  }
350 
351  App(const App &) = delete;
352  App &operator=(const App &) = delete;
353 
355  virtual ~App() = default;
356 
358  CLI11_NODISCARD char **ensure_utf8(char **argv);
359 
366  App *callback(std::function<void()> app_callback) {
367  if(immediate_callback_) {
368  parse_complete_callback_ = std::move(app_callback);
369  } else {
370  final_callback_ = std::move(app_callback);
371  }
372  return this;
373  }
374 
377  App *final_callback(std::function<void()> app_callback) {
378  final_callback_ = std::move(app_callback);
379  return this;
380  }
381 
384  App *parse_complete_callback(std::function<void()> pc_callback) {
385  parse_complete_callback_ = std::move(pc_callback);
386  return this;
387  }
388 
391  App *preparse_callback(std::function<void(std::size_t)> pp_callback) {
392  pre_parse_callback_ = std::move(pp_callback);
393  return this;
394  }
395 
397  App *name(std::string app_name = "");
398 
400  App *alias(std::string app_name);
401 
403  App *allow_extras(bool allow = true) {
405  return this;
406  }
407 
410  allow_extras_ = allow;
411  return this;
412  }
413 
415  App *required(bool require = true) {
416  required_ = require;
417  return this;
418  }
419 
421  App *disabled(bool disable = true) {
422  disabled_ = disable;
423  return this;
424  }
425 
427  App *silent(bool silence = true) {
428  silent_ = silence;
429  return this;
430  }
431 
433  App *allow_non_standard_option_names(bool allowed = true) {
434  allow_non_standard_options_ = allowed;
435  return this;
436  }
437 
439  App *allow_subcommand_prefix_matching(bool allowed = true) {
440  allow_prefix_matching_ = allowed;
441  return this;
442  }
444  App *disabled_by_default(bool disable = true) {
445  if(disable) {
447  } else {
449  }
450  return this;
451  }
452 
455  App *enabled_by_default(bool enable = true) {
456  if(enable) {
458  } else {
461  }
462  return this;
463  }
464 
466  App *immediate_callback(bool immediate = true);
467 
469  App *validate_positionals(bool validate = true) {
470  validate_positionals_ = validate;
471  return this;
472  }
473 
475  App *validate_optional_arguments(bool validate = true) {
476  validate_optional_arguments_ = validate;
477  return this;
478  }
479 
481  App *allow_config_extras(bool allow = true) {
482  if(allow) {
485  } else {
487  }
488  return this;
489  }
490 
493  allow_config_extras_ = static_cast<ConfigExtrasMode>(mode);
494  return this;
495  }
496 
499  allow_config_extras_ = mode;
500  return this;
501  }
502 
505  App *prefix_command(bool is_prefix = true) {
507  return this;
508  }
509 
513  prefix_command_ = mode;
514  return this;
515  }
516 
518  App *ignore_case(bool value = true);
519 
522  App *allow_windows_style_options(bool value = true) {
524  return this;
525  }
526 
528  App *positionals_at_end(bool value = true) {
529  positionals_at_end_ = value;
530  return this;
531  }
532 
534  App *configurable(bool value = true) {
535  configurable_ = value;
536  return this;
537  }
538 
540  App *ignore_underscore(bool value = true);
541 
543  App *formatter(std::shared_ptr<FormatterBase> fmt) {
544  formatter_ = fmt;
545  return this;
546  }
547 
549  App *formatter_fn(std::function<std::string(const App *, std::string, AppFormatMode)> fmt) {
550  formatter_ = std::make_shared<FormatterLambda>(fmt);
551  return this;
552  }
553 
555  App *config_formatter(std::shared_ptr<Config> fmt) {
556  config_formatter_ = fmt;
557  return this;
558  }
559 
561  CLI11_NODISCARD bool parsed() const { return parsed_ > 0; }
562 
565 
569 
584  Option *add_option(std::string option_name,
585  callback_t option_callback,
586  std::string option_description = "",
587  bool defaulted = false,
588  std::function<std::string()> func = {});
589 
591  template <typename AssignTo,
592  typename ConvertTo = AssignTo,
593  enable_if_t<!std::is_const<ConvertTo>::value, detail::enabler> = detail::dummy>
594  Option *add_option(std::string option_name,
595  AssignTo &variable,
596  std::string option_description = "") {
597 
598  auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
599  return detail::lexical_conversion<AssignTo, ConvertTo>(res, variable);
600  };
601 
602  Option *opt = add_option(option_name, fun, option_description, false, [&variable]() {
603  return CLI::detail::checked_to_string<AssignTo, ConvertTo>(variable);
604  });
605  opt->type_name(detail::type_name<ConvertTo>());
606  // these must be actual lvalues since (std::max) sometimes is defined in terms of references and references
607  // to structs used in the evaluation can be temporary so that would cause issues.
610  opt->type_size(detail::type_count_min<ConvertTo>::value, (std::max)(Tcount, XCcount));
611  opt->expected(detail::expected_count<ConvertTo>::value);
613  return opt;
614  }
615 
617  template <typename AssignTo, enable_if_t<!std::is_const<AssignTo>::value, detail::enabler> = detail::dummy>
618  Option *add_option_no_stream(std::string option_name,
619  AssignTo &variable,
620  std::string option_description = "") {
621 
622  auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
623  return detail::lexical_conversion<AssignTo, AssignTo>(res, variable);
624  };
625 
626  Option *opt = add_option(option_name, fun, option_description, false, []() { return std::string{}; });
627  opt->type_name(detail::type_name<AssignTo>());
628  opt->type_size(detail::type_count_min<AssignTo>::value, detail::type_count<AssignTo>::value);
629  opt->expected(detail::expected_count<AssignTo>::value);
631  return opt;
632  }
633 
635  template <typename ArgType>
636  Option *add_option_function(std::string option_name,
637  const std::function<void(const ArgType &)> &func,
638  std::string option_description = "") {
639 
640  auto fun = [func](const CLI::results_t &res) {
641  ArgType variable;
642  bool result = detail::lexical_conversion<ArgType, ArgType>(res, variable);
643  if(result) {
644  func(variable);
645  }
646  return result;
647  };
648 
649  Option *opt = add_option(option_name, std::move(fun), option_description, false);
650  opt->type_name(detail::type_name<ArgType>());
651  opt->type_size(detail::type_count_min<ArgType>::value, detail::type_count<ArgType>::value);
652  opt->expected(detail::expected_count<ArgType>::value);
653  return opt;
654  }
655 
657  Option *add_option(std::string option_name) {
658  return add_option(option_name, CLI::callback_t{}, std::string{}, false);
659  }
660 
662  template <typename T,
663  enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
665  Option *add_option(std::string option_name, T &option_description) {
666  return add_option(option_name, CLI::callback_t(), option_description, false);
667  }
668 
670  Option *set_help_flag(std::string flag_name = "", const std::string &help_description = "");
671 
673  Option *set_help_all_flag(std::string help_name = "", const std::string &help_description = "");
674 
676  Option *set_version_flag(std::string flag_name = "",
677  const std::string &versionString = "",
678  const std::string &version_help = "Display program version information and exit");
679 
681  Option *set_version_flag(std::string flag_name,
682  std::function<std::string()> vfunc,
683  const std::string &version_help = "Display program version information and exit");
684 
685  private:
687  Option *_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description);
688 
689  public:
691  Option *add_flag(std::string flag_name) { return _add_flag_internal(flag_name, CLI::callback_t(), std::string{}); }
692 
696  template <typename T,
698  std::is_rvalue_reference<T &&>::value) &&
699  std::is_constructible<std::string, typename std::remove_reference<T>::type>::value,
700  detail::enabler> = detail::dummy>
701  Option *add_flag(std::string flag_name, T &&flag_description) {
702  return _add_flag_internal(flag_name, CLI::callback_t(), std::forward<T>(flag_description));
703  }
704 
707  template <typename T,
708  enable_if_t<!detail::is_mutable_container<T>::value && !std::is_const<T>::value &&
709  !std::is_constructible<std::function<void(int)>, T>::value,
710  detail::enabler> = detail::dummy>
711  Option *add_flag(std::string flag_name,
712  T &flag_result,
713  std::string flag_description = "") {
714 
715  CLI::callback_t fun = [&flag_result](const CLI::results_t &res) {
717  return lexical_cast(res[0], flag_result);
718  };
719  auto *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
720  return detail::default_flag_modifiers<T>(opt);
721  }
722 
724  template <typename T,
727  Option *add_flag(std::string flag_name,
728  std::vector<T> &flag_results,
729  std::string flag_description = "") {
730  CLI::callback_t fun = [&flag_results](const CLI::results_t &res) {
731  bool retval = true;
732  for(const auto &elem : res) {
734  flag_results.emplace_back();
735  retval &= lexical_cast(elem, flag_results.back());
736  }
737  return retval;
738  };
739  return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
742  }
743 
745  Option *add_flag_callback(std::string flag_name,
746  std::function<void(void)> function,
747  std::string flag_description = "");
748 
750  Option *add_flag_function(std::string flag_name,
751  std::function<void(std::int64_t)> function,
752  std::string flag_description = "");
753 
754 #ifdef CLI11_CPP14
755  Option *add_flag(std::string flag_name,
757  std::function<void(std::int64_t)> function,
758  std::string flag_description = "") {
759  return add_flag_function(std::move(flag_name), std::move(function), std::move(flag_description));
760  }
761 #endif
762 
764  Option *set_config(std::string option_name = "",
765  std::string default_filename = "",
766  const std::string &help_message = "Read an ini file",
767  bool config_required = false);
768 
770  bool remove_option(Option *opt);
771 
773  template <typename T = Option_group>
774  T *add_option_group(std::string group_name, std::string group_description = "") {
775  if(!detail::valid_alias_name_string(group_name)) {
776  throw IncorrectConstruction("option group names may not contain newlines or null characters");
777  }
778  auto option_group = std::make_shared<T>(std::move(group_description), group_name, this);
779  auto *ptr = option_group.get();
780  // move to App_p for overload resolution on older gcc versions
781  App_p app_ptr = std::static_pointer_cast<App>(option_group);
782  // don't inherit the footer in option groups and clear the help flag by default
783  app_ptr->footer_ = "";
784  app_ptr->set_help_flag();
785  add_subcommand(std::move(app_ptr));
786  return ptr;
787  }
788 
792 
794  App *add_subcommand(std::string subcommand_name = "", std::string subcommand_description = "");
795 
797  App *add_subcommand(CLI::App_p subcom);
798 
800  bool remove_subcommand(App *subcom);
801 
804  App *get_subcommand(const App *subcom) const;
805 
807  CLI11_NODISCARD App *get_subcommand(std::string subcom) const;
808 
811  CLI11_NODISCARD App *get_subcommand_no_throw(std::string subcom) const noexcept;
812 
814  CLI11_NODISCARD App *get_subcommand(int index = 0) const;
815 
817  CLI::App_p get_subcommand_ptr(App *subcom) const;
818 
820  CLI11_NODISCARD CLI::App_p get_subcommand_ptr(std::string subcom) const;
821 
823  CLI11_NODISCARD CLI::App_p get_subcommand_ptr(int index = 0) const;
824 
826  CLI11_NODISCARD App *get_option_group(std::string group_name) const;
827 
831  CLI11_NODISCARD std::size_t count() const { return parsed_; }
832 
835  CLI11_NODISCARD std::size_t count_all() const;
836 
838  App *group(std::string group_name) {
839  group_ = group_name;
840  return this;
841  }
842 
847  return this;
848  }
849 
853  App *require_subcommand(int value) {
854  if(value < 0) {
856  require_subcommand_max_ = static_cast<std::size_t>(-value);
857  } else {
858  require_subcommand_min_ = static_cast<std::size_t>(value);
859  require_subcommand_max_ = static_cast<std::size_t>(value);
860  }
861  return this;
862  }
863 
866  App *require_subcommand(std::size_t min, std::size_t max) {
869  return this;
870  }
871 
876  return this;
877  }
878 
882  App *require_option(int value) {
883  if(value < 0) {
885  require_option_max_ = static_cast<std::size_t>(-value);
886  } else {
887  require_option_min_ = static_cast<std::size_t>(value);
888  require_option_max_ = static_cast<std::size_t>(value);
889  }
890  return this;
891  }
892 
895  App *require_option(std::size_t min, std::size_t max) {
896  require_option_min_ = min;
897  require_option_max_ = max;
898  return this;
899  }
900 
903  App *fallthrough(bool value = true) {
904  fallthrough_ = value;
905  return this;
906  }
907 
909  App *subcommand_fallthrough(bool value = true) {
910  subcommand_fallthrough_ = value;
911  return this;
912  }
913 
916  explicit operator bool() const { return parsed_ > 0; }
917 
921 
925  virtual void pre_callback() {}
926 
930  //
932  void clear();
933 
936  void parse(int argc, const char *const *argv);
937  void parse(int argc, const wchar_t *const *argv);
938 
939  private:
940  template <class CharT> void parse_char_t(int argc, const CharT *const *argv);
941 
942  public:
947  void parse(std::string commandline, bool program_name_included = false);
948  void parse(std::wstring commandline, bool program_name_included = false);
949 
952  void parse(std::vector<std::string> &args);
953 
955  void parse(std::vector<std::string> &&args);
956 
957  void parse_from_stream(std::istream &input);
958 
960  void failure_message(std::function<std::string(const App *, const Error &e)> function) {
961  failure_message_ = function;
962  }
963 
965  int exit(const Error &e, std::ostream &out = std::cout, std::ostream &err = std::cerr) const;
966 
970 
972  CLI11_NODISCARD std::size_t count(std::string option_name) const { return get_option(option_name)->count(); }
973 
976  CLI11_NODISCARD std::vector<App *> get_subcommands() const { return parsed_subcommands_; }
977 
980  std::vector<const App *> get_subcommands(const std::function<bool(const App *)> &filter) const;
981 
984  std::vector<App *> get_subcommands(const std::function<bool(App *)> &filter);
985 
987  bool got_subcommand(const App *subcom) const {
988  // get subcom needed to verify that this was a real subcommand
989  return get_subcommand(subcom)->parsed_ > 0;
990  }
991 
993  CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const noexcept {
994  App *sub = get_subcommand_no_throw(subcommand_name);
995  return (sub != nullptr) ? (sub->parsed_ > 0) : false;
996  }
997 
999  App *excludes(Option *opt) {
1000  if(opt == nullptr) {
1001  throw OptionNotFound("nullptr passed");
1002  }
1003  exclude_options_.insert(opt);
1004  return this;
1005  }
1006 
1008  App *excludes(App *app) {
1009  if(app == nullptr) {
1010  throw OptionNotFound("nullptr passed");
1011  }
1012  if(app == this) {
1013  throw OptionNotFound("cannot self reference in needs");
1014  }
1015  auto res = exclude_subcommands_.insert(app);
1016  // subcommand exclusion should be symmetric
1017  if(res.second) {
1018  app->exclude_subcommands_.insert(this);
1019  }
1020  return this;
1021  }
1022 
1023  App *needs(Option *opt) {
1024  if(opt == nullptr) {
1025  throw OptionNotFound("nullptr passed");
1026  }
1027  need_options_.insert(opt);
1028  return this;
1029  }
1030 
1031  App *needs(App *app) {
1032  if(app == nullptr) {
1033  throw OptionNotFound("nullptr passed");
1034  }
1035  if(app == this) {
1036  throw OptionNotFound("cannot self reference in needs");
1037  }
1038  need_subcommands_.insert(app);
1039  return this;
1040  }
1041 
1043  bool remove_excludes(Option *opt);
1044 
1046  bool remove_excludes(App *app);
1047 
1049  bool remove_needs(Option *opt);
1050 
1052  bool remove_needs(App *app);
1056 
1058  App *usage(std::string usage_string) {
1059  usage_ = std::move(usage_string);
1060  return this;
1061  }
1063  App *usage(std::function<std::string()> usage_function) {
1064  usage_callback_ = std::move(usage_function);
1065  return this;
1066  }
1068  App *footer(std::string footer_string) {
1069  footer_ = std::move(footer_string);
1070  return this;
1071  }
1073  App *footer(std::function<std::string()> footer_function) {
1074  footer_callback_ = std::move(footer_function);
1075  return this;
1076  }
1079  CLI11_NODISCARD std::string config_to_str(bool default_also = false, bool write_description = false) const {
1080  return config_formatter_->to_config(this, default_also, write_description, "");
1081  }
1082 
1085  CLI11_NODISCARD std::string help(std::string prev = "", AppFormatMode mode = AppFormatMode::Normal) const;
1086 
1088  CLI11_NODISCARD std::string version() const;
1092 
1094  CLI11_NODISCARD std::shared_ptr<FormatterBase> get_formatter() const { return formatter_; }
1095 
1097  CLI11_NODISCARD std::shared_ptr<Config> get_config_formatter() const { return config_formatter_; }
1098 
1100  CLI11_NODISCARD std::shared_ptr<ConfigBase> get_config_formatter_base() const {
1101  // This is safer as a dynamic_cast if we have RTTI, as Config -> ConfigBase
1102 #if CLI11_USE_STATIC_RTTI == 0
1103  return std::dynamic_pointer_cast<ConfigBase>(config_formatter_);
1104 #else
1105  return std::static_pointer_cast<ConfigBase>(config_formatter_);
1106 #endif
1107  }
1108 
1110  CLI11_NODISCARD std::string get_description() const { return description_; }
1111 
1113  App *description(std::string app_description) {
1114  description_ = std::move(app_description);
1115  return this;
1116  }
1117 
1119  std::vector<const Option *> get_options(const std::function<bool(const Option *)> filter = {}) const;
1120 
1122  std::vector<Option *> get_options(const std::function<bool(Option *)> filter = {});
1123 
1125  CLI11_NODISCARD Option *get_option_no_throw(std::string option_name) noexcept;
1126 
1128  CLI11_NODISCARD const Option *get_option_no_throw(std::string option_name) const noexcept;
1129 
1131  CLI11_NODISCARD const Option *get_option(std::string option_name) const {
1132  const auto *opt = get_option_no_throw(option_name);
1133  if(opt == nullptr) {
1134  throw OptionNotFound(option_name);
1135  }
1136  return opt;
1137  }
1138 
1140  CLI11_NODISCARD Option *get_option(std::string option_name) {
1141  auto *opt = get_option_no_throw(option_name);
1142  if(opt == nullptr) {
1143  throw OptionNotFound(option_name);
1144  }
1145  return opt;
1146  }
1147 
1149  const Option *operator[](const std::string &option_name) const { return get_option(option_name); }
1150 
1152  const Option *operator[](const char *option_name) const { return get_option(option_name); }
1153 
1156 
1159 
1162 
1165 
1168 
1171 
1174 
1176  CLI11_NODISCARD const std::string &get_group() const { return group_; }
1177 
1179  CLI11_NODISCARD std::string get_usage() const {
1180  return (usage_callback_) ? usage_callback_() + '\n' + usage_ : usage_;
1181  }
1182 
1184  CLI11_NODISCARD std::string get_footer() const {
1185  return (footer_callback_) ? footer_callback_() + '\n' + footer_ : footer_;
1186  }
1187 
1190 
1193 
1196 
1199 
1201  CLI11_NODISCARD bool get_prefix_command() const { return static_cast<bool>(prefix_command_); }
1202 
1205 
1208 
1211 
1213  CLI11_NODISCARD bool get_required() const { return required_; }
1214 
1216  CLI11_NODISCARD bool get_disabled() const { return disabled_; }
1217 
1219  CLI11_NODISCARD bool get_silent() const { return silent_; }
1220 
1223 
1226 
1229 
1232 
1239 
1242  return static_cast<config_extras_mode>(allow_config_extras_);
1243  }
1244 
1247 
1249  CLI11_NODISCARD const Option *get_help_ptr() const { return help_ptr_; }
1250 
1253 
1256 
1259 
1262 
1265 
1267  App *get_parent() { return parent_; }
1268 
1270  CLI11_NODISCARD const App *get_parent() const { return parent_; }
1271 
1273  CLI11_NODISCARD const std::string &get_name() const { return name_; }
1274 
1276  CLI11_NODISCARD const std::vector<std::string> &get_aliases() const { return aliases_; }
1277 
1280  aliases_.clear();
1281  return this;
1282  }
1283 
1285  CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
1286 
1289  CLI11_NODISCARD bool check_name(std::string name_to_check) const;
1290 
1292  enum class NameMatch : std::uint8_t { none = 0, exact = 1, prefix = 2 };
1293 
1297  CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
1298 
1300  CLI11_NODISCARD std::vector<std::string> get_groups() const;
1301 
1303  CLI11_NODISCARD const std::vector<Option *> &parse_order() const { return parse_order_; }
1304 
1306  CLI11_NODISCARD std::vector<std::string> remaining(bool recurse = false) const;
1307 
1309  CLI11_NODISCARD std::vector<std::string> remaining_for_passthrough(bool recurse = false) const;
1310 
1312  CLI11_NODISCARD std::size_t remaining_size(bool recurse = false) const;
1313 
1315 
1316  protected:
1321  void _validate() const;
1322 
1326  void _configure();
1327 
1329  void run_callback(bool final_mode = false, bool suppress_final_callback = false);
1330 
1332  CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used = true) const;
1333 
1335  CLI11_NODISCARD detail::Classifier _recognize(const std::string &current,
1336  bool ignore_used_subcommands = true) const;
1337 
1338  // The parse function is now broken into several parts, and part of process
1339 
1341  void _process_config_file();
1342 
1344  bool _process_config_file(const std::string &config_file, bool throw_error);
1345 
1347  void _process_env();
1348 
1350  void _process_callbacks(CallbackPriority priority);
1351 
1355  void _process_help_flags(CallbackPriority priority, bool trigger_help = false, bool trigger_all_help = false) const;
1356 
1358  void _process_requirements();
1359 
1361  void _process();
1362 
1364  void _process_extras();
1365 
1367  void increment_parsed();
1368 
1370  void _parse(std::vector<std::string> &args);
1371 
1373  void _parse(std::vector<std::string> &&args);
1374 
1376  void _parse_stream(std::istream &input);
1377 
1382  void _parse_config(const std::vector<ConfigItem> &args);
1383 
1385  bool _parse_single_config(const ConfigItem &item, std::size_t level = 0);
1386 
1388  bool _add_flag_like_result(Option *op, const ConfigItem &item, const std::vector<std::string> &inputs);
1389 
1392  bool _parse_single(std::vector<std::string> &args, bool &positional_only);
1393 
1395  CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only = false) const;
1396 
1399 
1403  bool _parse_positional(std::vector<std::string> &args, bool haltOnSubcommand);
1404 
1408  _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept;
1409 
1414  bool _parse_subcommand(std::vector<std::string> &args);
1415 
1419  bool _parse_arg(std::vector<std::string> &args, detail::Classifier current_type, bool local_processing_only);
1420 
1422  void _trigger_pre_parse(std::size_t remaining_args);
1423 
1426 
1428  CLI11_NODISCARD const App *_get_fallthrough_parent() const noexcept;
1429 
1431  CLI11_NODISCARD const std::string &_compare_subcommand_names(const App &subcom, const App &base) const;
1432 
1434  void _move_to_missing(detail::Classifier val_type, const std::string &val);
1435 
1436  public:
1438  void _move_option(Option *opt, App *app);
1439 }; // namespace CLI
1440 
1442 class Option_group : public App {
1443  public:
1444  Option_group(std::string group_description, std::string group_name, App *parent)
1445  : App(std::move(group_description), "", parent) {
1446  group(group_name);
1447  // option groups should have automatic fallthrough
1448  if(group_name.empty() || group_name.front() == '+') {
1449  // help will not be used by default in these contexts
1450  set_help_flag("");
1451  set_help_all_flag("");
1452  }
1453  }
1454  using App::add_option;
1457  if(get_parent() == nullptr) {
1458  throw OptionNotFound("Unable to locate the specified option");
1459  }
1460  get_parent()->_move_option(opt, this);
1461  return opt;
1462  }
1464  void add_options(Option *opt) { add_option(opt); }
1466  template <typename... Args> void add_options(Option *opt, Args... args) {
1467  add_option(opt);
1468  add_options(args...);
1469  }
1470  using App::add_subcommand;
1472  App *add_subcommand(App *subcom) {
1473  App_p subc = subcom->get_parent()->get_subcommand_ptr(subcom);
1474  subc->get_parent()->remove_subcommand(subcom);
1475  add_subcommand(std::move(subc));
1476  return subcom;
1477  }
1478 };
1479 
1481 CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable);
1482 
1484 CLI11_INLINE void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable);
1485 
1487 CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable);
1488 
1490 CLI11_INLINE void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable);
1491 
1493 CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement = "");
1494 
1496 inline void deprecate_option(App *app, const std::string &option_name, const std::string &replacement = "") {
1497  auto *opt = app->get_option(option_name);
1498  deprecate_option(opt, replacement);
1499 }
1500 
1502 inline void deprecate_option(App &app, const std::string &option_name, const std::string &replacement = "") {
1503  auto *opt = app.get_option(option_name);
1504  deprecate_option(opt, replacement);
1505 }
1506 
1508 CLI11_INLINE void retire_option(App *app, Option *opt);
1509 
1511 CLI11_INLINE void retire_option(App &app, Option *opt);
1512 
1514 CLI11_INLINE void retire_option(App *app, const std::string &option_name);
1515 
1517 CLI11_INLINE void retire_option(App &app, const std::string &option_name);
1518 
1519 namespace detail {
1521 struct AppFriend {
1522 #ifdef CLI11_CPP14
1523 
1525  template <typename... Args> static decltype(auto) parse_arg(App *app, Args &&...args) {
1526  return app->_parse_arg(std::forward<Args>(args)...);
1527  }
1528 
1530  template <typename... Args> static decltype(auto) parse_subcommand(App *app, Args &&...args) {
1531  return app->_parse_subcommand(std::forward<Args>(args)...);
1532  }
1533 #else
1534  template <typename... Args>
1536  static auto parse_arg(App *app, Args &&...args) ->
1537  typename std::result_of<decltype (&App::_parse_arg)(App, Args...)>::type {
1538  return app->_parse_arg(std::forward<Args>(args)...);
1539  }
1540 
1542  template <typename... Args>
1543  static auto parse_subcommand(App *app, Args &&...args) ->
1544  typename std::result_of<decltype (&App::_parse_subcommand)(App, Args...)>::type {
1545  return app->_parse_subcommand(std::forward<Args>(args)...);
1546  }
1547 #endif
1548  static App *get_fallthrough_parent(App *app) { return app->_get_fallthrough_parent(); }
1550 
1552  static const App *get_fallthrough_parent(const App *app) { return app->_get_fallthrough_parent(); }
1553 };
1554 } // namespace detail
1555 
1556 // [CLI11:app_hpp:end]
1557 } // namespace CLI
1558 
1559 #ifndef CLI11_COMPILE
1560 #include "impl/App_inl.hpp" // IWYU pragma: export
1561 #endif
CLI11_NODISCARD const Option * get_version_ptr() const
Get a pointer to the version option. (const)
Definition: App.hpp:1264
App * clear_aliases()
clear all the aliases of the current App
Definition: App.hpp:1279
CLI11_NODISCARD const Option * get_config_ptr() const
Get a pointer to the config option. (const)
Definition: App.hpp:1258
void _move_option(Option *opt, App *app)
function that could be used by subclasses of App to shift options around into subcommands ...
void failure_message(std::function< std::string(const App *, const Error &e)> function)
Provide a function to print a help message. The function gets access to the App pointer and error...
Definition: App.hpp:960
startup_mode
Definition: App.hpp:270
CLI11_NODISCARD std::size_t count(std::string option_name) const
Counts the number of times the given option was passed.
Definition: App.hpp:972
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition: App.hpp:1179
void run_callback(bool final_mode=false, bool suppress_final_callback=false)
Internal function to run (App) callback, bottom up.
This class is simply to allow tests access to App's protected functions.
Definition: App.hpp:1521
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition: Option.hpp:389
CLI11_NODISCARD std::size_t count() const
Definition: App.hpp:831
std::size_t require_subcommand_min_
Minimum required subcommands (not inheritable!)
Definition: App.hpp:298
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition: App.hpp:148
CLI11_INLINE std::string help(const App *app, const Error &e)
Printout the full help string on error (if this fn is set, the old default for CLI11) ...
void _validate() const
CLI11_NODISCARD const App * get_parent() const
Get the parent of this subcommand (or nullptr if main app) (const version)
Definition: App.hpp:1270
bool _parse_subcommand(std::vector< std::string > &args)
CLI11_NODISCARD App * _get_fallthrough_parent() noexcept
Get the appropriate parent to fallthrough to which is the first one that has a name or the main app...
App * config_formatter(std::shared_ptr< Config > fmt)
Set the config formatter.
Definition: App.hpp:555
PrefixCommandMode
enumeration of prefix command modes, separator requires that the first extra argument be a "--"...
Definition: App.hpp:88
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition: App.hpp:1276
bool has_automatic_name_
If set to true the name was automatically generated from the command line vs a user set name...
Definition: App.hpp:142
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition: App.hpp:1213
Option * add_flag(std::string flag_name, T &&flag_description)
Definition: App.hpp:701
App * needs(App *app)
Definition: App.hpp:1031
App * require_option()
The argumentless form of require option requires 1 or more options be used.
Definition: App.hpp:873
App * require_option(std::size_t min, std::size_t max)
Definition: App.hpp:895
CLI11_NODISCARD const std::string & _compare_subcommand_names(const App &subcom, const App &base) const
Helper function to run through all possible comparisons of subcommand names to check there is no over...
startup_mode default_startup
Definition: App.hpp:273
static const App * get_fallthrough_parent(const App *app)
Wrap the const fallthrough parent function to make sure that is working correctly.
Definition: App.hpp:1552
Definition: App.hpp:36
CLI::App_p get_subcommand_ptr(App *subcom) const
Check to see if a subcommand is part of this command and get a shared_ptr to it.
void clear()
Reset the parsed data.
Option * set_version_flag(std::string flag_name="", const std::string &versionString="", const std::string &version_help="Display program version information and exit")
Set a version flag and version display string, replace the existing one if present.
Option * add_option(std::string option_name, T &option_description)
Add option with description but with no variable assignment or callback.
Definition: App.hpp:665
std::vector< std::string > aliases_
Alias names for the subcommand.
Definition: App.hpp:316
CLI11_NODISCARD std::vector< std::string > remaining_for_passthrough(bool recurse=false) const
This returns the missing options in a form ready for processing by another command line program...
CLI11_NODISCARD char ** ensure_utf8(char **argv)
Convert the contents of argv to UTF-8. Only does something on Windows, does nothing elsewhere...
App * parent_
A pointer to the parent if this is a subcommand.
Definition: App.hpp:310
App * silent(bool silence=true)
silence the subcommand from showing up in the processed list
Definition: App.hpp:427
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
std::vector< App_p > subcommands_
Storage for subcommand list.
Definition: App.hpp:244
CLI11_NODISCARD std::shared_ptr< ConfigBase > get_config_formatter_base() const
Access the config formatter as a configBase pointer.
Definition: App.hpp:1100
App * prefix_command(PrefixCommandMode mode)
Definition: App.hpp:512
std::string description_
Description of the current program/subcommand.
Definition: App.hpp:129
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition: App.hpp:187
App * allow_config_extras(ConfigExtrasMode mode)
ignore extras in config files
Definition: App.hpp:498
App * allow_non_standard_option_names(bool allowed=true)
allow non standard option names
Definition: App.hpp:433
CLI11_NODISCARD ExtrasMode get_allow_extras_mode() const
Get the mode of allow_extras.
Definition: App.hpp:1210
App * allow_extras(bool allow=true)
Remove the error when extras are left over on the command line.
Definition: App.hpp:403
std::set< Option * > need_options_
Definition: App.hpp:237
Option * version_ptr_
A pointer to a version flag if there is one.
Definition: App.hpp:199
App(std::string app_description="", std::string app_name="")
Create a new program. Pass in the same arguments as main(), along with a help string.
Definition: App.hpp:346
Option * get_config_ptr()
Get a pointer to the config option.
Definition: App.hpp:1255
std::vector< Option_p > options_
The list of options, stored locally.
Definition: App.hpp:174
CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable)
Helper function to disable one option group/subcommand when another is used.
CLI11_NODISCARD bool get_ignore_case() const
Check the status of ignore_case.
Definition: App.hpp:1155
std::set< Option * > exclude_options_
Definition: App.hpp:229
Option * add_option_no_stream(std::string option_name, AssignTo &variable, std::string option_description="")
Add option for assigning to a variable.
Definition: App.hpp:618
Option * add_option(std::string option_name)
Add option with no description or variable assignment.
Definition: App.hpp:657
bool _parse_single_config(const ConfigItem &item, std::size_t level=0)
Fill in a single config option.
CLI11_NODISCARD std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
CLI11_NODISCARD std::size_t count_all() const
CLI11_NODISCARD const std::vector< Option * > & parse_order() const
This gets a vector of pointers with the original parse order.
Definition: App.hpp:1303
CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement="")
Helper function to mark an option as deprecated.
App * preparse_callback(std::function< void(std::size_t)> pp_callback)
Definition: App.hpp:391
CLI11_NODISCARD App * get_subcommand_no_throw(std::string subcom) const noexcept
std::size_t require_option_min_
Minimum required options (not inheritable!)
Definition: App.hpp:304
void _trigger_pre_parse(std::size_t remaining_args)
Trigger the pre_parse callback if needed.
CLI11_NODISCARD const Option * get_help_ptr() const
Get a pointer to the help flag. (const)
Definition: App.hpp:1249
CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const
App * validate_optional_arguments(bool validate=true)
Set the subcommand to validate optional vector arguments before assigning.
Definition: App.hpp:475
bool remove_needs(Option *opt)
Removes an option from the needs list of this subcommand.
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:36
int exit(const Error &e, std::ostream &out=std::cout, std::ostream &err=std::cerr) const
Print a nice error message and return the exit code.
Definition: Option.hpp:259
CLI11_NODISCARD bool get_validate_positionals() const
Get the status of validating positionals.
Definition: App.hpp:1236
void _process_extras()
Throw an error if anything is left over and should not be.
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition: App.hpp:219
CLI11_NODISCARD config_extras_mode get_allow_config_extras() const
Get the status of allow extras.
Definition: App.hpp:1241
void _process_callbacks(CallbackPriority priority)
Process callbacks. Runs on all subcommands.
STL namespace.
App * ignore_case(bool value=true)
Ignore case. Subcommands inherit value.
bool _parse_positional(std::vector< std::string > &args, bool haltOnSubcommand)
void _move_to_missing(detail::Classifier val_type, const std::string &val)
Helper function to place extra values in the most appropriate position.
Extension of App to better manage groups of options.
Definition: App.hpp:1442
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
ConfigExtrasMode
enumeration of modes of how to deal with extras in config files
Definition: App.hpp:80
App * formatter(std::shared_ptr< FormatterBase > fmt)
Set the help formatter.
Definition: App.hpp:543
CLI11_NODISCARD Option * get_option_no_throw(std::string option_name) noexcept
Get an option by name (noexcept non-const version)
App(std::string app_description, std::string app_name, App *parent)
Special private constructor for subcommand.
Option * add_flag_callback(std::string flag_name, std::function< void(void)> function, std::string flag_description="")
Add option for callback that is triggered with a true flag and takes no arguments.
CLI11_NODISCARD bool get_disabled_by_default() const
Get the status of disabled by default.
Definition: App.hpp:1231
CLI11_NODISCARD detail::Classifier _recognize(const std::string &current, bool ignore_used_subcommands=true) const
Selects a Classifier enum based on the type of the current argument.
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
const Option * operator[](const std::string &option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition: App.hpp:1149
NameMatch
enumeration of matching possibilities
Definition: App.hpp:1292
ConfigBase ConfigTOML
the default Config is the TOML file format
Definition: ConfigFwd.hpp:193
App * callback(std::function< void()> app_callback)
Definition: App.hpp:366
std::function< std::string()> usage_callback_
This is a function that generates a usage to put after program/subcommand description in help output...
Definition: App.hpp:184
CLI11_NODISCARD std::vector< std::string > remaining(bool recurse=false) const
This returns the missing options from the current subcommand.
std::function< void()> parse_complete_callback_
This is a function that runs when parsing has finished.
Definition: App.hpp:161
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition: App.hpp:1189
T * add_option_group(std::string group_name, std::string group_description="")
creates an option group as part of the given app
Definition: App.hpp:774
bool lexical_cast(const std::string &input, T &output)
Integer conversion.
Definition: TypeTools.hpp:1123
The normal, detailed help.
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition: App.hpp:691
static auto parse_arg(App *app, Args &&...args) -> typename std::result_of< decltype(&App::_parse_arg)(App, Args...)>::type
Wrap _parse_short, perfectly forward arguments and return.
Definition: App.hpp:1536
App * allow_extras(ExtrasMode allow)
Remove the error when extras are left over on the command line.
Definition: App.hpp:409
bool remove_subcommand(App *subcom)
Removes a subcommand from the App. Takes a subcommand pointer. Returns true if found and removed...
config_extras_mode
enumeration of modes of how to deal with extras in config files
Definition: App.hpp:83
CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable)
Helper function to enable one option group/subcommand when another is used.
std::vector< const Option * > get_options(const std::function< bool(const Option *)> filter={}) const
Get the list of options (user facing function, so returns raw pointers), has optional filter function...
bool allow_non_standard_options_
indicator that the subcommand should allow non-standard option arguments, such as -single_dash_flag ...
Definition: App.hpp:289
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition: App.hpp:1267
Option * help_ptr_
A pointer to the help flag if there is one INHERITABLE.
Definition: App.hpp:193
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
std::string group_
The group membership INHERITABLE.
Definition: App.hpp:313
CLI11_NODISCARD bool get_allow_windows_style_options() const
Check the status of the allow windows style options.
Definition: App.hpp:1167
const Option * operator[](const char *option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition: App.hpp:1152
bool pre_parse_called_
Flag indicating that the pre_parse_callback has been triggered.
Definition: App.hpp:151
App * enabled_by_default(bool enable=true)
Definition: App.hpp:455
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition: App.hpp:421
CLI11_NODISCARD bool get_enabled_by_default() const
Get the status of disabled by default.
Definition: App.hpp:1234
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition: App.hpp:279
Option * add_flag_function(std::string flag_name, std::function< void(std::int64_t)> function, std::string flag_description="")
Add option for callback with an integer value.
CLI11_INLINE std::string simple(const App *app, const Error &e)
Printout a clean, simple message on error (the default in CLI11 1.5+)
OptionDefaults * option_defaults()
Get the OptionDefault object, to set option defaults.
Definition: App.hpp:564
#define CLI11_INLINE
Definition: Macros.hpp:176
App * allow_config_extras(config_extras_mode mode)
ignore extras in config files
Definition: App.hpp:492
CLI11_NODISCARD bool get_allow_extras() const
Get the status of allow extras.
Definition: App.hpp:1207
Option * help_all_ptr_
A pointer to the help all flag if there is one INHERITABLE.
Definition: App.hpp:196
Option * get_version_ptr()
Get a pointer to the version option.
Definition: App.hpp:1261
App * get_subcommand(const App *subcom) const
CLI11_NODISCARD bool parsed() const
Check to see if this subcommand was parsed, true only if received on command line.
Definition: App.hpp:561
CLI11_NODISCARD std::size_t remaining_size(bool recurse=false) const
This returns the number of remaining options, minus the – separator.
void _parse(std::vector< std::string > &args)
Internal parse function.
App * allow_subcommand_prefix_matching(bool allowed=true)
allow prefix matching for subcommands
Definition: App.hpp:439
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise...
Definition: App.hpp:260
PrefixCommandMode prefix_command_
If true, cease processing on an unrecognized option (implies allow_extras) INHERITABLE.
Definition: App.hpp:139
App * configurable(bool value=true)
Specify that the subcommand can be triggered by a config file.
Definition: App.hpp:534
bool fallthrough_
Definition: App.hpp:254
CLI11_NODISCARD Option * get_option(std::string option_name)
Get an option by name (non-const version)
Definition: App.hpp:1140
CLI11_NODISCARD bool get_disabled() const
Get the status of disabled.
Definition: App.hpp:1216
std::vector< std::string > results_t
Definition: Option.hpp:31
App * description(std::string app_description)
Set the description of the app.
Definition: App.hpp:1113
Option * add_option(std::string option_name, AssignTo &variable, std::string option_description="")
Add option for assigning to a variable.
Definition: App.hpp:594
ConfigExtrasMode allow_config_extras_
Definition: App.hpp:136
ExtrasMode
enumeration of modes of how to deal with command line extras
Definition: App.hpp:70
std::function< std::string()> footer_callback_
This is a function that generates a footer to put after all other options in help output...
Definition: App.hpp:190
static auto parse_subcommand(App *app, Args &&...args) -> typename std::result_of< decltype(&App::_parse_subcommand)(App, Args...)>::type
Wrap _parse_subcommand, perfectly forward arguments and return.
Definition: App.hpp:1543
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition: App.hpp:1252
AppFormatMode
Definition: FormatterFwd.hpp:32
Option * run_callback_for_default(bool value=true)
Definition: Option.hpp:439
App * require_subcommand()
The argumentless form of require subcommand requires 1 or more subcommands.
Definition: App.hpp:844
CLI11_NODISCARD std::string version() const
Displays a version string.
App * add_subcommand(App *subcom)
Add an existing subcommand to be a member of an option_group.
Definition: App.hpp:1472
CLI11_INLINE void retire_option(App *app, Option *opt)
Helper function to mark an option as retired.
CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
OptionDefaults option_defaults_
The default values for options, customizable and changeable INHERITABLE.
Definition: App.hpp:171
void _parse_stream(std::istream &input)
Internal function to parse a stream.
All errors derive from this one.
Definition: Error.hpp:73
CLI11_NODISCARD bool get_immediate_callback() const
Get the status of disabled.
Definition: App.hpp:1228
bool _add_flag_like_result(Option *op, const ConfigItem &item, const std::vector< std::string > &inputs)
store the results for a flag like option
missing_t missing_
Definition: App.hpp:216
App * allow_windows_style_options(bool value=true)
Definition: App.hpp:522
void _process_config_file()
Read and process a configuration file (main app only)
void _configure()
void _process_help_flags(CallbackPriority priority, bool trigger_help=false, bool trigger_all_help=false) const
CLI11_NODISCARD App * get_option_group(std::string group_name) const
Check to see if an option group is part of this App.
void add_options(Option *opt)
Add an existing option to the Option_group.
Definition: App.hpp:1464
App * prefix_command(bool is_prefix=true)
Definition: App.hpp:505
CLI11_NODISCARD bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Classifier
Definition: App.hpp:49
App * required(bool require=true)
Remove the error when extras are left over on the command line.
Definition: App.hpp:415
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer) ...
Definition: App.hpp:326
std::set< App * > need_subcommands_
Definition: App.hpp:233
App * usage(std::string usage_string)
Set usage.
Definition: App.hpp:1058
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition: App.hpp:1176
std::string name_
Subcommand name or program name (from parser if name is empty)
Definition: App.hpp:126
Definition: Option.hpp:216
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition: App.hpp:1110
typename std::enable_if< B, T >::type enable_if_t
Definition: TypeTools.hpp:47
CLI11_NODISCARD bool get_positionals_at_end() const
Check the status of the allow windows style options.
Definition: App.hpp:1170
App * alias(std::string app_name)
Set an alias for the app.
App * formatter_fn(std::function< std::string(const App *, std::string, AppFormatMode)> fmt)
Set the help formatter.
Definition: App.hpp:549
CLI11_NODISCARD App * _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept
Option * get_help_ptr()
Get a pointer to the help flag.
Definition: App.hpp:1246
bool silent_
Definition: App.hpp:286
Option * set_config(std::string option_name="", std::string default_filename="", const std::string &help_message="Read an ini file", bool config_required=false)
Set a configuration ini file option, or clear it if no name passed.
Option * type_size(int option_type_size)
Set a custom option size.
App & operator=(const App &)=delete
App * immediate_callback(bool immediate=true)
Set the subcommand callback to be executed immediately on subcommand completion.
bool allow_prefix_matching_
indicator to allow subcommands to match with prefix matching
Definition: App.hpp:292
App * footer(std::string footer_string)
Set footer.
Definition: App.hpp:1068
App * allow_config_extras(bool allow=true)
ignore extras in config files
Definition: App.hpp:481
App * require_subcommand(int value)
Definition: App.hpp:853
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition: App.hpp:1184
Option * config_ptr_
Pointer to the config option.
Definition: App.hpp:323
CLI11_NODISCARD std::shared_ptr< FormatterBase > get_formatter() const
Access the formatter.
Definition: App.hpp:1094
void parse(int argc, const char *const *argv)
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition: Option.hpp:430
CLI11_NODISCARD bool get_allow_non_standard_option_names() const
Get the status of allowing non standard option names.
Definition: App.hpp:1222
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
CallbackPriority
enumeration for the callback priority
Definition: Option.hpp:54
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition: App.hpp:1198
#define CLI11_NODISCARD
Definition: Macros.hpp:58
App * final_callback(std::function< void()> app_callback)
Definition: App.hpp:377
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition: App.hpp:250
This will only trigger for actual void type.
Definition: TypeTools.hpp:504
App * name(std::string app_name="")
Set a name for the app (empty will use parser to set the name)
CLI11_NODISCARD bool get_subcommand_fallthrough() const
Check the status of subcommand fallthrough.
Definition: App.hpp:1164
std::function< void(std::size_t)> pre_parse_callback_
This is a function that runs prior to the start of parsing.
Definition: App.hpp:158
bool validate_optional_arguments_
If set to true optional vector arguments are validated before assigning INHERITABLE.
Definition: App.hpp:282
App * add_subcommand(std::string subcommand_name="", std::string subcommand_description="")
Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag.
CLI11_NODISCARD bool get_silent() const
Get the status of silence.
Definition: App.hpp:1219
CLI11_NODISCARD bool get_ignore_underscore() const
Check the status of ignore_underscore.
Definition: App.hpp:1158
std::size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE...
Definition: App.hpp:301
CLI11_NODISCARD PrefixCommandMode get_prefix_command_mode() const
Get the prefix command status.
Definition: App.hpp:1204
std::vector< std::pair< detail::Classifier, std::string >> missing_t
Definition: App.hpp:211
Option * add_option(std::string option_name, callback_t option_callback, std::string option_description="", bool defaulted=false, std::function< std::string()> func={})
CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used=true) const
Check to see if a subcommand is valid. Give up immediately if subcommand max has been reached...
Option * add_option_function(std::string option_name, const std::function< void(const ArgType &)> &func, std::string option_description="")
Add option for a callback of a specific type.
Definition: App.hpp:636
CLI11_NODISCARD bool check_name(std::string name_to_check) const
CLI11_NODISCARD bool get_prefix_command() const
Get the prefix command status.
Definition: App.hpp:1201
App * excludes(App *app)
Sets excluded subcommands for the subcommand.
Definition: App.hpp:1008
App * validate_positionals(bool validate=true)
Set the subcommand to validate positional arguments before assigning.
Definition: App.hpp:469
bool remove_option(Option *opt)
Removes an option from the App. Takes an option pointer. Returns true if found and removed...
App * positionals_at_end(bool value=true)
Specify that the positional arguments are only at the end of the sequence.
Definition: App.hpp:528
App * ignore_underscore(bool value=true)
Ignore underscore. Subcommands inherit value.
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition: Option.hpp:781
virtual ~App()=default
virtual destructor
Option * add_flag(std::string flag_name, std::vector< T > &flag_results, std::string flag_description="")
Vector version to capture multiple flags.
Definition: App.hpp:727
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition: App.hpp:976
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
CLI11_NODISCARD bool get_fallthrough() const
Check the status of fallthrough.
Definition: App.hpp:1161
App * needs(Option *opt)
Definition: App.hpp:1023
App * require_option(int value)
Definition: App.hpp:882
CLI11_NODISCARD const Option * get_option(std::string option_name) const
Get an option by name.
Definition: App.hpp:1131
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition: App.hpp:1192
CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const noexcept
Check with name instead of pointer to see if subcommand was selected.
Definition: App.hpp:993
Holds values to load into Options.
Definition: ConfigFwd.hpp:29
Definition: FormatterFwd.hpp:194
std::vector< App * > parsed_subcommands_
This is a list of the subcommands collected, in order.
Definition: App.hpp:222
std::shared_ptr< FormatterBase > formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer) ...
Definition: App.hpp:202
App * subcommand_fallthrough(bool value=true)
Set subcommand fallthrough, set to true so that subcommands on parents are recognized.
Definition: App.hpp:909
std::function< bool(const results_t &)> callback_t
callback function definition
Definition: Option.hpp:33
ExtrasMode allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition: App.hpp:132
CRTP * always_capture_default(bool value=true)
Definition: Option.hpp:126
App * excludes(Option *opt)
Sets excluded options for the subcommand.
Definition: App.hpp:999
CLI11_MODULE_INLINE constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:39
bool ignore_case_
If true, the program name is not case-sensitive INHERITABLE.
Definition: App.hpp:247
App * footer(std::function< std::string()> footer_function)
Set footer.
Definition: App.hpp:1073
Option * add_flag(std::string flag_name, T &flag_result, std::string flag_description="")
Definition: App.hpp:711
CLI11_NODISCARD bool get_allow_subcommand_prefix_matching() const
Get the status of allowing prefix matching for subcommands.
Definition: App.hpp:1225
CLI11_NODISCARD bool get_validate_optional_arguments() const
Get the status of validating optional vector arguments.
Definition: App.hpp:1238
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition: App.hpp:1273
bool required_
If set to true the subcommand is required to be processed and used, ignored for main app...
Definition: App.hpp:145
bool got_subcommand(const App *subcom) const
Check to see if given subcommand was selected.
Definition: App.hpp:987
Creates a command line program, with very few defaults.
Definition: App.hpp:115
App * group(std::string group_name)
Changes the group membership.
Definition: App.hpp:838
just get all the passed argument regardless
Option * set_help_flag(std::string flag_name="", const std::string &help_description="")
Set a help flag, replace the existing one if present.
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
std::function< std::string(const App *, const Error &e)> failure_message_
The error message printing function INHERITABLE.
Definition: App.hpp:205
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition: App.hpp:1195
bool configurable_
if set to true the subcommand can be triggered via configuration files INHERITABLE ...
Definition: App.hpp:276
CLI11_NODISCARD std::string config_to_str(bool default_also=false, bool write_description=false) const
Definition: App.hpp:1079
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition: ConfigFwd.hpp:90
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type, bool local_processing_only)
App * require_subcommand(std::size_t min, std::size_t max)
Definition: App.hpp:866
std::string usage_
Usage to put after program/subcommand description in the help output INHERITABLE. ...
Definition: App.hpp:181
CLI11_NODISCARD bool get_configurable() const
Check the status of the allow windows style options.
Definition: App.hpp:1173
void parse_from_stream(std::istream &input)
bool immediate_callback_
Definition: App.hpp:155
bool subcommand_fallthrough_
Allow subcommands to fallthrough, so that parent commands can trigger other subcommands after subcomm...
Definition: App.hpp:257
Option * default_flag_modifiers(Option *opt)
helper functions for adding in appropriate flag modifiers for add_flag
Definition: App.hpp:98
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition: App.hpp:225
App * parse_complete_callback(std::function< void()> pc_callback)
Definition: App.hpp:384
CLI11_NODISCARD std::shared_ptr< Config > get_config_formatter() const
Access the config formatter.
Definition: App.hpp:1097
sum all the arguments together if numerical or concatenate directly without delimiter ...
Option * expected(int value)
Set the number of expected arguments.
bool valid_alias_name_string(const std::string &str)
Verify an app name.
Definition: StringTools.hpp:167
App * usage(std::function< std::string()> usage_function)
Set usage.
Definition: App.hpp:1063
std::uint32_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition: App.hpp:295
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value) ...
Definition: Option.hpp:810
std::shared_ptr< App > App_p
Definition: App.hpp:92
bool positionals_at_end_
specify that positional arguments come at the end of the argument sequence not inheritable ...
Definition: App.hpp:268
Option * add_option(Option *opt)
Add an existing option to the Option_group.
Definition: App.hpp:1456
App * disabled_by_default(bool disable=true)
Set the subcommand to be disabled by default, so on clear(), at the start of each parse it is disable...
Definition: App.hpp:444
void _process()
Process callbacks and such.
App * fallthrough(bool value=true)
Definition: App.hpp:903
Option_group(std::string group_description, std::string group_name, App *parent)
Definition: App.hpp:1444
std::size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable)
Definition: App.hpp:307
void _parse_config(const std::vector< ConfigItem > &args)
virtual void pre_callback()
Definition: App.hpp:925
Option * set_help_all_flag(std::string help_name="", const std::string &help_description="")
Set a help all flag, replaced the existing one if present.
void add_options(Option *opt, Args...args)
Add a bunch of options to the group.
Definition: App.hpp:1466
std::function< void()> final_callback_
This is a function that runs when all processing has completed.
Definition: App.hpp:164