20 #include "CLI/ConfigFwd.hpp" 21 #include "CLI/Error.hpp" 22 #include "CLI/FormatterFwd.hpp" 23 #include "CLI/Macros.hpp" 24 #include "CLI/Option.hpp" 25 #include "CLI/Split.hpp" 26 #include "CLI/StringTools.hpp" 27 #include "CLI/TypeTools.hpp" 32 #define CLI11_PARSE(app, argc, argv) \ 34 (app).parse((argc), (argv)); \ 35 } catch(const CLI::ParseError &e) { \ 36 return (app).exit(e); \ 41 enum class Classifier { NONE, POSITIONAL_MARK, SHORT, LONG, WINDOWS, SUBCOMMAND, SUBCOMMAND_TERMINATOR };
45 namespace FailureMessage {
46 std::string simple(
const App *app,
const Error &e);
47 std::string help(
const App *app,
const Error &e);
52 using App_p = std::shared_ptr<App>;
133 std::function<std::string(const App *, const Error &e)>
failure_message_ = FailureMessage::simple;
139 using missing_t = std::vector<std::pair<detail::Classifier, std::string>>;
232 App(std::string app_description, std::string app_name,
App *parent)
269 explicit App(std::string app_description =
"", std::string app_name =
"")
270 :
App(app_description, app_name, nullptr) {
271 set_help_flag(
"-h,--help",
"Print this help message and exit");
275 virtual ~App() =
default;
364 if(subc.get() !=
this && (this->
check_name(subc->name_) || subc->check_name(this->name_)))
388 if(subc.get() !=
this && (this->
check_name(subc->name_) || subc->check_name(this->name_)))
403 formatter_ = std::make_shared<FormatterLambda>(fmt);
438 callback_t option_callback,
439 std::string option_description =
"",
440 bool defaulted =
false,
441 std::function<std::string()> func = {}) {
442 Option myopt{option_name, option_description, option_callback,
this};
444 if(std::find_if(std::begin(
options_), std::end(
options_), [&myopt](
const Option_p &v) {
449 option.reset(
new Option(option_name, option_description, option_callback,
this));
452 option->default_function(func);
456 option->capture_default_str();
462 if(!defaulted && option->get_always_capture_default())
463 option->capture_default_str();
468 throw OptionAlreadyAdded(myopt.get_name());
472 template <typename T, enable_if_t<!is_vector<T>::value & !std::is_const<T>::value, detail::enabler> = detail::dummy>
475 std::string option_description =
"",
476 bool defaulted =
false) {
478 auto fun = [&variable](CLI::results_t res) {
return detail::lexical_cast(res[0], variable); };
480 Option *opt =
add_option(option_name, fun, option_description, defaulted, [&variable]() {
481 return std::string(CLI::detail::to_string(variable));
489 template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
491 const std::function<
void(
const T &)> &func,
492 std::string option_description =
"") {
494 auto fun = [func](CLI::results_t res) {
496 bool result = detail::lexical_cast(res[0], variable);
503 Option *opt =
add_option(option_name, std::move(fun), option_description,
false);
510 return add_option(option_name, CLI::callback_t(), std::string{},
false);
514 template <
typename T,
515 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
518 return add_option(option_name, CLI::callback_t(), option_description,
false);
522 template <
typename T>
524 std::vector<T> &variable,
525 std::string option_description =
"",
526 bool defaulted =
false) {
528 auto fun = [&variable](CLI::results_t res) {
531 variable.reserve(res.size());
532 for(
const auto &elem : res) {
534 variable.emplace_back();
535 retval &= detail::lexical_cast(elem, variable.back());
537 return (!variable.empty()) && retval;
540 auto default_function = [&variable]() {
541 std::vector<std::string> defaults;
542 defaults.resize(variable.size());
543 std::transform(variable.begin(), variable.end(), defaults.begin(), [](T &val) {
544 return std::string(CLI::detail::to_string(val));
546 return std::string(
"[" + detail::join(defaults) +
"]");
549 Option *opt =
add_option(option_name, fun, option_description, defaulted, default_function);
556 template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail::dummy>
558 const std::function<
void(
const T &)> &func,
559 std::string option_description =
"") {
561 CLI::callback_t fun = [func](CLI::results_t res) {
564 values.reserve(res.size());
565 for(
const auto &elem : res) {
566 values.emplace_back();
567 retval &= detail::lexical_cast(elem, values.back());
575 Option *opt =
add_option(option_name, std::move(fun), std::move(option_description),
false);
589 if(!flag_name.empty()) {
606 if(!help_name.empty()) {
616 Option *_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description) {
618 if(detail::has_default_flag_values(flag_name)) {
620 auto flag_defaults = detail::get_default_flag_values(flag_name);
621 detail::remove_default_flag_values(flag_name);
622 opt =
add_option(std::move(flag_name), std::move(fun), std::move(flag_description),
false);
623 for(
const auto &fname : flag_defaults)
624 opt->
fnames_.push_back(fname.first);
627 opt =
add_option(std::move(flag_name), std::move(fun), std::move(flag_description),
false);
631 auto pos_name = opt->
get_name(
true);
633 throw IncorrectConstruction::PositionalFlag(pos_name);
642 Option *
add_flag(std::string flag_name) {
return _add_flag_internal(flag_name, CLI::callback_t(), std::string{}); }
647 template <
typename T,
648 enable_if_t<std::is_const<T>::value && std::is_constructible<std::string, T>::value, detail::enabler> =
651 return _add_flag_internal(flag_name, CLI::callback_t(), flag_description);
656 template <
typename T,
657 enable_if_t<std::is_integral<T>::value && !
is_bool<T>::value, detail::enabler> = detail::dummy>
660 std::string flag_description =
"") {
662 CLI::callback_t fun = [&flag_count](CLI::results_t res) {
664 detail::sum_flag_vector(res, flag_count);
665 }
catch(
const std::invalid_argument &) {
670 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
675 template <
typename T,
676 enable_if_t<!is_vector<T>::value && !std::is_const<T>::value &&
678 !std::is_constructible<std::function<
void(
int)>, T>::value,
679 detail::enabler> = detail::dummy>
682 std::string flag_description =
"") {
684 CLI::callback_t fun = [&flag_result](CLI::results_t res) {
685 if(res.size() != 1) {
688 return CLI::detail::lexical_cast(res[0], flag_result);
690 Option *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
696 template <
typename T,
697 enable_if_t<!std::is_assignable<std::function<void(int64_t)>, T>::value, detail::enabler> = detail::dummy>
699 std::vector<T> &flag_results,
700 std::string flag_description =
"") {
701 CLI::callback_t fun = [&flag_results](CLI::results_t res) {
703 for(
const auto &elem : res) {
704 flag_results.emplace_back();
705 retval &= detail::lexical_cast(elem, flag_results.back());
709 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
714 std::function<
void(
void)>
function,
715 std::string flag_description =
"") {
717 CLI::callback_t fun = [
function](CLI::results_t res) {
718 if(res.size() != 1) {
722 auto result = CLI::detail::lexical_cast(res[0], trigger);
727 Option *opt = _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
734 std::function<
void(int64_t)>
function,
735 std::string flag_description =
"") {
737 CLI::callback_t fun = [
function](CLI::results_t res) {
738 int64_t flag_count = 0;
739 detail::sum_flag_vector(res, flag_count);
740 function(flag_count);
743 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
749 std::function<
void(int64_t)>
function,
750 std::string flag_description =
"") {
751 return add_flag_function(std::move(flag_name), std::move(
function), std::move(flag_description));
756 template <
typename T>
760 std::string option_description =
"") {
762 Option *opt =
add_option(option_name, member, std::move(option_description));
768 template <
typename T>
771 const std::set<T> &options,
772 std::string option_description =
"") {
774 Option *opt =
add_option(option_name, member, std::move(option_description));
780 template <
typename T>
784 std::string option_description,
787 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
793 template <
typename T>
796 const std::set<T> &options,
797 std::string option_description,
800 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
807 Option *add_set_ignore_case(std::
string option_name,
809 std::set<std::
string> options,
810 std::
string option_description = "") {
812 Option *opt =
add_option(option_name, member, std::move(option_description));
819 CLI11_DEPRECATED(
"Use ->transform(CLI::IsMember(..., CLI::ignore_case)) with a (shared) pointer instead")
820 Option *add_mutable_set_ignore_case(std::
string option_name,
822 const std::set<std::
string> &options,
823 std::
string option_description = "") {
825 Option *opt =
add_option(option_name, member, std::move(option_description));
832 Option *add_set_ignore_case(std::
string option_name,
834 std::set<std::
string> options,
835 std::
string option_description,
838 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
846 Option *add_mutable_set_ignore_case(std::
string option_name,
848 const std::set<std::
string> &options,
849 std::
string option_description,
852 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
859 Option *add_set_ignore_underscore(std::
string option_name,
861 std::set<std::
string> options,
862 std::
string option_description = "") {
864 Option *opt =
add_option(option_name, member, std::move(option_description));
871 CLI11_DEPRECATED(
"Use ->transform(CLI::IsMember(..., CLI::ignore_underscore)) with a (shared) pointer instead")
872 Option *add_mutable_set_ignore_underscore(std::
string option_name,
874 const std::set<std::
string> &options,
875 std::
string option_description = "") {
877 Option *opt =
add_option(option_name, member, std::move(option_description));
884 Option *add_set_ignore_underscore(std::
string option_name,
886 std::set<std::
string> options,
887 std::
string option_description,
890 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
897 CLI11_DEPRECATED(
"Use ->transform(CLI::IsMember(..., CLI::ignore_underscore)) with a (shared) pointer instead")
898 Option *add_mutable_set_ignore_underscore(std::
string option_name,
900 const std::set<std::
string> &options,
901 std::
string option_description,
904 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
910 CLI11_DEPRECATED(
"Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) instead")
911 Option *add_set_ignore_case_underscore(std::
string option_name,
913 std::set<std::
string> options,
914 std::
string option_description = "") {
916 Option *opt =
add_option(option_name, member, std::move(option_description));
924 "Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) with a (shared) pointer instead")
925 Option *add_mutable_set_ignore_case_underscore(std::
string option_name,
927 const std::set<std::
string> &options,
928 std::
string option_description = "") {
930 Option *opt =
add_option(option_name, member, std::move(option_description));
936 CLI11_DEPRECATED(
"Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) instead")
937 Option *add_set_ignore_case_underscore(std::
string option_name,
939 std::set<std::
string> options,
940 std::
string option_description,
943 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
951 "Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) with a (shared) pointer instead")
952 Option *add_mutable_set_ignore_case_underscore(std::
string option_name,
954 const std::set<std::
string> &options,
955 std::
string option_description,
958 Option *opt =
add_option(option_name, member, std::move(option_description), defaulted);
964 template <
typename T>
967 std::string option_description =
"",
968 bool defaulted =
false,
969 std::string label =
"COMPLEX") {
971 std::string simple_name = CLI::detail::split(option_name,
',').at(0);
972 CLI::callback_t fun = [&variable, simple_name, label](results_t res) {
973 if(res[1].back() ==
'i')
976 bool worked = detail::lexical_cast(res[0], x) && detail::lexical_cast(res[1], y);
982 auto default_function = [&variable]() {
983 std::stringstream out;
989 add_option(option_name, std::move(fun), std::move(option_description), defaulted, default_function);
997 std::string default_filename =
"",
998 std::string help_message =
"Read an ini file",
999 bool config_required =
false) {
1006 if(!option_name.empty()) {
1020 op->remove_needs(opt);
1021 op->remove_excludes(opt);
1030 std::find_if(std::begin(
options_), std::end(
options_), [opt](
const Option_p &v) {
return v.get() == opt; });
1031 if(iterator != std::end(
options_)) {
1039 template <
typename T = Option_group>
1041 auto option_group = std::make_shared<T>(std::move(group_description), group_name,
nullptr);
1042 auto ptr = option_group.get();
1044 App_p app_ptr = std::dynamic_pointer_cast<
App>(option_group);
1055 CLI::App_p subcom = std::shared_ptr<App>(
new App(std::move(subcommand_description), subcommand_name,
this));
1063 if(!subcom->name_.empty()) {
1065 if(subc->check_name(subcom->name_) || subcom->check_name(subc->name_))
1068 subcom->parent_ =
this;
1077 sub->remove_excludes(subcom);
1080 auto iterator = std::find_if(
1091 if(subcom ==
nullptr)
1094 if(subcomptr.get() == subcom)
1109 auto uindex =
static_cast<unsigned>(index);
1118 if(subcom ==
nullptr)
1121 if(subcomptr.get() == subcom)
1129 if(subcomptr->check_name(subcom))
1137 auto uindex =
static_cast<unsigned>(index);
1147 if(app->name_.empty() && app->group_ == group_name) {
1164 cnt += opt->
count();
1167 cnt += sub->count_all();
1271 for(
const Option_p &opt :
options_) {
1281 void parse(
int argc,
const char *
const *argv) {
1288 std::vector<std::string> args;
1289 args.reserve(static_cast<size_t>(argc - 1));
1290 for(
int i = argc - 1; i > 0; i--)
1291 args.emplace_back(argv[i]);
1292 parse(std::move(args));
1299 void parse(std::string commandline,
bool program_name_included =
false) {
1301 if(program_name_included) {
1302 auto nstr = detail::split_program_name(commandline);
1307 commandline = std::move(nstr.second);
1309 detail::trim(commandline);
1311 if(!commandline.empty()) {
1312 commandline = detail::find_and_modify(commandline,
"=", detail::escape_detect);
1314 commandline = detail::find_and_modify(commandline,
":", detail::escape_detect);
1317 auto args = detail::split_up(std::move(commandline));
1319 args.erase(std::remove(args.begin(), args.end(), std::string{}), args.end());
1320 std::reverse(args.begin(), args.end());
1322 parse(std::move(args));
1327 void parse(std::vector<std::string> &args) {
1347 void parse(std::vector<std::string> &&args) {
1372 int exit(
const Error &e, std::ostream &out = std::cout, std::ostream &err = std::cerr)
const {
1375 if(dynamic_cast<const CLI::RuntimeError *>(&e) !=
nullptr)
1376 return e.get_exit_code();
1378 if(dynamic_cast<const CLI::CallForHelp *>(&e) !=
nullptr) {
1380 return e.get_exit_code();
1383 if(dynamic_cast<const CLI::CallForAllHelp *>(&e) !=
nullptr) {
1384 out <<
help(
"", AppFormatMode::All);
1385 return e.get_exit_code();
1388 if(e.get_exit_code() !=
static_cast<int>(ExitCodes::Success)) {
1393 return e.get_exit_code();
1410 std::vector<const App *> subcomms(
subcommands_.size());
1416 subcomms.erase(std::remove_if(std::begin(subcomms),
1418 [&filter](
const App *app) {
return !filter(app); }),
1419 std::end(subcomms));
1435 std::remove_if(std::begin(subcomms), std::end(subcomms), [&filter](
App *app) {
return !filter(app); }),
1436 std::end(subcomms));
1453 if(opt ==
nullptr) {
1462 if((app ==
this) || (app ==
nullptr)) {
1488 auto other_app = *iterator;
1490 other_app->remove_excludes(
this);
1503 footer_ = std::move(footer_string);
1509 std::string
config_to_str(
bool default_also =
false,
bool write_description =
false)
const {
1515 std::string
help(std::string prev =
"", AppFormatMode mode = AppFormatMode::Normal)
const {
1523 if(!selected_subcommands.empty())
1524 return selected_subcommands.at(0)->help(prev, mode);
1526 return formatter_->make_help(
this, prev, mode);
1549 std::vector<const Option *>
get_options(
const std::function<
bool(
const Option *)> filter = {})
const {
1550 std::vector<const Option *> options(
options_.size());
1551 std::transform(std::begin(
options_), std::end(
options_), std::begin(options), [](
const Option_p &val) {
1556 options.erase(std::remove_if(std::begin(options),
1558 [&filter](
const Option *opt) {
return !filter(opt); }),
1574 if(subc->get_name().empty()) {
1575 auto opt = subc->get_option_no_throw(option_name);
1576 if(opt !=
nullptr) {
1586 for(
const Option_p &opt :
options_) {
1593 if(subc->get_name().empty()) {
1594 auto opt = subc->get_option_no_throw(option_name);
1595 if(opt !=
nullptr) {
1606 if(opt ==
nullptr) {
1615 if(opt ==
nullptr) {
1715 std::string local_name =
name_;
1717 local_name = detail::remove_underscore(
name_);
1718 name_to_check = detail::remove_underscore(name_to_check);
1721 local_name = detail::to_lower(
name_);
1722 name_to_check = detail::to_lower(name_to_check);
1725 return local_name == name_to_check;
1730 std::vector<std::string> groups;
1732 for(
const Option_p &opt :
options_) {
1734 if(std::find(groups.begin(), groups.end(), opt->
get_group()) == groups.end()) {
1746 std::vector<std::string>
remaining(
bool recurse =
false)
const {
1747 std::vector<std::string> miss_list;
1748 for(
const std::pair<detail::Classifier, std::string> &miss :
missing_) {
1749 miss_list.push_back(std::get<1>(miss));
1755 if(sub->name_.empty() && !sub->missing_.empty()) {
1756 for(
const std::pair<detail::Classifier, std::string> &miss : sub->missing_) {
1757 miss_list.push_back(std::get<1>(miss));
1765 std::vector<std::string> output = sub->remaining(recurse);
1766 std::copy(std::begin(output), std::end(output), std::back_inserter(miss_list));
1774 std::vector<std::string> miss_list =
remaining(recurse);
1775 std::reverse(std::begin(miss_list), std::end(miss_list));
1781 auto remaining_options =
static_cast<size_t>(std::count_if(
1782 std::begin(
missing_), std::end(
missing_), [](
const std::pair<detail::Classifier, std::string> &val) {
1783 return val.first != detail::Classifier::POSITIONAL_MARK;
1788 remaining_options += sub->remaining_size(recurse);
1791 return remaining_options;
1802 auto pcount = std::count_if(std::begin(
options_), std::end(
options_), [](
const Option_p &opt) {
1803 return opt->get_items_expected() < 0 && opt->get_positional();
1808 size_t nameless_subs{0};
1811 if(app->get_name().empty())
1818 throw(
InvalidError(
"Required min options greater than required max options",
1819 ExitCodes::InvalidError));
1823 throw(
InvalidError(
"Required min options greater than number of available options",
1824 ExitCodes::InvalidError));
1840 if(app->has_automatic_name_) {
1843 if(app->name_.empty()) {
1844 app->fallthrough_ =
false;
1845 app->prefix_command_ =
false;
1848 app->parent_ =
this;
1857 if(!subc->immediate_callback_)
1858 subc->run_callback();
1862 if(!subc->immediate_callback_ && subc->name_.empty() && subc->count_all() > 0) {
1863 subc->run_callback();
1881 if(com !=
nullptr) {
1889 detail::Classifier
_recognize(
const std::string ¤t,
bool ignore_used_subcommands =
true)
const {
1890 std::string dummy1, dummy2;
1893 return detail::Classifier::POSITIONAL_MARK;
1895 return detail::Classifier::SUBCOMMAND;
1896 if(detail::split_long(current, dummy1, dummy2))
1897 return detail::Classifier::LONG;
1898 if(detail::split_short(current, dummy1, dummy2))
1899 return detail::Classifier::SHORT;
1901 return detail::Classifier::WINDOWS;
1902 if((current ==
"++") && !
name_.empty() &&
parent_ !=
nullptr)
1903 return detail::Classifier::SUBCOMMAND_TERMINATOR;
1904 return detail::Classifier::NONE;
1931 for(
const Option_p &opt :
options_) {
1933 char *buffer =
nullptr;
1934 std::string ename_string;
1939 if(_dupenv_s(&buffer, &sz, opt->
envname_.c_str()) == 0 && buffer !=
nullptr) {
1940 ename_string = std::string(buffer);
1945 buffer = std::getenv(opt->
envname_.c_str());
1946 if(buffer !=
nullptr)
1947 ename_string = std::string(buffer);
1950 if(!ename_string.empty()) {
1957 if(sub->get_name().empty() || !sub->immediate_callback_)
1958 sub->_process_env();
1967 if(sub->get_name().empty() && sub->immediate_callback_) {
1968 if(sub->count_all() > 0) {
1969 sub->_process_callbacks();
1970 sub->run_callback();
1975 for(
const Option_p &opt :
options_) {
1982 if(!sub->immediate_callback_) {
1983 sub->_process_callbacks();
1995 if(help_ptr !=
nullptr && help_ptr->
count() > 0)
1996 trigger_help =
true;
1997 if(help_all_ptr !=
nullptr && help_all_ptr->
count() > 0)
1998 trigger_all_help =
true;
2003 sub->_process_help_flags(trigger_help, trigger_all_help);
2006 }
else if(trigger_all_help) {
2008 }
else if(trigger_help) {
2016 bool excluded{
false};
2017 std::string excluder;
2019 if(opt->
count() > 0) {
2025 if(subc->count_all() > 0) {
2027 excluder = subc->get_display_name();
2037 size_t used_options = 0;
2038 for(
const Option_p &opt :
options_) {
2040 if(opt->
count() != 0) {
2055 if(opt->
count() > 0 && opt_req->
count() == 0)
2076 if(sub->name_.empty() && sub->count_all() > 0) {
2082 auto option_list = detail::join(
options_, [](
const Option_p &ptr) {
return ptr->get_name(
false,
true); });
2083 if(option_list.compare(0, 10,
"-h,--help,") == 0) {
2084 option_list.erase(0, 10);
2087 if(!subc_list.empty()) {
2088 option_list +=
"," + detail::join(subc_list, [](
const App *app) {
return app->
get_display_name(); });
2097 if(sub->name_.empty() && sub->required_ ==
false) {
2098 if(sub->count_all() == 0) {
2111 if(sub->count() > 0 || sub->name_.empty()) {
2112 sub->_process_requirements();
2115 if(sub->required_ && sub->count_all() == 0) {
2134 if(num_left_over > 0) {
2140 if(sub->count() > 0)
2141 sub->_process_extras();
2150 if(num_left_over > 0) {
2157 if(sub->count() > 0)
2158 sub->_process_extras(args);
2166 if(sub->get_name().empty())
2167 sub->increment_parsed();
2171 void _parse(std::vector<std::string> &args) {
2174 bool positional_only =
false;
2176 while(!args.empty()) {
2200 void _parse(std::vector<std::string> &&args) {
2205 bool positional_only =
false;
2207 while(!args.empty()) {
2223 throw ConfigError::Extras(item.fullname());
2229 if(level < item.
parents.size()) {
2248 throw ConfigError::NotConfigurable(item.
fullname());
2254 res = op->get_flag_value(item.
name, res);
2271 detail::Classifier classifier = positional_only ? detail::Classifier::NONE :
_recognize(args.back());
2272 switch(classifier) {
2273 case detail::Classifier::POSITIONAL_MARK:
2275 positional_only =
true;
2282 case detail::Classifier::SUBCOMMAND_TERMINATOR:
2287 case detail::Classifier::SUBCOMMAND:
2290 case detail::Classifier::LONG:
2291 case detail::Classifier::SHORT:
2292 case detail::Classifier::WINDOWS:
2296 case detail::Classifier::NONE:
2300 positional_only =
true;
2306 HorribleError(
"unrecognized classifier (you should not see this!)");
2315 for(
const Option_p &opt :
options_)
2325 for(
const Option_p &opt :
options_)
2337 const std::string &positional = args.back();
2338 for(
const Option_p &opt :
options_) {
2343 std::string pos = positional;
2344 pos = opt->_validate(pos);
2357 if((subc->name_.empty()) && (!subc->disabled_)) {
2358 if(subc->_parse_positional(args)) {
2359 if(!subc->pre_parse_called_) {
2360 subc->_trigger_pre_parse(args.size());
2381 if(com !=
nullptr && (com->parent_->require_subcommand_max_ == 0 ||
2382 com->parent_->require_subcommand_max_ > com->parent_->parsed_subcommands_.size())) {
2397 while(!args.empty()) {
2410 if(com->disabled_ && ignore_disabled)
2412 if(com->get_name().empty()) {
2413 auto subc = com->
_find_subcommand(subc_name, ignore_disabled, ignore_used);
2414 if(subc !=
nullptr) {
2417 }
else if(com->check_name(subc_name)) {
2418 if((!*com) || !ignore_used)
2435 if(com !=
nullptr) {
2439 auto parent_app = com->parent_;
2440 while(parent_app !=
this) {
2441 parent_app->_trigger_pre_parse(args.size());
2442 parent_app->parsed_subcommands_.push_back(com);
2443 parent_app = parent_app->parent_;
2449 throw HorribleError(
"Subcommand " + args.back() +
" missing");
2455 bool _parse_arg(std::vector<std::string> &args, detail::Classifier current_type) {
2457 std::string current = args.back();
2459 std::string arg_name;
2463 switch(current_type) {
2464 case detail::Classifier::LONG:
2465 if(!detail::split_long(current, arg_name, value))
2466 throw HorribleError(
"Long parsed but missing (you should not see this):" + args.back());
2468 case detail::Classifier::SHORT:
2469 if(!detail::split_short(current, arg_name, rest))
2470 throw HorribleError(
"Short parsed but missing! You should not see this");
2472 case detail::Classifier::WINDOWS:
2473 if(!detail::split_windows_style(current, arg_name, value))
2474 throw HorribleError(
"windows option parsed but missing! You should not see this");
2476 case detail::Classifier::SUBCOMMAND:
2477 case detail::Classifier::POSITIONAL_MARK:
2478 case detail::Classifier::NONE:
2480 throw HorribleError(
"parsing got called with invalid option! You should not see this");
2484 std::find_if(std::begin(
options_), std::end(
options_), [arg_name, current_type](
const Option_p &opt) {
2485 if(current_type == detail::Classifier::LONG)
2486 return opt->check_lname(arg_name);
2487 if(current_type == detail::Classifier::SHORT)
2488 return opt->check_sname(arg_name);
2490 return opt->check_lname(arg_name) || opt->check_sname(arg_name);
2496 if(subc->name_.empty() && !subc->disabled_) {
2497 if(subc->_parse_arg(args, current_type)) {
2498 if(!subc->pre_parse_called_) {
2499 subc->_trigger_pre_parse(args.size());
2521 Option_p &op = *op_ptr;
2523 int num = op->get_items_expected();
2527 int result_count = 0;
2530 auto res = op->get_flag_value(arg_name, value);
2531 op->add_result(res);
2535 else if(!value.empty()) {
2536 op->add_result(value, result_count);
2538 collected += result_count;
2541 num = (num >= result_count) ? num - result_count : 0;
2544 }
else if(!rest.empty()) {
2545 op->add_result(rest, result_count);
2548 collected += result_count;
2551 num = (num >= result_count) ? num - result_count : 0;
2556 while(!args.empty() &&
_recognize(args.back(),
false) == detail::Classifier::NONE) {
2557 if(collected >= -num) {
2564 op->add_result(args.back(), result_count);
2567 collected += result_count;
2571 if(!args.empty() &&
_recognize(args.back()) == detail::Classifier::POSITIONAL_MARK)
2575 while(num > 0 && !args.empty()) {
2576 std::string current_ = args.back();
2578 op->add_result(current_, result_count);
2580 num -= result_count;
2584 throw ArgumentMismatch::TypedAtLeast(op->get_name(), num, op->get_type_name());
2590 args.push_back(rest);
2603 if(!
name_.empty()) {
2619 auto fallthrough_parent =
parent_;
2620 while((fallthrough_parent->parent_ !=
nullptr) && (fallthrough_parent->get_name().empty())) {
2621 fallthrough_parent = fallthrough_parent->
parent_;
2623 return fallthrough_parent;
2629 missing_.emplace_back(val_type, val);
2634 if(subc->name_.empty() && subc->allow_extras_) {
2635 subc->missing_.emplace_back(val_type, val);
2640 missing_.emplace_back(val_type, val);
2646 if(opt ==
nullptr) {
2652 if(app == subc.get()) {
2667 std::find_if(std::begin(
options_), std::end(
options_), [opt](
const Option_p &v) {
return v.get() == opt; });
2668 if(iterator != std::end(
options_)) {
2669 const auto &opt_p = *iterator;
2670 if(std::find_if(std::begin(app->
options_), std::end(app->
options_), [&opt_p](
const Option_p &v) {
2671 return (*v == *opt_p);
2674 app->
options_.push_back(std::move(*iterator));
2688 Option_group(std::string group_description, std::string group_name,
App *parent)
2689 :
App(std::move(group_description),
"", parent) {
2713 subc->get_parent()->remove_subcommand(subcom);
2719 inline void TriggerOn(App *trigger_app, App *app_to_enable) {
2720 app_to_enable->enabled_by_default(
false);
2721 app_to_enable->disabled_by_default();
2722 trigger_app->preparse_callback([app_to_enable](
size_t) { app_to_enable->disabled(
false); });
2726 inline void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable) {
2727 for(
auto &app : apps_to_enable) {
2728 app->enabled_by_default(
false);
2729 app->disabled_by_default();
2732 trigger_app->preparse_callback([apps_to_enable](
size_t) {
2733 for(
auto &app : apps_to_enable) {
2734 app->disabled(
false);
2740 inline void TriggerOff(App *trigger_app, App *app_to_enable) {
2741 app_to_enable->disabled_by_default(
false);
2742 app_to_enable->enabled_by_default();
2743 trigger_app->preparse_callback([app_to_enable](
size_t) { app_to_enable->disabled(); });
2747 inline void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable) {
2748 for(
auto &app : apps_to_enable) {
2749 app->disabled_by_default(
false);
2750 app->enabled_by_default();
2753 trigger_app->preparse_callback([apps_to_enable](
size_t) {
2754 for(
auto &app : apps_to_enable) {
2760 namespace FailureMessage {
2763 inline std::string simple(
const App *app,
const Error &e) {
2764 std::string header = std::string(e.what()) +
"\n";
2765 std::vector<std::string> names;
2768 if(app->get_help_ptr() !=
nullptr)
2769 names.push_back(app->get_help_ptr()->get_name());
2771 if(app->get_help_all_ptr() !=
nullptr)
2772 names.push_back(app->get_help_all_ptr()->get_name());
2776 header +=
"Run with " + detail::join(names,
" or ") +
" for more information.\n";
2782 inline std::string help(
const App *app,
const Error &e) {
2783 std::string header = std::string(
"ERROR: ") + e.get_name() +
": " + e.what() +
"\n";
2784 header += app->help();
2795 template <
typename... Args>
2798 return app->_parse_arg(std::forward<Args>(args)...);
2802 template <
typename... Args>
2805 return app->_parse_subcommand(std::forward<Args>(args)...);
int get_type_size() const
The number of arguments the option expects.
Definition: Option.hpp:540
size_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition: App.hpp:196
size_t count() const
Definition: App.hpp:1157
std::string description_
Description of the current program/subcommand.
Definition: App.hpp:73
bool ignore_case_
If true, the program name is not case sensitive INHERITABLE.
Definition: App.hpp:167
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.hpp:437
All errors derive from this one.
Definition: Error.hpp:63
App * add_subcommand(std::string subcommand_name="", std::string subcommand_description="")
Add a subcommand. Inherits INHERITABLE and OptionDefaults, and help flag.
Definition: App.hpp:1054
App * callback(std::function< void()> app_callback)
Definition: App.hpp:283
std::vector< const App * > get_subcommands(const std::function< bool(const App *)> &filter) const
Definition: App.hpp:1409
App * require_option(size_t min, size_t max)
Definition: App.hpp:1233
void _process_help_flags(bool trigger_help=false, bool trigger_all_help=false) const
Definition: App.hpp:1991
std::vector< std::string > inputs
Listing of inputs.
Definition: ConfigFwd.hpp:50
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.hpp:2627
int get_items_expected() const
The total number of expected values (including the type) This is positive if exactly this number is e...
Definition: Option.hpp:589
bool get_positionals_at_end() const
Check the status of the allow windows style options.
Definition: App.hpp:1640
std::vector< std::string > parents
This is the list of parents.
Definition: ConfigFwd.hpp:44
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.hpp:1773
size_t count(std::string option_name) const
Counts the number of times the given option was passed.
Definition: App.hpp:1401
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_case)) with a (shared) pointer instead") Option *add_mutable_set_ignore_case(std
Definition: App.hpp:819
size_t get_require_subcommand_min() const
Get the required min subcommand value.
Definition: App.hpp:1649
This converter works with INI files.
Definition: ConfigFwd.hpp:94
void parse(std::vector< std::string > &args)
Definition: App.hpp:1327
bool get_disabled() const
Get the status of disabled.
Definition: App.hpp:1670
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition: App.hpp:2014
void _move_option(Option *opt, App *app)
function that could be used by subclasses of App to shift options around into subcommands ...
Definition: App.hpp:2645
bool get_enabled_by_default() const
Get the status of disabled by default.
Definition: App.hpp:1679
App * immediate_callback(bool immediate=true)
Set the subcommand callback to be executed immediately on subcommand completion.
Definition: App.hpp:334
bool _parse_positional(std::vector< std::string > &args)
Definition: App.hpp:2335
const Option * get_option(std::string option_name) const
Get an option by name.
Definition: App.hpp:1604
Option * add_option(std::string option_name, std::vector< T > &variable, std::string option_description="", bool defaulted=false)
Add option for vectors.
Definition: App.hpp:523
-h or –help on command line
Definition: Error.hpp:156
App * add_subcommand(CLI::App_p subcom)
Add a previously created app as a subcommand.
Definition: App.hpp:1060
Option * add_flag(std::string flag_name, T &flag_result, std::string flag_description="")
Definition: App.hpp:680
void _process_extras(std::vector< std::string > &args)
Definition: App.hpp:2147
static App * get_fallthrough_parent(App *app)
Wrap the fallthrough parent function to make sure that is working correctly.
Definition: App.hpp:2808
Option * add_option(Option *opt)
Add an existing option to the Option_group.
Definition: App.hpp:2695
void _process_callbacks()
Process callbacks. Runs on all subcommands.
Definition: App.hpp:1963
This class is simply to allow tests access to App's protected functions.
Definition: App.hpp:2792
bool prefix_command_
If true, return immediately on an unrecognized option (implies allow_extras) INHERITABLE.
Definition: App.hpp:82
Option * get_config_ptr()
Get a pointer to the config option.
Definition: App.hpp:1696
void _validate() const
Definition: App.hpp:1801
missing_t missing_
Definition: App.hpp:144
App * add_subcommand(App *subcom)
Add an existing subcommand to be a member of an option_group.
Definition: App.hpp:2711
Option * get_option(std::string option_name)
Get an option by name (non-const version)
Definition: App.hpp:1613
bool _parse_single_config(const ConfigItem &item, size_t level=0)
Fill in a single config option.
Definition: App.hpp:2228
std::string name
This is the name.
Definition: ConfigFwd.hpp:47
CLI::App_p get_subcommand_ptr(std::string subcom) const
Check to see if a subcommand is part of this command (text version)
Definition: App.hpp:1127
bool get_positional() const
True if the argument can be given directly.
Definition: Option.hpp:595
App * allow_extras(bool allow=true)
Remove the error when extras are left over on the command line.
Definition: App.hpp:303
Option * add_option(std::string option_name, T &option_description)
Add option with description but with no variable assignment or callback.
Definition: App.hpp:517
std::string group_
The group membership INHERITABLE.
Definition: App.hpp:211
bool get_ignore_case() const
Check the status of ignore_case.
Definition: App.hpp:1628
Option * add_set(std::string option_name, T &member, std::set< T > options, std::string option_description="")
Add set of options (No default, temp reference, such as an inline set) DEPRECATED.
Definition: App.hpp:757
Definition: Error.hpp:304
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.hpp:1117
void _configure()
Definition: App.hpp:1832
Option * add_mutable_set(std::string option_name, T &member, const std::set< T > &options, std::string option_description, bool defaulted)
Add set of options (with default, set can be changed afterwards - do not destroy the set) DEPRECATED...
Definition: App.hpp:794
size_t get_require_option_min() const
Get the required min option value.
Definition: App.hpp:1655
std::function< std::string(const App *, const Error &e)> failure_message_
The error message printing function INHERITABLE.
Definition: App.hpp:133
void parse(std::string commandline, bool program_name_included=false)
Definition: App.hpp:1299
bool required_
If set to true the subcommand is required to be processed and used, ignored for main app...
Definition: App.hpp:88
bool immediate_callback_
Definition: App.hpp:98
App * name(std::string app_name="")
Set a name for the app (empty will use parser to set the name)
Definition: App.hpp:296
void run_callback()
Process the callback.
Definition: Option.hpp:680
Thrown when an excludes option is present.
Definition: Error.hpp:268
std::shared_ptr< Config > get_config_formatter() const
Access the config formatter.
Definition: App.hpp:1537
std::string envname_
If given, check the environment for this option.
Definition: Option.hpp:230
size_t require_option_min_
Minimum required options (not inheritable!)
Definition: App.hpp:205
bool get_ignore_underscore() const
Check the status of ignore_underscore.
Definition: App.hpp:1631
std::vector< App * > parsed_subcommands_
This is a list of the subcommands collected, in order.
Definition: App.hpp:150
const std::string & get_group() const
Get the group of this option.
Definition: Option.hpp:105
size_t remaining_size(bool recurse=false) const
This returns the number of remaining options, minus the – separator.
Definition: App.hpp:1780
bool positionals_at_end_
specify that positional arguments come at the end of the argument sequence not inheritable ...
Definition: App.hpp:184
const std::string & get_group() const
Get the group of this subcommand.
Definition: App.hpp:1643
Extension of App to better manage groups of options.
Definition: App.hpp:2686
bool got_subcommand(App *subcom) const
Check to see if given subcommand was selected.
Definition: App.hpp:1443
size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
Definition: App.hpp:2313
Option * add_result(std::string s)
Puts a result at the end.
Definition: Option.hpp:833
bool get_disabled_by_default() const
Get the status of disabled by default.
Definition: App.hpp:1676
App * fallthrough(bool value=true)
Definition: App.hpp:1241
virtual void pre_callback()
Definition: App.hpp:1257
bool _valid_subcommand(const std::string ¤t, bool ignore_used=true) const
Check to see if a subcommand is valid. Give up immediately if subcommand max has been reached...
Definition: App.hpp:1875
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer) ...
Definition: App.hpp:227
void add_options(Option *opt, Args... args)
Add a bunch of options to the group.
Definition: App.hpp:2705
Option * add_option(std::string option_name, T &variable, std::string option_description="", bool defaulted=false)
Add option for non-vectors (duplicate copy needed without defaulted to avoid iostream << value) ...
Definition: App.hpp:473
App * require_subcommand(size_t min, size_t max)
Definition: App.hpp:1204
Option * add_option(std::string option_name)
Add option with no description or variable assignment.
Definition: App.hpp:509
App * footer(std::string footer_string)
Set footer.
Definition: App.hpp:1502
Holds values to load into Options.
Definition: ConfigFwd.hpp:42
std::shared_ptr< FormatterBase > formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer) ...
Definition: App.hpp:130
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) with a (shared) pointer instead") Option *add_mutable_set_ignore_case_underscore(std
Definition: App.hpp:923
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:1040
void clear()
Clear the parsed results (mostly for testing)
Definition: Option.hpp:316
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition: App.hpp:315
size_t get_require_option_max() const
Get the required max option value.
Definition: App.hpp:1658
bool remove_option(Option *opt)
Removes an option from the App. Takes an option pointer. Returns true if found and removed...
Definition: App.hpp:1017
Verify items are in a set.
Definition: Validators.hpp:540
const Option * get_option_no_throw(std::string option_name) const noexcept
Get an option by name (noexcept const version)
Definition: App.hpp:1585
bool check_name(std::string name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
Definition: Option.hpp:757
App * ignore_underscore(bool value=true)
Ignore underscore. Subcommands inherit value.
Definition: App.hpp:384
App * get_parent()
Get the parent of this subcommand (or nullptr if master app)
Definition: App.hpp:1702
std::string get_name() const
Get the name of the current app.
Definition: App.hpp:1708
bool remove_excludes(App *app)
Removes a subcommand from this excludes list of this subcommand.
Definition: App.hpp:1485
App * require_subcommand(int value)
Definition: App.hpp:1191
const std::vector< Option * > & parse_order() const
This gets a vector of pointers with the original parse order.
Definition: App.hpp:1743
Thrown when an option is set to conflicting values (non-vector and multi args, for example) ...
Definition: Error.hpp:86
void _parse(std::vector< std::string > &&args)
Internal parse function.
Definition: App.hpp:2200
void add_options(Option *opt)
Add an existing option to the Option_group.
Definition: App.hpp:2703
bool parsed() const
Check to see if this subcommand was parsed, true only if received on command line.
Definition: App.hpp:414
App * _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept
Definition: App.hpp:2408
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_case, CLI::ignore_underscore)) instead") Option *add_set_ignore_case_underscore(std
Add set of options, string only, ignore underscore and case (no default, static set) DEPRECATED...
Definition: App.hpp:910
bool allow_config_extras_
If true, allow extra arguments in the ini file (ie, don't throw an error). INHERITABLE.
Definition: App.hpp:79
App * description(std::string app_description)
Set the description of the app.
Definition: App.hpp:1543
bool got_subcommand(std::string subcommand_name) const
Check with name instead of pointer to see if subcommand was selected.
Definition: App.hpp:1449
std::vector< App * > get_subcommands() const
Definition: App.hpp:1405
const std::string & get_footer() const
Get footer.
Definition: App.hpp:1646
bool get_allow_windows_style_options() const
Check the status of the allow windows style options.
Definition: App.hpp:1637
bool check_name(std::string name_to_check) const
Check the name, case insensitive and underscore insensitive if set.
Definition: App.hpp:1714
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
Definition: App.hpp:2163
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition: App.hpp:147
const Option * get_help_ptr() const
Get a pointer to the help flag. (const)
Definition: App.hpp:1690
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_underscore)) instead") Option *add_set_ignore_underscore(std
Add set of options, string only, ignore underscore (no default, static set) DEPRECATED.
Definition: App.hpp:858
CLI::App_p get_subcommand_ptr(int index=0) const
Get an owning pointer to subcommand by index.
Definition: App.hpp:1135
detail::Classifier _recognize(const std::string ¤t, bool ignore_used_subcommands=true) const
Selects a Classifier enum based on the type of the current argument.
Definition: App.hpp:1889
App * allow_config_extras(bool allow=true)
Definition: App.hpp:347
App * formatter_fn(std::function< std::string(const App *, std::string, AppFormatMode)> fmt)
Set the help formatter.
Definition: App.hpp:402
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.hpp:581
Option * add_flag(std::string flag_name, T &flag_count, std::string flag_description="")
Definition: App.hpp:658
std::vector< std::string > remaining(bool recurse=false) const
This returns the missing options from the current subcommand.
Definition: App.hpp:1746
Creates a command line program, with very few defaults.
Definition: App.hpp:59
App * allow_windows_style_options(bool value=true)
Allow windows style options, such as /opt. First matching short or long name used. Subcommands inherit value.
Definition: App.hpp:372
OptionDefaults option_defaults_
The default values for options, customizable and changeable INHERITABLE.
Definition: App.hpp:111
bool get_required() const
True if this is a required option.
Definition: Option.hpp:108
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.hpp:1372
Option * type_size(int option_type_size)
Set a custom option size.
Definition: Option.hpp:929
std::set< Option * > exclude_options_
Definition: App.hpp:157
bool get_callback_run() const
See if the callback has been run already.
Definition: Option.hpp:910
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition: Option.hpp:224
void _process()
Process callbacks and such.
Definition: App.hpp:2122
Thrown when a requires option is missing.
Definition: Error.hpp:261
size_t count_all() const
Definition: App.hpp:1161
std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
Definition: App.hpp:1515
std::function< void(size_t)> pre_parse_callback_
This is a function that runs prior to the start of parsing.
Definition: App.hpp:101
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition: Option.hpp:269
Option * get_option_no_throw(std::string option_name) noexcept
Get an option by name (noexcept non-const version)
Definition: App.hpp:1566
bool _parse_subcommand(std::vector< std::string > &args)
Definition: App.hpp:2429
bool get_configurable() const
The status of configurable.
Definition: Option.hpp:117
const Option * get_config_ptr() const
Get a pointer to the config option. (const)
Definition: App.hpp:1699
bool get_allow_extras() const
Get the status of allow extras.
Definition: App.hpp:1664
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition: App.hpp:121
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:1367
App * positionals_at_end(bool value=true)
Specify that the positional arguments are only at the end of the sequence.
Definition: App.hpp:378
size_t empty() const
True if the option was not passed.
Definition: Option.hpp:310
Usually something like –help-all on command line.
Definition: Error.hpp:162
App * get_subcommand(std::string subcom) const
Check to see if a subcommand is part of this command (text version)
Definition: App.hpp:1100
App * get_subcommand(int index=0) const
Get a pointer to subcommand by index.
Definition: App.hpp:1107
App * get_option_group(std::string group_name) const
Check to see if an option group is part of this App.
Definition: App.hpp:1145
bool allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition: App.hpp:76
Thrown when validation fails before parsing.
Definition: Error.hpp:295
App * excludes(Option *opt)
Sets excluded options for the subcommand.
Definition: App.hpp:1452
App(std::string app_description, std::string app_name, App *parent)
Special private constructor for subcommand.
Definition: App.hpp:232
std::vector< App * > get_subcommands(const std::function< bool(App *)> &filter)
Definition: App.hpp:1427
Definition: Option.hpp:169
Thrown when parsing an INI file and it is missing.
Definition: Error.hpp:175
Option * help_all_ptr_
A pointer to the help all flag if there is one INHERITABLE.
Definition: App.hpp:127
Option * config_ptr_
Pointer to the config option.
Definition: App.hpp:224
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.hpp:713
bool config_required_
True if ini is required (throws if not present), if false simply keep going.
Definition: App.hpp:221
void _process_extras()
Throw an error if anything is left over and should not be.
Definition: App.hpp:2131
App * formatter(std::shared_ptr< FormatterBase > fmt)
Set the help formatter.
Definition: App.hpp:396
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.hpp:2615
std::string config_name_
The name of the connected config file.
Definition: App.hpp:218
void clear()
Reset the parsed data.
Definition: App.hpp:1264
void run_callback()
Internal function to run (App) callback, bottom up.
Definition: App.hpp:1853
Check to see if something is bool (fail check by default)
Definition: TypeTools.hpp:50
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
Definition: App.hpp:1930
std::string config_to_str(bool default_also=false, bool write_description=false) const
Definition: App.hpp:1509
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_underscore)) with a (shared) pointer instead") Option *add_mutable_set_ignore_underscore(std
Definition: App.hpp:871
size_t require_subcommand_min_
Minimum required subcommands (not inheritable!)
Definition: App.hpp:199
bool get_validate_positionals() const
Get the status of validating positionals.
Definition: App.hpp:1681
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition: App.hpp:91
const Option * operator[](const char *option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition: App.hpp:1625
size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE...
Definition: App.hpp:202
const App * get_parent() const
Get the parent of this subcommand (or nullptr if master app) (const version)
Definition: App.hpp:1705
std::string name_
Subcommand name or program name (from parser if name is empty)
Definition: App.hpp:70
Option * add_flag(std::string flag_name, T &flag_description)
Definition: App.hpp:650
Option * add_option_function(std::string option_name, const std::function< void(const T &)> &func, std::string option_description="")
Add option for a callback of a specific type.
Definition: App.hpp:490
bool pre_parse_called_
Flag indicating that the pre_parse_callback has been triggered.
Definition: App.hpp:94
std::shared_ptr< FormatterBase > get_formatter() const
Access the formatter.
Definition: App.hpp:1534
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.hpp:1549
bool enabled_by_default_
If set to true the subcommand will be reenabled at the start of each parse.
Definition: App.hpp:189
bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Definition: App.hpp:2324
Option * get_help_ptr()
Get a pointer to the help flag.
Definition: App.hpp:1687
Thrown when an option already exists.
Definition: Error.hpp:128
Thrown when counting a non-existent option.
Definition: Error.hpp:312
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type)
Definition: App.hpp:2455
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise...
Definition: App.hpp:176
void parse(std::vector< std::string > &&args)
The real work is done here. Expects a reversed vector.
Definition: App.hpp:1347
const Option * operator[](const std::string &option_name) const
Shortcut bracket operator for getting a pointer to an option.
Definition: App.hpp:1622
bool get_prefix_command() const
Get the prefix command status.
Definition: App.hpp:1661
void _process_ini()
Read and process an ini file (main app only)
Definition: App.hpp:1910
size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable)
Definition: App.hpp:208
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(...)) with a (shared) pointer instead") Option *add_mutable_set_ignore_case(std
Definition: App.hpp:845
std::string get_description() const
Get the app or subcommand description.
Definition: App.hpp:1540
std::string fullname() const
The list of parents and name joined by ".".
Definition: ConfigFwd.hpp:53
bool disabled_by_default_
If set to true the subcommand will start each parse disabled.
Definition: App.hpp:187
App * prefix_command(bool allow=true)
Do not parse anything after the first unrecognized option and return.
Definition: App.hpp:354
Option * add_mutable_set(std::string option_name, T &member, const std::set< T > &options, std::string option_description="")
Add set of options (No default, set can be changed afterwards - do not destroy the set) DEPRECATED...
Definition: App.hpp:769
App * require_subcommand()
The argumentless form of require subcommand requires 1 or more subcommands.
Definition: App.hpp:1182
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition: Option.hpp:221
Option * add_set(std::string option_name, T &member, std::set< T > options, std::string option_description, bool defaulted)
Add set of options (with default, static set, such as an inline set) DEPRECATED.
Definition: App.hpp:781
bool remove_subcommand(App *subcom)
Removes a subcommand from the App. Takes a subcommand pointer. Returns true if found and removed...
Definition: App.hpp:1074
App * enabled_by_default(bool enable=true)
Definition: App.hpp:328
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
Definition: App.hpp:1474
size_t get_require_subcommand_max() const
Get the required max subcommand value.
Definition: App.hpp:1652
std::vector< Option_p > options_
The list of options, stored locally.
Definition: App.hpp:114
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition: App.hpp:153
void _parse_config(std::vector< ConfigItem > &args)
Definition: App.hpp:2220
App * require_option()
The argumentless form of require option requires 1 or more options be used.
Definition: App.hpp:1211
App * required(bool require=true)
Remove the error when extras are left over on the command line.
Definition: App.hpp:309
std::string get_display_name() const
Get a display name for an app.
Definition: App.hpp:1711
App * get_subcommand(App *subcom) const
Definition: App.hpp:1090
const Option * get_help_all_ptr() const
Get a pointer to the help all flag. (const)
Definition: App.hpp:1693
App * validate_positionals(bool validate=true)
Set the subcommand to validate positional arguments before assigning.
Definition: App.hpp:340
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition: Option.hpp:923
void _trigger_pre_parse(size_t remaining_args)
Trigger the pre_parse callback if needed.
Definition: App.hpp:2596
bool get_immediate_callback() const
Get the status of disabled.
Definition: App.hpp:1673
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition: App.hpp:170
App * preparse_callback(std::function< void(size_t)> pp_callback)
Definition: App.hpp:290
bool get_required() const
Get the status of required.
Definition: App.hpp:1667
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:2803
Option * add_flag_function(std::string flag_name, std::function< void(int64_t)> function, std::string flag_description="")
Add option for callback with an integer value.
Definition: App.hpp:733
Option * transform(Validator validator, std::string validator_name="")
Adds a transforming validator with a built in type name.
Definition: Option.hpp:368
App * excludes(App *app)
Sets excluded subcommands for the subcommand.
Definition: App.hpp:1461
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.hpp:598
const std::string & get_description() const
Get the description.
Definition: Option.hpp:604
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition: App.hpp:2171
App * config_formatter(std::shared_ptr< Config > fmt)
Set the config formatter.
Definition: App.hpp:408
bool fallthrough_
Allow subcommand fallthrough, so that parent commands can collect commands after subcommand. INHERITABLE.
Definition: App.hpp:173
std::string get_name(bool positional=false, bool all_options=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition: Option.hpp:620
Option * add_complex(std::string option_name, T &variable, std::string option_description="", bool defaulted=false, std::string label="COMPLEX")
Add a complex number.
Definition: App.hpp:965
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition: Option.hpp:522
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
Definition: App.hpp:2269
size_t count() const
Count the total number of times an option was passed.
Definition: Option.hpp:307
void parse(int argc, const char *const *argv)
Definition: App.hpp:1281
Option * set_config(std::string option_name="", std::string default_filename="", 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.hpp:996
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:85
Definition: Option.hpp:206
std::function< void()> callback_
This is a function that runs when complete. Great for subcommands. Can throw.
Definition: App.hpp:104
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition: Option.hpp:67
bool get_allow_config_extras() const
Get the status of allow extras.
Definition: App.hpp:1684
std::vector< App_p > subcommands_
Storage for subcommand list.
Definition: App.hpp:164
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition: App.hpp:191
virtual ~App()=default
virtual destructor
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition: App.hpp:642
App * require_option(int value)
Definition: App.hpp:1220
OptionDefaults * option_defaults()
Get the OptionDefault object, to set option defaults.
Definition: App.hpp:417
App * group(std::string group_name)
Changes the group membership.
Definition: App.hpp:1176
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:2796
App * parent_
A pointer to the parent if this is a subcommand.
Definition: App.hpp:193
Option * check(Validator validator, std::string validator_name="")
Adds a Validator with a built in type name.
Definition: Option.hpp:350
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:698
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:269
App * ignore_case(bool value=true)
Ignore case. Subcommands inherit value.
Definition: App.hpp:360
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:321
std::set< Option * > needs_
A list of options that are required with this option.
Definition: Option.hpp:266
Thrown when a required option is missing.
Definition: Error.hpp:205
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition: Option.hpp:155
Option * help_ptr_
A pointer to the help flag if there is one INHERITABLE.
Definition: App.hpp:124
std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition: App.hpp:1729
CLI11_DEPRECATED("Use ->transform(CLI::IsMember(..., CLI::ignore_case)) instead") Option *add_set_ignore_case(std
Add set of options, string only, ignore case (no default, static set) DEPRECATED. ...
Definition: App.hpp:806
bool get_fallthrough() const
Check the status of fallthrough.
Definition: App.hpp:1634