CLI11
C++11 Command Line Interface Parser
Loading...
Searching...
No Matches
App.hpp
1// Copyright (c) 2017-2024, 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 { NONE, POSITIONAL_MARK, SHORT, LONG, WINDOWS_STYLE, SUBCOMMAND, SUBCOMMAND_TERMINATOR };
50struct AppFriend;
51} // namespace detail
52
53namespace FailureMessage {
55CLI11_INLINE std::string simple(const App *app, const Error &e);
56
58CLI11_INLINE std::string help(const App *app, const Error &e);
59} // namespace FailureMessage
60
62
63enum class config_extras_mode : char { error = 0, ignore, ignore_all, capture };
64
65class App;
66
67using App_p = std::shared_ptr<App>;
68
69namespace detail {
71
72template <typename T, enable_if_t<!std::is_integral<T>::value || (sizeof(T) <= 1U), detail::enabler> = detail::dummy>
73Option *default_flag_modifiers(Option *opt) {
74 return opt->always_capture_default();
75}
76
78template <typename T, enable_if_t<std::is_integral<T>::value && (sizeof(T) > 1U), detail::enabler> = detail::dummy>
79Option *default_flag_modifiers(Option *opt) {
80 return opt->multi_option_policy(MultiOptionPolicy::Sum)->default_str("0")->force_callback();
81}
82
83} // namespace detail
84
85class Option_group;
87
90class App {
91 friend Option;
92 friend detail::AppFriend;
93
94 protected:
95 // This library follows the Google style guide for member names ending in underscores
96
99
101 std::string name_{};
102
104 std::string description_{};
105
107 bool allow_extras_{false};
108
111 config_extras_mode allow_config_extras_{config_extras_mode::ignore};
112
114 bool prefix_command_{false};
115
118
120 bool required_{false};
121
123 bool disabled_{false};
124
126 bool pre_parse_called_{false};
127
131
133 std::function<void(std::size_t)> pre_parse_callback_{};
134
136 std::function<void()> parse_complete_callback_{};
137
139 std::function<void()> final_callback_{};
140
144
147
149 std::vector<Option_p> options_{};
150
154
156 std::string usage_{};
157
159 std::function<std::string()> usage_callback_{};
160
162 std::string footer_{};
163
165 std::function<std::string()> footer_callback_{};
166
168 Option *help_ptr_{nullptr};
169
172
175
177 std::shared_ptr<FormatterBase> formatter_{new Formatter()};
178
180 std::function<std::string(const App *, const Error &e)> failure_message_{FailureMessage::simple};
181
185
186 using missing_t = std::vector<std::pair<detail::Classifier, std::string>>;
187
191 missing_t missing_{};
192
194 std::vector<Option *> parse_order_{};
195
197 std::vector<App *> parsed_subcommands_{};
198
200 std::set<App *> exclude_subcommands_{};
201
204 std::set<Option *> exclude_options_{};
205
208 std::set<App *> need_subcommands_{};
209
212 std::set<Option *> need_options_{};
213
217
219 std::vector<App_p> subcommands_{};
220
222 bool ignore_case_{false};
223
226
228 bool fallthrough_{false};
229
232#ifdef _WIN32
233 true
234#else
235 false
236#endif
237 };
240
241 enum class startup_mode : char { stable, enabled, disabled };
244 startup_mode default_startup{startup_mode::stable};
245
247 bool configurable_{false};
248
251
254
257 bool silent_{false};
258
260 std::uint32_t parsed_{0U};
261
264
267
269 std::size_t require_option_min_{0};
270
272 std::size_t require_option_max_{0};
273
275 App *parent_{nullptr};
276
278 std::string group_{"Subcommands"};
279
281 std::vector<std::string> aliases_{};
282
286
289
291 std::shared_ptr<Config> config_formatter_{new ConfigTOML()};
292
294
295#ifdef _WIN32
297 std::vector<std::string> normalized_argv_{};
298
300 std::vector<char *> normalized_argv_view_{};
301#endif
302
304 App(std::string app_description, std::string app_name, App *parent);
305
306 public:
309
311 explicit App(std::string app_description = "", std::string app_name = "")
312 : App(app_description, app_name, nullptr) {
313 set_help_flag("-h,--help", "Print this help message and exit");
314 }
315
316 App(const App &) = delete;
317 App &operator=(const App &) = delete;
318
320 virtual ~App() = default;
321
323 CLI11_NODISCARD char **ensure_utf8(char **argv);
324
331 App *callback(std::function<void()> app_callback) {
333 parse_complete_callback_ = std::move(app_callback);
334 } else {
335 final_callback_ = std::move(app_callback);
336 }
337 return this;
338 }
339
342 App *final_callback(std::function<void()> app_callback) {
343 final_callback_ = std::move(app_callback);
344 return this;
345 }
346
349 App *parse_complete_callback(std::function<void()> pc_callback) {
350 parse_complete_callback_ = std::move(pc_callback);
351 return this;
352 }
353
356 App *preparse_callback(std::function<void(std::size_t)> pp_callback) {
357 pre_parse_callback_ = std::move(pp_callback);
358 return this;
359 }
360
362 App *name(std::string app_name = "");
363
365 App *alias(std::string app_name);
366
368 App *allow_extras(bool allow = true) {
369 allow_extras_ = allow;
370 return this;
371 }
372
374 App *required(bool require = true) {
375 required_ = require;
376 return this;
377 }
378
380 App *disabled(bool disable = true) {
381 disabled_ = disable;
382 return this;
383 }
384
386 App *silent(bool silence = true) {
387 silent_ = silence;
388 return this;
389 }
390
392 App *disabled_by_default(bool disable = true) {
393 if(disable) {
394 default_startup = startup_mode::disabled;
395 } else {
396 default_startup = (default_startup == startup_mode::enabled) ? startup_mode::enabled : startup_mode::stable;
397 }
398 return this;
399 }
400
403 App *enabled_by_default(bool enable = true) {
404 if(enable) {
405 default_startup = startup_mode::enabled;
406 } else {
408 (default_startup == startup_mode::disabled) ? startup_mode::disabled : startup_mode::stable;
409 }
410 return this;
411 }
412
414 App *immediate_callback(bool immediate = true);
415
417 App *validate_positionals(bool validate = true) {
418 validate_positionals_ = validate;
419 return this;
420 }
421
423 App *validate_optional_arguments(bool validate = true) {
425 return this;
426 }
427
429 App *allow_config_extras(bool allow = true) {
430 if(allow) {
431 allow_config_extras_ = config_extras_mode::capture;
432 allow_extras_ = true;
433 } else {
434 allow_config_extras_ = config_extras_mode::error;
435 }
436 return this;
437 }
438
440 App *allow_config_extras(config_extras_mode mode) {
442 return this;
443 }
444
446 App *prefix_command(bool allow = true) {
447 prefix_command_ = allow;
448 return this;
449 }
450
452 App *ignore_case(bool value = true);
453
456 App *allow_windows_style_options(bool value = true) {
458 return this;
459 }
460
462 App *positionals_at_end(bool value = true) {
463 positionals_at_end_ = value;
464 return this;
465 }
466
468 App *configurable(bool value = true) {
469 configurable_ = value;
470 return this;
471 }
472
474 App *ignore_underscore(bool value = true);
475
477 App *formatter(std::shared_ptr<FormatterBase> fmt) {
478 formatter_ = fmt;
479 return this;
480 }
481
483 App *formatter_fn(std::function<std::string(const App *, std::string, AppFormatMode)> fmt) {
484 formatter_ = std::make_shared<FormatterLambda>(fmt);
485 return this;
486 }
487
489 App *config_formatter(std::shared_ptr<Config> fmt) {
490 config_formatter_ = fmt;
491 return this;
492 }
493
495 CLI11_NODISCARD bool parsed() const { return parsed_ > 0; }
496
499
503
518 Option *add_option(std::string option_name,
519 callback_t option_callback,
520 std::string option_description = "",
521 bool defaulted = false,
522 std::function<std::string()> func = {});
523
525 template <typename AssignTo,
526 typename ConvertTo = AssignTo,
527 enable_if_t<!std::is_const<ConvertTo>::value, detail::enabler> = detail::dummy>
528 Option *add_option(std::string option_name,
529 AssignTo &variable,
530 std::string option_description = "") {
531
532 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
533 return detail::lexical_conversion<AssignTo, ConvertTo>(res, variable);
534 };
535
536 Option *opt = add_option(option_name, fun, option_description, false, [&variable]() {
537 return CLI::detail::checked_to_string<AssignTo, ConvertTo>(variable);
538 });
539 opt->type_name(detail::type_name<ConvertTo>());
540 // these must be actual lvalues since (std::max) sometimes is defined in terms of references and references
541 // to structs used in the evaluation can be temporary so that would cause issues.
544 opt->type_size(detail::type_count_min<ConvertTo>::value, (std::max)(Tcount, XCcount));
545 opt->expected(detail::expected_count<ConvertTo>::value);
546 opt->run_callback_for_default();
547 return opt;
548 }
549
551 template <typename AssignTo, enable_if_t<!std::is_const<AssignTo>::value, detail::enabler> = detail::dummy>
552 Option *add_option_no_stream(std::string option_name,
553 AssignTo &variable,
554 std::string option_description = "") {
555
556 auto fun = [&variable](const CLI::results_t &res) { // comment for spacing
557 return detail::lexical_conversion<AssignTo, AssignTo>(res, variable);
558 };
559
560 Option *opt = add_option(option_name, fun, option_description, false, []() { return std::string{}; });
561 opt->type_name(detail::type_name<AssignTo>());
562 opt->type_size(detail::type_count_min<AssignTo>::value, detail::type_count<AssignTo>::value);
563 opt->expected(detail::expected_count<AssignTo>::value);
565 return opt;
566 }
567
569 template <typename ArgType>
570 Option *add_option_function(std::string option_name,
571 const std::function<void(const ArgType &)> &func,
572 std::string option_description = "") {
573
574 auto fun = [func](const CLI::results_t &res) {
575 ArgType variable;
576 bool result = detail::lexical_conversion<ArgType, ArgType>(res, variable);
577 if(result) {
578 func(variable);
579 }
580 return result;
581 };
582
583 Option *opt = add_option(option_name, std::move(fun), option_description, false);
584 opt->type_name(detail::type_name<ArgType>());
585 opt->type_size(detail::type_count_min<ArgType>::value, detail::type_count<ArgType>::value);
586 opt->expected(detail::expected_count<ArgType>::value);
587 return opt;
588 }
589
591 Option *add_option(std::string option_name) {
592 return add_option(option_name, CLI::callback_t{}, std::string{}, false);
593 }
594
596 template <typename T,
597 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
598 detail::dummy>
599 Option *add_option(std::string option_name, T &option_description) {
600 return add_option(option_name, CLI::callback_t(), option_description, false);
601 }
602
604 Option *set_help_flag(std::string flag_name = "", const std::string &help_description = "");
605
607 Option *set_help_all_flag(std::string help_name = "", const std::string &help_description = "");
608
610 Option *set_version_flag(std::string flag_name = "",
611 const std::string &versionString = "",
612 const std::string &version_help = "Display program version information and exit");
613
615 Option *set_version_flag(std::string flag_name,
616 std::function<std::string()> vfunc,
617 const std::string &version_help = "Display program version information and exit");
618
619 private:
621 Option *_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description);
622
623 public:
625 Option *add_flag(std::string flag_name) { return _add_flag_internal(flag_name, CLI::callback_t(), std::string{}); }
626
630 template <typename T,
631 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
632 detail::dummy>
633 Option *add_flag(std::string flag_name, T &flag_description) {
634 return _add_flag_internal(flag_name, CLI::callback_t(), flag_description);
635 }
636
639 template <typename T,
640 enable_if_t<!detail::is_mutable_container<T>::value && !std::is_const<T>::value &&
641 !std::is_constructible<std::function<void(int)>, T>::value,
642 detail::enabler> = detail::dummy>
643 Option *add_flag(std::string flag_name,
644 T &flag_result,
645 std::string flag_description = "") {
646
647 CLI::callback_t fun = [&flag_result](const CLI::results_t &res) {
648 using CLI::detail::lexical_cast;
649 return lexical_cast(res[0], flag_result);
650 };
651 auto *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
652 return detail::default_flag_modifiers<T>(opt);
653 }
654
656 template <typename T,
657 enable_if_t<!std::is_assignable<std::function<void(std::int64_t)> &, T>::value, detail::enabler> =
658 detail::dummy>
659 Option *add_flag(std::string flag_name,
660 std::vector<T> &flag_results,
661 std::string flag_description = "") {
662 CLI::callback_t fun = [&flag_results](const CLI::results_t &res) {
663 bool retval = true;
664 for(const auto &elem : res) {
665 using CLI::detail::lexical_cast;
666 flag_results.emplace_back();
667 retval &= lexical_cast(elem, flag_results.back());
668 }
669 return retval;
670 };
671 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
672 ->multi_option_policy(MultiOptionPolicy::TakeAll)
674 }
675
677 Option *add_flag_callback(std::string flag_name,
678 std::function<void(void)> function,
679 std::string flag_description = "");
680
682 Option *add_flag_function(std::string flag_name,
683 std::function<void(std::int64_t)> function,
684 std::string flag_description = "");
685
686#ifdef CLI11_CPP14
688 Option *add_flag(std::string flag_name,
689 std::function<void(std::int64_t)> function,
690 std::string flag_description = "") {
691 return add_flag_function(std::move(flag_name), std::move(function), std::move(flag_description));
692 }
693#endif
694
696 Option *set_config(std::string option_name = "",
697 std::string default_filename = "",
698 const std::string &help_message = "Read an ini file",
699 bool config_required = false);
700
702 bool remove_option(Option *opt);
703
705 template <typename T = Option_group>
706 T *add_option_group(std::string group_name, std::string group_description = "") {
707 if(!detail::valid_alias_name_string(group_name)) {
708 throw IncorrectConstruction("option group names may not contain newlines or null characters");
709 }
710 auto option_group = std::make_shared<T>(std::move(group_description), group_name, this);
711 auto *ptr = option_group.get();
712 // move to App_p for overload resolution on older gcc versions
713 App_p app_ptr = std::dynamic_pointer_cast<App>(option_group);
714 add_subcommand(std::move(app_ptr));
715 return ptr;
716 }
717
721
723 App *add_subcommand(std::string subcommand_name = "", std::string subcommand_description = "");
724
726 App *add_subcommand(CLI::App_p subcom);
727
729 bool remove_subcommand(App *subcom);
730
733 App *get_subcommand(const App *subcom) const;
734
736 CLI11_NODISCARD App *get_subcommand(std::string subcom) const;
737
740 CLI11_NODISCARD App *get_subcommand_no_throw(std::string subcom) const noexcept;
741
743 CLI11_NODISCARD App *get_subcommand(int index = 0) const;
744
746 CLI::App_p get_subcommand_ptr(App *subcom) const;
747
749 CLI11_NODISCARD CLI::App_p get_subcommand_ptr(std::string subcom) const;
750
752 CLI11_NODISCARD CLI::App_p get_subcommand_ptr(int index = 0) const;
753
755 CLI11_NODISCARD App *get_option_group(std::string group_name) const;
756
760 CLI11_NODISCARD std::size_t count() const { return parsed_; }
761
764 CLI11_NODISCARD std::size_t count_all() const;
765
767 App *group(std::string group_name) {
768 group_ = group_name;
769 return this;
770 }
771
776 return this;
777 }
778
783 if(value < 0) {
785 require_subcommand_max_ = static_cast<std::size_t>(-value);
786 } else {
787 require_subcommand_min_ = static_cast<std::size_t>(value);
788 require_subcommand_max_ = static_cast<std::size_t>(value);
789 }
790 return this;
791 }
792
795 App *require_subcommand(std::size_t min, std::size_t max) {
798 return this;
799 }
800
805 return this;
806 }
807
811 App *require_option(int value) {
812 if(value < 0) {
814 require_option_max_ = static_cast<std::size_t>(-value);
815 } else {
816 require_option_min_ = static_cast<std::size_t>(value);
817 require_option_max_ = static_cast<std::size_t>(value);
818 }
819 return this;
820 }
821
824 App *require_option(std::size_t min, std::size_t max) {
827 return this;
828 }
829
832 App *fallthrough(bool value = true) {
833 fallthrough_ = value;
834 return this;
835 }
836
839 explicit operator bool() const { return parsed_ > 0; }
840
844
848 virtual void pre_callback() {}
849
853 //
855 void clear();
856
859 void parse(int argc, const char *const *argv);
860 void parse(int argc, const wchar_t *const *argv);
861
862 private:
863 template <class CharT> void parse_char_t(int argc, const CharT *const *argv);
864
865 public:
870 void parse(std::string commandline, bool program_name_included = false);
871 void parse(std::wstring commandline, bool program_name_included = false);
872
875 void parse(std::vector<std::string> &args);
876
878 void parse(std::vector<std::string> &&args);
879
880 void parse_from_stream(std::istream &input);
881
883 void failure_message(std::function<std::string(const App *, const Error &e)> function) {
884 failure_message_ = function;
885 }
886
888 int exit(const Error &e, std::ostream &out = std::cout, std::ostream &err = std::cerr) const;
889
893
895 CLI11_NODISCARD std::size_t count(std::string option_name) const { return get_option(option_name)->count(); }
896
899 CLI11_NODISCARD std::vector<App *> get_subcommands() const { return parsed_subcommands_; }
900
903 std::vector<const App *> get_subcommands(const std::function<bool(const App *)> &filter) const;
904
907 std::vector<App *> get_subcommands(const std::function<bool(App *)> &filter);
908
910 bool got_subcommand(const App *subcom) const {
911 // get subcom needed to verify that this was a real subcommand
912 return get_subcommand(subcom)->parsed_ > 0;
913 }
914
916 CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const noexcept {
917 App *sub = get_subcommand_no_throw(subcommand_name);
918 return (sub != nullptr) ? (sub->parsed_ > 0) : false;
919 }
920
923 if(opt == nullptr) {
924 throw OptionNotFound("nullptr passed");
925 }
926 exclude_options_.insert(opt);
927 return this;
928 }
929
931 App *excludes(App *app) {
932 if(app == nullptr) {
933 throw OptionNotFound("nullptr passed");
934 }
935 if(app == this) {
936 throw OptionNotFound("cannot self reference in needs");
937 }
938 auto res = exclude_subcommands_.insert(app);
939 // subcommand exclusion should be symmetric
940 if(res.second) {
941 app->exclude_subcommands_.insert(this);
942 }
943 return this;
944 }
945
946 App *needs(Option *opt) {
947 if(opt == nullptr) {
948 throw OptionNotFound("nullptr passed");
949 }
950 need_options_.insert(opt);
951 return this;
952 }
953
954 App *needs(App *app) {
955 if(app == nullptr) {
956 throw OptionNotFound("nullptr passed");
957 }
958 if(app == this) {
959 throw OptionNotFound("cannot self reference in needs");
960 }
961 need_subcommands_.insert(app);
962 return this;
963 }
964
966 bool remove_excludes(Option *opt);
967
969 bool remove_excludes(App *app);
970
972 bool remove_needs(Option *opt);
973
975 bool remove_needs(App *app);
979
981 App *usage(std::string usage_string) {
982 usage_ = std::move(usage_string);
983 return this;
984 }
986 App *usage(std::function<std::string()> usage_function) {
987 usage_callback_ = std::move(usage_function);
988 return this;
989 }
991 App *footer(std::string footer_string) {
992 footer_ = std::move(footer_string);
993 return this;
994 }
996 App *footer(std::function<std::string()> footer_function) {
997 footer_callback_ = std::move(footer_function);
998 return this;
999 }
1002 CLI11_NODISCARD std::string config_to_str(bool default_also = false, bool write_description = false) const {
1003 return config_formatter_->to_config(this, default_also, write_description, "");
1004 }
1005
1008 CLI11_NODISCARD std::string help(std::string prev = "", AppFormatMode mode = AppFormatMode::Normal) const;
1009
1011 CLI11_NODISCARD std::string version() const;
1015
1017 CLI11_NODISCARD std::shared_ptr<FormatterBase> get_formatter() const { return formatter_; }
1018
1020 CLI11_NODISCARD std::shared_ptr<Config> get_config_formatter() const { return config_formatter_; }
1021
1023 CLI11_NODISCARD std::shared_ptr<ConfigBase> get_config_formatter_base() const {
1024 // This is safer as a dynamic_cast if we have RTTI, as Config -> ConfigBase
1025#if CLI11_USE_STATIC_RTTI == 0
1026 return std::dynamic_pointer_cast<ConfigBase>(config_formatter_);
1027#else
1028 return std::static_pointer_cast<ConfigBase>(config_formatter_);
1029#endif
1030 }
1031
1033 CLI11_NODISCARD std::string get_description() const { return description_; }
1034
1036 App *description(std::string app_description) {
1037 description_ = std::move(app_description);
1038 return this;
1039 }
1040
1042 std::vector<const Option *> get_options(const std::function<bool(const Option *)> filter = {}) const;
1043
1045 std::vector<Option *> get_options(const std::function<bool(Option *)> filter = {});
1046
1048 CLI11_NODISCARD Option *get_option_no_throw(std::string option_name) noexcept;
1049
1051 CLI11_NODISCARD const Option *get_option_no_throw(std::string option_name) const noexcept;
1052
1054 CLI11_NODISCARD const Option *get_option(std::string option_name) const {
1055 const auto *opt = get_option_no_throw(option_name);
1056 if(opt == nullptr) {
1057 throw OptionNotFound(option_name);
1058 }
1059 return opt;
1060 }
1061
1063 Option *get_option(std::string option_name) {
1064 auto *opt = get_option_no_throw(option_name);
1065 if(opt == nullptr) {
1066 throw OptionNotFound(option_name);
1067 }
1068 return opt;
1069 }
1070
1072 const Option *operator[](const std::string &option_name) const { return get_option(option_name); }
1073
1075 const Option *operator[](const char *option_name) const { return get_option(option_name); }
1076
1078 CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
1079
1081 CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
1082
1084 CLI11_NODISCARD bool get_fallthrough() const { return fallthrough_; }
1085
1087 CLI11_NODISCARD bool get_allow_windows_style_options() const { return allow_windows_style_options_; }
1088
1090 CLI11_NODISCARD bool get_positionals_at_end() const { return positionals_at_end_; }
1091
1093 CLI11_NODISCARD bool get_configurable() const { return configurable_; }
1094
1096 CLI11_NODISCARD const std::string &get_group() const { return group_; }
1097
1099 CLI11_NODISCARD std::string get_usage() const {
1100 return (usage_callback_) ? usage_callback_() + '\n' + usage_ : usage_;
1101 }
1102
1104 CLI11_NODISCARD std::string get_footer() const {
1105 return (footer_callback_) ? footer_callback_() + '\n' + footer_ : footer_;
1106 }
1107
1109 CLI11_NODISCARD std::size_t get_require_subcommand_min() const { return require_subcommand_min_; }
1110
1112 CLI11_NODISCARD std::size_t get_require_subcommand_max() const { return require_subcommand_max_; }
1113
1115 CLI11_NODISCARD std::size_t get_require_option_min() const { return require_option_min_; }
1116
1118 CLI11_NODISCARD std::size_t get_require_option_max() const { return require_option_max_; }
1119
1121 CLI11_NODISCARD bool get_prefix_command() const { return prefix_command_; }
1122
1124 CLI11_NODISCARD bool get_allow_extras() const { return allow_extras_; }
1125
1127 CLI11_NODISCARD bool get_required() const { return required_; }
1128
1130 CLI11_NODISCARD bool get_disabled() const { return disabled_; }
1131
1133 CLI11_NODISCARD bool get_silent() const { return silent_; }
1134
1136 CLI11_NODISCARD bool get_immediate_callback() const { return immediate_callback_; }
1137
1139 CLI11_NODISCARD bool get_disabled_by_default() const { return (default_startup == startup_mode::disabled); }
1140
1142 CLI11_NODISCARD bool get_enabled_by_default() const { return (default_startup == startup_mode::enabled); }
1144 CLI11_NODISCARD bool get_validate_positionals() const { return validate_positionals_; }
1146 CLI11_NODISCARD bool get_validate_optional_arguments() const { return validate_optional_arguments_; }
1147
1149 CLI11_NODISCARD config_extras_mode get_allow_config_extras() const { return allow_config_extras_; }
1150
1153
1155 CLI11_NODISCARD const Option *get_help_ptr() const { return help_ptr_; }
1156
1158 CLI11_NODISCARD const Option *get_help_all_ptr() const { return help_all_ptr_; }
1159
1162
1164 CLI11_NODISCARD const Option *get_config_ptr() const { return config_ptr_; }
1165
1168
1170 CLI11_NODISCARD const Option *get_version_ptr() const { return version_ptr_; }
1171
1173 App *get_parent() { return parent_; }
1174
1176 CLI11_NODISCARD const App *get_parent() const { return parent_; }
1177
1179 CLI11_NODISCARD const std::string &get_name() const { return name_; }
1180
1182 CLI11_NODISCARD const std::vector<std::string> &get_aliases() const { return aliases_; }
1183
1186 aliases_.clear();
1187 return this;
1188 }
1189
1191 CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
1192
1194 CLI11_NODISCARD bool check_name(std::string name_to_check) const;
1195
1197 CLI11_NODISCARD std::vector<std::string> get_groups() const;
1198
1200 CLI11_NODISCARD const std::vector<Option *> &parse_order() const { return parse_order_; }
1201
1203 CLI11_NODISCARD std::vector<std::string> remaining(bool recurse = false) const;
1204
1206 CLI11_NODISCARD std::vector<std::string> remaining_for_passthrough(bool recurse = false) const;
1207
1209 CLI11_NODISCARD std::size_t remaining_size(bool recurse = false) const;
1210
1212
1213 protected:
1218 void _validate() const;
1219
1223 void _configure();
1224
1226 void run_callback(bool final_mode = false, bool suppress_final_callback = false);
1227
1229 CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used = true) const;
1230
1232 CLI11_NODISCARD detail::Classifier _recognize(const std::string &current,
1233 bool ignore_used_subcommands = true) const;
1234
1235 // The parse function is now broken into several parts, and part of process
1236
1238 void _process_config_file();
1239
1241 bool _process_config_file(const std::string &config_file, bool throw_error);
1242
1244 void _process_env();
1245
1247 void _process_callbacks();
1248
1252 void _process_help_flags(bool trigger_help = false, bool trigger_all_help = false) const;
1253
1255 void _process_requirements();
1256
1258 void _process();
1259
1261 void _process_extras();
1262
1265 void _process_extras(std::vector<std::string> &args);
1266
1268 void increment_parsed();
1269
1271 void _parse(std::vector<std::string> &args);
1272
1274 void _parse(std::vector<std::string> &&args);
1275
1277 void _parse_stream(std::istream &input);
1278
1283 void _parse_config(const std::vector<ConfigItem> &args);
1284
1286 bool _parse_single_config(const ConfigItem &item, std::size_t level = 0);
1287
1290 bool _parse_single(std::vector<std::string> &args, bool &positional_only);
1291
1293 CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only = false) const;
1294
1296 CLI11_NODISCARD bool _has_remaining_positionals() const;
1297
1301 bool _parse_positional(std::vector<std::string> &args, bool haltOnSubcommand);
1302
1305 CLI11_NODISCARD App *
1306 _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept;
1307
1312 bool _parse_subcommand(std::vector<std::string> &args);
1313
1317 bool _parse_arg(std::vector<std::string> &args, detail::Classifier current_type, bool local_processing_only);
1318
1320 void _trigger_pre_parse(std::size_t remaining_args);
1321
1324
1326 CLI11_NODISCARD const std::string &_compare_subcommand_names(const App &subcom, const App &base) const;
1327
1329 void _move_to_missing(detail::Classifier val_type, const std::string &val);
1330
1331 public:
1333 void _move_option(Option *opt, App *app);
1334}; // namespace CLI
1335
1337class Option_group : public App {
1338 public:
1339 Option_group(std::string group_description, std::string group_name, App *parent)
1340 : App(std::move(group_description), "", parent) {
1341 group(group_name);
1342 // option groups should have automatic fallthrough
1343 }
1344 using App::add_option;
1347 if(get_parent() == nullptr) {
1348 throw OptionNotFound("Unable to locate the specified option");
1349 }
1350 get_parent()->_move_option(opt, this);
1351 return opt;
1352 }
1354 void add_options(Option *opt) { add_option(opt); }
1356 template <typename... Args> void add_options(Option *opt, Args... args) {
1357 add_option(opt);
1358 add_options(args...);
1359 }
1360 using App::add_subcommand;
1363 App_p subc = subcom->get_parent()->get_subcommand_ptr(subcom);
1364 subc->get_parent()->remove_subcommand(subcom);
1365 add_subcommand(std::move(subc));
1366 return subcom;
1367 }
1368};
1369
1371CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable);
1372
1374CLI11_INLINE void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable);
1375
1377CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable);
1378
1380CLI11_INLINE void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable);
1381
1383CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement = "");
1384
1386inline void deprecate_option(App *app, const std::string &option_name, const std::string &replacement = "") {
1387 auto *opt = app->get_option(option_name);
1388 deprecate_option(opt, replacement);
1389}
1390
1392inline void deprecate_option(App &app, const std::string &option_name, const std::string &replacement = "") {
1393 auto *opt = app.get_option(option_name);
1394 deprecate_option(opt, replacement);
1395}
1396
1398CLI11_INLINE void retire_option(App *app, Option *opt);
1399
1401CLI11_INLINE void retire_option(App &app, Option *opt);
1402
1404CLI11_INLINE void retire_option(App *app, const std::string &option_name);
1405
1407CLI11_INLINE void retire_option(App &app, const std::string &option_name);
1408
1409namespace detail {
1412#ifdef CLI11_CPP14
1413
1415 template <typename... Args> static decltype(auto) parse_arg(App *app, Args &&...args) {
1416 return app->_parse_arg(std::forward<Args>(args)...);
1417 }
1418
1420 template <typename... Args> static decltype(auto) parse_subcommand(App *app, Args &&...args) {
1421 return app->_parse_subcommand(std::forward<Args>(args)...);
1422 }
1423#else
1425 template <typename... Args>
1426 static auto parse_arg(App *app, Args &&...args) ->
1427 typename std::result_of<decltype (&App::_parse_arg)(App, Args...)>::type {
1428 return app->_parse_arg(std::forward<Args>(args)...);
1429 }
1430
1432 template <typename... Args>
1433 static auto parse_subcommand(App *app, Args &&...args) ->
1434 typename std::result_of<decltype (&App::_parse_subcommand)(App, Args...)>::type {
1435 return app->_parse_subcommand(std::forward<Args>(args)...);
1436 }
1437#endif
1440};
1441} // namespace detail
1442
1443// [CLI11:app_hpp:end]
1444} // namespace CLI
1445
1446#ifndef CLI11_COMPILE
1447#include "impl/App_inl.hpp" // IWYU pragma: export
1448#endif
Creates a command line program, with very few defaults.
Definition App.hpp:90
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:805
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:648
CLI11_NODISCARD std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
Definition App_inl.hpp:744
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:930
const Option * operator[](const std::string &option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1072
CLI11_NODISCARD bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Definition App_inl.hpp:1621
App * configurable(bool value=true)
Specify that the subcommand can be triggered by a config file.
Definition App.hpp:468
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:1039
App * immediate_callback(bool immediate=true)
Set the subcommand callback to be executed immediately on subcommand completion.
Definition App_inl.hpp:119
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:225
App * _get_fallthrough_parent()
Get the appropriate parent to fallthrough to which is the first one that has a name or the main app.
Definition App_inl.hpp:2043
CLI11_NODISCARD std::string config_to_str(bool default_also=false, bool write_description=false) const
Definition App.hpp:1002
App * usage(std::function< std::string()> usage_function)
Set usage.
Definition App.hpp:986
CLI11_NODISCARD bool get_enabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1142
Option * config_ptr_
Pointer to the config option.
Definition App.hpp:288
CLI11_NODISCARD bool get_configurable() const
Check the status of the allow windows style options.
Definition App.hpp:1093
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:552
CLI11_NODISCARD std::string get_usage() const
Generate and return the usage.
Definition App.hpp:1099
CLI11_NODISCARD std::shared_ptr< FormatterBase > get_formatter() const
Access the formatter.
Definition App.hpp:1017
Option * add_option(std::string option_name)
Add option with no description or variable assignment.
Definition App.hpp:591
App * config_formatter(std::shared_ptr< Config > fmt)
Set the config formatter.
Definition App.hpp:489
App * allow_config_extras(bool allow=true)
ignore extras in config files
Definition App.hpp:429
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:392
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:2105
std::size_t require_option_min_
Minimum required options (not inheritable!)
Definition App.hpp:269
App * silent(bool silence=true)
silence the subcommand from showing up in the processed list
Definition App.hpp:386
App * clear_aliases()
clear all the aliases of the current App
Definition App.hpp:1185
CLI11_NODISCARD bool get_ignore_underscore() const
Check the status of ignore_underscore.
Definition App.hpp:1081
App * ignore_underscore(bool value=true)
Ignore underscore. Subcommands inherit value.
Definition App_inl.hpp:145
App * allow_extras(bool allow=true)
Remove the error when extras are left over on the command line.
Definition App.hpp:368
App * fallthrough(bool value=true)
Definition App.hpp:832
std::size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE.
Definition App.hpp:266
Option * get_config_ptr()
Get a pointer to the config option.
Definition App.hpp:1161
std::vector< App_p > subcommands_
Storage for subcommand list.
Definition App.hpp:219
CLI11_NODISCARD std::vector< std::string > remaining(bool recurse=false) const
This returns the missing options from the current subcommand.
Definition App_inl.hpp:898
CLI11_NODISCARD bool get_allow_extras() const
Get the status of allow extras.
Definition App.hpp:1124
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:924
std::uint32_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition App.hpp:260
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:500
App * require_subcommand()
The argumentless form of require subcommand requires 1 or more subcommands.
Definition App.hpp:773
Option * get_option(std::string option_name)
Get an option by name (non-const version)
Definition App.hpp:1063
std::string usage_
Usage to put after program/subcommand description in the help output INHERITABLE.
Definition App.hpp:156
OptionDefaults option_defaults_
The default values for options, customizable and changeable INHERITABLE.
Definition App.hpp:146
void _process_help_flags(bool trigger_help=false, bool trigger_all_help=false) const
Definition App_inl.hpp:1176
CLI11_NODISCARD const Option * get_version_ptr() const
Get a pointer to the version option. (const)
Definition App.hpp:1170
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition App_inl.hpp:1198
CLI11_NODISCARD std::size_t count_all() const
Definition App_inl.hpp:509
CLI11_NODISCARD bool get_fallthrough() const
Check the status of fallthrough.
Definition App.hpp:1084
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition App.hpp:123
CLI11_NODISCARD bool get_prefix_command() const
Get the prefix command status.
Definition App.hpp:1121
Option * add_option(std::string option_name, AssignTo &variable, std::string option_description="")
Add option for assigning to a variable.
Definition App.hpp:528
CLI11_NODISCARD std::size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition App.hpp:1112
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:258
bool required_
If set to true the subcommand is required to be processed and used, ignored for main app.
Definition App.hpp:120
bool remove_needs(Option *opt)
Removes an option from the needs list of this subcommand.
Definition App_inl.hpp:726
const Option * operator[](const char *option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition App.hpp:1075
CLI11_NODISCARD bool get_immediate_callback() const
Get the status of disabled.
Definition App.hpp:1136
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1152
App * require_subcommand(int value)
Definition App.hpp:782
void _configure()
Definition App_inl.hpp:979
CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
Definition App_inl.hpp:1609
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:334
OptionDefaults * option_defaults()
Get the OptionDefault object, to set option defaults.
Definition App.hpp:498
void parse(int argc, const char *const *argv)
Definition App_inl.hpp:538
void _process_config_file()
Read and process a configuration file (main app only)
Definition App_inl.hpp:1094
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition App.hpp:162
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
Definition App_inl.hpp:1377
config_extras_mode allow_config_extras_
Definition App.hpp:111
CLI11_NODISCARD bool check_name(std::string name_to_check) const
Check the name, case insensitive and underscore insensitive if set.
Definition App_inl.hpp:857
CLI11_NODISCARD std::string get_footer() const
Generate and return the footer.
Definition App.hpp:1104
App * required(bool require=true)
Remove the error when extras are left over on the command line.
Definition App.hpp:374
Option * version_ptr_
A pointer to a version flag if there is one.
Definition App.hpp:174
CLI11_NODISCARD const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition App.hpp:1158
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:430
App * parent_
A pointer to the parent if this is a subcommand.
Definition App.hpp:275
std::set< Option * > exclude_options_
Definition App.hpp:204
void _trigger_pre_parse(std::size_t remaining_args)
Trigger the pre_parse callback if needed.
Definition App_inl.hpp:2025
App * prefix_command(bool allow=true)
Do not parse anything after the first unrecognized option and return.
Definition App.hpp:446
App * group(std::string group_name)
Changes the group membership.
Definition App.hpp:767
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:475
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:165
std::function< void()> parse_complete_callback_
This is a function that runs when parsing has finished.
Definition App.hpp:136
virtual void pre_callback()
Definition App.hpp:848
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:706
App * get_parent()
Get the parent of this subcommand (or nullptr if main app)
Definition App.hpp:1173
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition App.hpp:625
void _validate() const
Definition App_inl.hpp:944
std::string name_
Subcommand name or program name (from parser if name is empty)
Definition App.hpp:101
std::vector< App * > parsed_subcommands_
This is a list of the subcommands collected, in order.
Definition App.hpp:197
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:311
CLI11_NODISCARD const Option * get_help_ptr() const
Get a pointer to the help flag. (const)
Definition App.hpp:1155
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition App.hpp:225
App * allow_windows_style_options(bool value=true)
Definition App.hpp:456
missing_t missing_
Definition App.hpp:191
CLI11_NODISCARD bool get_validate_positionals() const
Get the status of validating positionals.
Definition App.hpp:1144
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:570
void run_callback(bool final_mode=false, bool suppress_final_callback=false)
Internal function to run (App) callback, bottom up.
Definition App_inl.hpp:999
std::size_t require_subcommand_min_
Minimum required subcommands (not inheritable!)
Definition App.hpp:263
App * usage(std::string usage_string)
Set usage.
Definition App.hpp:981
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
Definition App_inl.hpp:1130
std::function< std::string(const App *, const Error &e)> failure_message_
The error message printing function INHERITABLE.
Definition App.hpp:180
void _parse_stream(std::istream &input)
Internal function to parse a stream.
Definition App_inl.hpp:1429
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:841
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:883
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:117
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:2054
void clear()
Reset the parsed data.
Definition App_inl.hpp:523
CLI11_NODISCARD std::size_t get_require_option_max() const
Get the required max option value.
Definition App.hpp:1118
App * enabled_by_default(bool enable=true)
Definition App.hpp:403
App * footer(std::string footer_string)
Set footer.
Definition App.hpp:991
App * get_subcommand(const App *subcom) const
Definition App_inl.hpp:446
App * require_option(int value)
Definition App.hpp:811
CLI11_NODISCARD std::string version() const
Displays a version string.
Definition App_inl.hpp:758
virtual ~App()=default
virtual destructor
CLI11_NODISCARD App * get_subcommand_no_throw(std::string subcom) const noexcept
Definition App_inl.hpp:462
CLI11_NODISCARD std::size_t count(std::string option_name) const
Counts the number of times the given option was passed.
Definition App.hpp:895
std::vector< Option_p > options_
The list of options, stored locally.
Definition App.hpp:149
Option * help_all_ptr_
A pointer to the help all flag if there is one INHERITABLE.
Definition App.hpp:171
bool validate_optional_arguments_
If set to true optional vector arguments are validated before assigning INHERITABLE.
Definition App.hpp:253
std::function< void()> final_callback_
This is a function that runs when all processing has completed.
Definition App.hpp:139
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:378
App * require_option()
The argumentless form of require option requires 1 or more options be used.
Definition App.hpp:802
App(std::string app_description, std::string app_name, App *parent)
Special private constructor for subcommand.
Definition App_inl.hpp:28
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:159
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:399
App * preparse_callback(std::function< void(std::size_t)> pp_callback)
Definition App.hpp:356
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:317
bool positionals_at_end_
specify that positional arguments come at the end of the argument sequence not inheritable
Definition App.hpp:239
void _process()
Process callbacks and such.
Definition App_inl.hpp:1326
bool immediate_callback_
Definition App.hpp:130
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
Definition App_inl.hpp:1567
App * name(std::string app_name="")
Set a name for the app (empty will use parser to set the name)
Definition App_inl.hpp:84
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:2121
CLI11_NODISCARD bool get_required() const
Get the status of required.
Definition App.hpp:1127
void _process_extras()
Throw an error if anything is left over and should not be.
Definition App_inl.hpp:1348
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:1026
Option * add_flag(std::string flag_name, T &flag_description)
Definition App.hpp:633
CLI11_NODISCARD bool get_ignore_case() const
Check the status of ignore_case.
Definition App.hpp:1078
App * parse_complete_callback(std::function< void()> pc_callback)
Definition App.hpp:349
bool configurable_
if set to true the subcommand can be triggered via configuration files INHERITABLE
Definition App.hpp:247
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:885
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:916
App * formatter_fn(std::function< std::string(const App *, std::string, AppFormatMode)> fmt)
Set the help formatter.
Definition App.hpp:483
CLI11_NODISCARD const std::vector< std::string > & get_aliases() const
Get the aliases of the current app.
Definition App.hpp:1182
void _parse_config(const std::vector< ConfigItem > &args)
Definition App_inl.hpp:1440
App * description(std::string app_description)
Set the description of the app.
Definition App.hpp:1036
CLI11_NODISCARD std::shared_ptr< ConfigBase > get_config_formatter_base() const
Access the config formatter as a configBase pointer.
Definition App.hpp:1023
std::string description_
Description of the current program/subcommand.
Definition App.hpp:104
App * excludes(App *app)
Sets excluded subcommands for the subcommand.
Definition App.hpp:931
CLI11_NODISCARD bool get_positionals_at_end() const
Check the status of the allow windows style options.
Definition App.hpp:1090
Option * get_version_ptr()
Get a pointer to the version option.
Definition App.hpp:1167
CLI11_NODISCARD std::size_t get_require_option_min() const
Get the required min option value.
Definition App.hpp:1115
std::size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable)
Definition App.hpp:272
CLI11_NODISCARD bool get_disabled() const
Get the status of disabled.
Definition App.hpp:1130
CLI11_NODISCARD bool get_allow_windows_style_options() const
Check the status of the allow windows style options.
Definition App.hpp:1087
std::vector< std::string > aliases_
Alias names for the subcommand.
Definition App.hpp:281
CLI11_NODISCARD bool parsed() const
Check to see if this subcommand was parsed, true only if received on command line.
Definition App.hpp:495
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition App.hpp:200
CLI11_NODISCARD bool get_disabled_by_default() const
Get the status of disabled by default.
Definition App.hpp:1139
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:659
CLI11_NODISCARD std::size_t count() const
Definition App.hpp:760
bool _parse_positional(std::vector< std::string > &args, bool haltOnSubcommand)
Definition App_inl.hpp:1631
bool ignore_case_
If true, the program name is not case sensitive INHERITABLE.
Definition App.hpp:222
CLI11_NODISCARD const std::string & get_group() const
Get the group of this subcommand.
Definition App.hpp:1096
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type, bool local_processing_only)
Definition App_inl.hpp:1806
bool silent_
Definition App.hpp:257
CLI11_NODISCARD const App * get_parent() const
Get the parent of this subcommand (or nullptr if main app) (const version)
Definition App.hpp:1176
std::function< void(std::size_t)> pre_parse_callback_
This is a function that runs prior to the start of parsing.
Definition App.hpp:133
App * final_callback(std::function< void()> app_callback)
Definition App.hpp:342
std::string group_
The group membership INHERITABLE.
Definition App.hpp:278
App * alias(std::string app_name)
Set an alias for the app.
Definition App_inl.hpp:101
bool pre_parse_called_
Flag indicating that the pre_parse_callback has been triggered.
Definition App.hpp:126
void _process_callbacks()
Process callbacks. Runs on all subcommands.
Definition App_inl.hpp:1152
App * validate_positionals(bool validate=true)
Set the subcommand to validate positional arguments before assigning.
Definition App.hpp:417
Option * help_ptr_
A pointer to the help flag if there is one INHERITABLE.
Definition App.hpp:168
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:349
App * footer(std::function< std::string()> footer_function)
Set footer.
Definition App.hpp:996
App * ignore_case(bool value=true)
Ignore case. Subcommands inherit value.
Definition App_inl.hpp:131
App * excludes(Option *opt)
Sets excluded options for the subcommand.
Definition App.hpp:922
CLI11_NODISCARD std::shared_ptr< Config > get_config_formatter() const
Access the config formatter.
Definition App.hpp:1020
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
Definition App_inl.hpp:706
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:899
bool got_subcommand(const App *subcom) const
Check to see if given subcommand was selected.
Definition App.hpp:910
CLI11_NODISCARD config_extras_mode get_allow_config_extras() const
Get the status of allow extras.
Definition App.hpp:1149
bool _parse_subcommand(std::vector< std::string > &args)
Definition App_inl.hpp:1766
App * positionals_at_end(bool value=true)
Specify that the positional arguments are only at the end of the sequence.
Definition App.hpp:462
bool fallthrough_
Allow subcommand fallthrough, so that parent commands can collect commands after subcommand....
Definition App.hpp:228
std::set< Option * > need_options_
Definition App.hpp:212
CLI11_NODISCARD bool get_validate_optional_arguments() const
Get the status of validating optional vector arguments.
Definition App.hpp:1146
CLI11_NODISCARD bool get_silent() const
Get the status of silence.
Definition App.hpp:1133
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:776
std::set< App * > need_subcommands_
Definition App.hpp:208
App * validate_optional_arguments(bool validate=true)
Set the subcommand to validate optional vector arguments before assigning.
Definition App.hpp:423
bool prefix_command_
If true, return immediately on an unrecognized option (implies allow_extras) INHERITABLE.
Definition App.hpp:114
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:159
App * formatter(std::shared_ptr< FormatterBase > fmt)
Set the help formatter.
Definition App.hpp:477
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition App.hpp:194
Option * add_option(std::string option_name, T &option_description)
Add option with description but with no variable assignment or callback.
Definition App.hpp:599
CLI11_NODISCARD const std::vector< Option * > & parse_order() const
This gets a vector of pointers with the original parse order.
Definition App.hpp:1200
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition App_inl.hpp:1385
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition App.hpp:250
bool _parse_single_config(const ConfigItem &item, std::size_t level=0)
Fill in a single config option.
Definition App_inl.hpp:1447
startup_mode default_startup
Definition App.hpp:244
App * allow_config_extras(config_extras_mode mode)
ignore extras in config files
Definition App.hpp:440
Option * add_flag(std::string flag_name, T &flag_result, std::string flag_description="")
Definition App.hpp:643
bool allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition App.hpp:107
CLI11_NODISCARD std::string get_description() const
Get the app or subcommand description.
Definition App.hpp:1033
App * require_option(std::size_t min, std::size_t max)
Definition App.hpp:824
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:62
CLI11_NODISCARD App * _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept
Definition App_inl.hpp:1748
CLI11_NODISCARD std::size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition App.hpp:1109
CLI11_NODISCARD const Option * get_config_ptr() const
Get a pointer to the config option. (const)
Definition App.hpp:1164
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1179
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition App.hpp:380
App * callback(std::function< void()> app_callback)
Definition App.hpp:331
std::shared_ptr< FormatterBase > formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
Definition App.hpp:177
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:241
App * require_subcommand(std::size_t min, std::size_t max)
Definition App.hpp:795
CLI11_NODISCARD const Option * get_option(std::string option_name) const
Get an option by name.
Definition App.hpp:1054
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise....
Definition App.hpp:231
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
Definition App.hpp:291
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:83
All errors derive from this one.
Definition Error.hpp:73
Definition FormatterFwd.hpp:120
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:96
Extension of App to better manage groups of options.
Definition App.hpp:1337
App * add_subcommand(App *subcom)
Add an existing subcommand to be a member of an option_group.
Definition App.hpp:1362
Option * add_option(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1346
void add_options(Option *opt, Args... args)
Add a bunch of options to the group.
Definition App.hpp:1356
void add_options(Option *opt)
Add an existing option to the Option_group.
Definition App.hpp:1354
Definition Option.hpp:194
Definition Option.hpp:231
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:462
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:36
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:724
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:357
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:407
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:220
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:753
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:398
Thrown when counting a non-existent option.
Definition Error.hpp:351
Holds values to load into Options.
Definition ConfigFwd.hpp:29
This class is simply to allow tests access to App's protected functions.
Definition App.hpp:1411
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:1433
static App * get_fallthrough_parent(App *app)
Wrap the fallthrough parent function to make sure that is working correctly.
Definition App.hpp:1439
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:1426
This will only trigger for actual void type.
Definition TypeTools.hpp:423