15#include "../Encoding.hpp"
29CLI11_INLINE
App::App(std::string app_description, std::string app_name,
App *parent)
30 : name_(std::move(app_name)), description_(std::move(app_description)), parent_(parent) {
67 normalized_argv_ = detail::compute_win32_argv();
69 if(!normalized_argv_view_.empty()) {
70 normalized_argv_view_.clear();
73 normalized_argv_view_.reserve(normalized_argv_.size());
74 for(
auto &arg : normalized_argv_) {
76 normalized_argv_view_.push_back(
const_cast<char *
>(arg.data()));
79 return normalized_argv_view_.data();
88 std::string oname =
name_;
103 if(app_name.empty() || !detail::valid_alias_name_string(app_name)) {
111 throw(
OptionAlreadyAdded(
"alias already matches an existing subcommand: " + app_name));
139 throw OptionAlreadyAdded(
"ignore case would cause subcommand name conflicts: " + match);
153 throw OptionAlreadyAdded(
"ignore underscore would cause subcommand name conflicts: " + match);
161 callback_t option_callback,
162 std::string option_description,
164 std::function<std::string()> func) {
167 if(std::find_if(std::begin(
options_), std::end(
options_), [&myopt](
const Option_p &v) {
return *v == myopt; }) ==
169 if(myopt.lnames_.empty() && myopt.snames_.empty()) {
173 if(test_name.size() == 3) {
174 test_name.erase(0, 1);
178 if(op !=
nullptr && op->get_configurable()) {
179 throw(
OptionAlreadyAdded(
"added option positional name matches existing option: " + test_name));
181 }
else if(
parent_ !=
nullptr) {
182 for(
auto &ln : myopt.lnames_) {
184 if(op !=
nullptr && op->get_configurable()) {
188 for(
auto &sn : myopt.snames_) {
190 if(op !=
nullptr && op->get_configurable()) {
196 for(
auto &sname : myopt.snames_) {
197 if(sname.length() > 1) {
198 std::string test_name;
199 test_name.push_back(
'-');
200 test_name.push_back(sname.front());
203 throw(
OptionAlreadyAdded(
"added option interferes with existing short option: " + sname));
208 for(
const auto &osn : opt->snames_) {
210 std::string test_name;
211 test_name.push_back(osn.front());
212 if(myopt.check_sname(test_name)) {
213 throw(
OptionAlreadyAdded(
"added option interferes with existing non standard option: " +
225 option->default_function(func);
229 option->capture_default_str();
235 if(!defaulted && option->get_always_capture_default())
236 option->capture_default_str();
242 const auto &matchname = opt->matching_name(myopt);
243 if(!matchname.empty()) {
259 if(!flag_name.empty()) {
275 if(!help_name.empty()) {
292 if(!flag_name.empty()) {
294 flag_name, [versionString]() {
throw(
CLI::CallForVersion(versionString, 0)); }, version_help);
309 if(!flag_name.empty()) {
318CLI11_INLINE
Option *App::_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description) {
320 if(detail::has_default_flag_values(flag_name)) {
322 auto flag_defaults = detail::get_default_flag_values(flag_name);
323 detail::remove_default_flag_values(flag_name);
324 opt =
add_option(std::move(flag_name), std::move(fun), std::move(flag_description),
false);
325 for(
const auto &fname : flag_defaults)
326 opt->fnames_.push_back(fname.first);
327 opt->default_flag_values_ = std::move(flag_defaults);
329 opt =
add_option(std::move(flag_name), std::move(fun), std::move(flag_description),
false);
333 auto pos_name = opt->
get_name(
true);
335 throw IncorrectConstruction::PositionalFlag(pos_name);
344 std::function<
void(
void)> function,
345 std::string flag_description) {
347 CLI::callback_t fun = [function](
const CLI::results_t &res) {
348 using CLI::detail::lexical_cast;
350 auto result = lexical_cast(res[0], trigger);
351 if(result && trigger) {
356 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
361 std::function<
void(std::int64_t)> function,
362 std::string flag_description) {
364 CLI::callback_t fun = [function](
const CLI::results_t &res) {
365 using CLI::detail::lexical_cast;
366 std::int64_t flag_count{0};
367 lexical_cast(res[0], flag_count);
368 function(flag_count);
371 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
376 std::string default_filename,
377 const std::string &help_message,
378 bool config_required) {
387 if(!option_name.empty()) {
389 if(config_required) {
392 if(!default_filename.empty()) {
407 op->remove_needs(opt);
408 op->remove_excludes(opt);
417 std::find_if(std::begin(
options_), std::end(
options_), [opt](
const Option_p &v) {
return v.get() == opt; });
418 if(iterator != std::end(
options_)) {
426 if(!subcommand_name.empty() && !detail::valid_name_string(subcommand_name)) {
427 if(!detail::valid_first_char(subcommand_name[0])) {
429 "Subcommand name starts with invalid character, '!' and '-' and control characters");
431 for(
auto c : subcommand_name) {
432 if(!detail::valid_later_char(c)) {
434 "'), all characters are allowed except"
435 "'=',':','{','}', ' ', and control characters");
439 CLI::App_p subcom = std::shared_ptr<App>(
new App(std::move(subcommand_description), subcommand_name,
this));
449 throw(
OptionAlreadyAdded(
"subcommand name or alias matches existing subcommand: " + mstrg));
451 subcom->parent_ =
this;
459 sub->remove_excludes(subcom);
460 sub->remove_needs(subcom);
463 auto iterator = std::find_if(
473 if(subcom ==
nullptr)
476 if(subcomptr.get() == subcom)
477 return subcomptr.get();
489 return _find_subcommand(subcom,
false,
false);
494 auto uindex =
static_cast<unsigned>(index);
502 if(subcom ==
nullptr)
505 if(subcomptr.get() == subcom)
512 if(subcomptr->check_name(subcom))
519 auto uindex =
static_cast<unsigned>(index);
528 if(app->name_.empty() && app->group_ == group_name) {
541 cnt += sub->count_all();
556 for(
const Option_p &opt :
options_) {
564CLI11_INLINE
void App::parse(
int argc,
const char *
const *argv) { parse_char_t(argc, argv); }
565CLI11_INLINE
void App::parse(
int argc,
const wchar_t *
const *argv) { parse_char_t(argc, argv); }
570CLI11_INLINE
const char *maybe_narrow(
const char *str) {
return str; }
571CLI11_INLINE std::string maybe_narrow(
const wchar_t *str) {
return narrow(str); }
575template <
class CharT> CLI11_INLINE
void App::parse_char_t(
int argc,
const CharT *
const *argv) {
579 name_ = detail::maybe_narrow(argv[0]);
582 std::vector<std::string> args;
583 args.reserve(
static_cast<std::size_t
>(argc) - 1U);
584 for(
auto i =
static_cast<std::size_t
>(argc) - 1U; i > 0U; --i)
585 args.emplace_back(detail::maybe_narrow(argv[i]));
587 parse(std::move(args));
590CLI11_INLINE
void App::parse(std::string commandline,
bool program_name_included) {
592 if(program_name_included) {
593 auto nstr = detail::split_program_name(commandline);
598 commandline = std::move(nstr.second);
600 detail::trim(commandline);
603 if(!commandline.empty()) {
604 commandline = detail::find_and_modify(commandline,
"=", detail::escape_detect);
606 commandline = detail::find_and_modify(commandline,
":", detail::escape_detect);
609 auto args = detail::split_up(std::move(commandline));
611 args.erase(std::remove(args.begin(), args.end(), std::string{}), args.end());
613 detail::remove_quotes(args);
614 }
catch(
const std::invalid_argument &arg) {
617 std::reverse(args.begin(), args.end());
618 parse(std::move(args));
621CLI11_INLINE
void App::parse(std::wstring commandline,
bool program_name_included) {
622 parse(narrow(commandline), program_name_included);
625CLI11_INLINE
void App::parse(std::vector<std::string> &args) {
644CLI11_INLINE
void App::parse(std::vector<std::string> &&args) {
663CLI11_INLINE
void App::parse_from_stream(std::istream &input) {
674CLI11_INLINE
int App::exit(
const Error &e, std::ostream &out, std::ostream &err)
const {
677 if(e.get_name() ==
"RuntimeError")
678 return e.get_exit_code();
680 if(e.get_name() ==
"CallForHelp") {
682 return e.get_exit_code();
685 if(e.get_name() ==
"CallForAllHelp") {
686 out <<
help(
"", AppFormatMode::All);
687 return e.get_exit_code();
690 if(e.get_name() ==
"CallForVersion") {
691 out << e.what() <<
'\n';
692 return e.get_exit_code();
695 if(e.get_exit_code() !=
static_cast<int>(ExitCodes::Success)) {
700 return e.get_exit_code();
709 subcomms.erase(std::remove_if(std::begin(subcomms),
711 [&filter](
const App *app) {
return !filter(app); }),
725 std::remove_if(std::begin(subcomms), std::end(subcomms), [&filter](
App *app) {
return !filter(app); }),
746 auto *other_app = *iterator;
748 other_app->remove_excludes(
this);
770CLI11_NODISCARD CLI11_INLINE std::string
App::help(std::string prev, AppFormatMode mode)
const {
778 if(!selected_subcommands.empty()) {
779 return selected_subcommands.back()->help(prev, mode);
781 return formatter_->make_help(
this, prev, mode);
803 std::vector<const Option *> options(
options_.size());
805 std::begin(
options_), std::end(
options_), std::begin(options), [](
const Option_p &val) {
return val.get(); });
808 options.erase(std::remove_if(std::begin(options),
810 [&filter](
const Option *opt) {
return !filter(opt); }),
815 const App *subc = subcp.get();
816 if(subc->get_name().empty() && !subc->get_group().empty() && subc->get_group().front() ==
'+') {
817 std::vector<const Option *> subcopts = subc->
get_options(filter);
818 options.insert(options.end(), subcopts.begin(), subcopts.end());
825 std::vector<Option *> options(
options_.size());
827 std::begin(
options_), std::end(
options_), std::begin(options), [](
const Option_p &val) {
return val.get(); });
831 std::remove_if(std::begin(options), std::end(options), [&filter](
Option *opt) {
return !filter(opt); }),
836 if(subc->get_name().empty() && !subc->get_group().empty() && subc->get_group().front() ==
'+') {
837 auto subcopts = subc->get_options(filter);
838 options.insert(options.end(), subcopts.begin(), subcopts.end());
845 for(Option_p &opt : options_) {
846 if(opt->check_name(option_name)) {
850 for(
auto &subc : subcommands_) {
852 if(subc->get_name().empty()) {
853 auto *opt = subc->get_option_no_throw(option_name);
863 for(
const Option_p &opt : options_) {
864 if(opt->check_name(option_name)) {
868 for(
const auto &subc : subcommands_) {
870 if(subc->get_name().empty()) {
871 auto *opt = subc->get_option_no_throw(option_name);
882 return std::string(
"[Option Group: ") +
get_group() +
"]";
884 if(
aliases_.empty() || !with_aliases) {
887 std::string dispname =
name_;
888 for(
const auto &lalias :
aliases_) {
889 dispname.push_back(
',');
890 dispname.push_back(
' ');
891 dispname.append(lalias);
897 std::string local_name =
name_;
899 local_name = detail::remove_underscore(
name_);
900 name_to_check = detail::remove_underscore(name_to_check);
903 local_name = detail::to_lower(
name_);
904 name_to_check = detail::to_lower(name_to_check);
907 if(local_name == name_to_check) {
912 les = detail::remove_underscore(les);
915 les = detail::to_lower(les);
917 if(les == name_to_check) {
925 std::vector<std::string> groups;
927 for(
const Option_p &opt :
options_) {
929 if(std::find(groups.begin(), groups.end(), opt->get_group()) == groups.end()) {
930 groups.push_back(opt->get_group());
937CLI11_NODISCARD CLI11_INLINE std::vector<std::string>
App::remaining(
bool recurse)
const {
938 std::vector<std::string> miss_list;
939 for(
const std::pair<detail::Classifier, std::string> &miss :
missing_) {
940 miss_list.push_back(std::get<1>(miss));
946 if(sub->name_.empty() && !sub->missing_.empty()) {
947 for(
const std::pair<detail::Classifier, std::string> &miss : sub->missing_) {
948 miss_list.push_back(std::get<1>(miss));
956 std::vector<std::string> output = sub->remaining(recurse);
957 std::copy(std::begin(output), std::end(output), std::back_inserter(miss_list));
964 std::vector<std::string> miss_list =
remaining(recurse);
965 std::reverse(std::begin(miss_list), std::end(miss_list));
970 auto remaining_options =
static_cast<std::size_t
>(std::count_if(
971 std::begin(
missing_), std::end(
missing_), [](
const std::pair<detail::Classifier, std::string> &val) {
972 return val.first != detail::Classifier::POSITIONAL_MARK;
977 remaining_options += sub->remaining_size(recurse);
980 return remaining_options;
985 auto pcount = std::count_if(std::begin(
options_), std::end(
options_), [](
const Option_p &opt) {
986 return opt->get_items_expected_max() >= detail::expected_max_vector_size && !opt->nonpositional();
989 auto pcount_req = std::count_if(std::begin(
options_), std::end(
options_), [](
const Option_p &opt) {
990 return opt->get_items_expected_max() >= detail::expected_max_vector_size && !opt->nonpositional() &&
993 if(pcount - pcount_req > 1) {
998 std::size_t nameless_subs{0};
1001 if(app->get_name().empty())
1008 throw(
InvalidError(
"Required min options greater than required max options", ExitCodes::InvalidError));
1013 InvalidError(
"Required min options greater than number of available options", ExitCodes::InvalidError));
1025 if(app->has_automatic_name_) {
1028 if(app->name_.empty()) {
1029 app->fallthrough_ =
false;
1030 app->prefix_command_ =
false;
1033 app->parent_ =
this;
1046 if(subc->parent_ ==
this) {
1047 subc->run_callback(
true, suppress_final_callback);
1052 if(subc->name_.empty() && subc->count_all() > 0) {
1053 subc->run_callback(
true, suppress_final_callback);
1072 if(com !=
nullptr) {
1082CLI11_NODISCARD CLI11_INLINE detail::Classifier
App::_recognize(
const std::string ¤t,
1083 bool ignore_used_subcommands)
const {
1084 std::string dummy1, dummy2;
1087 return detail::Classifier::POSITIONAL_MARK;
1089 return detail::Classifier::SUBCOMMAND;
1090 if(detail::split_long(current, dummy1, dummy2))
1091 return detail::Classifier::LONG;
1092 if(detail::split_short(current, dummy1, dummy2)) {
1093 if(dummy1[0] >=
'0' && dummy1[0] <=
'9') {
1095 return detail::Classifier::NONE;
1098 return detail::Classifier::SHORT;
1101 return detail::Classifier::WINDOWS_STYLE;
1102 if((current ==
"++") && !
name_.empty() &&
parent_ !=
nullptr)
1103 return detail::Classifier::SUBCOMMAND_TERMINATOR;
1104 auto dotloc = current.find_first_of(
'.');
1105 if(dotloc != std::string::npos) {
1106 auto *cm =
_find_subcommand(current.substr(0, dotloc),
true, ignore_used_subcommands);
1108 auto res = cm->_recognize(current.substr(dotloc + 1), ignore_used_subcommands);
1109 if(res == detail::Classifier::SUBCOMMAND) {
1114 return detail::Classifier::NONE;
1118 auto path_result = detail::check_path(config_file.c_str());
1119 if(path_result == detail::path_type::file) {
1130 }
else if(throw_error) {
1131 throw FileError::Missing(config_file);
1143 if(!ename_string.empty()) {
1149 auto config_files =
config_ptr_->
as<std::vector<std::string>>();
1150 bool files_used{file_given};
1151 if(config_files.empty() || config_files.front().empty()) {
1152 if(config_required) {
1153 throw FileError(
"config file is required but none was given");
1157 for(
const auto &config_file : config_files) {
1174 for(
const Option_p &opt :
options_) {
1175 if(opt->count() == 0 && !opt->envname_.empty()) {
1176 std::string ename_string = detail::get_environment_value(opt->envname_);
1177 if(!ename_string.empty()) {
1178 std::string result = ename_string;
1179 result = opt->_validate(result, 0);
1180 if(result.empty()) {
1181 opt->add_result(ename_string);
1188 if(sub->get_name().empty() || (sub->count_all() > 0 && !sub->parse_complete_callback_)) {
1190 sub->_process_env();
1199 if(sub->get_name().empty() && sub->parse_complete_callback_) {
1200 if(sub->count_all() > 0) {
1201 sub->_process_callbacks();
1202 sub->run_callback();
1207 for(
const Option_p &opt :
options_) {
1208 if((*opt) && !opt->get_callback_run()) {
1209 opt->run_callback();
1213 if(!sub->parse_complete_callback_) {
1214 sub->_process_callbacks();
1223 if(help_ptr !=
nullptr && help_ptr->count() > 0)
1224 trigger_help =
true;
1225 if(help_all_ptr !=
nullptr && help_all_ptr->count() > 0)
1226 trigger_all_help =
true;
1231 sub->_process_help_flags(trigger_help, trigger_all_help);
1234 }
else if(trigger_all_help) {
1236 }
else if(trigger_help) {
1243 bool excluded{
false};
1244 std::string excluder;
1246 if(opt->count() > 0) {
1248 excluder = opt->get_name();
1252 if(subc->count_all() > 0) {
1254 excluder = subc->get_display_name();
1266 bool missing_needed{
false};
1267 std::string missing_need;
1269 if(opt->count() == 0) {
1270 missing_needed =
true;
1271 missing_need = opt->get_name();
1275 if(subc->count_all() == 0) {
1276 missing_needed =
true;
1277 missing_need = subc->get_display_name();
1280 if(missing_needed) {
1288 std::size_t used_options = 0;
1289 for(
const Option_p &opt :
options_) {
1291 if(opt->count() != 0) {
1295 if(opt->get_required() && opt->count() == 0) {
1299 for(
const Option *opt_req : opt->needs_)
1300 if(opt->count() > 0 && opt_req->count() == 0)
1303 for(
const Option *opt_ex : opt->excludes_)
1304 if(opt->count() > 0 && opt_ex->count() != 0)
1321 if(sub->name_.empty() && sub->count_all() > 0) {
1327 auto option_list = detail::join(
options_, [
this](
const Option_p &ptr) {
1329 return std::string{};
1331 return ptr->get_name(
false,
true);
1334 auto subc_list =
get_subcommands([](
App *app) {
return ((app->get_name().empty()) && (!app->disabled_)); });
1335 if(!subc_list.empty()) {
1336 option_list +=
"," + detail::join(subc_list, [](
const App *app) {
return app->get_display_name(); });
1342 for(App_p &sub : subcommands_) {
1345 if(sub->name_.empty() && sub->required_ ==
false) {
1346 if(sub->count_all() == 0) {
1347 if(require_option_min_ > 0 && require_option_min_ <= used_options) {
1352 if(require_option_max_ > 0 && used_options >= require_option_min_) {
1359 if(sub->count() > 0 || sub->name_.empty()) {
1360 sub->_process_requirements();
1363 if(sub->required_ && sub->count_all() == 0) {
1369CLI11_INLINE
void App::_process() {
1373 _process_config_file();
1380 _process_callbacks();
1381 _process_help_flags();
1385 _process_callbacks();
1386 _process_help_flags();
1388 _process_requirements();
1391CLI11_INLINE
void App::_process_extras() {
1392 if(!(allow_extras_ || prefix_command_)) {
1393 std::size_t num_left_over = remaining_size();
1394 if(num_left_over > 0) {
1399 for(App_p &sub : subcommands_) {
1400 if(sub->count() > 0)
1401 sub->_process_extras();
1405CLI11_INLINE
void App::_process_extras(std::vector<std::string> &args) {
1406 if(!(allow_extras_ || prefix_command_)) {
1407 std::size_t num_left_over = remaining_size();
1408 if(num_left_over > 0) {
1409 args = remaining(
false);
1414 for(App_p &sub : subcommands_) {
1415 if(sub->count() > 0)
1416 sub->_process_extras(args);
1420CLI11_INLINE
void App::increment_parsed() {
1422 for(App_p &sub : subcommands_) {
1423 if(sub->get_name().empty())
1424 sub->increment_parsed();
1428CLI11_INLINE
void App::_parse(std::vector<std::string> &args) {
1430 _trigger_pre_parse(args.size());
1431 bool positional_only =
false;
1433 while(!args.empty()) {
1434 if(!_parse_single(args, positional_only)) {
1439 if(parent_ ==
nullptr) {
1443 _process_extras(args);
1446 args = remaining_for_passthrough(
false);
1447 }
else if(parse_complete_callback_) {
1449 _process_callbacks();
1450 _process_help_flags();
1451 _process_requirements();
1452 run_callback(
false,
true);
1456CLI11_INLINE
void App::_parse(std::vector<std::string> &&args) {
1460 _trigger_pre_parse(args.size());
1461 bool positional_only =
false;
1463 while(!args.empty()) {
1464 _parse_single(args, positional_only);
1472CLI11_INLINE
void App::_parse_stream(std::istream &input) {
1473 auto values = config_formatter_->from_config(input);
1474 _parse_config(values);
1476 _trigger_pre_parse(values.size());
1483CLI11_INLINE
void App::_parse_config(
const std::vector<ConfigItem> &args) {
1485 if(!_parse_single_config(item) && allow_config_extras_ == config_extras_mode::error)
1486 throw ConfigError::Extras(item.fullname());
1490CLI11_INLINE
bool App::_parse_single_config(
const ConfigItem &item, std::size_t level) {
1492 if(level < item.parents.size()) {
1493 auto *subcom = get_subcommand_no_throw(item.parents.at(level));
1494 return (subcom !=
nullptr) ? subcom->_parse_single_config(item, level + 1) :
false;
1497 if(item.name ==
"++") {
1500 _trigger_pre_parse(2);
1501 if(parent_ !=
nullptr) {
1502 parent_->parsed_subcommands_.push_back(
this);
1508 if(item.name ==
"--") {
1509 if(configurable_ && parse_complete_callback_) {
1510 _process_callbacks();
1511 _process_requirements();
1516 Option *op = get_option_no_throw(
"--" + item.name);
1518 if(item.name.size() == 1) {
1519 op = get_option_no_throw(
"-" + item.name);
1522 op = get_option_no_throw(item.name);
1523 }
else if(!op->get_configurable()) {
1524 auto *testop = get_option_no_throw(item.name);
1525 if(testop !=
nullptr && testop->get_configurable()) {
1529 }
else if(!op->get_configurable()) {
1530 if(item.name.size() == 1) {
1531 auto *testop = get_option_no_throw(
"-" + item.name);
1532 if(testop !=
nullptr && testop->get_configurable()) {
1536 if(!op->get_configurable()) {
1537 auto *testop = get_option_no_throw(item.name);
1538 if(testop !=
nullptr && testop->get_configurable()) {
1546 if(get_allow_config_extras() == config_extras_mode::capture) {
1548 missing_.emplace_back(detail::Classifier::NONE, item.fullname());
1549 for(
const auto &input : item.inputs) {
1550 missing_.emplace_back(detail::Classifier::NONE, input);
1556 if(!op->get_configurable()) {
1557 if(get_allow_config_extras() == config_extras_mode::ignore_all) {
1560 throw ConfigError::NotConfigurable(item.fullname());
1563 std::vector<std::string> buffer;
1564 bool useBuffer{
false};
1565 if(item.multiline) {
1566 if(!op->get_inject_separator()) {
1567 buffer = item.inputs;
1568 buffer.erase(std::remove(buffer.begin(), buffer.end(),
"%%"), buffer.end());
1572 const std::vector<std::string> &inputs = (useBuffer) ? buffer : item.inputs;
1573 if(op->get_expected_min() == 0) {
1574 if(item.inputs.size() <= 1) {
1576 auto res = config_formatter_->to_flag(item);
1577 bool converted{
false};
1578 if(op->get_disable_flag_override()) {
1579 auto val = detail::to_flag_value(res);
1581 res = op->get_flag_value(item.name,
"{}");
1588 if(res !=
"{}" || op->get_expected_max() <= 1) {
1589 res = op->get_flag_value(item.name, res);
1593 op->add_result(res);
1596 if(
static_cast<int>(inputs.size()) > op->get_items_expected_max() &&
1597 op->get_multi_option_policy() != MultiOptionPolicy::TakeAll) {
1598 if(op->get_items_expected_max() > 1) {
1599 throw ArgumentMismatch::AtMost(item.fullname(), op->get_items_expected_max(), inputs.size());
1602 if(!op->get_disable_flag_override()) {
1603 throw ConversionError::TooManyInputsFlag(item.fullname());
1607 for(
const auto &res : inputs) {
1608 bool valid_value{
false};
1609 if(op->default_flag_values_.empty()) {
1610 if(res ==
"true" || res ==
"false" || res ==
"1" || res ==
"0") {
1614 for(
const auto &valid_res : op->default_flag_values_) {
1615 if(valid_res.second == res) {
1623 op->add_result(res);
1631 op->add_result(inputs);
1638CLI11_INLINE
bool App::_parse_single(std::vector<std::string> &args,
bool &positional_only) {
1640 detail::Classifier classifier = positional_only ? detail::Classifier::NONE : _recognize(args.back());
1641 switch(classifier) {
1642 case detail::Classifier::POSITIONAL_MARK:
1644 positional_only =
true;
1645 if((!_has_remaining_positionals()) && (parent_ !=
nullptr)) {
1648 _move_to_missing(classifier,
"--");
1651 case detail::Classifier::SUBCOMMAND_TERMINATOR:
1656 case detail::Classifier::SUBCOMMAND:
1657 retval = _parse_subcommand(args);
1659 case detail::Classifier::LONG:
1660 case detail::Classifier::SHORT:
1661 case detail::Classifier::WINDOWS_STYLE:
1663 retval = _parse_arg(args, classifier,
false);
1665 case detail::Classifier::NONE:
1667 retval = _parse_positional(args,
false);
1668 if(retval && positionals_at_end_) {
1669 positional_only =
true;
1674 throw HorribleError(
"unrecognized classifier (you should not see this!)");
1680CLI11_NODISCARD CLI11_INLINE std::size_t App::_count_remaining_positionals(
bool required_only)
const {
1681 std::size_t retval = 0;
1682 for(
const Option_p &opt : options_) {
1683 if(opt->get_positional() && (!required_only || opt->get_required())) {
1684 if(opt->get_items_expected_min() > 0 &&
static_cast<int>(opt->count()) < opt->get_items_expected_min()) {
1685 retval +=
static_cast<std::size_t
>(opt->get_items_expected_min()) - opt->count();
1692CLI11_NODISCARD CLI11_INLINE
bool App::_has_remaining_positionals()
const {
1693 for(
const Option_p &opt : options_) {
1694 if(opt->get_positional() && ((
static_cast<int>(opt->count()) < opt->get_items_expected_min()))) {
1702CLI11_INLINE
bool App::_parse_positional(std::vector<std::string> &args,
bool haltOnSubcommand) {
1704 const std::string &positional = args.back();
1707 if(positionals_at_end_) {
1709 auto arg_rem = args.size();
1710 auto remreq = _count_remaining_positionals(
true);
1711 if(arg_rem <= remreq) {
1712 for(
const Option_p &opt : options_) {
1713 if(opt->get_positional() && opt->required_) {
1714 if(
static_cast<int>(opt->count()) < opt->get_items_expected_min()) {
1715 if(validate_positionals_) {
1716 std::string pos = positional;
1717 pos = opt->_validate(pos, 0);
1729 if(posOpt ==
nullptr) {
1730 for(
const Option_p &opt : options_) {
1732 if(opt->get_positional() &&
1733 (
static_cast<int>(opt->count()) < opt->get_items_expected_min() || opt->get_allow_extra_args())) {
1734 if(validate_positionals_) {
1735 std::string pos = positional;
1736 pos = opt->_validate(pos, 0);
1746 if(posOpt !=
nullptr) {
1747 parse_order_.push_back(posOpt);
1748 if(posOpt->get_inject_separator()) {
1749 if(!posOpt->results().empty() && !posOpt->results().back().empty()) {
1750 posOpt->add_result(std::string{});
1753 if(posOpt->get_trigger_on_parse() && posOpt->current_option_state_ == Option::option_state::callback_run) {
1756 posOpt->add_result(positional);
1757 if(posOpt->get_trigger_on_parse()) {
1758 posOpt->run_callback();
1765 for(
auto &subc : subcommands_) {
1766 if((subc->name_.empty()) && (!subc->disabled_)) {
1767 if(subc->_parse_positional(args,
false)) {
1768 if(!subc->pre_parse_called_) {
1769 subc->_trigger_pre_parse(args.size());
1776 if(parent_ !=
nullptr && fallthrough_) {
1777 return _get_fallthrough_parent()->_parse_positional(args,
static_cast<bool>(parse_complete_callback_));
1780 auto *com = _find_subcommand(args.back(),
true,
false);
1781 if(com !=
nullptr && (require_subcommand_max_ == 0 || require_subcommand_max_ > parsed_subcommands_.size())) {
1782 if(haltOnSubcommand) {
1789 if(subcommand_fallthrough_) {
1792 auto *parent_app = (parent_ !=
nullptr) ? _get_fallthrough_parent() :
this;
1793 com = parent_app->_find_subcommand(args.back(),
true,
false);
1794 if(com !=
nullptr && (com->parent_->require_subcommand_max_ == 0 ||
1795 com->parent_->require_subcommand_max_ > com->parent_->parsed_subcommands_.size())) {
1799 if(positionals_at_end_) {
1803 if(parent_ !=
nullptr && name_.empty()) {
1807 _move_to_missing(detail::Classifier::NONE, positional);
1809 if(prefix_command_) {
1810 while(!args.empty()) {
1811 _move_to_missing(detail::Classifier::NONE, args.back());
1819CLI11_NODISCARD CLI11_INLINE
App *
1820App::_find_subcommand(
const std::string &subc_name,
bool ignore_disabled,
bool ignore_used)
const noexcept {
1821 for(
const App_p &com : subcommands_) {
1822 if(com->disabled_ && ignore_disabled)
1824 if(com->get_name().empty()) {
1825 auto *subc = com->
_find_subcommand(subc_name, ignore_disabled, ignore_used);
1826 if(subc !=
nullptr) {
1830 if(com->check_name(subc_name)) {
1831 if((!*com) || !ignore_used)
1838CLI11_INLINE
bool App::_parse_subcommand(std::vector<std::string> &args) {
1839 if(_count_remaining_positionals(
true) > 0) {
1840 _parse_positional(args,
false);
1843 auto *com = _find_subcommand(args.back(),
true,
true);
1844 if(com ==
nullptr) {
1846 auto dotloc = args.back().find_first_of(
'.');
1847 if(dotloc != std::string::npos) {
1848 com = _find_subcommand(args.back().substr(0, dotloc),
true,
true);
1849 if(com !=
nullptr) {
1850 args.back() = args.back().substr(dotloc + 1);
1851 args.push_back(com->get_display_name());
1855 if(com !=
nullptr) {
1858 parsed_subcommands_.push_back(com);
1861 auto *parent_app = com->parent_;
1862 while(parent_app !=
this) {
1863 parent_app->_trigger_pre_parse(args.size());
1865 parent_app->parsed_subcommands_.push_back(com);
1867 parent_app = parent_app->parent_;
1872 if(parent_ ==
nullptr)
1873 throw HorribleError(
"Subcommand " + args.back() +
" missing");
1878App::_parse_arg(std::vector<std::string> &args, detail::Classifier current_type,
bool local_processing_only) {
1880 std::string current = args.back();
1882 std::string arg_name;
1886 switch(current_type) {
1887 case detail::Classifier::LONG:
1888 if(!detail::split_long(current, arg_name, value))
1889 throw HorribleError(
"Long parsed but missing (you should not see this):" + args.back());
1891 case detail::Classifier::SHORT:
1892 if(!detail::split_short(current, arg_name, rest))
1893 throw HorribleError(
"Short parsed but missing! You should not see this");
1895 case detail::Classifier::WINDOWS_STYLE:
1896 if(!detail::split_windows_style(current, arg_name, value))
1897 throw HorribleError(
"windows option parsed but missing! You should not see this");
1899 case detail::Classifier::SUBCOMMAND:
1900 case detail::Classifier::SUBCOMMAND_TERMINATOR:
1901 case detail::Classifier::POSITIONAL_MARK:
1902 case detail::Classifier::NONE:
1904 throw HorribleError(
"parsing got called with invalid option! You should not see this");
1907 auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [arg_name, current_type](
const Option_p &opt) {
1908 if(current_type == detail::Classifier::LONG)
1909 return opt->check_lname(arg_name);
1910 if(current_type == detail::Classifier::SHORT)
1911 return opt->check_sname(arg_name);
1913 return opt->check_lname(arg_name) || opt->check_sname(arg_name);
1917 while(op_ptr == std::end(options_)) {
1919 for(
auto &subc : subcommands_) {
1920 if(subc->name_.empty() && !subc->disabled_) {
1921 if(subc->_parse_arg(args, current_type, local_processing_only)) {
1922 if(!subc->pre_parse_called_) {
1923 subc->_trigger_pre_parse(args.size());
1929 if(allow_non_standard_options_ && current_type == detail::Classifier::SHORT && current.size() > 2) {
1930 std::string narg_name;
1932 detail::split_long(std::string{
'-'} + current, narg_name, nvalue);
1933 op_ptr = std::find_if(std::begin(options_), std::end(options_), [narg_name](
const Option_p &opt) {
1934 return opt->check_sname(narg_name);
1936 if(op_ptr != std::end(options_)) {
1937 arg_name = narg_name;
1945 if(parent_ !=
nullptr && name_.empty()) {
1950 auto dotloc = arg_name.find_first_of(
'.', 1);
1951 if(dotloc != std::string::npos) {
1953 auto *sub = _find_subcommand(arg_name.substr(0, dotloc),
true,
false);
1954 if(sub !=
nullptr) {
1955 std::string v = args.back();
1957 arg_name = arg_name.substr(dotloc + 1);
1958 if(arg_name.size() > 1) {
1959 args.push_back(std::string(
"--") + v.substr(dotloc + 3));
1960 current_type = detail::Classifier::LONG;
1962 auto nval = v.substr(dotloc + 2);
1964 if(nval.size() > 2) {
1966 args.push_back(nval.substr(3));
1969 args.push_back(nval);
1970 current_type = detail::Classifier::SHORT;
1972 auto val = sub->_parse_arg(args, current_type,
true);
1975 parsed_subcommands_.push_back(sub);
1979 _trigger_pre_parse(args.size());
1981 if(sub->parse_complete_callback_) {
1982 sub->_process_env();
1983 sub->_process_callbacks();
1984 sub->_process_help_flags();
1985 sub->_process_requirements();
1986 sub->run_callback(
false,
true);
1994 if(local_processing_only) {
1998 if(parent_ !=
nullptr && fallthrough_)
1999 return _get_fallthrough_parent()->_parse_arg(args, current_type,
false);
2003 _move_to_missing(current_type, current);
2010 Option_p &op = *op_ptr;
2012 if(op->get_inject_separator()) {
2013 if(!op->results().empty() && !op->results().back().empty()) {
2014 op->add_result(std::string{});
2017 if(op->get_trigger_on_parse() && op->current_option_state_ == Option::option_state::callback_run) {
2020 int min_num = (std::min)(op->get_type_size_min(), op->get_items_expected_min());
2021 int max_num = op->get_items_expected_max();
2024 if(max_num >= detail::expected_max_vector_size / 16 && !op->get_allow_extra_args()) {
2025 auto tmax = op->get_type_size_max();
2026 max_num = detail::checked_multiply(tmax, op->get_expected_min()) ? tmax : detail::expected_max_vector_size;
2030 int result_count = 0;
2033 auto res = op->get_flag_value(arg_name, value);
2034 op->add_result(res);
2035 parse_order_.push_back(op.get());
2036 }
else if(!value.empty()) {
2037 op->add_result(value, result_count);
2038 parse_order_.push_back(op.get());
2039 collected += result_count;
2041 }
else if(!rest.empty()) {
2042 op->add_result(rest, result_count);
2043 parse_order_.push_back(op.get());
2045 collected += result_count;
2049 while(min_num > collected && !args.empty()) {
2050 std::string current_ = args.back();
2052 op->add_result(current_, result_count);
2053 parse_order_.push_back(op.get());
2054 collected += result_count;
2057 if(min_num > collected) {
2058 throw ArgumentMismatch::TypedAtLeast(op->get_name(), min_num, op->get_type_name());
2062 if(max_num > collected || op->get_allow_extra_args()) {
2063 auto remreqpos = _count_remaining_positionals(
true);
2065 while((collected < max_num || op->get_allow_extra_args()) && !args.empty() &&
2066 _recognize(args.back(),
false) == detail::Classifier::NONE) {
2068 if(remreqpos >= args.size()) {
2071 if(validate_optional_arguments_) {
2072 std::string arg = args.back();
2073 arg = op->_validate(arg, 0);
2078 op->add_result(args.back(), result_count);
2079 parse_order_.push_back(op.get());
2081 collected += result_count;
2085 if(!args.empty() && _recognize(args.back()) == detail::Classifier::POSITIONAL_MARK)
2088 if(min_num == 0 && max_num > 0 && collected == 0) {
2089 auto res = op->get_flag_value(arg_name, std::string{});
2090 op->add_result(res);
2091 parse_order_.push_back(op.get());
2095 if(min_num > 0 && (collected % op->get_type_size_max()) != 0) {
2096 if(op->get_type_size_max() != op->get_type_size_min()) {
2097 op->add_result(std::string{});
2099 throw ArgumentMismatch::PartialType(op->get_name(), op->get_type_size_min(), op->get_type_name());
2102 if(op->get_trigger_on_parse()) {
2107 args.push_back(rest);
2112CLI11_INLINE
void App::_trigger_pre_parse(std::size_t remaining_args) {
2113 if(!pre_parse_called_) {
2114 pre_parse_called_ =
true;
2115 if(pre_parse_callback_) {
2116 pre_parse_callback_(remaining_args);
2118 }
else if(immediate_callback_) {
2119 if(!name_.empty()) {
2120 auto pcnt = parsed_;
2121 missing_t extras = std::move(missing_);
2124 pre_parse_called_ =
true;
2125 missing_ = std::move(extras);
2130CLI11_INLINE
App *App::_get_fallthrough_parent() {
2131 if(parent_ ==
nullptr) {
2134 auto *fallthrough_parent = parent_;
2135 while((fallthrough_parent->parent_ !=
nullptr) && (fallthrough_parent->get_name().empty())) {
2136 fallthrough_parent = fallthrough_parent->
parent_;
2138 return fallthrough_parent;
2141CLI11_NODISCARD CLI11_INLINE
const std::string &App::_compare_subcommand_names(
const App &subcom,
2142 const App &base)
const {
2143 static const std::string estring;
2144 if(subcom.disabled_) {
2147 for(
const auto &subc : base.subcommands_) {
2148 if(subc.get() != &subcom) {
2149 if(subc->disabled_) {
2152 if(!subcom.get_name().empty()) {
2153 if(subc->check_name(subcom.get_name())) {
2154 return subcom.get_name();
2157 if(!subc->get_name().empty()) {
2158 if(subcom.check_name(subc->get_name())) {
2159 return subc->get_name();
2162 for(
const auto &les : subcom.aliases_) {
2163 if(subc->check_name(les)) {
2168 for(
const auto &les : subc->aliases_) {
2169 if(subcom.check_name(les)) {
2174 if(subc->get_name().empty()) {
2175 const auto &cmpres = _compare_subcommand_names(subcom, *subc);
2176 if(!cmpres.empty()) {
2181 if(subcom.get_name().empty()) {
2182 const auto &cmpres = _compare_subcommand_names(*subc, subcom);
2183 if(!cmpres.empty()) {
2192CLI11_INLINE
void App::_move_to_missing(detail::Classifier val_type,
const std::string &val) {
2193 if(allow_extras_ || subcommands_.empty()) {
2194 missing_.emplace_back(val_type, val);
2198 for(
auto &subc : subcommands_) {
2199 if(subc->name_.empty() && subc->allow_extras_) {
2200 subc->missing_.emplace_back(val_type, val);
2205 missing_.emplace_back(val_type, val);
2209 if(opt ==
nullptr) {
2214 for(
auto &subc : subcommands_) {
2215 if(app == subc.get()) {
2223 if((help_ptr_ == opt) || (help_all_ptr_ == opt))
2226 if(config_ptr_ == opt)
2230 std::find_if(std::begin(options_), std::end(options_), [opt](
const Option_p &v) {
return v.get() == opt; });
2231 if(iterator != std::end(options_)) {
2232 const auto &opt_p = *iterator;
2233 if(std::find_if(std::begin(app->options_), std::end(app->options_), [&opt_p](
const Option_p &v) {
2234 return (*v == *opt_p);
2235 }) == std::end(app->options_)) {
2237 app->options_.push_back(std::move(*iterator));
2238 options_.erase(iterator);
2247CLI11_INLINE
void TriggerOn(
App *trigger_app,
App *app_to_enable) {
2248 app_to_enable->enabled_by_default(
false);
2249 app_to_enable->disabled_by_default();
2250 trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(
false); });
2253CLI11_INLINE
void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable) {
2254 for(
auto &app : apps_to_enable) {
2255 app->enabled_by_default(
false);
2256 app->disabled_by_default();
2259 trigger_app->preparse_callback([apps_to_enable](std::size_t) {
2260 for(
const auto &app : apps_to_enable) {
2261 app->disabled(
false);
2266CLI11_INLINE
void TriggerOff(App *trigger_app, App *app_to_enable) {
2267 app_to_enable->disabled_by_default(
false);
2268 app_to_enable->enabled_by_default();
2269 trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(); });
2272CLI11_INLINE
void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable) {
2273 for(
auto &app : apps_to_enable) {
2274 app->disabled_by_default(
false);
2275 app->enabled_by_default();
2278 trigger_app->preparse_callback([apps_to_enable](std::size_t) {
2279 for(
const auto &app : apps_to_enable) {
2285CLI11_INLINE
void deprecate_option(Option *opt,
const std::string &replacement) {
2286 Validator deprecate_warning{[opt, replacement](std::string &) {
2287 std::cout << opt->get_name() <<
" is deprecated please use '" << replacement
2289 return std::string();
2292 deprecate_warning.application_index(0);
2293 opt->check(deprecate_warning);
2294 if(!replacement.empty()) {
2295 opt->description(opt->get_description() +
" DEPRECATED: please use '" + replacement +
"' instead");
2299CLI11_INLINE
void retire_option(App *app, Option *opt) {
2301 auto *option_copy = temp.add_option(opt->get_name(
false,
true))
2302 ->type_size(opt->get_type_size_min(), opt->get_type_size_max())
2303 ->expected(opt->get_expected_min(), opt->get_expected_max())
2304 ->allow_extra_args(opt->get_allow_extra_args());
2306 app->remove_option(opt);
2307 auto *opt2 = app->add_option(option_copy->get_name(
false,
true),
"option has been retired and has no effect");
2308 opt2->type_name(
"RETIRED")
2309 ->default_str(
"RETIRED")
2310 ->type_size(option_copy->get_type_size_min(), option_copy->get_type_size_max())
2311 ->expected(option_copy->get_expected_min(), option_copy->get_expected_max())
2312 ->allow_extra_args(option_copy->get_allow_extra_args());
2316 Validator retired_warning{[opt2](std::string &) {
2317 std::cout <<
"WARNING " << opt2->get_name() <<
" is retired and has no effect\n";
2318 return std::string();
2322 retired_warning.application_index(0);
2323 opt2->check(retired_warning);
2326CLI11_INLINE
void retire_option(App &app, Option *opt) { retire_option(&app, opt); }
2328CLI11_INLINE
void retire_option(App *app,
const std::string &option_name) {
2330 auto *opt = app->get_option_no_throw(option_name);
2331 if(opt !=
nullptr) {
2332 retire_option(app, opt);
2335 auto *opt2 = app->add_option(option_name,
"option has been retired and has no effect")
2336 ->type_name(
"RETIRED")
2338 ->default_str(
"RETIRED");
2341 Validator retired_warning{[opt2](std::string &) {
2342 std::cout <<
"WARNING " << opt2->get_name() <<
" is retired and has no effect\n";
2343 return std::string();
2347 retired_warning.application_index(0);
2348 opt2->check(retired_warning);
2351CLI11_INLINE
void retire_option(App &app,
const std::string &option_name) { retire_option(&app, option_name); }
2353namespace FailureMessage {
2355CLI11_INLINE std::string simple(
const App *app,
const Error &e) {
2356 std::string header = std::string(e.what()) +
"\n";
2357 std::vector<std::string> names;
2360 if(app->get_help_ptr() !=
nullptr)
2361 names.push_back(app->get_help_ptr()->get_name());
2363 if(app->get_help_all_ptr() !=
nullptr)
2364 names.push_back(app->get_help_all_ptr()->get_name());
2368 header +=
"Run with " + detail::join(names,
" or ") +
" for more information.\n";
2373CLI11_INLINE std::string help(
const App *app,
const Error &e) {
2374 std::string header = std::string(
"ERROR: ") + e.get_name() +
": " + e.what() +
"\n";
2375 header += app->help();
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:844
bool subcommand_fallthrough_
Allow subcommands to fallthrough, so that parent commands can trigger other subcommands after subcomm...
Definition App.hpp:232
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:674
CLI11_NODISCARD std::string help(std::string prev="", AppFormatMode mode=AppFormatMode::Normal) const
Definition App_inl.hpp:770
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:969
CLI11_NODISCARD 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_inl.hpp:1082
App * immediate_callback(bool immediate=true)
Set the subcommand callback to be executed immediately on subcommand completion.
Definition App_inl.hpp:120
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:251
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:2130
bool allow_non_standard_options_
indicator that the subcommand should allow non-standard option arguments, such as -single_dash_flag
Definition App.hpp:264
Option * config_ptr_
Pointer to the config option.
Definition App.hpp:295
std::size_t require_option_min_
Minimum required options (not inheritable!)
Definition App.hpp:276
App * ignore_underscore(bool value=true)
Ignore underscore. Subcommands inherit value.
Definition App_inl.hpp:146
std::size_t require_subcommand_max_
Max number of subcommands allowed (parsing stops after this number). 0 is unlimited INHERITABLE.
Definition App.hpp:273
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:937
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:963
std::uint32_t parsed_
Counts the number of times this command/subcommand was parsed.
Definition App.hpp:267
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:526
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:1219
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition App_inl.hpp:1241
CLI11_NODISCARD std::size_t count_all() const
Definition App_inl.hpp:535
bool disabled_
If set to true the subcommand is disabled and cannot be used, ignored for main app.
Definition App.hpp:123
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:284
bool remove_needs(Option *opt)
Removes an option from the needs list of this subcommand.
Definition App_inl.hpp:752
Option * get_help_ptr()
Get a pointer to the help flag.
Definition App.hpp:1178
void _configure()
Definition App_inl.hpp:1018
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:360
void parse(int argc, const char *const *argv)
Definition App_inl.hpp:564
void _process_config_file()
Read and process a configuration file (main app only)
Definition App_inl.hpp:1137
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition App.hpp:162
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:896
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:1184
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:456
App * parent_
A pointer to the parent if this is a subcommand.
Definition App.hpp:282
std::set< Option * > exclude_options_
Definition App.hpp:204
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:501
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:868
Option * add_flag(std::string flag_name)
Add a flag with no description or variable assignment.
Definition App.hpp:639
void _validate() const
Definition App_inl.hpp:983
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
bool ignore_underscore_
If true, the program should ignore underscores INHERITABLE.
Definition App.hpp:225
missing_t missing_
Definition App.hpp:191
void run_callback(bool final_mode=false, bool suppress_final_callback=false)
Internal function to run (App) callback, bottom up.
Definition App_inl.hpp:1038
std::size_t require_subcommand_min_
Minimum required subcommands (not inheritable!)
Definition App.hpp:270
void _process_env()
Get envname options if not yet passed. Runs on all subcommands.
Definition App_inl.hpp:1173
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:1472
CLI11_NODISCARD std::string get_display_name(bool with_aliases=false) const
Get a display name for an app.
Definition App_inl.hpp:880
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:2141
void clear()
Reset the parsed data.
Definition App_inl.hpp:549
App * get_subcommand(const App *subcom) const
Definition App_inl.hpp:472
CLI11_NODISCARD std::string version() const
Displays a version string.
Definition App_inl.hpp:784
CLI11_NODISCARD App * get_subcommand_no_throw(std::string subcom) const noexcept
Definition App_inl.hpp:488
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:257
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:404
App(std::string app_description, std::string app_name, App *parent)
Special private constructor for subcommand.
Definition App_inl.hpp:29
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:425
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:343
bool immediate_callback_
Definition App.hpp:130
App * name(std::string app_name="")
Set a name for the app (empty will use parser to set the name)
Definition App_inl.hpp:85
CLI11_NODISCARD 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_inl.hpp:1065
bool configurable_
if set to true the subcommand can be triggered via configuration files INHERITABLE
Definition App.hpp:251
CLI11_NODISCARD std::vector< std::string > get_groups() const
Get the groups available directly from this option (in order)
Definition App_inl.hpp:924
void _parse_config(const std::vector< ConfigItem > &args)
Definition App_inl.hpp:1483
std::size_t require_option_max_
Max number of options allowed. 0 is unlimited (not inheritable)
Definition App.hpp:279
std::vector< std::string > aliases_
Alias names for the subcommand.
Definition App.hpp:288
std::set< App * > exclude_subcommands_
this is a list of subcommands that are exclusionary to this one
Definition App.hpp:200
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:1119
std::string group_
The group membership INHERITABLE.
Definition App.hpp:285
App * alias(std::string app_name)
Set an alias for the app.
Definition App_inl.hpp:102
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:1195
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:375
App * ignore_case(bool value=true)
Ignore case. Subcommands inherit value.
Definition App_inl.hpp:132
bool remove_excludes(Option *opt)
Removes an option from the excludes list of this subcommand.
Definition App_inl.hpp:732
CLI11_NODISCARD std::vector< App * > get_subcommands() const
Definition App.hpp:919
bool fallthrough_
Definition App.hpp:229
std::set< Option * > need_options_
Definition App.hpp:212
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:802
std::set< App * > need_subcommands_
Definition App.hpp:208
bool prefix_command_
If true, cease processing 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:160
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition App_inl.hpp:1428
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition App.hpp:254
startup_mode default_startup
Definition App.hpp:248
bool allow_extras_
If true, allow extra arguments (ie, don't throw an error). INHERITABLE.
Definition App.hpp:107
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:63
CLI11_NODISCARD App * _find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept
Definition App_inl.hpp:1820
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1205
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:267
bool allow_windows_style_options_
Allow '/' for options for Windows like options. Defaults to true on Windows, false otherwise....
Definition App.hpp:235
std::shared_ptr< Config > config_formatter_
This is the formatter for help printing. Default provided. INHERITABLE (same pointer)
Definition App.hpp:298
Usually something like –help-all on command line.
Definition Error.hpp:178
-h or –help on command line
Definition Error.hpp:172
-v or –version on command line
Definition Error.hpp:185
All errors derive from this one.
Definition Error.hpp:73
Thrown when an excludes option is present.
Definition Error.hpp:301
Thrown when parsing an INI file and it is missing.
Definition Error.hpp:198
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:96
Thrown when validation fails before parsing.
Definition Error.hpp:334
Thrown when an option already exists.
Definition Error.hpp:144
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:118
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:180
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition Option_inl.hpp:24
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:99
Definition Option.hpp:231
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:36
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:587
void run_callback()
Process the callback.
Definition Option_inl.hpp:286
CLI11_NODISCARD 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_inl.hpp:233
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:340
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:671
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:361
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:220
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:596
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, sname, pname, envname.
Definition Option.hpp:555
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:370
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition Option.hpp:757
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:255
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:429
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:708
Thrown when counting a non-existent option.
Definition Error.hpp:351
Anything that can error in Parse.
Definition Error.hpp:159
Thrown when a required option is missing.
Definition Error.hpp:228
Thrown when a requires option is missing.
Definition Error.hpp:294
Holds values to load into Options.
Definition ConfigFwd.hpp:29