CLI11 2.7.1
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
App.hpp
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
36namespace 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
48namespace detail {
49enum class Classifier : std::uint8_t {
50 NONE,
51 POSITIONAL_MARK,
52 SHORT,
53 LONG,
54 WINDOWS_STYLE,
55 SUBCOMMAND,
56 SUBCOMMAND_TERMINATOR
57};
58struct AppFriend;
59} // namespace detail
60
61namespace FailureMessage {
63CLI11_INLINE std::string simple(const App *app, const Error &e);
64
66CLI11_INLINE std::string help(const App *app, const Error &e);
67} // namespace FailureMessage
68
70enum class ExtrasMode : std::uint8_t {
71 Error = 0,
72 ErrorImmediately,
73 Ignore,
74 AssumeSingleArgument,
75 AssumeMultipleArguments,
76 Capture
77};
78
80enum class ConfigExtrasMode : std::uint8_t { Error = 0, Ignore, IgnoreAll, Capture };
81
83enum class config_extras_mode : std::uint8_t { error = 0, ignore, ignore_all, capture };
84
89enum class PrefixCommandMode : std::uint8_t { Off = 0, SeparatorOnly = 1, On = 2, PositionalOnly = 3 };
90
91class App;
92
93using App_p = std::shared_ptr<App>;
94
95namespace detail {
97
98template <typename T, enable_if_t<!std::is_integral<T>::value || (sizeof(T) <= 1U), detail::enabler> = detail::dummy>
99Option *default_flag_modifiers(Option *opt) {
100 return opt->always_capture_default();
101}
102
104template <typename T, enable_if_t<std::is_integral<T>::value && (sizeof(T) > 1U), detail::enabler> = detail::dummy>
105Option *default_flag_modifiers(Option *opt) {
106 return opt->multi_option_policy(MultiOptionPolicy::Sum)->default_str("0")->force_callback();
107}
108
109} // namespace detail
110
111class Option_group;
113
115class 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
132 ExtrasMode allow_extras_{ExtrasMode::Error};
133
136 ConfigExtrasMode allow_config_extras_{ConfigExtrasMode::Ignore};
137
139 PrefixCommandMode prefix_command_{PrefixCommandMode::Off};
140
143
145 bool required_{false};
146
148 bool disabled_{false};
149
151 bool pre_parse_called_{false};
152
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
196 Option *help_all_ptr_{nullptr};
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
216 missing_t missing_{};
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
251
254 bool fallthrough_{false};
255
258
261#ifdef _WIN32
262 true
263#else
264 false
265#endif
266 };
267
269
270 enum class startup_mode : std::uint8_t { stable, enabled, disabled };
273 startup_mode default_startup{startup_mode::stable};
274
276 bool configurable_{false};
277
280
283
286 bool silent_{false};
287
290
293
295 std::uint32_t parsed_{0U};
296
299
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
332 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) {
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) {
404 allow_extras_ = allow ? ExtrasMode::Capture : ExtrasMode::Error;
405 return this;
406 }
407
409 App *allow_extras(ExtrasMode allow) {
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) {
435 return this;
436 }
437
439 App *allow_subcommand_prefix_matching(bool allowed = true) {
440 allow_prefix_matching_ = allowed;
441 return this;
442 }
443
444 App *disabled_by_default(bool disable = true) {
445 if(disable) {
446 default_startup = startup_mode::disabled;
447 } else {
448 default_startup = (default_startup == startup_mode::enabled) ? startup_mode::enabled : startup_mode::stable;
449 }
450 return this;
451 }
452
455 App *enabled_by_default(bool enable = true) {
456 if(enable) {
457 default_startup = startup_mode::enabled;
458 } else {
460 (default_startup == startup_mode::disabled) ? startup_mode::disabled : startup_mode::stable;
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) {
477 return this;
478 }
479
481 App *allow_config_extras(bool allow = true) {
482 if(allow) {
483 allow_config_extras_ = ConfigExtrasMode::Capture;
484 allow_extras_ = ExtrasMode::Capture;
485 } else {
486 allow_config_extras_ = ConfigExtrasMode::Error;
487 }
488 return this;
489 }
490
492 App *allow_config_extras(config_extras_mode mode) {
493 allow_config_extras_ = static_cast<ConfigExtrasMode>(mode);
494 return this;
495 }
496
498 App *allow_config_extras(ConfigExtrasMode mode) {
500 return this;
501 }
502
507 App *prefix_command(bool is_prefix = true) {
508 prefix_command_ = is_prefix ? PrefixCommandMode::On : PrefixCommandMode::Off;
509 return this;
510 }
511
513 App *prefix_command(PrefixCommandMode mode) {
514 prefix_command_ = mode;
515 return this;
516 }
517
519 App *ignore_case(bool value = true);
520
523 App *allow_windows_style_options(bool value = true) {
525 return this;
526 }
527
529 App *positionals_at_end(bool value = true) {
530 positionals_at_end_ = value;
531 return this;
532 }
533
535 App *configurable(bool value = true) {
536 configurable_ = value;
537 return this;
538 }
539
541 App *ignore_underscore(bool value = true);
542
544 App *formatter(std::shared_ptr<FormatterBase> fmt) {
545 formatter_ = std::move(fmt);
546 return this;
547 }
548
550 App *formatter_fn(std::function<std::string(const App *, std::string, AppFormatMode)> fmt) {
551 formatter_ = std::make_shared<FormatterLambda>(fmt);
552 return this;
553 }
554
556 App *config_formatter(std::shared_ptr<Config> fmt) {
557 config_formatter_ = std::move(fmt);
558 return this;
559 }
560
562 CLI11_NODISCARD bool parsed() const { return parsed_ > 0; }
563
566
570
585 Option *add_option(std::string option_name,
586 callback_t option_callback,
587 std::string option_description = "",
588 bool defaulted = false,
589 std::function<std::string()> func = {});
590
592 template <typename AssignTo,
593 typename ConvertTo = AssignTo,
594 enable_if_t<!std::is_const<ConvertTo>::value, detail::enabler> = detail::dummy>
595 Option *add_option(std::string option_name,
596 AssignTo &variable,
597 std::string option_description = "") {
598
599 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
600 return detail::lexical_conversion<AssignTo, ConvertTo>(res, variable);
601 };
602
603 Option *opt = add_option(option_name, fun, option_description, false, [&variable]() {
604 return CLI::detail::checked_to_string<AssignTo, ConvertTo>(variable);
605 });
606 opt->type_name(detail::type_name<ConvertTo>());
607 // these must be actual lvalues since (std::max) sometimes is defined in terms of references and references
608 // to structs used in the evaluation can be temporary so that would cause issues.
609 auto Tcount = detail::type_count<AssignTo>::value;
610 auto XCcount = detail::type_count<ConvertTo>::value;
611 opt->type_size(detail::type_count_min<ConvertTo>::value, (std::max)(Tcount, XCcount));
612 opt->expected(detail::expected_count<ConvertTo>::value);
614 return opt;
615 }
616
618 template <typename AssignTo, enable_if_t<!std::is_const<AssignTo>::value, detail::enabler> = detail::dummy>
619 Option *add_option_no_stream(std::string option_name,
620 AssignTo &variable,
621 std::string option_description = "") {
622
623 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
624 return detail::lexical_conversion<AssignTo, AssignTo>(res, variable);
625 };
626
627 Option *opt = add_option(option_name, fun, option_description, false, []() { return std::string{}; });
628 opt->type_name(detail::type_name<AssignTo>());
629 opt->type_size(detail::type_count_min<AssignTo>::value, detail::type_count<AssignTo>::value);
630 opt->expected(detail::expected_count<AssignTo>::value);
632 return opt;
633 }
634
636 template <typename ArgType>
637 Option *add_option_function(std::string option_name,
638 const std::function<void(const ArgType &)> &func,
639 std::string option_description = "") {
640
641 auto fun = [func](const CLI::results_t &res) {
642 ArgType variable;
643 bool result = detail::lexical_conversion<ArgType, ArgType>(res, variable);
644 if(result) {
645 func(variable);
646 }
647 return result;
648 };
649
650 Option *opt = add_option(option_name, std::move(fun), option_description, false);
651 opt->type_name(detail::type_name<ArgType>());
652 opt->type_size(detail::type_count_min<ArgType>::value, detail::type_count<ArgType>::value);
653 opt->expected(detail::expected_count<ArgType>::value);
654 return opt;
655 }
656
658 Option *add_option(std::string option_name) {
659 return add_option(option_name, CLI::callback_t{}, std::string{}, false);
660 }
661
663 template <typename T,
664 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
665 detail::dummy>
666 Option *add_option(std::string option_name, T &option_description) {
667 return add_option(option_name, CLI::callback_t(), option_description, false);
668 }
669
671 Option *set_help_flag(std::string flag_name = "", const std::string &help_description = "");
672
674 Option *set_help_all_flag(std::string help_name = "", const std::string &help_description = "");
675
677 Option *set_version_flag(std::string flag_name = "",
678 const std::string &versionString = "",
679 const std::string &version_help = "Display program version information and exit");
680
682 Option *set_version_flag(std::string flag_name,
683 std::function<std::string()> vfunc,
684 const std::string &version_help = "Display program version information and exit");
685
686 private:
688 Option *_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description);
689
690 public:
692 Option *add_flag(std::string flag_name) { return _add_flag_internal(flag_name, CLI::callback_t(), std::string{}); }
693
697 template <typename T,
698 enable_if_t<(std::is_const<typename std::remove_reference<T>::type>::value ||
699 std::is_rvalue_reference<T &&>::value) &&
700 std::is_constructible<std::string, typename std::remove_reference<T>::type>::value,
701 detail::enabler> = detail::dummy>
702 Option *add_flag(std::string flag_name, T &&flag_description) {
703 return _add_flag_internal(flag_name, CLI::callback_t(), std::forward<T>(flag_description));
704 }
705
708 template <typename T,
709 enable_if_t<!detail::is_mutable_container<T>::value && !std::is_const<T>::value &&
710 !std::is_constructible<std::function<void(int)>, T>::value,
711 detail::enabler> = detail::dummy>
712 Option *add_flag(std::string flag_name,
713 T &flag_result,
714 std::string flag_description = "") {
715
716 CLI::callback_t fun = [&flag_result](const CLI::results_t &res) {
717 using CLI::detail::lexical_cast;
718 return lexical_cast(res[0], flag_result);
719 };
720 auto *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
721 return detail::default_flag_modifiers<T>(opt);
722 }
723
725 template <typename T,
726 enable_if_t<!std::is_assignable<std::function<void(std::int64_t)> &, T>::value, detail::enabler> =
727 detail::dummy>
728 Option *add_flag(std::string flag_name,
729 std::vector<T> &flag_results,
730 std::string flag_description = "") {
731 CLI::callback_t fun = [&flag_results](const CLI::results_t &res) {
732 bool retval = true;
733 for(const auto &elem : res) {
734 using CLI::detail::lexical_cast;
735 flag_results.emplace_back();
736 retval &= lexical_cast(elem, flag_results.back());
737 }
738 return retval;
739 };
740 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
741 ->multi_option_policy(MultiOptionPolicy::TakeAll)
743 }
744
746 Option *add_flag_callback(std::string flag_name,
747 std::function<void(void)> function,
748 std::string flag_description = "");
749
751 Option *add_flag_function(std::string flag_name,
752 std::function<void(std::int64_t)> function,
753 std::string flag_description = "");
754
755#ifdef CLI11_CPP14
757 Option *add_flag(std::string flag_name,
758 std::function<void(std::int64_t)> function,
759 std::string flag_description = "") {
760 return add_flag_function(std::move(flag_name), std::move(function), std::move(flag_description));
761 }
762#endif
763
765 Option *set_config(std::string option_name = "",
766 std::string default_filename = "",
767 const std::string &help_message = "Read an ini file",
768 bool config_required = false);
769
771 bool remove_option(Option *opt);
772
774 template <typename T = Option_group>
775 T *add_option_group(std::string group_name, std::string group_description = "") {
776 if(!detail::valid_alias_name_string(group_name)) {
777 throw IncorrectConstruction("option group names may not contain newlines or null characters");
778 }
779 auto option_group = std::make_shared<T>(std::move(group_description), group_name, this);
780 option_group->fallthrough(false);
781 auto *ptr = option_group.get();
782 // move to App_p for overload resolution on older gcc versions
783 App_p app_ptr = std::static_pointer_cast<App>(option_group);
784 // don't inherit the footer in option groups and clear the help flag by default
785 app_ptr->footer_ = "";
786 app_ptr->set_help_flag();
787 add_subcommand(std::move(app_ptr));
788 return ptr;
789 }
790
794
796 App *add_subcommand(std::string subcommand_name = "", std::string subcommand_description = "");
797
799 App *add_subcommand(CLI::App_p subcom);
800
802 bool remove_subcommand(App *subcom);
803
806 App *get_subcommand(const App *subcom) const;
807
809 CLI11_NODISCARD App *get_subcommand(std::string subcom) const;
810
813 CLI11_NODISCARD App *get_subcommand_no_throw(std::string subcom) const noexcept;
814
816 CLI11_NODISCARD App *get_subcommand(int index = 0) const;
817
819 CLI::App_p get_subcommand_ptr(App *subcom) const;
820
822 CLI11_NODISCARD CLI::App_p get_subcommand_ptr(std::string subcom) const;
823
825 CLI11_NODISCARD CLI::App_p get_subcommand_ptr(int index = 0) const;
826
828 CLI11_NODISCARD App *get_option_group(std::string group_name) const;
829
833 CLI11_NODISCARD std::size_t count() const { return parsed_; }
834
837 CLI11_NODISCARD std::size_t count_all() const;
838
840 App *group(std::string group_name) {
841 group_ = std::move(group_name);
842 return this;
843 }
844
849 return this;
850 }
851
856 if(value < 0) {
858 require_subcommand_max_ = static_cast<std::size_t>(-value);
859 } else {
860 require_subcommand_min_ = static_cast<std::size_t>(value);
861 require_subcommand_max_ = static_cast<std::size_t>(value);
862 }
863 return this;
864 }
865
868 App *require_subcommand(std::size_t min, std::size_t max) {
871 return this;
872 }
873
878 return this;
879 }
880
884 App *require_option(int value) {
885 if(value < 0) {
887 require_option_max_ = static_cast<std::size_t>(-value);
888 } else {
889 require_option_min_ = static_cast<std::size_t>(value);
890 require_option_max_ = static_cast<std::size_t>(value);
891 }
892 return this;
893 }
894
897 App *require_option(std::size_t min, std::size_t max) {
900 return this;
901 }
902
905 App *fallthrough(bool value = true) {
906 fallthrough_ = value;
907 return this;
908 }
909
911 App *subcommand_fallthrough(bool value = true) {
913 return this;
914 }
915
918 explicit operator bool() const { return parsed_ > 0; }
919
923
927 virtual void pre_callback() {}
928
932 //
934 void clear();
935
938 void parse(int argc, const char *const *argv);
939 void parse(int argc, const wchar_t *const *argv);
940
941 private:
942 template <class CharT> void parse_char_t(int argc, const CharT *const *argv);
943
944 public:
949 void parse(std::string commandline, bool program_name_included = false);
950 void parse(std::wstring commandline, bool program_name_included = false);
951
954 void parse(std::vector<std::string> &args);
955
957 void parse(std::vector<std::string> &&args);
958
959 void parse_from_stream(std::istream &input);
960
962 void failure_message(std::function<std::string(const App *, const Error &e)> function) {
963 failure_message_ = std::move(function);
964 }
965
967 int exit(const Error &e, std::ostream &out = std::cout, std::ostream &err = std::cerr) const;
968
972
974 CLI11_NODISCARD std::size_t count(std::string option_name) const { return get_option(option_name)->count(); }
975
978 CLI11_NODISCARD std::vector<App *> get_subcommands() const { return parsed_subcommands_; }
979
982 std::vector<const App *> get_subcommands(const std::function<bool(const App *)> &filter) const;
983
986 std::vector<App *> get_subcommands(const std::function<bool(App *)> &filter);
987
989 bool got_subcommand(const App *subcom) const {
990 // get subcom needed to verify that this was a real subcommand
991 return get_subcommand(subcom)->parsed_ > 0;
992 }
993
995 CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const noexcept {
996 App *sub = get_subcommand_no_throw(subcommand_name);
997 return (sub != nullptr) ? (sub->parsed_ > 0) : false;
998 }
999
1001 App *excludes(Option *opt) {
1002 if(opt == nullptr) {
1003 throw OptionNotFound("nullptr passed");
1004 }
1005 exclude_options_.insert(opt);
1006 return this;
1007 }
1008
1011 if(app == nullptr) {
1012 throw OptionNotFound("nullptr passed");
1013 }
1014 if(app == this) {
1015 throw OptionNotFound("cannot self reference in excludes");
1016 }
1017 auto res = exclude_subcommands_.insert(app);
1018 // subcommand exclusion should be symmetric
1019 if(res.second) {
1020 app->exclude_subcommands_.insert(this);
1021 }
1022 return this;
1023 }
1024
1025 App *needs(Option *opt) {
1026 if(opt == nullptr) {
1027 throw OptionNotFound("nullptr passed");
1028 }
1029 need_options_.insert(opt);
1030 return this;
1031 }
1032
1033 App *needs(App *app) {
1034 if(app == nullptr) {
1035 throw OptionNotFound("nullptr passed");
1036 }
1037 if(app == this) {
1038 throw OptionNotFound("cannot self reference in needs");
1039 }
1040 need_subcommands_.insert(app);
1041 return this;
1042 }
1043
1045 bool remove_excludes(Option *opt);
1046
1048 bool remove_excludes(App *app);
1049
1051 bool remove_needs(Option *opt);
1052
1054 bool remove_needs(App *app);
1058
1060 App *usage(std::string usage_string) {
1061 usage_ = std::move(usage_string);
1062 return this;
1063 }
1064
1065 App *usage(std::function<std::string()> usage_function) {
1066 usage_callback_ = std::move(usage_function);
1067 return this;
1068 }
1069
1070 App *footer(std::string footer_string) {
1071 footer_ = std::move(footer_string);
1072 return this;
1073 }
1074
1075 App *footer(std::function<std::string()> footer_function) {
1076 footer_callback_ = std::move(footer_function);
1077 return this;
1078 }
1079
1081 CLI11_NODISCARD std::string config_to_str() const { return config_to_str(ConfigOutputMode::Active, false); }
1082
1084 CLI11_NODISCARD std::string config_to_str(ConfigOutputMode mode, bool write_description = false) const {
1085 return config_formatter_->to_config(this, mode, write_description, "");
1086 }
1087
1091 CLI11_NODISCARD std::string config_to_str(bool default_also, bool write_description = false) const {
1092 return config_to_str(default_also ? ConfigOutputMode::AllDefaults : ConfigOutputMode::Active,
1093 write_description);
1094 }
1095
1098 CLI11_NODISCARD std::string help(std::string prev = "", AppFormatMode mode = AppFormatMode::Normal) const;
1099
1101 CLI11_NODISCARD std::string version() const;
1105
1107 CLI11_NODISCARD std::shared_ptr<FormatterBase> get_formatter() const { return formatter_; }
1108
1110 CLI11_NODISCARD std::shared_ptr<Config> get_config_formatter() const { return config_formatter_; }
1111
1113 CLI11_NODISCARD std::shared_ptr<ConfigBase> get_config_formatter_base() const {
1114 // This is safer as a dynamic_cast if we have RTTI, as Config -> ConfigBase
1115#if CLI11_USE_STATIC_RTTI == 0
1116 return std::dynamic_pointer_cast<ConfigBase>(config_formatter_);
1117#else
1118 return std::static_pointer_cast<ConfigBase>(config_formatter_);
1119#endif
1120 }
1121
1123 CLI11_NODISCARD std::string get_description() const { return description_; }
1124
1126 App *description(std::string app_description) {
1127 description_ = std::move(app_description);
1128 return this;
1129 }
1130
1132 std::vector<const Option *> get_options(const std::function<bool(const Option *)> filter = {}) const;
1133
1135 std::vector<Option *> get_options(const std::function<bool(Option *)> filter = {});
1136
1138 CLI11_NODISCARD Option *get_option_no_throw(std::string option_name) noexcept;
1139
1141 CLI11_NODISCARD const Option *get_option_no_throw(std::string option_name) const noexcept;
1142
1144 CLI11_NODISCARD const Option *get_option(std::string option_name) const;
1145
1147 CLI11_NODISCARD Option *get_option(std::string option_name);
1148
1150 const Option *operator[](const std::string &option_name) const { return get_option(option_name); }
1151
1153 const Option *operator[](const char *option_name) const { return get_option(option_name); }
1154
1156 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
1157
1159 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
1160
1162 CLI11_NODISCARD bool get_fallthrough() const { return fallthrough_; }
1163
1165 CLI11_NODISCARD bool get_subcommand_fallthrough() const { return subcommand_fallthrough_; }
1166
1168 CLI11_NODISCARD bool get_allow_windows_style_options() const { return allow_windows_style_options_; }
1169
1171 CLI11_NODISCARD bool get_positionals_at_end() const { return positionals_at_end_; }
1172
1174 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
1175
1177 CLI11_NODISCARD const std::string &get_group() const { return group_; }
1178
1180 CLI11_NODISCARD std::string get_usage() const {
1181 return (usage_callback_) ? usage_callback_() + '\n' + usage_ : usage_;
1182 }
1183
1185 CLI11_NODISCARD std::string get_footer() const {
1186 return (footer_callback_) ? footer_callback_() + '\n' + footer_ : footer_;
1187 }
1188
1190 CLI11_NODISCARD std::size_t get_require_subcommand_min() const { return require_subcommand_min_; }
1191
1193 CLI11_NODISCARD std::size_t get_require_subcommand_max() const { return require_subcommand_max_; }
1194
1196 CLI11_NODISCARD std::size_t get_require_option_min() const { return require_option_min_; }
1197
1199 CLI11_NODISCARD std::size_t get_require_option_max() const { return require_option_max_; }
1200
1202 CLI11_NODISCARD bool get_prefix_command() const { return static_cast<bool>(prefix_command_); }
1203
1205 CLI11_NODISCARD PrefixCommandMode get_prefix_command_mode() const { return prefix_command_; }
1206
1208 CLI11_NODISCARD bool get_allow_extras() const { return allow_extras_ > ExtrasMode::Ignore; }
1209
1211 CLI11_NODISCARD ExtrasMode get_allow_extras_mode() const { return allow_extras_; }
1212
1214 CLI11_NODISCARD bool get_required() const { return required_; }
1215
1217 CLI11_NODISCARD bool get_disabled() const { return disabled_; }
1218
1220 CLI11_NODISCARD bool get_silent() const { return silent_; }
1221
1224
1226 CLI11_NODISCARD bool get_allow_subcommand_prefix_matching() const { return allow_prefix_matching_; }
1227
1229 CLI11_NODISCARD bool get_immediate_callback() const { return immediate_callback_; }
1230
1232 CLI11_NODISCARD bool get_disabled_by_default() const { return (default_startup == startup_mode::disabled); }
1233
1235 CLI11_NODISCARD bool get_enabled_by_default() const { return (default_startup == startup_mode::enabled); }
1237 CLI11_NODISCARD bool get_validate_positionals() const { return validate_positionals_; }
1239 CLI11_NODISCARD bool get_validate_optional_arguments() const { return validate_optional_arguments_; }
1240
1242 CLI11_NODISCARD config_extras_mode get_allow_config_extras() const {
1243 return static_cast<config_extras_mode>(allow_config_extras_);
1244 }
1245
1247 Option *get_help_ptr() { return help_ptr_; }
1248
1250 CLI11_NODISCARD const Option *get_help_ptr() const { return help_ptr_; }
1251
1253 CLI11_NODISCARD const Option *get_help_all_ptr() const { return help_all_ptr_; }
1254
1256 Option *get_config_ptr() { return config_ptr_; }
1257
1259 CLI11_NODISCARD const Option *get_config_ptr() const { return config_ptr_; }
1260
1262 Option *get_version_ptr() { return version_ptr_; }
1263
1265 CLI11_NODISCARD const Option *get_version_ptr() const { return version_ptr_; }
1266
1268 App *get_parent() { return parent_; }
1269
1271 CLI11_NODISCARD const App *get_parent() const { return parent_; }
1272
1274 CLI11_NODISCARD const std::string &get_name() const { return name_; }
1275
1277 CLI11_NODISCARD const std::vector<std::string> &get_aliases() const { return aliases_; }
1278
1281 aliases_.clear();
1282 return this;
1283 }
1284
1286 CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
1287
1290 CLI11_NODISCARD bool check_name(std::string name_to_check) const;
1291
1293 enum class NameMatch : std::uint8_t { none = 0, exact = 1, prefix = 2 };
1294
1298 CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
1299
1301 CLI11_NODISCARD std::vector<std::string> get_groups() const;
1302
1304 CLI11_NODISCARD const std::vector<Option *> &parse_order() const { return parse_order_; }
1305
1307 CLI11_NODISCARD std::vector<std::string> remaining(bool recurse = false) const;
1308
1310 CLI11_NODISCARD std::vector<std::string> remaining_for_passthrough(bool recurse = false) const;
1311
1313 CLI11_NODISCARD std::size_t remaining_size(bool recurse = false) const;
1314
1316
1317 protected:
1322 void _validate() const;
1323
1327 void _configure();
1328
1330 void run_callback(bool final_mode = false, bool suppress_final_callback = false);
1331
1333 CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used = true) const;
1334
1336 CLI11_NODISCARD detail::Classifier _recognize(const std::string &current,
1337 bool ignore_used_subcommands = true) const;
1338
1339 // The parse function is now broken into several parts, and part of process
1340
1342 void _process_config_file();
1343
1345 bool _process_config_file(const std::string &config_file, bool throw_error);
1346
1348 void _process_env();
1349
1351 void _process_callbacks(CallbackPriority priority);
1352
1356 void _process_help_flags(CallbackPriority priority, bool trigger_help = false, bool trigger_all_help = false) const;
1357
1362 void _process_completion_callbacks(bool with_help_flags);
1363
1365 void _process_requirements();
1366
1368 void _process();
1369
1371 void _process_extras();
1372
1374 void increment_parsed();
1375
1378 void _parse_setup();
1379
1381 void _parse(std::vector<std::string> &args);
1382
1384 void _parse(std::vector<std::string> &&args);
1385
1387 void _parse_stream(std::istream &input);
1388
1393 void _parse_config(const std::vector<ConfigItem> &args);
1394
1396 bool _parse_single_config(const ConfigItem &item, std::size_t level = 0);
1397
1399 bool _add_flag_like_result(Option *op, const ConfigItem &item, const std::vector<std::string> &inputs);
1400
1403 bool _parse_single(std::vector<std::string> &args, bool &positional_only);
1404
1406 CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only = false) const;
1407
1409 CLI11_NODISCARD bool _has_remaining_positionals() const;
1410
1414 bool _parse_positional(std::vector<std::string> &args, bool haltOnSubcommand);
1415
1418 CLI11_NODISCARD App *
1419 _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept;
1420
1425 bool _parse_subcommand(std::vector<std::string> &args);
1426
1430 bool _parse_arg(std::vector<std::string> &args, detail::Classifier current_type, bool local_processing_only);
1431
1433 void _trigger_pre_parse(std::size_t remaining_args);
1434
1436 CLI11_NODISCARD App *_get_fallthrough_parent() noexcept;
1437
1439 CLI11_NODISCARD const App *_get_fallthrough_parent() const noexcept;
1440
1442 CLI11_NODISCARD const std::string &_compare_subcommand_names(const App &subcom, const App &base) const;
1443
1445 void _move_to_missing(detail::Classifier val_type, const std::string &val);
1446
1447 public:
1449 void _move_option(Option *opt, App *app);
1450}; // namespace CLI
1451
1453class Option_group : public App {
1454 public:
1455 Option_group(std::string group_description, std::string group_name, App *parent)
1456 : App(std::move(group_description), "", parent) {
1457 group(group_name);
1458 // option groups should have automatic fallthrough
1459 if(group_name.empty() || group_name.front() == '+') {
1460 // help will not be used by default in these contexts
1461 set_help_flag("");
1463 }
1464 }
1465 using App::add_option;
1467 Option *add_option(Option *opt) {
1468 if(get_parent() == nullptr) {
1469 throw OptionNotFound("Unable to locate the specified option");
1470 }
1471 get_parent()->_move_option(opt, this);
1472 return opt;
1473 }
1474
1475 void add_options(Option *opt) { add_option(opt); }
1477 template <typename... Args> void add_options(Option *opt, Args... args) {
1478 add_option(opt);
1479 add_options(args...);
1480 }
1481 using App::add_subcommand;
1484 App_p subc = subcom->get_parent()->get_subcommand_ptr(subcom);
1485 subc->get_parent()->remove_subcommand(subcom);
1486 add_subcommand(std::move(subc));
1487 return subcom;
1488 }
1489};
1490
1492CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable);
1493
1495CLI11_INLINE void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable);
1496
1498CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable);
1499
1501CLI11_INLINE void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable);
1502
1504CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement = "");
1505
1507inline void deprecate_option(App *app, const std::string &option_name, const std::string &replacement = "") {
1508 auto *opt = app->get_option(option_name);
1509 deprecate_option(opt, replacement);
1510}
1511
1513inline void deprecate_option(App &app, const std::string &option_name, const std::string &replacement = "") {
1514 auto *opt = app.get_option(option_name);
1515 deprecate_option(opt, replacement);
1516}
1517
1519CLI11_INLINE void retire_option(App *app, Option *opt);
1520
1522CLI11_INLINE void retire_option(App &app, Option *opt);
1523
1525CLI11_INLINE void retire_option(App *app, const std::string &option_name);
1526
1528CLI11_INLINE void retire_option(App &app, const std::string &option_name);
1529
1530namespace detail {
1533#ifdef CLI11_CPP14
1534
1536 template <typename... Args> static decltype(auto) parse_arg(App *app, Args &&...args) {
1537 return app->_parse_arg(std::forward<Args>(args)...);
1538 }
1539
1541 template <typename... Args> static decltype(auto) parse_subcommand(App *app, Args &&...args) {
1542 return app->_parse_subcommand(std::forward<Args>(args)...);
1543 }
1544#else
1546 template <typename... Args>
1547 static auto parse_arg(App *app, Args &&...args) ->
1548 typename std::result_of<decltype (&App::_parse_arg)(App, Args...)>::type {
1549 return app->_parse_arg(std::forward<Args>(args)...);
1550 }
1551
1553 template <typename... Args>
1554 static auto parse_subcommand(App *app, Args &&...args) ->
1555 typename std::result_of<decltype (&App::_parse_subcommand)(App, Args...)>::type {
1556 return app->_parse_subcommand(std::forward<Args>(args)...);
1557 }
1558#endif
1561
1563 static const App *get_fallthrough_parent(const App *app) { return app->_get_fallthrough_parent(); }
1564};
1565} // namespace detail
1566
1567// [CLI11:app_hpp:end]
1568} // namespace CLI
1569
1570#ifndef CLI11_COMPILE
1571#include "impl/App_inl.hpp" // IWYU pragma: export
1572#endif
Creates a command line program, with very few defaults.
Definition App.hpp:115
CLI11_NODISCARD Option * get_option_no_throw(std::string option_name) noexcept
Get an option by name (noexcept non-const version).
Definition App_inl.hpp:923
bool subcommand_fallthrough_
Allow subcommands to fallthrough, so that parent commands can trigger other subcommands after subcomm...
Definition App.hpp:257
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 App_inl.hpp:700
CLI11_NODISCARD std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
Definition App_inl.hpp:796
CLI11_NODISCARD std::size_t remaining_size(bool recurse=false) const
This returns the number of remaining options, minus the – separator.
Definition App_inl.hpp:1071
const Option * operator[](const std::string &option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1150
CLI11_NODISCARD bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Definition App_inl.hpp:1848
App * configurable(bool value=true)
Specify that the subcommand can be triggered by a config file.
Definition App.hpp:535
CLI11_NODISCARD bool get_allow_subcommand_prefix_matching() const
Get the status of allowing prefix matching for subcommands.
Definition App.hpp:1226
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.
Definition App_inl.hpp:1183
App * immediate_callback(bool immediate=true)
Set the subcommand callback to be executed immediately on subcommand completion.
Definition App_inl.hpp:122
Option * set_help_flag(std::string flag_name="", const std::string &help_description="")
Set a help flag, replace the existing one if present.
Definition App_inl.hpp:278
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 * usage(std::function< std::string()> usage_function)
Set usage.
Definition App.hpp:1065
CLI11_NODISCARD bool get_enabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1235
Option * config_ptr_
Pointer to the config option.
Definition App.hpp:323
CLI11_NODISCARD bool get_allow_non_standard_option_names() const
Get the status of allowing non standard option names.
Definition App.hpp:1223
CLI11_NODISCARD bool get_configurable() const
Check the status of the allow windows style options.
Definition App.hpp:1174
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:619
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1180
CLI11_NODISCARD std::shared_ptr< FormatterBase > get_formatter() const
Access the formatter.
Definition App.hpp:1107
Option * add_option(std::string option_name)
Add option with no description or variable assignment.
Definition App.hpp:658
App * subcommand_fallthrough(bool value=true)
Set subcommand fallthrough, set to true so that subcommands on parents are recognized.
Definition App.hpp:911
App * config_formatter(std::shared_ptr< Config > fmt)
Set the config formatter.
Definition App.hpp:556
App * allow_config_extras(bool allow=true)
ignore extras in config files
Definition App.hpp:481
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 _move_to_missing(detail::Classifier val_type, const std::string &val)
Helper function to place extra values in the most appropriate position.
Definition App_inl.hpp:2432
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.
Definition App_inl.hpp:2355
std::size_t require_option_min_
Minimum required options (not inheritable!).
Definition App.hpp:304
NameMatch
enumeration of matching possibilities
Definition App.hpp:1293
App * silent(bool silence=true)
silence the subcommand from showing up in the processed list
Definition App.hpp:427
App * clear_aliases()
clear all the aliases of the current App
Definition App.hpp:1280
CLI11_NODISCARD bool get_ignore_underscore() const
Check the status of ignore_underscore.
Definition App.hpp:1159
App * ignore_underscore(bool value=true)
Ignore underscore. Subcommands inherit value.
Definition App_inl.hpp:148
App * allow_extras(bool allow=true)
Remove the error when extras are left over on the command line.
Definition App.hpp:403
App * fallthrough(bool value=true)
Definition App.hpp:905
std::size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE.
Definition App.hpp:301
Option * get_config_ptr()
Get a pointer to the config option.
Definition App.hpp:1256
std::vector< App_p > subcommands_
Storage for subcommand list.
Definition App.hpp:244
CLI11_NODISCARD std::vector< std::string > remaining(bool recurse=false) const
This returns the missing options from the current subcommand.
Definition App_inl.hpp:1039
CLI11_NODISCARD bool get_allow_extras() const
Get the status of allow extras.
Definition App.hpp:1208
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.
Definition App_inl.hpp:1065
std::uint32_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition App.hpp:295
CLI11_NODISCARD App * get_option_group(std::string group_name) const
Check to see if an option group is part of this App.
Definition App_inl.hpp:555
App * require_subcommand()
The argumentless form of require subcommand requires 1 or more subcommands.
Definition App.hpp:846
std::string usage_
Usage to put after program/subcommand description in the help output INHERITABLE.
Definition App.hpp:181
OptionDefaults option_defaults_
The default values for options, customizable and changeable INHERITABLE.
Definition App.hpp:171
CLI11_NODISCARD const Option * get_version_ptr() const
Get a pointer to the version option. (const).
Definition App.hpp:1265
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition App_inl.hpp:1352
App * allow_extras(ExtrasMode allow)
Remove the error when extras are left over on the command line.
Definition App.hpp:409
CLI11_NODISCARD std::size_t count_all() const
Definition App_inl.hpp:564
void _parse_setup()
Definition App_inl.hpp:661
CLI11_NODISCARD bool get_fallthrough() const
Check the status of fallthrough.
Definition App.hpp:1162
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition App.hpp:148
CLI11_NODISCARD bool get_prefix_command() const
Get the prefix command status.
Definition App.hpp:1202
Option * add_option(std::string option_name, AssignTo &variable, std::string option_description="")
Add option for assigning to a variable.
Definition App.hpp:595
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1193
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.
Definition App_inl.hpp:311
bool required_
If set to true the subcommand is required to be processed and used, ignored for main app.
Definition App.hpp:145
bool remove_needs(Option *opt)
Removes an option from the needs list of this subcommand.
Definition App_inl.hpp:778
const Option * operator[](const char *option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1153
CLI11_NODISCARD bool get_immediate_callback() const
Get the status of disabled.
Definition App.hpp:1229
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1247
App * require_subcommand(int value)
Definition App.hpp:855
void _configure()
Definition App_inl.hpp:1120
CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
Definition App_inl.hpp:1836
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.
Definition App_inl.hpp:387
OptionDefaults * option_defaults()
Get the OptionDefault object, to set option defaults.
Definition App.hpp:565
void parse(int argc, const char *const *argv)
Definition App_inl.hpp:594
void _process_config_file()
Read and process a configuration file (main app only).
Definition App_inl.hpp:1240
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition App.hpp:187
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
Definition App_inl.hpp:1539
CLI11_NODISCARD bool check_name(std::string name_to_check) const
Definition App_inl.hpp:983
CLI11_NODISCARD const Option * get_option(std::string option_name) const
Get an option by name.
Definition App_inl.hpp:894
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1185
App * required(bool require=true)
Set whether this subcommand/option-group is required to be present on the command line.
Definition App.hpp:415
Option * version_ptr_
A pointer to a version flag if there is one.
Definition App.hpp:199
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const).
Definition App.hpp:1253
bool remove_subcommand(App *subcom)
Removes a subcommand from the App. Takes a subcommand pointer. Returns true if found and removed.
Definition App_inl.hpp:485
App * parent_
A pointer to the parent if this is a subcommand.
Definition App.hpp:310
Option * add_flag(std::string flag_name, T &&flag_description)
Definition App.hpp:702
std::set< Option * > exclude_options_
Definition App.hpp:229
void _trigger_pre_parse(std::size_t remaining_args)
Trigger the pre_parse callback if needed.
Definition App_inl.hpp:2337
App * group(std::string group_name)
Changes the group membership.
Definition App.hpp:840
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.
Definition App_inl.hpp:530
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
ExtrasMode allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition App.hpp:132
PrefixCommandMode prefix_command_
If true, cease processing on an unrecognized option (implies allow_extras) INHERITABLE.
Definition App.hpp:139
std::function< void()> parse_complete_callback_
This is a function that runs when parsing has finished.
Definition App.hpp:161
CLI11_NODISCARD bool get_subcommand_fallthrough() const
Check the status of subcommand fallthrough.
Definition App.hpp:1165
virtual void pre_callback()
Definition App.hpp:927
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:775
App * get_parent()
Get the parent of this subcommand (or nullptr if main app).
Definition App.hpp:1268
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition App.hpp:692
void _validate() const
Definition App_inl.hpp:1085
std::string name_
Subcommand name or program name (from parser if name is empty).
Definition App.hpp:126
std::vector< App * > parsed_subcommands_
This is a list of the subcommands collected, in order.
Definition App.hpp:222
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
CLI11_NODISCARD const Option * get_help_ptr() const
Get a pointer to the help flag. (const).
Definition App.hpp:1250
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition App.hpp:250
App * allow_windows_style_options(bool value=true)
Definition App.hpp:523
missing_t missing_
Definition App.hpp:216
CLI11_NODISCARD bool get_validate_positionals() const
Get the status of validating positionals.
Definition App.hpp:1237
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:637
void run_callback(bool final_mode=false, bool suppress_final_callback=false)
Internal function to run (App) callback, bottom up.
Definition App_inl.hpp:1140
bool allow_prefix_matching_
indicator to allow subcommands to match with prefix matching
Definition App.hpp:292
std::size_t require_subcommand_min_
Minimum required subcommands (not inheritable!).
Definition App.hpp:298
App * usage(std::string usage_string)
Set usage.
Definition App.hpp:1060
CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const
Definition App_inl.hpp:988
App * allow_subcommand_prefix_matching(bool allowed=true)
allow prefix matching for subcommands
Definition App.hpp:439
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
Definition App_inl.hpp:1276
std::function< std::string(const App *, const Error &e)> failure_message_
The error message printing function INHERITABLE.
Definition App.hpp:205
void _parse_stream(std::istream &input)
Internal function to parse a stream.
Definition App_inl.hpp:1616
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:967
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:962
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 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...
Definition App_inl.hpp:2377
void clear()
Reset the parsed data.
Definition App_inl.hpp:578
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1199
App * enabled_by_default(bool enable=true)
Definition App.hpp:455
App * footer(std::string footer_string)
Set footer.
Definition App.hpp:1070
App * get_subcommand(const App *subcom) const
Definition App_inl.hpp:501
App * require_option(int value)
Definition App.hpp:884
CLI11_NODISCARD std::string version() const
Displays a version string.
Definition App_inl.hpp:810
virtual ~App()=default
virtual destructor
CLI11_NODISCARD App * get_subcommand_no_throw(std::string subcom) const noexcept
Definition App_inl.hpp:517
CLI11_NODISCARD std::size_t count(std::string option_name) const
Counts the number of times the given option was passed.
Definition App.hpp:974
bool _add_flag_like_result(Option *op, const ConfigItem &item, const std::vector< std::string > &inputs)
store the results for a flag like option
Definition App_inl.hpp:1635
std::vector< Option_p > options_
The list of options, stored locally.
Definition App.hpp:174
Option * help_all_ptr_
A pointer to the help all flag if there is one INHERITABLE.
Definition App.hpp:196
bool validate_optional_arguments_
If set to true optional vector arguments are validated before assigning INHERITABLE.
Definition App.hpp:282
std::function< void()> final_callback_
This is a function that runs when all processing has completed.
Definition App.hpp:164
bool remove_option(Option *opt)
Removes an option from the App. Takes an option pointer. Returns true if found and removed.
Definition App_inl.hpp:431
App * require_option()
The argumentless form of require option requires 1 or more options be used.
Definition App.hpp:875
App(std::string app_description, std::string app_name, App *parent)
Special private constructor for subcommand.
Definition App_inl.hpp:30
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
App * add_subcommand(std::string subcommand_name="", std::string subcommand_description="")
Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag.
Definition App_inl.hpp:454
App * preparse_callback(std::function< void(std::size_t)> pp_callback)
Definition App.hpp:391
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.
Definition App_inl.hpp:370
bool positionals_at_end_
specify that positional arguments come at the end of the argument sequence not inheritable
Definition App.hpp:268
void _process()
Process callbacks and such.
Definition App_inl.hpp:1480
bool immediate_callback_
Definition App.hpp:155
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
Definition App_inl.hpp:1787
App * name(std::string app_name="")
Set a name for the app (empty will use parser to set the name).
Definition App_inl.hpp:87
void _move_option(Option *opt, App *app)
function that could be used by subclasses of App to shift options around into subcommands
Definition App_inl.hpp:2455
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1214
void _process_extras()
Throw an error if anything is left over and should not be.
Definition App_inl.hpp:1519
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.
Definition App_inl.hpp:1167
CLI11_NODISCARD PrefixCommandMode get_prefix_command_mode() const
Get the prefix command status.
Definition App.hpp:1205
CLI11_NODISCARD bool get_ignore_case() const
Check the status of ignore_case.
Definition App.hpp:1156
App * parse_complete_callback(std::function< void()> pc_callback)
Definition App.hpp:384
bool configurable_
if set to true the subcommand can be triggered via configuration files INHERITABLE
Definition App.hpp:276
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order).
Definition App_inl.hpp:1026
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:995
App * formatter_fn(std::function< std::string(const App *, std::string, AppFormatMode)> fmt)
Set the help formatter.
Definition App.hpp:550
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1277
void _parse_config(const std::vector< ConfigItem > &args)
Definition App_inl.hpp:1627
App * description(std::string app_description)
Set the description of the app.
Definition App.hpp:1126
CLI11_NODISCARD std::shared_ptr< ConfigBase > get_config_formatter_base() const
Access the config formatter as a configBase pointer.
Definition App.hpp:1113
std::string description_
Description of the current program/subcommand.
Definition App.hpp:129
App * excludes(App *app)
Sets excluded subcommands for the subcommand.
Definition App.hpp:1010
App * allow_non_standard_option_names(bool allowed=true)
allow non standard option names
Definition App.hpp:433
CLI11_NODISCARD bool get_positionals_at_end() const
Check the status of the allow windows style options.
Definition App.hpp:1171
Option * get_version_ptr()
Get a pointer to the version option.
Definition App.hpp:1262
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1196
std::size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable).
Definition App.hpp:307
CLI11_NODISCARD bool get_disabled() const
Get the status of disabled.
Definition App.hpp:1217
CLI11_NODISCARD bool get_allow_windows_style_options() const
Check the status of the allow windows style options.
Definition App.hpp:1168
std::vector< std::string > aliases_
Alias names for the subcommand.
Definition App.hpp:316
CLI11_NODISCARD bool parsed() const
Check to see if this subcommand was parsed, true only if received on command line.
Definition App.hpp:562
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition App.hpp:225
void _process_completion_callbacks(bool with_help_flags)
Definition App_inl.hpp:1547
CLI11_NODISCARD bool get_disabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1232
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:728
ConfigExtrasMode allow_config_extras_
Definition App.hpp:136
CLI11_NODISCARD std::size_t count() const
Definition App.hpp:833
bool _parse_positional(std::vector< std::string > &args, bool haltOnSubcommand)
Definition App_inl.hpp:1858
bool ignore_case_
If true, the program name is not case-sensitive INHERITABLE.
Definition App.hpp:247
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1177
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type, bool local_processing_only)
Definition App_inl.hpp:2070
bool silent_
Definition App.hpp:286
CLI11_NODISCARD const App * get_parent() const
Get the parent of this subcommand (or nullptr if main app) (const version).
Definition App.hpp:1271
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
App * final_callback(std::function< void()> app_callback)
Definition App.hpp:377
std::string group_
The group membership INHERITABLE.
Definition App.hpp:313
App * alias(std::string app_name)
Set an alias for the app.
Definition App_inl.hpp:104
bool pre_parse_called_
Flag indicating that the pre_parse_callback has been triggered.
Definition App.hpp:151
CLI11_NODISCARD ExtrasMode get_allow_extras_mode() const
Get the mode of allow_extras.
Definition App.hpp:1211
App * validate_positionals(bool validate=true)
Set the subcommand to validate positional arguments before assigning.
Definition App.hpp:469
Option * help_ptr_
A pointer to the help flag if there is one INHERITABLE.
Definition App.hpp:193
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.
Definition App_inl.hpp:402
App * footer(std::function< std::string()> footer_function)
Set footer.
Definition App.hpp:1075
App * ignore_case(bool value=true)
Ignore case. Subcommands inherit value.
Definition App_inl.hpp:134
App * excludes(Option *opt)
Sets excluded options for the subcommand.
Definition App.hpp:1001
CLI11_NODISCARD std::shared_ptr< Config > get_config_formatter() const
Access the config formatter.
Definition App.hpp:1110
App * prefix_command(PrefixCommandMode mode)
Set the prefix command mode directly.
Definition App.hpp:513
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
Definition App_inl.hpp:758
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:978
bool got_subcommand(const App *subcom) const
Check to see if given subcommand was selected.
Definition App.hpp:989
CLI11_NODISCARD config_extras_mode get_allow_config_extras() const
Get the status of allow extras.
Definition App.hpp:1242
App * allow_config_extras(ConfigExtrasMode mode)
ignore extras in config files
Definition App.hpp:498
CLI11_NODISCARD std::string config_to_str(ConfigOutputMode mode, bool write_description=false) const
Produce a string that could be read in as a config of the current values of the App.
Definition App.hpp:1084
bool _parse_subcommand(std::vector< std::string > &args)
Definition App_inl.hpp:2030
App * positionals_at_end(bool value=true)
Specify that the positional arguments are only at the end of the sequence.
Definition App.hpp:529
bool fallthrough_
Definition App.hpp:254
std::set< Option * > need_options_
Definition App.hpp:237
CLI11_NODISCARD bool get_validate_optional_arguments() const
Get the status of validating optional vector arguments.
Definition App.hpp:1239
CLI11_NODISCARD bool get_silent() const
Get the status of silence.
Definition App.hpp:1220
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...
Definition App_inl.hpp:828
std::set< App * > need_subcommands_
Definition App.hpp:233
App * validate_optional_arguments(bool validate=true)
Set the subcommand to validate optional vector arguments before assigning.
Definition App.hpp:475
Option * add_option(std::string option_name, callback_t option_callback, std::string option_description="", bool defaulted=false, std::function< std::string()> func={})
Definition App_inl.hpp:162
App * formatter(std::shared_ptr< FormatterBase > fmt)
Set the help formatter.
Definition App.hpp:544
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition App.hpp:219
Option * add_option(std::string option_name, T &option_description)
Add option with description but with no variable assignment or callback.
Definition App.hpp:666
App * prefix_command(bool is_prefix=true)
Definition App.hpp:507
CLI11_NODISCARD const std::vector< Option * > & parse_order() const
This gets a vector of pointers with the original parse order.
Definition App.hpp:1304
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition App_inl.hpp:1575
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition App.hpp:279
bool _parse_single_config(const ConfigItem &item, std::size_t level=0)
Fill in a single config option.
Definition App_inl.hpp:1696
void _process_help_flags(CallbackPriority priority, bool trigger_help=false, bool trigger_all_help=false) const
Definition App_inl.hpp:1327
startup_mode default_startup
Definition App.hpp:273
App * allow_config_extras(config_extras_mode mode)
ignore extras in config files
Definition App.hpp:492
Option * add_flag(std::string flag_name, T &flag_result, std::string flag_description="")
Definition App.hpp:712
void _process_callbacks(CallbackPriority priority)
Process callbacks. Runs on all subcommands.
Definition App_inl.hpp:1298
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1123
App * require_option(std::size_t min, std::size_t max)
Definition App.hpp:897
CLI11_NODISCARD char ** ensure_utf8(char **argv)
Convert the contents of argv to UTF-8. Only does something on Windows, does nothing elsewhere.
Definition App_inl.hpp:65
CLI11_NODISCARD App * _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept
Definition App_inl.hpp:1994
CLI11_NODISCARD std::string config_to_str(bool default_also, bool write_description=false) const
Definition App.hpp:1091
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1190
CLI11_NODISCARD const Option * get_config_ptr() const
Get a pointer to the config option. (const).
Definition App.hpp:1259
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1274
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition App.hpp:421
App * callback(std::function< void()> app_callback)
Definition App.hpp:366
std::shared_ptr< FormatterBase > formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer).
Definition App.hpp:202
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.
Definition App_inl.hpp:294
CLI11_NODISCARD std::string config_to_str() const
Definition App.hpp:1081
App * require_subcommand(std::size_t min, std::size_t max)
Definition App.hpp:868
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise....
Definition App.hpp:260
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer).
Definition App.hpp:326
All errors derive from this one.
Definition Error.hpp:73
Definition FormatterFwd.hpp:197
Thrown when an option is set to conflicting values (non-vector and multi args, for example).
Definition Error.hpp:97
Extension of App to better manage groups of options.
Definition App.hpp:1453
App * add_subcommand(App *subcom)
Add an existing subcommand to be a member of an option_group.
Definition App.hpp:1483
Option * add_option(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1467
void add_options(Option *opt, Args... args)
Add a bunch of options to the group.
Definition App.hpp:1477
void add_options(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1475
Definition Option.hpp:216
Definition Option.hpp:259
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:507
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:39
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:785
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:389
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:440
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy).
Definition Option_inl.hpp:256
Thrown when counting a nonexistent option.
Definition Error.hpp:352
Holds values to load into Options.
Definition ConfigFwd.hpp:34
This class is simply to allow tests access to App's protected functions.
Definition App.hpp:1532
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:1554
static App * get_fallthrough_parent(App *app)
Wrap the fallthrough parent function to make sure that is working correctly.
Definition App.hpp:1560
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:1547
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:1563