CLI11
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
88enum class PrefixCommandMode : std::uint8_t { Off = 0, SeparatorOnly = 1, On = 2 };
89
90class App;
91
92using App_p = std::shared_ptr<App>;
93
94namespace detail {
96
97template <typename T, enable_if_t<!std::is_integral<T>::value || (sizeof(T) <= 1U), detail::enabler> = detail::dummy>
98Option *default_flag_modifiers(Option *opt) {
99 return opt->always_capture_default();
100}
101
103template <typename T, enable_if_t<std::is_integral<T>::value && (sizeof(T) > 1U), detail::enabler> = detail::dummy>
104Option *default_flag_modifiers(Option *opt) {
105 return opt->multi_option_policy(MultiOptionPolicy::Sum)->default_str("0")->force_callback();
106}
107
108} // namespace detail
109
110class Option_group;
112
114class App {
115 friend Option;
116 friend detail::AppFriend;
117
118 protected:
119 // This library follows the Google style guide for member names ending in underscores
120
123
125 std::string name_{};
126
128 std::string description_{};
129
131 ExtrasMode allow_extras_{ExtrasMode::Error};
132
135 ConfigExtrasMode allow_config_extras_{ConfigExtrasMode::Ignore};
136
138 PrefixCommandMode prefix_command_{PrefixCommandMode::Off};
139
142
144 bool required_{false};
145
147 bool disabled_{false};
148
150 bool pre_parse_called_{false};
151
155
157 std::function<void(std::size_t)> pre_parse_callback_{};
158
160 std::function<void()> parse_complete_callback_{};
161
163 std::function<void()> final_callback_{};
164
168
171
173 std::vector<Option_p> options_{};
174
178
180 std::string usage_{};
181
183 std::function<std::string()> usage_callback_{};
184
186 std::string footer_{};
187
189 std::function<std::string()> footer_callback_{};
190
192 Option *help_ptr_{nullptr};
193
196
199
201 std::shared_ptr<FormatterBase> formatter_{new Formatter()};
202
204 std::function<std::string(const App *, const Error &e)> failure_message_{FailureMessage::simple};
205
209
210 using missing_t = std::vector<std::pair<detail::Classifier, std::string>>;
211
215 missing_t missing_{};
216
218 std::vector<Option *> parse_order_{};
219
221 std::vector<App *> parsed_subcommands_{};
222
224 std::set<App *> exclude_subcommands_{};
225
228 std::set<Option *> exclude_options_{};
229
232 std::set<App *> need_subcommands_{};
233
236 std::set<Option *> need_options_{};
237
241
243 std::vector<App_p> subcommands_{};
244
246 bool ignore_case_{false};
247
250
253 bool fallthrough_{false};
254
257
260#ifdef _WIN32
261 true
262#else
263 false
264#endif
265 };
268
269 enum class startup_mode : std::uint8_t { stable, enabled, disabled };
272 startup_mode default_startup{startup_mode::stable};
273
275 bool configurable_{false};
276
279
282
285 bool silent_{false};
286
289
292
294 std::uint32_t parsed_{0U};
295
298
301
303 std::size_t require_option_min_{0};
304
306 std::size_t require_option_max_{0};
307
309 App *parent_{nullptr};
310
312 std::string group_{"SUBCOMMANDS"};
313
315 std::vector<std::string> aliases_{};
316
320
323
325 std::shared_ptr<Config> config_formatter_{new ConfigTOML()};
326
328
329#ifdef _WIN32
331 std::vector<std::string> normalized_argv_{};
332
334 std::vector<char *> normalized_argv_view_{};
335#endif
336
338 App(std::string app_description, std::string app_name, App *parent);
339
340 public:
343
345 explicit App(std::string app_description = "", std::string app_name = "")
346 : App(app_description, app_name, nullptr) {
347 set_help_flag("-h,--help", "Print this help message and exit");
348 }
349
350 App(const App &) = delete;
351 App &operator=(const App &) = delete;
352
354 virtual ~App() = default;
355
357 CLI11_NODISCARD char **ensure_utf8(char **argv);
358
365 App *callback(std::function<void()> app_callback) {
367 parse_complete_callback_ = std::move(app_callback);
368 } else {
369 final_callback_ = std::move(app_callback);
370 }
371 return this;
372 }
373
376 App *final_callback(std::function<void()> app_callback) {
377 final_callback_ = std::move(app_callback);
378 return this;
379 }
380
383 App *parse_complete_callback(std::function<void()> pc_callback) {
384 parse_complete_callback_ = std::move(pc_callback);
385 return this;
386 }
387
390 App *preparse_callback(std::function<void(std::size_t)> pp_callback) {
391 pre_parse_callback_ = std::move(pp_callback);
392 return this;
393 }
394
396 App *name(std::string app_name = "");
397
399 App *alias(std::string app_name);
400
402 App *allow_extras(bool allow = true) {
403 allow_extras_ = allow ? ExtrasMode::Capture : ExtrasMode::Error;
404 return this;
405 }
406
408 App *allow_extras(ExtrasMode allow) {
409 allow_extras_ = allow;
410 return this;
411 }
412
414 App *required(bool require = true) {
415 required_ = require;
416 return this;
417 }
418
420 App *disabled(bool disable = true) {
421 disabled_ = disable;
422 return this;
423 }
424
426 App *silent(bool silence = true) {
427 silent_ = silence;
428 return this;
429 }
430
432 App *allow_non_standard_option_names(bool allowed = true) {
434 return this;
435 }
436
438 App *allow_subcommand_prefix_matching(bool allowed = true) {
439 allow_prefix_matching_ = allowed;
440 return this;
441 }
443 App *disabled_by_default(bool disable = true) {
444 if(disable) {
445 default_startup = startup_mode::disabled;
446 } else {
447 default_startup = (default_startup == startup_mode::enabled) ? startup_mode::enabled : startup_mode::stable;
448 }
449 return this;
450 }
451
454 App *enabled_by_default(bool enable = true) {
455 if(enable) {
456 default_startup = startup_mode::enabled;
457 } else {
459 (default_startup == startup_mode::disabled) ? startup_mode::disabled : startup_mode::stable;
460 }
461 return this;
462 }
463
465 App *immediate_callback(bool immediate = true);
466
468 App *validate_positionals(bool validate = true) {
469 validate_positionals_ = validate;
470 return this;
471 }
472
474 App *validate_optional_arguments(bool validate = true) {
476 return this;
477 }
478
480 App *allow_config_extras(bool allow = true) {
481 if(allow) {
482 allow_config_extras_ = ConfigExtrasMode::Capture;
483 allow_extras_ = ExtrasMode::Capture;
484 } else {
485 allow_config_extras_ = ConfigExtrasMode::Error;
486 }
487 return this;
488 }
489
491 App *allow_config_extras(config_extras_mode mode) {
492 allow_config_extras_ = static_cast<ConfigExtrasMode>(mode);
493 return this;
494 }
495
497 App *allow_config_extras(ConfigExtrasMode mode) {
499 return this;
500 }
501
505 App *prefix_command(bool is_prefix = true) {
506 prefix_command_ = is_prefix ? PrefixCommandMode::On : PrefixCommandMode::Off;
507 return this;
508 }
509
511 App *prefix_command(PrefixCommandMode mode) {
512 prefix_command_ = mode;
513 return this;
514 }
515
517 App *ignore_case(bool value = true);
518
521 App *allow_windows_style_options(bool value = true) {
523 return this;
524 }
525
527 App *positionals_at_end(bool value = true) {
528 positionals_at_end_ = value;
529 return this;
530 }
531
533 App *configurable(bool value = true) {
534 configurable_ = value;
535 return this;
536 }
537
539 App *ignore_underscore(bool value = true);
540
542 App *formatter(std::shared_ptr<FormatterBase> fmt) {
543 formatter_ = std::move(fmt);
544 return this;
545 }
546
548 App *formatter_fn(std::function<std::string(const App *, std::string, AppFormatMode)> fmt) {
549 formatter_ = std::make_shared<FormatterLambda>(fmt);
550 return this;
551 }
552
554 App *config_formatter(std::shared_ptr<Config> fmt) {
555 config_formatter_ = std::move(fmt);
556 return this;
557 }
558
560 CLI11_NODISCARD bool parsed() const { return parsed_ > 0; }
561
564
568
583 Option *add_option(std::string option_name,
584 callback_t option_callback,
585 std::string option_description = "",
586 bool defaulted = false,
587 std::function<std::string()> func = {});
588
590 template <typename AssignTo,
591 typename ConvertTo = AssignTo,
592 enable_if_t<!std::is_const<ConvertTo>::value, detail::enabler> = detail::dummy>
593 Option *add_option(std::string option_name,
594 AssignTo &variable,
595 std::string option_description = "") {
596
597 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
598 return detail::lexical_conversion<AssignTo, ConvertTo>(res, variable);
599 };
600
601 Option *opt = add_option(option_name, fun, option_description, false, [&variable]() {
602 return CLI::detail::checked_to_string<AssignTo, ConvertTo>(variable);
603 });
604 opt->type_name(detail::type_name<ConvertTo>());
605 // these must be actual lvalues since (std::max) sometimes is defined in terms of references and references
606 // to structs used in the evaluation can be temporary so that would cause issues.
609 opt->type_size(detail::type_count_min<ConvertTo>::value, (std::max)(Tcount, XCcount));
610 opt->expected(detail::expected_count<ConvertTo>::value);
612 return opt;
613 }
614
616 template <typename AssignTo, enable_if_t<!std::is_const<AssignTo>::value, detail::enabler> = detail::dummy>
617 Option *add_option_no_stream(std::string option_name,
618 AssignTo &variable,
619 std::string option_description = "") {
620
621 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
622 return detail::lexical_conversion<AssignTo, AssignTo>(res, variable);
623 };
624
625 Option *opt = add_option(option_name, fun, option_description, false, []() { return std::string{}; });
626 opt->type_name(detail::type_name<AssignTo>());
627 opt->type_size(detail::type_count_min<AssignTo>::value, detail::type_count<AssignTo>::value);
628 opt->expected(detail::expected_count<AssignTo>::value);
630 return opt;
631 }
632
634 template <typename ArgType>
635 Option *add_option_function(std::string option_name,
636 const std::function<void(const ArgType &)> &func,
637 std::string option_description = "") {
638
639 auto fun = [func](const CLI::results_t &res) {
640 ArgType variable;
641 bool result = detail::lexical_conversion<ArgType, ArgType>(res, variable);
642 if(result) {
643 func(variable);
644 }
645 return result;
646 };
647
648 Option *opt = add_option(option_name, std::move(fun), option_description, false);
649 opt->type_name(detail::type_name<ArgType>());
650 opt->type_size(detail::type_count_min<ArgType>::value, detail::type_count<ArgType>::value);
651 opt->expected(detail::expected_count<ArgType>::value);
652 return opt;
653 }
654
656 Option *add_option(std::string option_name) {
657 return add_option(option_name, CLI::callback_t{}, std::string{}, false);
658 }
659
661 template <typename T,
662 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
663 detail::dummy>
664 Option *add_option(std::string option_name, T &option_description) {
665 return add_option(option_name, CLI::callback_t(), option_description, false);
666 }
667
669 Option *set_help_flag(std::string flag_name = "", const std::string &help_description = "");
670
672 Option *set_help_all_flag(std::string help_name = "", const std::string &help_description = "");
673
675 Option *set_version_flag(std::string flag_name = "",
676 const std::string &versionString = "",
677 const std::string &version_help = "Display program version information and exit");
678
680 Option *set_version_flag(std::string flag_name,
681 std::function<std::string()> vfunc,
682 const std::string &version_help = "Display program version information and exit");
683
684 private:
686 Option *_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description);
687
688 public:
690 Option *add_flag(std::string flag_name) { return _add_flag_internal(flag_name, CLI::callback_t(), std::string{}); }
691
695 template <typename T,
696 enable_if_t<(std::is_const<typename std::remove_reference<T>::type>::value ||
697 std::is_rvalue_reference<T &&>::value) &&
698 std::is_constructible<std::string, typename std::remove_reference<T>::type>::value,
699 detail::enabler> = detail::dummy>
700 Option *add_flag(std::string flag_name, T &&flag_description) {
701 return _add_flag_internal(flag_name, CLI::callback_t(), std::forward<T>(flag_description));
702 }
703
706 template <typename T,
707 enable_if_t<!detail::is_mutable_container<T>::value && !std::is_const<T>::value &&
708 !std::is_constructible<std::function<void(int)>, T>::value,
709 detail::enabler> = detail::dummy>
710 Option *add_flag(std::string flag_name,
711 T &flag_result,
712 std::string flag_description = "") {
713
714 CLI::callback_t fun = [&flag_result](const CLI::results_t &res) {
715 using CLI::detail::lexical_cast;
716 return lexical_cast(res[0], flag_result);
717 };
718 auto *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
719 return detail::default_flag_modifiers<T>(opt);
720 }
721
723 template <typename T,
724 enable_if_t<!std::is_assignable<std::function<void(std::int64_t)> &, T>::value, detail::enabler> =
725 detail::dummy>
726 Option *add_flag(std::string flag_name,
727 std::vector<T> &flag_results,
728 std::string flag_description = "") {
729 CLI::callback_t fun = [&flag_results](const CLI::results_t &res) {
730 bool retval = true;
731 for(const auto &elem : res) {
732 using CLI::detail::lexical_cast;
733 flag_results.emplace_back();
734 retval &= lexical_cast(elem, flag_results.back());
735 }
736 return retval;
737 };
738 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
739 ->multi_option_policy(MultiOptionPolicy::TakeAll)
741 }
742
744 Option *add_flag_callback(std::string flag_name,
745 std::function<void(void)> function,
746 std::string flag_description = "");
747
749 Option *add_flag_function(std::string flag_name,
750 std::function<void(std::int64_t)> function,
751 std::string flag_description = "");
752
753#ifdef CLI11_CPP14
755 Option *add_flag(std::string flag_name,
756 std::function<void(std::int64_t)> function,
757 std::string flag_description = "") {
758 return add_flag_function(std::move(flag_name), std::move(function), std::move(flag_description));
759 }
760#endif
761
763 Option *set_config(std::string option_name = "",
764 std::string default_filename = "",
765 const std::string &help_message = "Read an ini file",
766 bool config_required = false);
767
769 bool remove_option(Option *opt);
770
772 template <typename T = Option_group>
773 T *add_option_group(std::string group_name, std::string group_description = "") {
774 if(!detail::valid_alias_name_string(group_name)) {
775 throw IncorrectConstruction("option group names may not contain newlines or null characters");
776 }
777 auto option_group = std::make_shared<T>(std::move(group_description), group_name, this);
778 option_group->fallthrough(false);
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_ = std::move(group_name);
840 return this;
841 }
842
847 return this;
848 }
849
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) {
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) {
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_ = std::move(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
1000 if(opt == nullptr) {
1001 throw OptionNotFound("nullptr passed");
1002 }
1003 exclude_options_.insert(opt);
1004 return this;
1005 }
1006
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() const { return config_to_str(ConfigOutputMode::Active, false); }
1080
1082 CLI11_NODISCARD std::string config_to_str(ConfigOutputMode mode, bool write_description = false) const {
1083 return config_formatter_->to_config(this, mode, write_description, "");
1084 }
1085
1089 CLI11_NODISCARD std::string config_to_str(bool default_also, bool write_description = false) const {
1090 return config_to_str(default_also ? ConfigOutputMode::AllDefaults : ConfigOutputMode::Active,
1091 write_description);
1092 }
1093
1096 CLI11_NODISCARD std::string help(std::string prev = "", AppFormatMode mode = AppFormatMode::Normal) const;
1097
1099 CLI11_NODISCARD std::string version() const;
1103
1105 CLI11_NODISCARD std::shared_ptr<FormatterBase> get_formatter() const { return formatter_; }
1106
1108 CLI11_NODISCARD std::shared_ptr<Config> get_config_formatter() const { return config_formatter_; }
1109
1111 CLI11_NODISCARD std::shared_ptr<ConfigBase> get_config_formatter_base() const {
1112 // This is safer as a dynamic_cast if we have RTTI, as Config -> ConfigBase
1113#if CLI11_USE_STATIC_RTTI == 0
1114 return std::dynamic_pointer_cast<ConfigBase>(config_formatter_);
1115#else
1116 return std::static_pointer_cast<ConfigBase>(config_formatter_);
1117#endif
1118 }
1119
1121 CLI11_NODISCARD std::string get_description() const { return description_; }
1122
1124 App *description(std::string app_description) {
1125 description_ = std::move(app_description);
1126 return this;
1127 }
1128
1130 std::vector<const Option *> get_options(const std::function<bool(const Option *)> filter = {}) const;
1131
1133 std::vector<Option *> get_options(const std::function<bool(Option *)> filter = {});
1134
1136 CLI11_NODISCARD Option *get_option_no_throw(std::string option_name) noexcept;
1137
1139 CLI11_NODISCARD const Option *get_option_no_throw(std::string option_name) const noexcept;
1140
1142 CLI11_NODISCARD const Option *get_option(std::string option_name) const;
1143
1145 CLI11_NODISCARD Option *get_option(std::string option_name);
1146
1148 const Option *operator[](const std::string &option_name) const { return get_option(option_name); }
1149
1151 const Option *operator[](const char *option_name) const { return get_option(option_name); }
1152
1154 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
1155
1157 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
1158
1160 CLI11_NODISCARD bool get_fallthrough() const { return fallthrough_; }
1161
1163 CLI11_NODISCARD bool get_subcommand_fallthrough() const { return subcommand_fallthrough_; }
1164
1166 CLI11_NODISCARD bool get_allow_windows_style_options() const { return allow_windows_style_options_; }
1167
1169 CLI11_NODISCARD bool get_positionals_at_end() const { return positionals_at_end_; }
1170
1172 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
1173
1175 CLI11_NODISCARD const std::string &get_group() const { return group_; }
1176
1178 CLI11_NODISCARD std::string get_usage() const {
1179 return (usage_callback_) ? usage_callback_() + '\n' + usage_ : usage_;
1180 }
1181
1183 CLI11_NODISCARD std::string get_footer() const {
1184 return (footer_callback_) ? footer_callback_() + '\n' + footer_ : footer_;
1185 }
1186
1188 CLI11_NODISCARD std::size_t get_require_subcommand_min() const { return require_subcommand_min_; }
1189
1191 CLI11_NODISCARD std::size_t get_require_subcommand_max() const { return require_subcommand_max_; }
1192
1194 CLI11_NODISCARD std::size_t get_require_option_min() const { return require_option_min_; }
1195
1197 CLI11_NODISCARD std::size_t get_require_option_max() const { return require_option_max_; }
1198
1200 CLI11_NODISCARD bool get_prefix_command() const { return static_cast<bool>(prefix_command_); }
1201
1203 CLI11_NODISCARD PrefixCommandMode get_prefix_command_mode() const { return prefix_command_; }
1204
1206 CLI11_NODISCARD bool get_allow_extras() const { return allow_extras_ > ExtrasMode::Ignore; }
1207
1209 CLI11_NODISCARD ExtrasMode get_allow_extras_mode() const { return allow_extras_; }
1210
1212 CLI11_NODISCARD bool get_required() const { return required_; }
1213
1215 CLI11_NODISCARD bool get_disabled() const { return disabled_; }
1216
1218 CLI11_NODISCARD bool get_silent() const { return silent_; }
1219
1222
1224 CLI11_NODISCARD bool get_allow_subcommand_prefix_matching() const { return allow_prefix_matching_; }
1225
1227 CLI11_NODISCARD bool get_immediate_callback() const { return immediate_callback_; }
1228
1230 CLI11_NODISCARD bool get_disabled_by_default() const { return (default_startup == startup_mode::disabled); }
1231
1233 CLI11_NODISCARD bool get_enabled_by_default() const { return (default_startup == startup_mode::enabled); }
1235 CLI11_NODISCARD bool get_validate_positionals() const { return validate_positionals_; }
1237 CLI11_NODISCARD bool get_validate_optional_arguments() const { return validate_optional_arguments_; }
1238
1240 CLI11_NODISCARD config_extras_mode get_allow_config_extras() const {
1241 return static_cast<config_extras_mode>(allow_config_extras_);
1242 }
1243
1246
1248 CLI11_NODISCARD const Option *get_help_ptr() const { return help_ptr_; }
1249
1251 CLI11_NODISCARD const Option *get_help_all_ptr() const { return help_all_ptr_; }
1252
1255
1257 CLI11_NODISCARD const Option *get_config_ptr() const { return config_ptr_; }
1258
1261
1263 CLI11_NODISCARD const Option *get_version_ptr() const { return version_ptr_; }
1264
1266 App *get_parent() { return parent_; }
1267
1269 CLI11_NODISCARD const App *get_parent() const { return parent_; }
1270
1272 CLI11_NODISCARD const std::string &get_name() const { return name_; }
1273
1275 CLI11_NODISCARD const std::vector<std::string> &get_aliases() const { return aliases_; }
1276
1279 aliases_.clear();
1280 return this;
1281 }
1282
1284 CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
1285
1288 CLI11_NODISCARD bool check_name(std::string name_to_check) const;
1289
1291 enum class NameMatch : std::uint8_t { none = 0, exact = 1, prefix = 2 };
1292
1296 CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
1297
1299 CLI11_NODISCARD std::vector<std::string> get_groups() const;
1300
1302 CLI11_NODISCARD const std::vector<Option *> &parse_order() const { return parse_order_; }
1303
1305 CLI11_NODISCARD std::vector<std::string> remaining(bool recurse = false) const;
1306
1308 CLI11_NODISCARD std::vector<std::string> remaining_for_passthrough(bool recurse = false) const;
1309
1311 CLI11_NODISCARD std::size_t remaining_size(bool recurse = false) const;
1312
1314
1315 protected:
1320 void _validate() const;
1321
1325 void _configure();
1326
1328 void run_callback(bool final_mode = false, bool suppress_final_callback = false);
1329
1331 CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used = true) const;
1332
1334 CLI11_NODISCARD detail::Classifier _recognize(const std::string &current,
1335 bool ignore_used_subcommands = true) const;
1336
1337 // The parse function is now broken into several parts, and part of process
1338
1340 void _process_config_file();
1341
1343 bool _process_config_file(const std::string &config_file, bool throw_error);
1344
1346 void _process_env();
1347
1349 void _process_callbacks(CallbackPriority priority);
1350
1354 void _process_help_flags(CallbackPriority priority, bool trigger_help = false, bool trigger_all_help = false) const;
1355
1360 void _process_completion_callbacks(bool with_help_flags);
1361
1363 void _process_requirements();
1364
1366 void _process();
1367
1369 void _process_extras();
1370
1372 void increment_parsed();
1373
1376 void _parse_setup();
1377
1379 void _parse(std::vector<std::string> &args);
1380
1382 void _parse(std::vector<std::string> &&args);
1383
1385 void _parse_stream(std::istream &input);
1386
1391 void _parse_config(const std::vector<ConfigItem> &args);
1392
1394 bool _parse_single_config(const ConfigItem &item, std::size_t level = 0);
1395
1397 bool _add_flag_like_result(Option *op, const ConfigItem &item, const std::vector<std::string> &inputs);
1398
1401 bool _parse_single(std::vector<std::string> &args, bool &positional_only);
1402
1404 CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only = false) const;
1405
1407 CLI11_NODISCARD bool _has_remaining_positionals() const;
1408
1412 bool _parse_positional(std::vector<std::string> &args, bool haltOnSubcommand);
1413
1416 CLI11_NODISCARD App *
1417 _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept;
1418
1423 bool _parse_subcommand(std::vector<std::string> &args);
1424
1428 bool _parse_arg(std::vector<std::string> &args, detail::Classifier current_type, bool local_processing_only);
1429
1431 void _trigger_pre_parse(std::size_t remaining_args);
1432
1434 CLI11_NODISCARD App *_get_fallthrough_parent() noexcept;
1435
1437 CLI11_NODISCARD const App *_get_fallthrough_parent() const noexcept;
1438
1440 CLI11_NODISCARD const std::string &_compare_subcommand_names(const App &subcom, const App &base) const;
1441
1443 void _move_to_missing(detail::Classifier val_type, const std::string &val);
1444
1445 public:
1447 void _move_option(Option *opt, App *app);
1448}; // namespace CLI
1449
1451class Option_group : public App {
1452 public:
1453 Option_group(std::string group_description, std::string group_name, App *parent)
1454 : App(std::move(group_description), "", parent) {
1455 group(group_name);
1456 // option groups should have automatic fallthrough
1457 if(group_name.empty() || group_name.front() == '+') {
1458 // help will not be used by default in these contexts
1459 set_help_flag("");
1461 }
1462 }
1463 using App::add_option;
1466 if(get_parent() == nullptr) {
1467 throw OptionNotFound("Unable to locate the specified option");
1468 }
1469 get_parent()->_move_option(opt, this);
1470 return opt;
1471 }
1473 void add_options(Option *opt) { add_option(opt); }
1475 template <typename... Args> void add_options(Option *opt, Args... args) {
1476 add_option(opt);
1477 add_options(args...);
1478 }
1479 using App::add_subcommand;
1482 App_p subc = subcom->get_parent()->get_subcommand_ptr(subcom);
1483 subc->get_parent()->remove_subcommand(subcom);
1484 add_subcommand(std::move(subc));
1485 return subcom;
1486 }
1487};
1488
1490CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable);
1491
1493CLI11_INLINE void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable);
1494
1496CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable);
1497
1499CLI11_INLINE void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable);
1500
1502CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement = "");
1503
1505inline void deprecate_option(App *app, const std::string &option_name, const std::string &replacement = "") {
1506 auto *opt = app->get_option(option_name);
1507 deprecate_option(opt, replacement);
1508}
1509
1511inline void deprecate_option(App &app, const std::string &option_name, const std::string &replacement = "") {
1512 auto *opt = app.get_option(option_name);
1513 deprecate_option(opt, replacement);
1514}
1515
1517CLI11_INLINE void retire_option(App *app, Option *opt);
1518
1520CLI11_INLINE void retire_option(App &app, Option *opt);
1521
1523CLI11_INLINE void retire_option(App *app, const std::string &option_name);
1524
1526CLI11_INLINE void retire_option(App &app, const std::string &option_name);
1527
1528namespace detail {
1531#ifdef CLI11_CPP14
1532
1534 template <typename... Args> static decltype(auto) parse_arg(App *app, Args &&...args) {
1535 return app->_parse_arg(std::forward<Args>(args)...);
1536 }
1537
1539 template <typename... Args> static decltype(auto) parse_subcommand(App *app, Args &&...args) {
1540 return app->_parse_subcommand(std::forward<Args>(args)...);
1541 }
1542#else
1544 template <typename... Args>
1545 static auto parse_arg(App *app, Args &&...args) ->
1546 typename std::result_of<decltype (&App::_parse_arg)(App, Args...)>::type {
1547 return app->_parse_arg(std::forward<Args>(args)...);
1548 }
1549
1551 template <typename... Args>
1552 static auto parse_subcommand(App *app, Args &&...args) ->
1553 typename std::result_of<decltype (&App::_parse_subcommand)(App, Args...)>::type {
1554 return app->_parse_subcommand(std::forward<Args>(args)...);
1555 }
1556#endif
1559
1561 static const App *get_fallthrough_parent(const App *app) { return app->_get_fallthrough_parent(); }
1562};
1563} // namespace detail
1564
1565// [CLI11:app_hpp:end]
1566} // namespace CLI
1567
1568#ifndef CLI11_COMPILE
1569#include "impl/App_inl.hpp" // IWYU pragma: export
1570#endif
Creates a command line program, with very few defaults.
Definition App.hpp:114
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:916
bool subcommand_fallthrough_
Allow subcommands to fallthrough, so that parent commands can trigger other subcommands after subcomm...
Definition App.hpp:256
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:694
CLI11_NODISCARD std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
Definition App_inl.hpp:790
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:1064
const Option * operator[](const std::string &option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1148
CLI11_NODISCARD bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Definition App_inl.hpp:1842
App * configurable(bool value=true)
Specify that the subcommand can be triggered by a config file.
Definition App.hpp:533
CLI11_NODISCARD bool get_allow_subcommand_prefix_matching() const
Get the status of allowing prefix matching for subcommands.
Definition App.hpp:1224
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:1177
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:288
App * usage(std::function< std::string()> usage_function)
Set usage.
Definition App.hpp:1063
CLI11_NODISCARD bool get_enabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1233
Option * config_ptr_
Pointer to the config option.
Definition App.hpp:322
CLI11_NODISCARD bool get_allow_non_standard_option_names() const
Get the status of allowing non standard option names.
Definition App.hpp:1221
CLI11_NODISCARD bool get_configurable() const
Check the status of the allow windows style options.
Definition App.hpp:1172
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:617
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1178
CLI11_NODISCARD std::shared_ptr< FormatterBase > get_formatter() const
Access the formatter.
Definition App.hpp:1105
Option * add_option(std::string option_name)
Add option with no description or variable assignment.
Definition App.hpp:656
App * subcommand_fallthrough(bool value=true)
Set subcommand fallthrough, set to true so that subcommands on parents are recognized.
Definition App.hpp:909
App * config_formatter(std::shared_ptr< Config > fmt)
Set the config formatter.
Definition App.hpp:554
App * allow_config_extras(bool allow=true)
ignore extras in config files
Definition App.hpp:480
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:443
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:2412
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:2335
std::size_t require_option_min_
Minimum required options (not inheritable!)
Definition App.hpp:303
NameMatch
enumeration of matching possibilities
Definition App.hpp:1291
App * silent(bool silence=true)
silence the subcommand from showing up in the processed list
Definition App.hpp:426
App * clear_aliases()
clear all the aliases of the current App
Definition App.hpp:1278
CLI11_NODISCARD bool get_ignore_underscore() const
Check the status of ignore_underscore.
Definition App.hpp:1157
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:402
App * fallthrough(bool value=true)
Definition App.hpp:903
std::size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE.
Definition App.hpp:300
Option * get_config_ptr()
Get a pointer to the config option.
Definition App.hpp:1254
std::vector< App_p > subcommands_
Storage for subcommand list.
Definition App.hpp:243
CLI11_NODISCARD std::vector< std::string > remaining(bool recurse=false) const
This returns the missing options from the current subcommand.
Definition App_inl.hpp:1032
CLI11_NODISCARD bool get_allow_extras() const
Get the status of allow extras.
Definition App.hpp:1206
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:1058
std::uint32_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition App.hpp:294
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:844
std::string usage_
Usage to put after program/subcommand description in the help output INHERITABLE.
Definition App.hpp:180
OptionDefaults option_defaults_
The default values for options, customizable and changeable INHERITABLE.
Definition App.hpp:170
CLI11_NODISCARD const Option * get_version_ptr() const
Get a pointer to the version option. (const)
Definition App.hpp:1263
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition App_inl.hpp:1346
App * allow_extras(ExtrasMode allow)
Remove the error when extras are left over on the command line.
Definition App.hpp:408
CLI11_NODISCARD std::size_t count_all() const
Definition App_inl.hpp:564
void _parse_setup()
Definition App_inl.hpp:655
CLI11_NODISCARD bool get_fallthrough() const
Check the status of fallthrough.
Definition App.hpp:1160
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition App.hpp:147
CLI11_NODISCARD bool get_prefix_command() const
Get the prefix command status.
Definition App.hpp:1200
Option * add_option(std::string option_name, AssignTo &variable, std::string option_description="")
Add option for assigning to a variable.
Definition App.hpp:593
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1191
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:144
bool remove_needs(Option *opt)
Removes an option from the needs list of this subcommand.
Definition App_inl.hpp:772
const Option * operator[](const char *option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1151
CLI11_NODISCARD bool get_immediate_callback() const
Get the status of disabled.
Definition App.hpp:1227
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1245
App * require_subcommand(int value)
Definition App.hpp:853
void _configure()
Definition App_inl.hpp:1113
CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
Definition App_inl.hpp:1830
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:563
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:1234
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition App.hpp:186
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
Definition App_inl.hpp:1533
CLI11_NODISCARD bool check_name(std::string name_to_check) const
Definition App_inl.hpp:976
CLI11_NODISCARD const Option * get_option(std::string option_name) const
Get an option by name.
Definition App_inl.hpp:887
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1183
App * required(bool require=true)
Set whether this subcommand/option-group is required to be present on the command line.
Definition App.hpp:414
Option * version_ptr_
A pointer to a version flag if there is one.
Definition App.hpp:198
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition App.hpp:1251
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:309
Option * add_flag(std::string flag_name, T &&flag_description)
Definition App.hpp:700
std::set< Option * > exclude_options_
Definition App.hpp:228
void _trigger_pre_parse(std::size_t remaining_args)
Trigger the pre_parse callback if needed.
Definition App_inl.hpp:2317
App * group(std::string group_name)
Changes the group membership.
Definition App.hpp:838
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:189
ExtrasMode allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition App.hpp:131
PrefixCommandMode prefix_command_
If true, cease processing on an unrecognized option (implies allow_extras) INHERITABLE.
Definition App.hpp:138
std::function< void()> parse_complete_callback_
This is a function that runs when parsing has finished.
Definition App.hpp:160
CLI11_NODISCARD bool get_subcommand_fallthrough() const
Check the status of subcommand fallthrough.
Definition App.hpp:1163
virtual void pre_callback()
Definition App.hpp:925
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:773
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1266
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition App.hpp:690
void _validate() const
Definition App_inl.hpp:1078
std::string name_
Subcommand name or program name (from parser if name is empty)
Definition App.hpp:125
std::vector< App * > parsed_subcommands_
This is a list of the subcommands collected, in order.
Definition App.hpp:221
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:345
CLI11_NODISCARD const Option * get_help_ptr() const
Get a pointer to the help flag. (const)
Definition App.hpp:1248
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition App.hpp:249
App * allow_windows_style_options(bool value=true)
Definition App.hpp:521
missing_t missing_
Definition App.hpp:215
CLI11_NODISCARD bool get_validate_positionals() const
Get the status of validating positionals.
Definition App.hpp:1235
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:635
void run_callback(bool final_mode=false, bool suppress_final_callback=false)
Internal function to run (App) callback, bottom up.
Definition App_inl.hpp:1133
bool allow_prefix_matching_
indicator to allow subcommands to match with prefix matching
Definition App.hpp:291
std::size_t require_subcommand_min_
Minimum required subcommands (not inheritable!)
Definition App.hpp:297
App * usage(std::string usage_string)
Set usage.
Definition App.hpp:1058
CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const
Definition App_inl.hpp:981
App * allow_subcommand_prefix_matching(bool allowed=true)
allow prefix matching for subcommands
Definition App.hpp:438
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
Definition App_inl.hpp:1270
std::function< std::string(const App *, const Error &e)> failure_message_
The error message printing function INHERITABLE.
Definition App.hpp:204
void _parse_stream(std::istream &input)
Internal function to parse a stream.
Definition App_inl.hpp:1610
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:960
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
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:141
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:2357
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:1197
App * enabled_by_default(bool enable=true)
Definition App.hpp:454
App * footer(std::string footer_string)
Set footer.
Definition App.hpp:1068
App * get_subcommand(const App *subcom) const
Definition App_inl.hpp:501
App * require_option(int value)
Definition App.hpp:882
CLI11_NODISCARD std::string version() const
Displays a version string.
Definition App_inl.hpp:804
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:972
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:1629
std::vector< Option_p > options_
The list of options, stored locally.
Definition App.hpp:173
Option * help_all_ptr_
A pointer to the help all flag if there is one INHERITABLE.
Definition App.hpp:195
bool validate_optional_arguments_
If set to true optional vector arguments are validated before assigning INHERITABLE.
Definition App.hpp:281
std::function< void()> final_callback_
This is a function that runs when all processing has completed.
Definition App.hpp:163
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:873
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:183
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:390
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:267
void _process()
Process callbacks and such.
Definition App_inl.hpp:1474
bool immediate_callback_
Definition App.hpp:154
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
Definition App_inl.hpp:1781
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:2435
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1212
void _process_extras()
Throw an error if anything is left over and should not be.
Definition App_inl.hpp:1513
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:1160
CLI11_NODISCARD PrefixCommandMode get_prefix_command_mode() const
Get the prefix command status.
Definition App.hpp:1203
CLI11_NODISCARD bool get_ignore_case() const
Check the status of ignore_case.
Definition App.hpp:1154
App * parse_complete_callback(std::function< void()> pc_callback)
Definition App.hpp:383
bool configurable_
if set to true the subcommand can be triggered via configuration files INHERITABLE
Definition App.hpp:275
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:1019
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
App * formatter_fn(std::function< std::string(const App *, std::string, AppFormatMode)> fmt)
Set the help formatter.
Definition App.hpp:548
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1275
void _parse_config(const std::vector< ConfigItem > &args)
Definition App_inl.hpp:1621
App * description(std::string app_description)
Set the description of the app.
Definition App.hpp:1124
CLI11_NODISCARD std::shared_ptr< ConfigBase > get_config_formatter_base() const
Access the config formatter as a configBase pointer.
Definition App.hpp:1111
std::string description_
Description of the current program/subcommand.
Definition App.hpp:128
App * excludes(App *app)
Sets excluded subcommands for the subcommand.
Definition App.hpp:1008
App * allow_non_standard_option_names(bool allowed=true)
allow non standard option names
Definition App.hpp:432
CLI11_NODISCARD bool get_positionals_at_end() const
Check the status of the allow windows style options.
Definition App.hpp:1169
Option * get_version_ptr()
Get a pointer to the version option.
Definition App.hpp:1260
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1194
std::size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable)
Definition App.hpp:306
CLI11_NODISCARD bool get_disabled() const
Get the status of disabled.
Definition App.hpp:1215
CLI11_NODISCARD bool get_allow_windows_style_options() const
Check the status of the allow windows style options.
Definition App.hpp:1166
std::vector< std::string > aliases_
Alias names for the subcommand.
Definition App.hpp:315
CLI11_NODISCARD bool parsed() const
Check to see if this subcommand was parsed, true only if received on command line.
Definition App.hpp:560
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition App.hpp:224
void _process_completion_callbacks(bool with_help_flags)
Definition App_inl.hpp:1541
CLI11_NODISCARD bool get_disabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1230
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:726
ConfigExtrasMode allow_config_extras_
Definition App.hpp:135
CLI11_NODISCARD std::size_t count() const
Definition App.hpp:831
bool _parse_positional(std::vector< std::string > &args, bool haltOnSubcommand)
Definition App_inl.hpp:1852
bool ignore_case_
If true, the program name is not case-sensitive INHERITABLE.
Definition App.hpp:246
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1175
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type, bool local_processing_only)
Definition App_inl.hpp:2064
bool silent_
Definition App.hpp:285
CLI11_NODISCARD const App * get_parent() const
Get the parent of this subcommand (or nullptr if main app) (const version)
Definition App.hpp:1269
std::function< void(std::size_t)> pre_parse_callback_
This is a function that runs prior to the start of parsing.
Definition App.hpp:157
App * final_callback(std::function< void()> app_callback)
Definition App.hpp:376
std::string group_
The group membership INHERITABLE.
Definition App.hpp:312
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:150
CLI11_NODISCARD ExtrasMode get_allow_extras_mode() const
Get the mode of allow_extras.
Definition App.hpp:1209
App * validate_positionals(bool validate=true)
Set the subcommand to validate positional arguments before assigning.
Definition App.hpp:468
Option * help_ptr_
A pointer to the help flag if there is one INHERITABLE.
Definition App.hpp:192
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:1073
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:999
CLI11_NODISCARD std::shared_ptr< Config > get_config_formatter() const
Access the config formatter.
Definition App.hpp:1108
App * prefix_command(PrefixCommandMode mode)
Set the prefix command mode directly.
Definition App.hpp:511
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
Definition App_inl.hpp:752
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:976
bool got_subcommand(const App *subcom) const
Check to see if given subcommand was selected.
Definition App.hpp:987
CLI11_NODISCARD config_extras_mode get_allow_config_extras() const
Get the status of allow extras.
Definition App.hpp:1240
App * allow_config_extras(ConfigExtrasMode mode)
ignore extras in config files
Definition App.hpp:497
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:1082
bool _parse_subcommand(std::vector< std::string > &args)
Definition App_inl.hpp:2024
App * positionals_at_end(bool value=true)
Specify that the positional arguments are only at the end of the sequence.
Definition App.hpp:527
bool fallthrough_
Definition App.hpp:253
std::set< Option * > need_options_
Definition App.hpp:236
CLI11_NODISCARD bool get_validate_optional_arguments() const
Get the status of validating optional vector arguments.
Definition App.hpp:1237
CLI11_NODISCARD bool get_silent() const
Get the status of silence.
Definition App.hpp:1218
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:822
std::set< App * > need_subcommands_
Definition App.hpp:232
App * validate_optional_arguments(bool validate=true)
Set the subcommand to validate optional vector arguments before assigning.
Definition App.hpp:474
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:542
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition App.hpp:218
Option * add_option(std::string option_name, T &option_description)
Add option with description but with no variable assignment or callback.
Definition App.hpp:664
App * prefix_command(bool is_prefix=true)
Definition App.hpp:505
CLI11_NODISCARD const std::vector< Option * > & parse_order() const
This gets a vector of pointers with the original parse order.
Definition App.hpp:1302
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition App_inl.hpp:1569
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition App.hpp:278
bool _parse_single_config(const ConfigItem &item, std::size_t level=0)
Fill in a single config option.
Definition App_inl.hpp:1690
void _process_help_flags(CallbackPriority priority, bool trigger_help=false, bool trigger_all_help=false) const
Definition App_inl.hpp:1321
startup_mode default_startup
Definition App.hpp:272
App * allow_config_extras(config_extras_mode mode)
ignore extras in config files
Definition App.hpp:491
Option * add_flag(std::string flag_name, T &flag_result, std::string flag_description="")
Definition App.hpp:710
void _process_callbacks(CallbackPriority priority)
Process callbacks. Runs on all subcommands.
Definition App_inl.hpp:1292
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1121
App * require_option(std::size_t min, std::size_t max)
Definition App.hpp:895
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:1988
CLI11_NODISCARD std::string config_to_str(bool default_also, bool write_description=false) const
Definition App.hpp:1089
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1188
CLI11_NODISCARD const Option * get_config_ptr() const
Get a pointer to the config option. (const)
Definition App.hpp:1257
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1272
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition App.hpp:420
App * callback(std::function< void()> app_callback)
Definition App.hpp:365
std::shared_ptr< FormatterBase > formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
Definition App.hpp:201
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:1079
App * require_subcommand(std::size_t min, std::size_t max)
Definition App.hpp:866
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise....
Definition App.hpp:259
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
Definition App.hpp:325
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:101
All errors derive from this one.
Definition Error.hpp:73
Definition FormatterFwd.hpp:196
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:1451
App * add_subcommand(App *subcom)
Add an existing subcommand to be a member of an option_group.
Definition App.hpp:1481
Option * add_option(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1465
void add_options(Option *opt, Args... args)
Add a bunch of options to the group.
Definition App.hpp:1475
void add_options(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1473
Definition Option.hpp:216
Definition Option.hpp:259
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:506
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:38
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:255
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:814
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:431
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:1530
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:1552
static App * get_fallthrough_parent(App *app)
Wrap the fallthrough parent function to make sure that is working correctly.
Definition App.hpp:1558
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:1545
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:1561
This will only trigger for actual void type.
Definition TypeTools.hpp:504