CLI11
C++11 Command Line Interface Parser
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
App_inl.hpp
1// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
2// under NSF AWARD 1414736 and by the respective contributors.
3// All rights reserved.
4//
5// SPDX-License-Identifier: BSD-3-Clause
6
7#pragma once
8
9// IWYU pragma: private, include "CLI/CLI.hpp"
10
11// This include is only needed for IDEs to discover symbols
12#include "../App.hpp"
13
14#include "../Argv.hpp"
15#include "../Encoding.hpp"
16
17// [CLI11:public_includes:set]
18#include <algorithm>
19#include <iostream>
20#include <memory>
21#include <string>
22#include <utility>
23#include <vector>
24// [CLI11:public_includes:end]
25
26namespace CLI {
27// [CLI11:app_inl_hpp:verbatim]
28
62
63CLI11_NODISCARD CLI11_INLINE char **App::ensure_utf8(char **argv) {
64#ifdef _WIN32
65 (void)argv;
66
67 normalized_argv_ = detail::compute_win32_argv();
68
69 if(!normalized_argv_view_.empty()) {
70 normalized_argv_view_.clear();
71 }
72
73 normalized_argv_view_.reserve(normalized_argv_.size());
74 for(auto &arg : normalized_argv_) {
75 // using const_cast is well-defined, string is known to not be const.
76 normalized_argv_view_.push_back(const_cast<char *>(arg.data()));
77 }
78
79 return normalized_argv_view_.data();
80#else
81 return argv;
82#endif
83}
84
85CLI11_INLINE App *App::name(std::string app_name) {
86
87 if(parent_ != nullptr) {
88 std::string oname = name_;
89 name_ = app_name;
90 const auto &res = _compare_subcommand_names(*this, *_get_fallthrough_parent());
91 if(!res.empty()) {
92 name_ = oname;
93 throw(OptionAlreadyAdded(app_name + " conflicts with existing subcommand names"));
94 }
95 } else {
96 name_ = app_name;
97 }
98 has_automatic_name_ = false;
99 return this;
100}
101
102CLI11_INLINE App *App::alias(std::string app_name) {
103 if(app_name.empty() || !detail::valid_alias_name_string(app_name)) {
104 throw IncorrectConstruction("Aliases may not be empty or contain newlines or null characters");
105 }
106 if(parent_ != nullptr) {
107 aliases_.push_back(app_name);
108 const auto &res = _compare_subcommand_names(*this, *_get_fallthrough_parent());
109 if(!res.empty()) {
110 aliases_.pop_back();
111 throw(OptionAlreadyAdded("alias already matches an existing subcommand: " + app_name));
112 }
113 } else {
114 aliases_.push_back(app_name);
115 }
116
117 return this;
118}
119
120CLI11_INLINE App *App::immediate_callback(bool immediate) {
121 immediate_callback_ = immediate;
125 }
128 }
129 return this;
130}
131
132CLI11_INLINE App *App::ignore_case(bool value) {
133 if(value && !ignore_case_) {
134 ignore_case_ = true;
135 auto *p = (parent_ != nullptr) ? _get_fallthrough_parent() : this;
136 const auto &match = _compare_subcommand_names(*this, *p);
137 if(!match.empty()) {
138 ignore_case_ = false; // we are throwing so need to be exception invariant
139 throw OptionAlreadyAdded("ignore case would cause subcommand name conflicts: " + match);
140 }
141 }
142 ignore_case_ = value;
143 return this;
144}
145
146CLI11_INLINE App *App::ignore_underscore(bool value) {
147 if(value && !ignore_underscore_) {
148 ignore_underscore_ = true;
149 auto *p = (parent_ != nullptr) ? _get_fallthrough_parent() : this;
150 const auto &match = _compare_subcommand_names(*this, *p);
151 if(!match.empty()) {
152 ignore_underscore_ = false;
153 throw OptionAlreadyAdded("ignore underscore would cause subcommand name conflicts: " + match);
154 }
155 }
156 ignore_underscore_ = value;
157 return this;
158}
159
160CLI11_INLINE Option *App::add_option(std::string option_name,
161 callback_t option_callback,
162 std::string option_description,
163 bool defaulted,
164 std::function<std::string()> func) {
165 Option myopt{option_name, option_description, option_callback, this, allow_non_standard_options_};
166
167 if(std::find_if(std::begin(options_), std::end(options_), [&myopt](const Option_p &v) { return *v == myopt; }) ==
168 std::end(options_)) {
169 if(myopt.lnames_.empty() && myopt.snames_.empty()) {
170 // if the option is positional only there is additional potential for ambiguities in config files and needs
171 // to be checked
172 std::string test_name = "--" + myopt.get_single_name();
173 if(test_name.size() == 3) {
174 test_name.erase(0, 1);
175 }
176
177 auto *op = get_option_no_throw(test_name);
178 if(op != nullptr && op->get_configurable()) {
179 throw(OptionAlreadyAdded("added option positional name matches existing option: " + test_name));
180 }
181 } else if(parent_ != nullptr) {
182 for(auto &ln : myopt.lnames_) {
183 auto *op = parent_->get_option_no_throw(ln);
184 if(op != nullptr && op->get_configurable()) {
185 throw(OptionAlreadyAdded("added option matches existing positional option: " + ln));
186 }
187 }
188 for(auto &sn : myopt.snames_) {
189 auto *op = parent_->get_option_no_throw(sn);
190 if(op != nullptr && op->get_configurable()) {
191 throw(OptionAlreadyAdded("added option matches existing positional option: " + sn));
192 }
193 }
194 }
195 if(allow_non_standard_options_ && !myopt.snames_.empty()) {
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());
201 auto *op = get_option_no_throw(test_name);
202 if(op != nullptr) {
203 throw(OptionAlreadyAdded("added option interferes with existing short option: " + sname));
204 }
205 }
206 }
207 for(auto &opt : options_) {
208 for(const auto &osn : opt->snames_) {
209 if(osn.size() > 1) {
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: " +
214 osn));
215 }
216 }
217 }
218 }
219 }
220 options_.emplace_back();
221 Option_p &option = options_.back();
222 option.reset(new Option(option_name, option_description, option_callback, this, allow_non_standard_options_));
223
224 // Set the default string capture function
225 option->default_function(func);
226
227 // For compatibility with CLI11 1.7 and before, capture the default string here
228 if(defaulted)
229 option->capture_default_str();
230
231 // Transfer defaults to the new option
232 option_defaults_.copy_to(option.get());
233
234 // Don't bother to capture if we already did
235 if(!defaulted && option->get_always_capture_default())
236 option->capture_default_str();
237
238 return option.get();
239 }
240 // we know something matches now find what it is so we can produce more error information
241 for(auto &opt : options_) {
242 const auto &matchname = opt->matching_name(myopt);
243 if(!matchname.empty()) {
244 throw(OptionAlreadyAdded("added option matched existing option name: " + matchname));
245 }
246 }
247 // this line should not be reached the above loop should trigger the throw
248 throw(OptionAlreadyAdded("added option matched existing option name")); // LCOV_EXCL_LINE
249}
250
251CLI11_INLINE Option *App::set_help_flag(std::string flag_name, const std::string &help_description) {
252 // take flag_description by const reference otherwise add_flag tries to assign to help_description
253 if(help_ptr_ != nullptr) {
255 help_ptr_ = nullptr;
256 }
257
258 // Empty name will simply remove the help flag
259 if(!flag_name.empty()) {
260 help_ptr_ = add_flag(flag_name, help_description);
261 help_ptr_->configurable(false);
262 }
263
264 return help_ptr_;
265}
266
267CLI11_INLINE Option *App::set_help_all_flag(std::string help_name, const std::string &help_description) {
268 // take flag_description by const reference otherwise add_flag tries to assign to flag_description
269 if(help_all_ptr_ != nullptr) {
271 help_all_ptr_ = nullptr;
272 }
273
274 // Empty name will simply remove the help all flag
275 if(!help_name.empty()) {
276 help_all_ptr_ = add_flag(help_name, help_description);
278 }
279
280 return help_all_ptr_;
281}
282
283CLI11_INLINE Option *
284App::set_version_flag(std::string flag_name, const std::string &versionString, const std::string &version_help) {
285 // take flag_description by const reference otherwise add_flag tries to assign to version_description
286 if(version_ptr_ != nullptr) {
288 version_ptr_ = nullptr;
289 }
290
291 // Empty name will simply remove the version flag
292 if(!flag_name.empty()) {
294 flag_name, [versionString]() { throw(CLI::CallForVersion(versionString, 0)); }, version_help);
296 }
297
298 return version_ptr_;
299}
300
301CLI11_INLINE Option *
302App::set_version_flag(std::string flag_name, std::function<std::string()> vfunc, const std::string &version_help) {
303 if(version_ptr_ != nullptr) {
305 version_ptr_ = nullptr;
306 }
307
308 // Empty name will simply remove the version flag
309 if(!flag_name.empty()) {
311 add_flag_callback(flag_name, [vfunc]() { throw(CLI::CallForVersion(vfunc(), 0)); }, version_help);
313 }
314
315 return version_ptr_;
316}
317
318CLI11_INLINE Option *App::_add_flag_internal(std::string flag_name, CLI::callback_t fun, std::string flag_description) {
319 Option *opt = nullptr;
320 if(detail::has_default_flag_values(flag_name)) {
321 // check for default values and if it has them
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);
328 } else {
329 opt = add_option(std::move(flag_name), std::move(fun), std::move(flag_description), false);
330 }
331 // flags cannot have positional values
332 if(opt->get_positional()) {
333 auto pos_name = opt->get_name(true);
334 remove_option(opt);
335 throw IncorrectConstruction::PositionalFlag(pos_name);
336 }
337 opt->multi_option_policy(MultiOptionPolicy::TakeLast);
338 opt->expected(0);
339 opt->required(false);
340 return opt;
341}
342
343CLI11_INLINE Option *App::add_flag_callback(std::string flag_name,
344 std::function<void(void)> function,
345 std::string flag_description) {
346
347 CLI::callback_t fun = [function](const CLI::results_t &res) {
348 using CLI::detail::lexical_cast;
349 bool trigger{false};
350 auto result = lexical_cast(res[0], trigger);
351 if(result && trigger) {
352 function();
353 }
354 return result;
355 };
356 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description));
357}
358
359CLI11_INLINE Option *
360App::add_flag_function(std::string flag_name,
361 std::function<void(std::int64_t)> function,
362 std::string flag_description) {
363
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);
369 return true;
370 };
371 return _add_flag_internal(flag_name, std::move(fun), std::move(flag_description))
372 ->multi_option_policy(MultiOptionPolicy::Sum);
373}
374
375CLI11_INLINE Option *App::set_config(std::string option_name,
376 std::string default_filename,
377 const std::string &help_message,
378 bool config_required) {
379
380 // Remove existing config if present
381 if(config_ptr_ != nullptr) {
383 config_ptr_ = nullptr; // need to remove the config_ptr completely
384 }
385
386 // Only add config if option passed
387 if(!option_name.empty()) {
388 config_ptr_ = add_option(option_name, help_message);
389 if(config_required) {
391 }
392 if(!default_filename.empty()) {
393 config_ptr_->default_str(std::move(default_filename));
395 }
397 // set the option to take the last value and reverse given by default
398 config_ptr_->multi_option_policy(MultiOptionPolicy::Reverse);
399 }
400
401 return config_ptr_;
402}
403
404CLI11_INLINE bool App::remove_option(Option *opt) {
405 // Make sure no links exist
406 for(Option_p &op : options_) {
407 op->remove_needs(opt);
408 op->remove_excludes(opt);
409 }
410
411 if(help_ptr_ == opt)
412 help_ptr_ = nullptr;
413 if(help_all_ptr_ == opt)
414 help_all_ptr_ = nullptr;
415
416 auto iterator =
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_)) {
419 options_.erase(iterator);
420 return true;
421 }
422 return false;
423}
424
425CLI11_INLINE App *App::add_subcommand(std::string subcommand_name, std::string subcommand_description) {
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");
430 }
431 for(auto c : subcommand_name) {
432 if(!detail::valid_later_char(c)) {
433 throw IncorrectConstruction(std::string("Subcommand name contains invalid character ('") + c +
434 "'), all characters are allowed except"
435 "'=',':','{','}', ' ', and control characters");
436 }
437 }
438 }
439 CLI::App_p subcom = std::shared_ptr<App>(new App(std::move(subcommand_description), subcommand_name, this));
440 return add_subcommand(std::move(subcom));
441}
442
443CLI11_INLINE App *App::add_subcommand(CLI::App_p subcom) {
444 if(!subcom)
445 throw IncorrectConstruction("passed App is not valid");
446 auto *ckapp = (name_.empty() && parent_ != nullptr) ? _get_fallthrough_parent() : this;
447 const auto &mstrg = _compare_subcommand_names(*subcom, *ckapp);
448 if(!mstrg.empty()) {
449 throw(OptionAlreadyAdded("subcommand name or alias matches existing subcommand: " + mstrg));
450 }
451 subcom->parent_ = this;
452 subcommands_.push_back(std::move(subcom));
453 return subcommands_.back().get();
454}
455
456CLI11_INLINE bool App::remove_subcommand(App *subcom) {
457 // Make sure no links exist
458 for(App_p &sub : subcommands_) {
459 sub->remove_excludes(subcom);
460 sub->remove_needs(subcom);
461 }
462
463 auto iterator = std::find_if(
464 std::begin(subcommands_), std::end(subcommands_), [subcom](const App_p &v) { return v.get() == subcom; });
465 if(iterator != std::end(subcommands_)) {
466 subcommands_.erase(iterator);
467 return true;
468 }
469 return false;
470}
471
472CLI11_INLINE App *App::get_subcommand(const App *subcom) const {
473 if(subcom == nullptr)
474 throw OptionNotFound("nullptr passed");
475 for(const App_p &subcomptr : subcommands_)
476 if(subcomptr.get() == subcom)
477 return subcomptr.get();
478 throw OptionNotFound(subcom->get_name());
479}
480
481CLI11_NODISCARD CLI11_INLINE App *App::get_subcommand(std::string subcom) const {
482 auto *subc = _find_subcommand(subcom, false, false);
483 if(subc == nullptr)
484 throw OptionNotFound(subcom);
485 return subc;
486}
487
488CLI11_NODISCARD CLI11_INLINE App *App::get_subcommand_no_throw(std::string subcom) const noexcept {
489 return _find_subcommand(subcom, false, false);
490}
491
492CLI11_NODISCARD CLI11_INLINE App *App::get_subcommand(int index) const {
493 if(index >= 0) {
494 auto uindex = static_cast<unsigned>(index);
495 if(uindex < subcommands_.size())
496 return subcommands_[uindex].get();
497 }
498 throw OptionNotFound(std::to_string(index));
499}
500
501CLI11_INLINE CLI::App_p App::get_subcommand_ptr(App *subcom) const {
502 if(subcom == nullptr)
503 throw OptionNotFound("nullptr passed");
504 for(const App_p &subcomptr : subcommands_)
505 if(subcomptr.get() == subcom)
506 return subcomptr;
507 throw OptionNotFound(subcom->get_name());
508}
509
510CLI11_NODISCARD CLI11_INLINE CLI::App_p App::get_subcommand_ptr(std::string subcom) const {
511 for(const App_p &subcomptr : subcommands_)
512 if(subcomptr->check_name(subcom))
513 return subcomptr;
514 throw OptionNotFound(subcom);
515}
516
517CLI11_NODISCARD CLI11_INLINE CLI::App_p App::get_subcommand_ptr(int index) const {
518 if(index >= 0) {
519 auto uindex = static_cast<unsigned>(index);
520 if(uindex < subcommands_.size())
521 return subcommands_[uindex];
522 }
523 throw OptionNotFound(std::to_string(index));
524}
525
526CLI11_NODISCARD CLI11_INLINE CLI::App *App::get_option_group(std::string group_name) const {
527 for(const App_p &app : subcommands_) {
528 if(app->name_.empty() && app->group_ == group_name) {
529 return app.get();
530 }
531 }
532 throw OptionNotFound(group_name);
533}
534
535CLI11_NODISCARD CLI11_INLINE std::size_t App::count_all() const {
536 std::size_t cnt{0};
537 for(const auto &opt : options_) {
538 cnt += opt->count();
539 }
540 for(const auto &sub : subcommands_) {
541 cnt += sub->count_all();
542 }
543 if(!get_name().empty()) { // for named subcommands add the number of times the subcommand was called
544 cnt += parsed_;
545 }
546 return cnt;
547}
548
549CLI11_INLINE void App::clear() {
550
551 parsed_ = 0;
552 pre_parse_called_ = false;
553
554 missing_.clear();
555 parsed_subcommands_.clear();
556 for(const Option_p &opt : options_) {
557 opt->clear();
558 }
559 for(const App_p &subc : subcommands_) {
560 subc->clear();
561 }
562}
563
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); }
566
567namespace detail {
568
569// Do nothing or perform narrowing
570CLI11_INLINE const char *maybe_narrow(const char *str) { return str; }
571CLI11_INLINE std::string maybe_narrow(const wchar_t *str) { return narrow(str); }
572
573} // namespace detail
574
575template <class CharT> CLI11_INLINE void App::parse_char_t(int argc, const CharT *const *argv) {
576 // If the name is not set, read from command line
577 if(name_.empty() || has_automatic_name_) {
578 has_automatic_name_ = true;
579 name_ = detail::maybe_narrow(argv[0]);
580 }
581
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]));
586
587 parse(std::move(args));
588}
589
590CLI11_INLINE void App::parse(std::string commandline, bool program_name_included) {
591
592 if(program_name_included) {
593 auto nstr = detail::split_program_name(commandline);
594 if((name_.empty()) || (has_automatic_name_)) {
595 has_automatic_name_ = true;
596 name_ = nstr.first;
597 }
598 commandline = std::move(nstr.second);
599 } else {
600 detail::trim(commandline);
601 }
602 // the next section of code is to deal with quoted arguments after an '=' or ':' for windows like operations
603 if(!commandline.empty()) {
604 commandline = detail::find_and_modify(commandline, "=", detail::escape_detect);
606 commandline = detail::find_and_modify(commandline, ":", detail::escape_detect);
607 }
608
609 auto args = detail::split_up(std::move(commandline));
610 // remove all empty strings
611 args.erase(std::remove(args.begin(), args.end(), std::string{}), args.end());
612 try {
613 detail::remove_quotes(args);
614 } catch(const std::invalid_argument &arg) {
615 throw CLI::ParseError(arg.what(), CLI::ExitCodes::InvalidError);
616 }
617 std::reverse(args.begin(), args.end());
618 parse(std::move(args));
619}
620
621CLI11_INLINE void App::parse(std::wstring commandline, bool program_name_included) {
622 parse(narrow(commandline), program_name_included);
623}
624
625CLI11_INLINE void App::parse(std::vector<std::string> &args) {
626 // Clear if parsed
627 if(parsed_ > 0)
628 clear();
629
630 // parsed_ is incremented in commands/subcommands,
631 // but placed here to make sure this is cleared when
632 // running parse after an error is thrown, even by _validate or _configure.
633 parsed_ = 1;
634 _validate();
635 _configure();
636 // set the parent as nullptr as this object should be the top now
637 parent_ = nullptr;
638 parsed_ = 0;
639
640 _parse(args);
641 run_callback();
642}
643
644CLI11_INLINE void App::parse(std::vector<std::string> &&args) {
645 // Clear if parsed
646 if(parsed_ > 0)
647 clear();
648
649 // parsed_ is incremented in commands/subcommands,
650 // but placed here to make sure this is cleared when
651 // running parse after an error is thrown, even by _validate or _configure.
652 parsed_ = 1;
653 _validate();
654 _configure();
655 // set the parent as nullptr as this object should be the top now
656 parent_ = nullptr;
657 parsed_ = 0;
658
659 _parse(std::move(args));
660 run_callback();
661}
662
663CLI11_INLINE void App::parse_from_stream(std::istream &input) {
664 if(parsed_ == 0) {
665 _validate();
666 _configure();
667 // set the parent as nullptr as this object should be the top now
668 }
669
670 _parse_stream(input);
671 run_callback();
672}
673
674CLI11_INLINE int App::exit(const Error &e, std::ostream &out, std::ostream &err) const {
675
677 if(e.get_name() == "RuntimeError")
678 return e.get_exit_code();
679
680 if(e.get_name() == "CallForHelp") {
681 out << help();
682 return e.get_exit_code();
683 }
684
685 if(e.get_name() == "CallForAllHelp") {
686 out << help("", AppFormatMode::All);
687 return e.get_exit_code();
688 }
689
690 if(e.get_name() == "CallForVersion") {
691 out << e.what() << '\n';
692 return e.get_exit_code();
693 }
694
695 if(e.get_exit_code() != static_cast<int>(ExitCodes::Success)) {
697 err << failure_message_(this, e) << std::flush;
698 }
699
700 return e.get_exit_code();
701}
702
703CLI11_INLINE std::vector<const App *> App::get_subcommands(const std::function<bool(const App *)> &filter) const {
704 std::vector<const App *> subcomms(subcommands_.size());
705 std::transform(
706 std::begin(subcommands_), std::end(subcommands_), std::begin(subcomms), [](const App_p &v) { return v.get(); });
707
708 if(filter) {
709 subcomms.erase(std::remove_if(std::begin(subcomms),
710 std::end(subcomms),
711 [&filter](const App *app) { return !filter(app); }),
712 std::end(subcomms));
713 }
714
715 return subcomms;
716}
717
718CLI11_INLINE std::vector<App *> App::get_subcommands(const std::function<bool(App *)> &filter) {
719 std::vector<App *> subcomms(subcommands_.size());
720 std::transform(
721 std::begin(subcommands_), std::end(subcommands_), std::begin(subcomms), [](const App_p &v) { return v.get(); });
722
723 if(filter) {
724 subcomms.erase(
725 std::remove_if(std::begin(subcomms), std::end(subcomms), [&filter](App *app) { return !filter(app); }),
726 std::end(subcomms));
727 }
728
729 return subcomms;
730}
731
732CLI11_INLINE bool App::remove_excludes(Option *opt) {
733 auto iterator = std::find(std::begin(exclude_options_), std::end(exclude_options_), opt);
734 if(iterator == std::end(exclude_options_)) {
735 return false;
736 }
737 exclude_options_.erase(iterator);
738 return true;
739}
741CLI11_INLINE bool App::remove_excludes(App *app) {
742 auto iterator = std::find(std::begin(exclude_subcommands_), std::end(exclude_subcommands_), app);
743 if(iterator == std::end(exclude_subcommands_)) {
744 return false;
745 }
746 auto *other_app = *iterator;
747 exclude_subcommands_.erase(iterator);
748 other_app->remove_excludes(this);
749 return true;
750}
751
752CLI11_INLINE bool App::remove_needs(Option *opt) {
753 auto iterator = std::find(std::begin(need_options_), std::end(need_options_), opt);
754 if(iterator == std::end(need_options_)) {
755 return false;
756 }
757 need_options_.erase(iterator);
758 return true;
759}
760
761CLI11_INLINE bool App::remove_needs(App *app) {
762 auto iterator = std::find(std::begin(need_subcommands_), std::end(need_subcommands_), app);
763 if(iterator == std::end(need_subcommands_)) {
764 return false;
765 }
766 need_subcommands_.erase(iterator);
767 return true;
768}
769
770CLI11_NODISCARD CLI11_INLINE std::string App::help(std::string prev, AppFormatMode mode) const {
771 if(prev.empty())
772 prev = get_name();
773 else
774 prev += " " + get_name();
775
776 // Delegate to subcommand if needed
777 auto selected_subcommands = get_subcommands();
778 if(!selected_subcommands.empty()) {
779 return selected_subcommands.back()->help(prev, mode);
780 }
781 return formatter_->make_help(this, prev, mode);
782}
783
784CLI11_NODISCARD CLI11_INLINE std::string App::version() const {
785 std::string val;
786 if(version_ptr_ != nullptr) {
787 // copy the results for reuse later
788 results_t rv = version_ptr_->results();
790 version_ptr_->add_result("true");
791 try {
793 } catch(const CLI::CallForVersion &cfv) {
794 val = cfv.what();
795 }
798 }
799 return val;
800}
801
802CLI11_INLINE std::vector<const Option *> App::get_options(const std::function<bool(const Option *)> filter) const {
803 std::vector<const Option *> options(options_.size());
804 std::transform(
805 std::begin(options_), std::end(options_), std::begin(options), [](const Option_p &val) { return val.get(); });
806
807 if(filter) {
808 options.erase(std::remove_if(std::begin(options),
809 std::end(options),
810 [&filter](const Option *opt) { return !filter(opt); }),
811 std::end(options));
812 }
813 for(const auto &subcp : subcommands_) {
814 // also check down into nameless subcommands
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());
819 }
820 }
821 return options;
822}
823
824CLI11_INLINE std::vector<Option *> App::get_options(const std::function<bool(Option *)> filter) {
825 std::vector<Option *> options(options_.size());
826 std::transform(
827 std::begin(options_), std::end(options_), std::begin(options), [](const Option_p &val) { return val.get(); });
828
829 if(filter) {
830 options.erase(
831 std::remove_if(std::begin(options), std::end(options), [&filter](Option *opt) { return !filter(opt); }),
832 std::end(options));
833 }
834 for(auto &subc : subcommands_) {
835 // also check down into nameless subcommands
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());
839 }
840 }
841 return options;
842}
843
844CLI11_NODISCARD CLI11_INLINE Option *App::get_option_no_throw(std::string option_name) noexcept {
845 for(Option_p &opt : options_) {
846 if(opt->check_name(option_name)) {
847 return opt.get();
848 }
849 }
850 for(auto &subc : subcommands_) {
851 // also check down into nameless subcommands
852 if(subc->get_name().empty()) {
853 auto *opt = subc->get_option_no_throw(option_name);
854 if(opt != nullptr) {
855 return opt;
856 }
857 }
858 }
859 return nullptr;
860}
861
862CLI11_NODISCARD CLI11_INLINE const Option *App::get_option_no_throw(std::string option_name) const noexcept {
863 for(const Option_p &opt : options_) {
864 if(opt->check_name(option_name)) {
865 return opt.get();
866 }
867 }
868 for(const auto &subc : subcommands_) {
869 // also check down into nameless subcommands
870 if(subc->get_name().empty()) {
871 auto *opt = subc->get_option_no_throw(option_name);
872 if(opt != nullptr) {
873 return opt;
874 }
875 }
876 }
877 return nullptr;
878}
879
880CLI11_NODISCARD CLI11_INLINE std::string App::get_display_name(bool with_aliases) const {
881 if(name_.empty()) {
882 return std::string("[Option Group: ") + get_group() + "]";
883 }
884 if(aliases_.empty() || !with_aliases) {
885 return name_;
886 }
887 std::string dispname = name_;
888 for(const auto &lalias : aliases_) {
889 dispname.push_back(',');
890 dispname.push_back(' ');
891 dispname.append(lalias);
892 }
893 return dispname;
894}
895
896CLI11_NODISCARD CLI11_INLINE bool App::check_name(std::string name_to_check) const {
897 std::string local_name = name_;
899 local_name = detail::remove_underscore(name_);
900 name_to_check = detail::remove_underscore(name_to_check);
901 }
902 if(ignore_case_) {
903 local_name = detail::to_lower(name_);
904 name_to_check = detail::to_lower(name_to_check);
905 }
906
907 if(local_name == name_to_check) {
908 return true;
909 }
910 for(std::string les : aliases_) { // NOLINT(performance-for-range-copy)
912 les = detail::remove_underscore(les);
913 }
914 if(ignore_case_) {
915 les = detail::to_lower(les);
916 }
917 if(les == name_to_check) {
918 return true;
919 }
920 }
921 return false;
922}
923
924CLI11_NODISCARD CLI11_INLINE std::vector<std::string> App::get_groups() const {
925 std::vector<std::string> groups;
926
927 for(const Option_p &opt : options_) {
928 // Add group if it is not already in there
929 if(std::find(groups.begin(), groups.end(), opt->get_group()) == groups.end()) {
930 groups.push_back(opt->get_group());
931 }
932 }
933
934 return groups;
935}
936
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));
941 }
942 // Get from a subcommand that may allow extras
943 if(recurse) {
944 if(!allow_extras_) {
945 for(const auto &sub : subcommands_) {
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));
949 }
950 }
951 }
952 }
953 // Recurse into subcommands
954
955 for(const App *sub : parsed_subcommands_) {
956 std::vector<std::string> output = sub->remaining(recurse);
957 std::copy(std::begin(output), std::end(output), std::back_inserter(miss_list));
958 }
959 }
960 return miss_list;
961}
962
963CLI11_NODISCARD CLI11_INLINE std::vector<std::string> App::remaining_for_passthrough(bool recurse) const {
964 std::vector<std::string> miss_list = remaining(recurse);
965 std::reverse(std::begin(miss_list), std::end(miss_list));
966 return miss_list;
967}
968
969CLI11_NODISCARD CLI11_INLINE std::size_t App::remaining_size(bool recurse) const {
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;
973 }));
974
975 if(recurse) {
976 for(const App_p &sub : subcommands_) {
977 remaining_options += sub->remaining_size(recurse);
978 }
979 }
980 return remaining_options;
981}
982
983CLI11_INLINE void App::_validate() const {
984 // count the number of positional only args
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();
987 });
988 if(pcount > 1) {
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() &&
991 opt->get_required();
992 });
993 if(pcount - pcount_req > 1) {
994 throw InvalidError(name_);
995 }
996 }
997
998 std::size_t nameless_subs{0};
999 for(const App_p &app : subcommands_) {
1000 app->_validate();
1001 if(app->get_name().empty())
1002 ++nameless_subs;
1003 }
1004
1005 if(require_option_min_ > 0) {
1006 if(require_option_max_ > 0) {
1008 throw(InvalidError("Required min options greater than required max options", ExitCodes::InvalidError));
1009 }
1010 }
1011 if(require_option_min_ > (options_.size() + nameless_subs)) {
1012 throw(
1013 InvalidError("Required min options greater than number of available options", ExitCodes::InvalidError));
1014 }
1015 }
1016}
1017
1018CLI11_INLINE void App::_configure() {
1019 if(default_startup == startup_mode::enabled) {
1020 disabled_ = false;
1021 } else if(default_startup == startup_mode::disabled) {
1022 disabled_ = true;
1023 }
1024 for(const App_p &app : subcommands_) {
1025 if(app->has_automatic_name_) {
1026 app->name_.clear();
1027 }
1028 if(app->name_.empty()) {
1029 app->fallthrough_ = false; // make sure fallthrough_ is false to prevent infinite loop
1030 app->prefix_command_ = false;
1031 }
1032 // make sure the parent is set to be this object in preparation for parse
1033 app->parent_ = this;
1034 app->_configure();
1035 }
1036}
1037
1038CLI11_INLINE void App::run_callback(bool final_mode, bool suppress_final_callback) {
1039 pre_callback();
1040 // in the main app if immediate_callback_ is set it runs the main callback before the used subcommands
1041 if(!final_mode && parse_complete_callback_) {
1043 }
1044 // run the callbacks for the received subcommands
1045 for(App *subc : get_subcommands()) {
1046 if(subc->parent_ == this) {
1047 subc->run_callback(true, suppress_final_callback);
1048 }
1049 }
1050 // now run callbacks for option_groups
1051 for(auto &subc : subcommands_) {
1052 if(subc->name_.empty() && subc->count_all() > 0) {
1053 subc->run_callback(true, suppress_final_callback);
1054 }
1055 }
1056
1057 // finally run the main callback
1058 if(final_callback_ && (parsed_ > 0) && (!suppress_final_callback)) {
1059 if(!name_.empty() || count_all() > 0 || parent_ == nullptr) {
1061 }
1062 }
1063}
1064
1065CLI11_NODISCARD CLI11_INLINE bool App::_valid_subcommand(const std::string &current, bool ignore_used) const {
1066 // Don't match if max has been reached - but still check parents
1069 return parent_ != nullptr && parent_->_valid_subcommand(current, ignore_used);
1070 }
1071 auto *com = _find_subcommand(current, true, ignore_used);
1072 if(com != nullptr) {
1073 return true;
1074 }
1075 // Check parent if exists, else return false
1077 return parent_ != nullptr && parent_->_valid_subcommand(current, ignore_used);
1078 }
1079 return false;
1080}
1081
1082CLI11_NODISCARD CLI11_INLINE detail::Classifier App::_recognize(const std::string &current,
1083 bool ignore_used_subcommands) const {
1084 std::string dummy1, dummy2;
1085
1086 if(current == "--")
1087 return detail::Classifier::POSITIONAL_MARK;
1088 if(_valid_subcommand(current, ignore_used_subcommands))
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') ||
1094 (dummy1[0] == '.' && !dummy2.empty() && (dummy2[0] >= '0' && dummy2[0] <= '9'))) {
1095 // it looks like a number but check if it could be an option
1096 if(get_option_no_throw(std::string{'-', dummy1[0]}) == nullptr) {
1097 return detail::Classifier::NONE;
1098 }
1099 }
1100 return detail::Classifier::SHORT;
1101 }
1102 if((allow_windows_style_options_) && (detail::split_windows_style(current, dummy1, dummy2)))
1103 return detail::Classifier::WINDOWS_STYLE;
1104 if((current == "++") && !name_.empty() && parent_ != nullptr)
1105 return detail::Classifier::SUBCOMMAND_TERMINATOR;
1106 auto dotloc = current.find_first_of('.');
1107 if(dotloc != std::string::npos) {
1108 auto *cm = _find_subcommand(current.substr(0, dotloc), true, ignore_used_subcommands);
1109 if(cm != nullptr) {
1110 auto res = cm->_recognize(current.substr(dotloc + 1), ignore_used_subcommands);
1111 if(res == detail::Classifier::SUBCOMMAND) {
1112 return res;
1113 }
1114 }
1115 }
1116 return detail::Classifier::NONE;
1117}
1118
1119CLI11_INLINE bool App::_process_config_file(const std::string &config_file, bool throw_error) {
1120 auto path_result = detail::check_path(config_file.c_str());
1121 if(path_result == detail::path_type::file) {
1122 try {
1123 std::vector<ConfigItem> values = config_formatter_->from_file(config_file);
1124 _parse_config(values);
1125 return true;
1126 } catch(const FileError &) {
1127 if(throw_error) {
1128 throw;
1129 }
1130 return false;
1131 }
1132 } else if(throw_error) {
1133 throw FileError::Missing(config_file);
1134 } else {
1135 return false;
1136 }
1137}
1138
1139CLI11_INLINE void App::_process_config_file() {
1140 if(config_ptr_ != nullptr) {
1141 bool config_required = config_ptr_->get_required();
1142 auto file_given = config_ptr_->count() > 0;
1143 if(!(file_given || config_ptr_->envname_.empty())) {
1144 std::string ename_string = detail::get_environment_value(config_ptr_->envname_);
1145 if(!ename_string.empty()) {
1146 config_ptr_->add_result(ename_string);
1147 }
1148 }
1150
1151 auto config_files = config_ptr_->as<std::vector<std::string>>();
1152 bool files_used{file_given};
1153 if(config_files.empty() || config_files.front().empty()) {
1154 if(config_required) {
1155 throw FileError("config file is required but none was given");
1156 }
1157 return;
1158 }
1159 for(const auto &config_file : config_files) {
1160 if(_process_config_file(config_file, config_required || file_given)) {
1161 files_used = true;
1162 }
1163 }
1164 if(!files_used) {
1165 // this is done so the count shows as 0 if no callbacks were processed
1166 config_ptr_->clear();
1167 bool force = config_ptr_->force_callback_;
1171 }
1172 }
1173}
1174
1175CLI11_INLINE void App::_process_env() {
1176 for(const Option_p &opt : options_) {
1177 if(opt->count() == 0 && !opt->envname_.empty()) {
1178 std::string ename_string = detail::get_environment_value(opt->envname_);
1179 if(!ename_string.empty()) {
1180 std::string result = ename_string;
1181 result = opt->_validate(result, 0);
1182 if(result.empty()) {
1183 opt->add_result(ename_string);
1184 }
1185 }
1186 }
1187 }
1188
1189 for(App_p &sub : subcommands_) {
1190 if(sub->get_name().empty() || (sub->count_all() > 0 && !sub->parse_complete_callback_)) {
1191 // only process environment variables if the callback has actually been triggered already
1192 sub->_process_env();
1193 }
1194 }
1195}
1196
1197CLI11_INLINE void App::_process_callbacks() {
1198
1199 for(App_p &sub : subcommands_) {
1200 // process the priority option_groups first
1201 if(sub->get_name().empty() && sub->parse_complete_callback_) {
1202 if(sub->count_all() > 0) {
1203 sub->_process_callbacks();
1204 sub->run_callback();
1205 }
1206 }
1207 }
1208
1209 for(const Option_p &opt : options_) {
1210 if((*opt) && !opt->get_callback_run()) {
1211 opt->run_callback();
1212 }
1213 }
1214 for(App_p &sub : subcommands_) {
1215 if(!sub->parse_complete_callback_) {
1216 sub->_process_callbacks();
1217 }
1218 }
1219}
1220
1221CLI11_INLINE void App::_process_help_flags(bool trigger_help, bool trigger_all_help) const {
1222 const Option *help_ptr = get_help_ptr();
1223 const Option *help_all_ptr = get_help_all_ptr();
1224
1225 if(help_ptr != nullptr && help_ptr->count() > 0)
1226 trigger_help = true;
1227 if(help_all_ptr != nullptr && help_all_ptr->count() > 0)
1228 trigger_all_help = true;
1229
1230 // If there were parsed subcommands, call those. First subcommand wins if there are multiple ones.
1231 if(!parsed_subcommands_.empty()) {
1232 for(const App *sub : parsed_subcommands_)
1233 sub->_process_help_flags(trigger_help, trigger_all_help);
1234
1235 // Only the final subcommand should call for help. All help wins over help.
1236 } else if(trigger_all_help) {
1237 throw CallForAllHelp();
1238 } else if(trigger_help) {
1239 throw CallForHelp();
1240 }
1241}
1242
1243CLI11_INLINE void App::_process_requirements() {
1244 // check excludes
1245 bool excluded{false};
1246 std::string excluder;
1247 for(const auto &opt : exclude_options_) {
1248 if(opt->count() > 0) {
1249 excluded = true;
1250 excluder = opt->get_name();
1251 }
1252 }
1253 for(const auto &subc : exclude_subcommands_) {
1254 if(subc->count_all() > 0) {
1255 excluded = true;
1256 excluder = subc->get_display_name();
1257 }
1258 }
1259 if(excluded) {
1260 if(count_all() > 0) {
1261 throw ExcludesError(get_display_name(), excluder);
1262 }
1263 // if we are excluded but didn't receive anything, just return
1264 return;
1265 }
1266
1267 // check excludes
1268 bool missing_needed{false};
1269 std::string missing_need;
1270 for(const auto &opt : need_options_) {
1271 if(opt->count() == 0) {
1272 missing_needed = true;
1273 missing_need = opt->get_name();
1274 }
1275 }
1276 for(const auto &subc : need_subcommands_) {
1277 if(subc->count_all() == 0) {
1278 missing_needed = true;
1279 missing_need = subc->get_display_name();
1280 }
1281 }
1282 if(missing_needed) {
1283 if(count_all() > 0) {
1284 throw RequiresError(get_display_name(), missing_need);
1285 }
1286 // if we missing something but didn't have any options, just return
1287 return;
1288 }
1289
1290 std::size_t used_options = 0;
1291 for(const Option_p &opt : options_) {
1292
1293 if(opt->count() != 0) {
1294 ++used_options;
1295 }
1296 // Required but empty
1297 if(opt->get_required() && opt->count() == 0) {
1298 throw RequiredError(opt->get_name());
1299 }
1300 // Requires
1301 for(const Option *opt_req : opt->needs_)
1302 if(opt->count() > 0 && opt_req->count() == 0)
1303 throw RequiresError(opt->get_name(), opt_req->get_name());
1304 // Excludes
1305 for(const Option *opt_ex : opt->excludes_)
1306 if(opt->count() > 0 && opt_ex->count() != 0)
1307 throw ExcludesError(opt->get_name(), opt_ex->get_name());
1308 }
1309 // check for the required number of subcommands
1310 if(require_subcommand_min_ > 0) {
1311 auto selected_subcommands = get_subcommands();
1312 if(require_subcommand_min_ > selected_subcommands.size())
1313 throw RequiredError::Subcommand(require_subcommand_min_);
1314 }
1315
1316 // Max error cannot occur, the extra subcommand will parse as an ExtrasError or a remaining item.
1317
1318 // run this loop to check how many unnamed subcommands were actually used since they are considered options
1319 // from the perspective of an App
1320 for(App_p &sub : subcommands_) {
1321 if(sub->disabled_)
1322 continue;
1323 if(sub->name_.empty() && sub->count_all() > 0) {
1324 ++used_options;
1325 }
1326 }
1327
1328 if(require_option_min_ > used_options || (require_option_max_ > 0 && require_option_max_ < used_options)) {
1329 auto option_list = detail::join(options_, [this](const Option_p &ptr) {
1330 if(ptr.get() == help_ptr_ || ptr.get() == help_all_ptr_) {
1331 return std::string{};
1332 }
1333 return ptr->get_name(false, true);
1334 });
1335
1336 auto subc_list = get_subcommands([](App *app) { return ((app->get_name().empty()) && (!app->disabled_)); });
1337 if(!subc_list.empty()) {
1338 option_list += "," + detail::join(subc_list, [](const App *app) { return app->get_display_name(); });
1339 }
1340 throw RequiredError::Option(require_option_min_, require_option_max_, used_options, option_list);
1341 }
1342
1343 // now process the requirements for subcommands if needed
1344 for(App_p &sub : subcommands_) {
1345 if(sub->disabled_)
1346 continue;
1347 if(sub->name_.empty() && sub->required_ == false) {
1348 if(sub->count_all() == 0) {
1349 if(require_option_min_ > 0 && require_option_min_ <= used_options) {
1350 continue;
1351 // if we have met the requirement and there is nothing in this option group skip checking
1352 // requirements
1353 }
1354 if(require_option_max_ > 0 && used_options >= require_option_min_) {
1355 continue;
1356 // if we have met the requirement and there is nothing in this option group skip checking
1357 // requirements
1358 }
1359 }
1360 }
1361 if(sub->count() > 0 || sub->name_.empty()) {
1362 sub->_process_requirements();
1363 }
1364
1365 if(sub->required_ && sub->count_all() == 0) {
1366 throw(CLI::RequiredError(sub->get_display_name()));
1367 }
1368 }
1369}
1370
1371CLI11_INLINE void App::_process() {
1372 // help takes precedence over other potential errors and config and environment shouldn't be processed if help
1373 // throws
1375 try {
1376 // the config file might generate a FileError but that should not be processed until later in the process
1377 // to allow for help, version and other errors to generate first.
1379
1380 // process env shouldn't throw but no reason to process it if config generated an error
1381 _process_env();
1382 } catch(const CLI::FileError &) {
1383 // callbacks can generate exceptions which should take priority
1384 // over the config file error if one exists.
1386 throw;
1387 }
1388
1390
1392}
1393
1394CLI11_INLINE void App::_process_extras() {
1395 if(!(allow_extras_ || prefix_command_)) {
1396 std::size_t num_left_over = remaining_size();
1397 if(num_left_over > 0) {
1398 throw ExtrasError(name_, remaining(false));
1399 }
1400 }
1401
1402 for(App_p &sub : subcommands_) {
1403 if(sub->count() > 0)
1404 sub->_process_extras();
1405 }
1406}
1407
1408CLI11_INLINE void App::_process_extras(std::vector<std::string> &args) {
1409 if(!(allow_extras_ || prefix_command_)) {
1410 std::size_t num_left_over = remaining_size();
1411 if(num_left_over > 0) {
1412 args = remaining(false);
1413 throw ExtrasError(name_, args);
1414 }
1415 }
1416
1417 for(App_p &sub : subcommands_) {
1418 if(sub->count() > 0)
1419 sub->_process_extras(args);
1420 }
1421}
1422
1423CLI11_INLINE void App::increment_parsed() {
1424 ++parsed_;
1425 for(App_p &sub : subcommands_) {
1426 if(sub->get_name().empty())
1427 sub->increment_parsed();
1428 }
1429}
1430
1431CLI11_INLINE void App::_parse(std::vector<std::string> &args) {
1433 _trigger_pre_parse(args.size());
1434 bool positional_only = false;
1435
1436 while(!args.empty()) {
1437 if(!_parse_single(args, positional_only)) {
1438 break;
1439 }
1440 }
1441
1442 if(parent_ == nullptr) {
1443 _process();
1444
1445 // Throw error if any items are left over (depending on settings)
1446 _process_extras(args);
1447
1448 // Convert missing (pairs) to extras (string only) ready for processing in another app
1449 args = remaining_for_passthrough(false);
1450 } else if(parse_complete_callback_) {
1451 _process_env();
1455 run_callback(false, true);
1456 }
1457}
1458
1459CLI11_INLINE void App::_parse(std::vector<std::string> &&args) {
1460 // this can only be called by the top level in which case parent == nullptr by definition
1461 // operation is simplified
1463 _trigger_pre_parse(args.size());
1464 bool positional_only = false;
1465
1466 while(!args.empty()) {
1467 _parse_single(args, positional_only);
1468 }
1469 _process();
1470
1471 // Throw error if any items are left over (depending on settings)
1473}
1474
1475CLI11_INLINE void App::_parse_stream(std::istream &input) {
1476 auto values = config_formatter_->from_config(input);
1477 _parse_config(values);
1479 _trigger_pre_parse(values.size());
1480 _process();
1481
1482 // Throw error if any items are left over (depending on settings)
1484}
1485
1486CLI11_INLINE void App::_parse_config(const std::vector<ConfigItem> &args) {
1487 for(const ConfigItem &item : args) {
1488 if(!_parse_single_config(item) && allow_config_extras_ == config_extras_mode::error)
1489 throw ConfigError::Extras(item.fullname());
1490 }
1491}
1492
1493CLI11_INLINE bool App::_parse_single_config(const ConfigItem &item, std::size_t level) {
1494
1495 if(level < item.parents.size()) {
1496 auto *subcom = get_subcommand_no_throw(item.parents.at(level));
1497 return (subcom != nullptr) ? subcom->_parse_single_config(item, level + 1) : false;
1498 }
1499 // check for section open
1500 if(item.name == "++") {
1501 if(configurable_) {
1504 if(parent_ != nullptr) {
1505 parent_->parsed_subcommands_.push_back(this);
1506 }
1507 }
1508 return true;
1509 }
1510 // check for section close
1511 if(item.name == "--") {
1515 run_callback();
1516 }
1517 return true;
1518 }
1519 Option *op = get_option_no_throw("--" + item.name);
1520 if(op == nullptr) {
1521 if(item.name.size() == 1) {
1522 op = get_option_no_throw("-" + item.name);
1523 }
1524 if(op == nullptr) {
1525 op = get_option_no_throw(item.name);
1526 } else if(!op->get_configurable()) {
1527 auto *testop = get_option_no_throw(item.name);
1528 if(testop != nullptr && testop->get_configurable()) {
1529 op = testop;
1530 }
1531 }
1532 } else if(!op->get_configurable()) {
1533 if(item.name.size() == 1) {
1534 auto *testop = get_option_no_throw("-" + item.name);
1535 if(testop != nullptr && testop->get_configurable()) {
1536 op = testop;
1537 }
1538 }
1539 if(!op->get_configurable()) {
1540 auto *testop = get_option_no_throw(item.name);
1541 if(testop != nullptr && testop->get_configurable()) {
1542 op = testop;
1543 }
1544 }
1545 }
1546
1547 if(op == nullptr) {
1548 // If the option was not present
1549 if(get_allow_config_extras() == config_extras_mode::capture) {
1550 // Should we worry about classifying the extras properly?
1551 missing_.emplace_back(detail::Classifier::NONE, item.fullname());
1552 for(const auto &input : item.inputs) {
1553 missing_.emplace_back(detail::Classifier::NONE, input);
1554 }
1555 }
1556 return false;
1557 }
1558
1559 if(!op->get_configurable()) {
1560 if(get_allow_config_extras() == config_extras_mode::ignore_all) {
1561 return false;
1562 }
1563 throw ConfigError::NotConfigurable(item.fullname());
1564 }
1565 if(op->empty()) {
1566 std::vector<std::string> buffer; // a buffer to use for copying an modifying inputs in a few cases
1567 bool useBuffer{false};
1568 if(item.multiline) {
1569 if(!op->get_inject_separator()) {
1570 buffer = item.inputs;
1571 buffer.erase(std::remove(buffer.begin(), buffer.end(), "%%"), buffer.end());
1572 useBuffer = true;
1573 }
1574 }
1575 const std::vector<std::string> &inputs = (useBuffer) ? buffer : item.inputs;
1576 if(op->get_expected_min() == 0) {
1577 if(item.inputs.size() <= 1) {
1578 // Flag parsing
1579 auto res = config_formatter_->to_flag(item);
1580 bool converted{false};
1581 if(op->get_disable_flag_override()) {
1582 auto val = detail::to_flag_value(res);
1583 if(val == 1) {
1584 res = op->get_flag_value(item.name, "{}");
1585 converted = true;
1586 }
1587 }
1588
1589 if(!converted) {
1590 errno = 0;
1591 if(res != "{}" || op->get_expected_max() <= 1) {
1592 res = op->get_flag_value(item.name, res);
1593 }
1594 }
1595
1596 op->add_result(res);
1597 return true;
1598 }
1599 if(static_cast<int>(inputs.size()) > op->get_items_expected_max() &&
1600 op->get_multi_option_policy() != MultiOptionPolicy::TakeAll) {
1601 if(op->get_items_expected_max() > 1) {
1602 throw ArgumentMismatch::AtMost(item.fullname(), op->get_items_expected_max(), inputs.size());
1603 }
1604
1605 if(!op->get_disable_flag_override()) {
1606 throw ConversionError::TooManyInputsFlag(item.fullname());
1607 }
1608 // if the disable flag override is set then we must have the flag values match a known flag value
1609 // this is true regardless of the output value, so an array input is possible and must be accounted for
1610 for(const auto &res : inputs) {
1611 bool valid_value{false};
1612 if(op->default_flag_values_.empty()) {
1613 if(res == "true" || res == "false" || res == "1" || res == "0") {
1614 valid_value = true;
1615 }
1616 } else {
1617 for(const auto &valid_res : op->default_flag_values_) {
1618 if(valid_res.second == res) {
1619 valid_value = true;
1620 break;
1621 }
1622 }
1623 }
1624
1625 if(valid_value) {
1626 op->add_result(res);
1627 } else {
1628 throw InvalidError("invalid flag argument given");
1629 }
1630 }
1631 return true;
1632 }
1633 }
1634 op->add_result(inputs);
1635 op->run_callback();
1636 }
1637
1638 return true;
1639}
1640
1641CLI11_INLINE bool App::_parse_single(std::vector<std::string> &args, bool &positional_only) {
1642 bool retval = true;
1643 detail::Classifier classifier = positional_only ? detail::Classifier::NONE : _recognize(args.back());
1644 switch(classifier) {
1645 case detail::Classifier::POSITIONAL_MARK:
1646 args.pop_back();
1647 positional_only = true;
1648 if((!_has_remaining_positionals()) && (parent_ != nullptr)) {
1649 retval = false;
1650 } else {
1651 _move_to_missing(classifier, "--");
1652 }
1653 break;
1654 case detail::Classifier::SUBCOMMAND_TERMINATOR:
1655 // treat this like a positional mark if in the parent app
1656 args.pop_back();
1657 retval = false;
1658 break;
1659 case detail::Classifier::SUBCOMMAND:
1660 retval = _parse_subcommand(args);
1661 break;
1662 case detail::Classifier::LONG:
1663 case detail::Classifier::SHORT:
1664 case detail::Classifier::WINDOWS_STYLE:
1665 // If already parsed a subcommand, don't accept options_
1666 retval = _parse_arg(args, classifier, false);
1667 break;
1668 case detail::Classifier::NONE:
1669 // Probably a positional or something for a parent (sub)command
1670 retval = _parse_positional(args, false);
1671 if(retval && positionals_at_end_) {
1672 positional_only = true;
1673 }
1674 break;
1675 // LCOV_EXCL_START
1676 default:
1677 throw HorribleError("unrecognized classifier (you should not see this!)");
1678 // LCOV_EXCL_STOP
1679 }
1680 return retval;
1681}
1682
1683CLI11_NODISCARD CLI11_INLINE std::size_t App::_count_remaining_positionals(bool required_only) const {
1684 std::size_t retval = 0;
1685 for(const Option_p &opt : options_) {
1686 if(opt->get_positional() && (!required_only || opt->get_required())) {
1687 if(opt->get_items_expected_min() > 0 && static_cast<int>(opt->count()) < opt->get_items_expected_min()) {
1688 retval += static_cast<std::size_t>(opt->get_items_expected_min()) - opt->count();
1689 }
1690 }
1691 }
1692 return retval;
1693}
1694
1695CLI11_NODISCARD CLI11_INLINE bool App::_has_remaining_positionals() const {
1696 for(const Option_p &opt : options_) {
1697 if(opt->get_positional() && ((static_cast<int>(opt->count()) < opt->get_items_expected_min()))) {
1698 return true;
1699 }
1700 }
1701
1702 return false;
1703}
1704
1705CLI11_INLINE bool App::_parse_positional(std::vector<std::string> &args, bool haltOnSubcommand) {
1706
1707 const std::string &positional = args.back();
1708 Option *posOpt{nullptr};
1709
1711 // deal with the case of required arguments at the end which should take precedence over other arguments
1712 auto arg_rem = args.size();
1713 auto remreq = _count_remaining_positionals(true);
1714 if(arg_rem <= remreq) {
1715 for(const Option_p &opt : options_) {
1716 if(opt->get_positional() && opt->required_) {
1717 if(static_cast<int>(opt->count()) < opt->get_items_expected_min()) {
1719 std::string pos = positional;
1720 pos = opt->_validate(pos, 0);
1721 if(!pos.empty()) {
1722 continue;
1723 }
1724 }
1725 posOpt = opt.get();
1726 break;
1727 }
1728 }
1729 }
1730 }
1731 }
1732 if(posOpt == nullptr) {
1733 for(const Option_p &opt : options_) {
1734 // Eat options, one by one, until done
1735 if(opt->get_positional() &&
1736 (static_cast<int>(opt->count()) < opt->get_items_expected_max() || opt->get_allow_extra_args())) {
1738 std::string pos = positional;
1739 pos = opt->_validate(pos, 0);
1740 if(!pos.empty()) {
1741 continue;
1742 }
1743 }
1744 posOpt = opt.get();
1745 break;
1746 }
1747 }
1748 }
1749 if(posOpt != nullptr) {
1750 parse_order_.push_back(posOpt);
1751 if(posOpt->get_inject_separator()) {
1752 if(!posOpt->results().empty() && !posOpt->results().back().empty()) {
1753 posOpt->add_result(std::string{});
1754 }
1755 }
1756 if(posOpt->get_trigger_on_parse() && posOpt->current_option_state_ == Option::option_state::callback_run) {
1757 posOpt->clear();
1758 }
1759 posOpt->add_result(positional);
1760 if(posOpt->get_trigger_on_parse()) {
1761 posOpt->run_callback();
1762 }
1763
1764 args.pop_back();
1765 return true;
1766 }
1767
1768 for(auto &subc : subcommands_) {
1769 if((subc->name_.empty()) && (!subc->disabled_)) {
1770 if(subc->_parse_positional(args, false)) {
1771 if(!subc->pre_parse_called_) {
1772 subc->_trigger_pre_parse(args.size());
1773 }
1774 return true;
1775 }
1776 }
1777 }
1778 // let the parent deal with it if possible
1779 if(parent_ != nullptr && fallthrough_) {
1780 return _get_fallthrough_parent()->_parse_positional(args, static_cast<bool>(parse_complete_callback_));
1781 }
1783 auto *com = _find_subcommand(args.back(), true, false);
1784 if(com != nullptr && (require_subcommand_max_ == 0 || require_subcommand_max_ > parsed_subcommands_.size())) {
1785 if(haltOnSubcommand) {
1786 return false;
1787 }
1788 args.pop_back();
1789 com->_parse(args);
1790 return true;
1791 }
1795 auto *parent_app = (parent_ != nullptr) ? _get_fallthrough_parent() : this;
1796 com = parent_app->_find_subcommand(args.back(), true, false);
1797 if(com != nullptr && (com->parent_->require_subcommand_max_ == 0 ||
1798 com->parent_->require_subcommand_max_ > com->parent_->parsed_subcommands_.size())) {
1799 return false;
1800 }
1801 }
1803 throw CLI::ExtrasError(name_, args);
1804 }
1806 if(parent_ != nullptr && name_.empty()) {
1807 return false;
1808 }
1810 _move_to_missing(detail::Classifier::NONE, positional);
1811 args.pop_back();
1812 if(prefix_command_) {
1813 while(!args.empty()) {
1814 _move_to_missing(detail::Classifier::NONE, args.back());
1815 args.pop_back();
1816 }
1817 }
1818
1819 return true;
1820}
1821
1822CLI11_NODISCARD CLI11_INLINE App *
1823App::_find_subcommand(const std::string &subc_name, bool ignore_disabled, bool ignore_used) const noexcept {
1824 for(const App_p &com : subcommands_) {
1825 if(com->disabled_ && ignore_disabled)
1826 continue;
1827 if(com->get_name().empty()) {
1828 auto *subc = com->_find_subcommand(subc_name, ignore_disabled, ignore_used);
1829 if(subc != nullptr) {
1830 return subc;
1831 }
1832 }
1833 if(com->check_name(subc_name)) {
1834 if((!*com) || !ignore_used)
1835 return com.get();
1836 }
1837 }
1838 return nullptr;
1839}
1840
1841CLI11_INLINE bool App::_parse_subcommand(std::vector<std::string> &args) {
1842 if(_count_remaining_positionals(/* required */ true) > 0) {
1843 _parse_positional(args, false);
1844 return true;
1845 }
1846 auto *com = _find_subcommand(args.back(), true, true);
1847 if(com == nullptr) {
1848 // the main way to get here is using .notation
1849 auto dotloc = args.back().find_first_of('.');
1850 if(dotloc != std::string::npos) {
1851 com = _find_subcommand(args.back().substr(0, dotloc), true, true);
1852 if(com != nullptr) {
1853 args.back() = args.back().substr(dotloc + 1);
1854 args.push_back(com->get_display_name());
1855 }
1856 }
1857 }
1858 if(com != nullptr) {
1859 args.pop_back();
1860 if(!com->silent_) {
1861 parsed_subcommands_.push_back(com);
1862 }
1863 com->_parse(args);
1864 auto *parent_app = com->parent_;
1865 while(parent_app != this) {
1866 parent_app->_trigger_pre_parse(args.size());
1867 if(!com->silent_) {
1868 parent_app->parsed_subcommands_.push_back(com);
1869 }
1870 parent_app = parent_app->parent_;
1871 }
1872 return true;
1873 }
1874
1875 if(parent_ == nullptr)
1876 throw HorribleError("Subcommand " + args.back() + " missing");
1877 return false;
1878}
1879
1880CLI11_INLINE bool
1881App::_parse_arg(std::vector<std::string> &args, detail::Classifier current_type, bool local_processing_only) {
1882
1883 std::string current = args.back();
1884
1885 std::string arg_name;
1886 std::string value;
1887 std::string rest;
1888
1889 switch(current_type) {
1890 case detail::Classifier::LONG:
1891 if(!detail::split_long(current, arg_name, value))
1892 throw HorribleError("Long parsed but missing (you should not see this):" + args.back());
1893 break;
1894 case detail::Classifier::SHORT:
1895 if(!detail::split_short(current, arg_name, rest))
1896 throw HorribleError("Short parsed but missing! You should not see this");
1897 break;
1898 case detail::Classifier::WINDOWS_STYLE:
1899 if(!detail::split_windows_style(current, arg_name, value))
1900 throw HorribleError("windows option parsed but missing! You should not see this");
1901 break;
1902 case detail::Classifier::SUBCOMMAND:
1903 case detail::Classifier::SUBCOMMAND_TERMINATOR:
1904 case detail::Classifier::POSITIONAL_MARK:
1905 case detail::Classifier::NONE:
1906 default:
1907 throw HorribleError("parsing got called with invalid option! You should not see this");
1908 }
1909
1910 auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [arg_name, current_type](const Option_p &opt) {
1911 if(current_type == detail::Classifier::LONG)
1912 return opt->check_lname(arg_name);
1913 if(current_type == detail::Classifier::SHORT)
1914 return opt->check_sname(arg_name);
1915 // this will only get called for detail::Classifier::WINDOWS_STYLE
1916 return opt->check_lname(arg_name) || opt->check_sname(arg_name);
1917 });
1918
1919 // Option not found
1920 while(op_ptr == std::end(options_)) {
1921 // using while so we can break
1922 for(auto &subc : subcommands_) {
1923 if(subc->name_.empty() && !subc->disabled_) {
1924 if(subc->_parse_arg(args, current_type, local_processing_only)) {
1925 if(!subc->pre_parse_called_) {
1926 subc->_trigger_pre_parse(args.size());
1927 }
1928 return true;
1929 }
1930 }
1931 }
1932 if(allow_non_standard_options_ && current_type == detail::Classifier::SHORT && current.size() > 2) {
1933 std::string narg_name;
1934 std::string nvalue;
1935 detail::split_long(std::string{'-'} + current, narg_name, nvalue);
1936 op_ptr = std::find_if(std::begin(options_), std::end(options_), [narg_name](const Option_p &opt) {
1937 return opt->check_sname(narg_name);
1938 });
1939 if(op_ptr != std::end(options_)) {
1940 arg_name = narg_name;
1941 value = nvalue;
1942 rest.clear();
1943 break;
1944 }
1945 }
1946
1947 // don't capture missing if this is a nameless subcommand and nameless subcommands can't fallthrough
1948 if(parent_ != nullptr && name_.empty()) {
1949 return false;
1950 }
1951
1952 // now check for '.' notation of subcommands
1953 auto dotloc = arg_name.find_first_of('.', 1);
1954 if(dotloc != std::string::npos && dotloc < arg_name.size() - 1) {
1955 // using dot notation is equivalent to single argument subcommand
1956 auto *sub = _find_subcommand(arg_name.substr(0, dotloc), true, false);
1957 if(sub != nullptr) {
1958 std::string v = args.back();
1959 args.pop_back();
1960 arg_name = arg_name.substr(dotloc + 1);
1961 if(arg_name.size() > 1) {
1962 args.push_back(std::string("--") + v.substr(dotloc + 3));
1963 current_type = detail::Classifier::LONG;
1964 } else {
1965 auto nval = v.substr(dotloc + 2);
1966 nval.front() = '-';
1967 if(nval.size() > 2) {
1968 // '=' not allowed in short form arguments
1969 args.push_back(nval.substr(3));
1970 nval.resize(2);
1971 }
1972 args.push_back(nval);
1973 current_type = detail::Classifier::SHORT;
1974 }
1975 std::string dummy1, dummy2;
1976 bool val = false;
1977 if(current_type == detail::Classifier::SHORT || detail::split_long(args.back(), dummy1, dummy2)) {
1978 val = sub->_parse_arg(args, current_type, true);
1979 }
1980
1981 if(val) {
1982 if(!sub->silent_) {
1983 parsed_subcommands_.push_back(sub);
1984 }
1985 // deal with preparsing
1987 _trigger_pre_parse(args.size());
1988 // run the parse complete callback since the subcommand processing is now complete
1989 if(sub->parse_complete_callback_) {
1990 sub->_process_env();
1991 sub->_process_callbacks();
1992 sub->_process_help_flags();
1993 sub->_process_requirements();
1994 sub->run_callback(false, true);
1995 }
1996 return true;
1997 }
1998 args.pop_back();
1999 args.push_back(v);
2000 }
2001 }
2002 if(local_processing_only) {
2003 return false;
2004 }
2005 // If a subcommand, try the main command
2006 if(parent_ != nullptr && fallthrough_)
2007 return _get_fallthrough_parent()->_parse_arg(args, current_type, false);
2008
2009 // Otherwise, add to missing
2010 args.pop_back();
2011 _move_to_missing(current_type, current);
2012 return true;
2013 }
2014
2015 args.pop_back();
2016
2017 // Get a reference to the pointer to make syntax bearable
2018 Option_p &op = *op_ptr;
2020 if(op->get_inject_separator()) {
2021 if(!op->results().empty() && !op->results().back().empty()) {
2022 op->add_result(std::string{});
2023 }
2024 }
2025 if(op->get_trigger_on_parse() && op->current_option_state_ == Option::option_state::callback_run) {
2026 op->clear();
2027 }
2028 int min_num = (std::min)(op->get_type_size_min(), op->get_items_expected_min());
2029 int max_num = op->get_items_expected_max();
2030 // check container like options to limit the argument size to a single type if the allow_extra_flags argument is
2031 // set. 16 is somewhat arbitrary (needs to be at least 4)
2032 if(max_num >= detail::expected_max_vector_size / 16 && !op->get_allow_extra_args()) {
2033 auto tmax = op->get_type_size_max();
2034 max_num = detail::checked_multiply(tmax, op->get_expected_min()) ? tmax : detail::expected_max_vector_size;
2035 }
2036 // Make sure we always eat the minimum for unlimited vectors
2037 int collected = 0; // total number of arguments collected
2038 int result_count = 0; // local variable for number of results in a single arg string
2039 // deal with purely flag like things
2040 if(max_num == 0) {
2041 auto res = op->get_flag_value(arg_name, value);
2042 op->add_result(res);
2043 parse_order_.push_back(op.get());
2044 } else if(!value.empty()) { // --this=value
2045 op->add_result(value, result_count);
2046 parse_order_.push_back(op.get());
2047 collected += result_count;
2048 // -Trest
2049 } else if(!rest.empty()) {
2050 op->add_result(rest, result_count);
2051 parse_order_.push_back(op.get());
2052 rest = "";
2053 collected += result_count;
2054 }
2055
2056 // gather the minimum number of arguments
2057 while(min_num > collected && !args.empty()) {
2058 std::string current_ = args.back();
2059 args.pop_back();
2060 op->add_result(current_, result_count);
2061 parse_order_.push_back(op.get());
2062 collected += result_count;
2063 }
2064
2065 if(min_num > collected) { // if we have run out of arguments and the minimum was not met
2066 throw ArgumentMismatch::TypedAtLeast(op->get_name(), min_num, op->get_type_name());
2067 }
2068
2069 // now check for optional arguments
2070 if(max_num > collected || op->get_allow_extra_args()) { // we allow optional arguments
2071 auto remreqpos = _count_remaining_positionals(true);
2072 // we have met the minimum now optionally check up to the maximum
2073 while((collected < max_num || op->get_allow_extra_args()) && !args.empty() &&
2074 _recognize(args.back(), false) == detail::Classifier::NONE) {
2075 // If any required positionals remain, don't keep eating
2076 if(remreqpos >= args.size()) {
2077 break;
2078 }
2080 std::string arg = args.back();
2081 arg = op->_validate(arg, 0);
2082 if(!arg.empty()) {
2083 break;
2084 }
2085 }
2086 op->add_result(args.back(), result_count);
2087 parse_order_.push_back(op.get());
2088 args.pop_back();
2089 collected += result_count;
2090 }
2091
2092 // Allow -- to end an unlimited list and "eat" it
2093 if(!args.empty() && _recognize(args.back()) == detail::Classifier::POSITIONAL_MARK)
2094 args.pop_back();
2095 // optional flag that didn't receive anything now get the default value
2096 if(min_num == 0 && max_num > 0 && collected == 0) {
2097 auto res = op->get_flag_value(arg_name, std::string{});
2098 op->add_result(res);
2099 parse_order_.push_back(op.get());
2100 }
2101 }
2102 // if we only partially completed a type then add an empty string if allowed for later processing
2103 if(min_num > 0 && (collected % op->get_type_size_max()) != 0) {
2104 if(op->get_type_size_max() != op->get_type_size_min()) {
2105 op->add_result(std::string{});
2106 } else {
2107 throw ArgumentMismatch::PartialType(op->get_name(), op->get_type_size_min(), op->get_type_name());
2108 }
2109 }
2110 if(op->get_trigger_on_parse()) {
2111 op->run_callback();
2112 }
2113 if(!rest.empty()) {
2114 rest = "-" + rest;
2115 args.push_back(rest);
2116 }
2117 return true;
2118}
2119
2120CLI11_INLINE void App::_trigger_pre_parse(std::size_t remaining_args) {
2121 if(!pre_parse_called_) {
2122 pre_parse_called_ = true;
2124 pre_parse_callback_(remaining_args);
2125 }
2126 } else if(immediate_callback_) {
2127 if(!name_.empty()) {
2128 auto pcnt = parsed_;
2129 missing_t extras = std::move(missing_);
2130 clear();
2131 parsed_ = pcnt;
2132 pre_parse_called_ = true;
2133 missing_ = std::move(extras);
2134 }
2135 }
2136}
2137
2139 if(parent_ == nullptr) {
2140 throw(HorribleError("No Valid parent"));
2141 }
2142 auto *fallthrough_parent = parent_;
2143 while((fallthrough_parent->parent_ != nullptr) && (fallthrough_parent->get_name().empty())) {
2144 fallthrough_parent = fallthrough_parent->parent_;
2145 }
2146 return fallthrough_parent;
2147}
2148
2149CLI11_NODISCARD CLI11_INLINE const std::string &App::_compare_subcommand_names(const App &subcom,
2150 const App &base) const {
2151 static const std::string estring;
2152 if(subcom.disabled_) {
2153 return estring;
2154 }
2155 for(const auto &subc : base.subcommands_) {
2156 if(subc.get() != &subcom) {
2157 if(subc->disabled_) {
2158 continue;
2159 }
2160 if(!subcom.get_name().empty()) {
2161 if(subc->check_name(subcom.get_name())) {
2162 return subcom.get_name();
2163 }
2164 }
2165 if(!subc->get_name().empty()) {
2166 if(subcom.check_name(subc->get_name())) {
2167 return subc->get_name();
2168 }
2169 }
2170 for(const auto &les : subcom.aliases_) {
2171 if(subc->check_name(les)) {
2172 return les;
2173 }
2174 }
2175 // this loop is needed in case of ignore_underscore or ignore_case on one but not the other
2176 for(const auto &les : subc->aliases_) {
2177 if(subcom.check_name(les)) {
2178 return les;
2179 }
2180 }
2181 // if the subcommand is an option group we need to check deeper
2182 if(subc->get_name().empty()) {
2183 const auto &cmpres = _compare_subcommand_names(subcom, *subc);
2184 if(!cmpres.empty()) {
2185 return cmpres;
2186 }
2187 }
2188 // if the test subcommand is an option group we need to check deeper
2189 if(subcom.get_name().empty()) {
2190 const auto &cmpres = _compare_subcommand_names(*subc, subcom);
2191 if(!cmpres.empty()) {
2192 return cmpres;
2193 }
2194 }
2195 }
2196 }
2197 return estring;
2198}
2199
2200CLI11_INLINE void App::_move_to_missing(detail::Classifier val_type, const std::string &val) {
2201 if(allow_extras_ || subcommands_.empty()) {
2202 missing_.emplace_back(val_type, val);
2203 return;
2204 }
2205 // allow extra arguments to be places in an option group if it is allowed there
2206 for(auto &subc : subcommands_) {
2207 if(subc->name_.empty() && subc->allow_extras_) {
2208 subc->missing_.emplace_back(val_type, val);
2209 return;
2210 }
2211 }
2212 // if we haven't found any place to put them yet put them in missing
2213 missing_.emplace_back(val_type, val);
2214}
2215
2216CLI11_INLINE void App::_move_option(Option *opt, App *app) {
2217 if(opt == nullptr) {
2218 throw OptionNotFound("the option is NULL");
2219 }
2220 // verify that the give app is actually a subcommand
2221 bool found = false;
2222 for(auto &subc : subcommands_) {
2223 if(app == subc.get()) {
2224 found = true;
2225 }
2226 }
2227 if(!found) {
2228 throw OptionNotFound("The Given app is not a subcommand");
2229 }
2230
2231 if((help_ptr_ == opt) || (help_all_ptr_ == opt))
2232 throw OptionAlreadyAdded("cannot move help options");
2233
2234 if(config_ptr_ == opt)
2235 throw OptionAlreadyAdded("cannot move config file options");
2236
2237 auto iterator =
2238 std::find_if(std::begin(options_), std::end(options_), [opt](const Option_p &v) { return v.get() == opt; });
2239 if(iterator != std::end(options_)) {
2240 const auto &opt_p = *iterator;
2241 if(std::find_if(std::begin(app->options_), std::end(app->options_), [&opt_p](const Option_p &v) {
2242 return (*v == *opt_p);
2243 }) == std::end(app->options_)) {
2244 // only erase after the insertion was successful
2245 app->options_.push_back(std::move(*iterator));
2246 options_.erase(iterator);
2247 } else {
2248 throw OptionAlreadyAdded("option was not located: " + opt->get_name());
2249 }
2250 } else {
2251 throw OptionNotFound("could not locate the given Option");
2252 }
2253}
2254
2255CLI11_INLINE void TriggerOn(App *trigger_app, App *app_to_enable) {
2256 app_to_enable->enabled_by_default(false);
2257 app_to_enable->disabled_by_default();
2258 trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(false); });
2259}
2260
2261CLI11_INLINE void TriggerOn(App *trigger_app, std::vector<App *> apps_to_enable) {
2262 for(auto &app : apps_to_enable) {
2263 app->enabled_by_default(false);
2264 app->disabled_by_default();
2265 }
2266
2267 trigger_app->preparse_callback([apps_to_enable](std::size_t) {
2268 for(const auto &app : apps_to_enable) {
2269 app->disabled(false);
2270 }
2271 });
2272}
2273
2274CLI11_INLINE void TriggerOff(App *trigger_app, App *app_to_enable) {
2275 app_to_enable->disabled_by_default(false);
2276 app_to_enable->enabled_by_default();
2277 trigger_app->preparse_callback([app_to_enable](std::size_t) { app_to_enable->disabled(); });
2278}
2279
2280CLI11_INLINE void TriggerOff(App *trigger_app, std::vector<App *> apps_to_enable) {
2281 for(auto &app : apps_to_enable) {
2282 app->disabled_by_default(false);
2283 app->enabled_by_default();
2284 }
2285
2286 trigger_app->preparse_callback([apps_to_enable](std::size_t) {
2287 for(const auto &app : apps_to_enable) {
2288 app->disabled();
2289 }
2290 });
2291}
2292
2293CLI11_INLINE void deprecate_option(Option *opt, const std::string &replacement) {
2294 Validator deprecate_warning{[opt, replacement](std::string &) {
2295 std::cout << opt->get_name() << " is deprecated please use '" << replacement
2296 << "' instead\n";
2297 return std::string();
2298 },
2299 "DEPRECATED"};
2300 deprecate_warning.application_index(0);
2301 opt->check(deprecate_warning);
2302 if(!replacement.empty()) {
2303 opt->description(opt->get_description() + " DEPRECATED: please use '" + replacement + "' instead");
2304 }
2305}
2306
2307CLI11_INLINE void retire_option(App *app, Option *opt) {
2308 App temp;
2309 auto *option_copy = temp.add_option(opt->get_name(false, true))
2310 ->type_size(opt->get_type_size_min(), opt->get_type_size_max())
2311 ->expected(opt->get_expected_min(), opt->get_expected_max())
2312 ->allow_extra_args(opt->get_allow_extra_args());
2313
2314 app->remove_option(opt);
2315 auto *opt2 = app->add_option(option_copy->get_name(false, true), "option has been retired and has no effect");
2316 opt2->type_name("RETIRED")
2317 ->default_str("RETIRED")
2318 ->type_size(option_copy->get_type_size_min(), option_copy->get_type_size_max())
2319 ->expected(option_copy->get_expected_min(), option_copy->get_expected_max())
2320 ->allow_extra_args(option_copy->get_allow_extra_args());
2321
2322 // LCOV_EXCL_START
2323 // something odd with coverage on new compilers
2324 Validator retired_warning{[opt2](std::string &) {
2325 std::cout << "WARNING " << opt2->get_name() << " is retired and has no effect\n";
2326 return std::string();
2327 },
2328 ""};
2329 // LCOV_EXCL_STOP
2330 retired_warning.application_index(0);
2331 opt2->check(retired_warning);
2332}
2333
2334CLI11_INLINE void retire_option(App &app, Option *opt) { retire_option(&app, opt); }
2335
2336CLI11_INLINE void retire_option(App *app, const std::string &option_name) {
2337
2338 auto *opt = app->get_option_no_throw(option_name);
2339 if(opt != nullptr) {
2340 retire_option(app, opt);
2341 return;
2342 }
2343 auto *opt2 = app->add_option(option_name, "option has been retired and has no effect")
2344 ->type_name("RETIRED")
2345 ->expected(0, 1)
2346 ->default_str("RETIRED");
2347 // LCOV_EXCL_START
2348 // something odd with coverage on new compilers
2349 Validator retired_warning{[opt2](std::string &) {
2350 std::cout << "WARNING " << opt2->get_name() << " is retired and has no effect\n";
2351 return std::string();
2352 },
2353 ""};
2354 // LCOV_EXCL_STOP
2355 retired_warning.application_index(0);
2356 opt2->check(retired_warning);
2357}
2358
2359CLI11_INLINE void retire_option(App &app, const std::string &option_name) { retire_option(&app, option_name); }
2360
2361namespace FailureMessage {
2362
2363CLI11_INLINE std::string simple(const App *app, const Error &e) {
2364 std::string header = std::string(e.what()) + "\n";
2365 std::vector<std::string> names;
2366
2367 // Collect names
2368 if(app->get_help_ptr() != nullptr)
2369 names.push_back(app->get_help_ptr()->get_name());
2370
2371 if(app->get_help_all_ptr() != nullptr)
2372 names.push_back(app->get_help_all_ptr()->get_name());
2373
2374 // If any names found, suggest those
2375 if(!names.empty())
2376 header += "Run with " + detail::join(names, " or ") + " for more information.\n";
2377
2378 return header;
2379}
2380
2381CLI11_INLINE std::string help(const App *app, const Error &e) {
2382 std::string header = std::string("ERROR: ") + e.get_name() + ": " + e.what() + "\n";
2383 header += app->help();
2384 return header;
2385}
2386
2387} // namespace FailureMessage
2388
2389// [CLI11:app_inl_hpp:end]
2390} // namespace CLI
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 bool _has_remaining_positionals() const
Count the required remaining positional arguments.
Definition App_inl.hpp:1695
CLI11_NODISCARD detail::Classifier _recognize(const std::string &current, bool ignore_used_subcommands=true) const
Selects a Classifier enum based on the type of the current argument.
Definition App_inl.hpp: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:2138
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
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:405
void _move_to_missing(detail::Classifier val_type, const std::string &val)
Helper function to place extra values in the most appropriate position.
Definition App_inl.hpp:2200
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:1221
void _process_requirements()
Verify required options and cross requirements. Subcommands too (only if selected).
Definition App_inl.hpp:1243
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
CLI11_NODISCARD std::size_t _count_remaining_positionals(bool required_only=false) const
Count the required remaining positional arguments.
Definition App_inl.hpp:1683
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:1139
std::string footer_
Footer to put after all options in the help output INHERITABLE.
Definition App.hpp:162
void increment_parsed()
Internal function to recursively increment the parsed counter on the current app as well unnamed subc...
Definition App_inl.hpp:1423
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
void _trigger_pre_parse(std::size_t remaining_args)
Trigger the pre_parse callback if needed.
Definition App_inl.hpp:2120
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:1175
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:1475
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:2149
void clear()
Reset the parsed data.
Definition App_inl.hpp:549
App * enabled_by_default(bool enable=true)
Definition App.hpp:416
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
App * preparse_callback(std::function< void(std::size_t)> pp_callback)
Definition App.hpp:363
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 positionals_at_end_
specify that positional arguments come at the end of the argument sequence not inheritable
Definition App.hpp:243
void _process()
Process callbacks and such.
Definition App_inl.hpp:1371
bool immediate_callback_
Definition App.hpp:130
bool _parse_single(std::vector< std::string > &args, bool &positional_only)
Definition App_inl.hpp:1641
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
void _move_option(Option *opt, App *app)
function that could be used by subclasses of App to shift options around into subcommands
Definition App_inl.hpp:2216
void _process_extras()
Throw an error if anything is left over and should not be.
Definition App_inl.hpp:1394
CLI11_NODISCARD bool _valid_subcommand(const std::string &current, bool ignore_used=true) const
Check to see if a subcommand is valid. Give up immediately if subcommand max has been reached.
Definition App_inl.hpp: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:1486
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 _parse_positional(std::vector< std::string > &args, bool haltOnSubcommand)
Definition App_inl.hpp:1705
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
bool _parse_arg(std::vector< std::string > &args, detail::Classifier current_type, bool local_processing_only)
Definition App_inl.hpp:1881
std::function< void(std::size_t)> pre_parse_callback_
This is a function that runs prior to the start of parsing.
Definition App.hpp:133
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:1197
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
CLI11_NODISCARD config_extras_mode get_allow_config_extras() const
Get the status of allow extras.
Definition App.hpp:1175
bool _parse_subcommand(std::vector< std::string > &args)
Definition App_inl.hpp:1841
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
std::vector< Option * > parse_order_
This is a list of pointers to options with the original parse order.
Definition App.hpp:194
void _parse(std::vector< std::string > &args)
Internal parse function.
Definition App_inl.hpp:1431
bool validate_positionals_
If set to true positional options are validated before assigning INHERITABLE.
Definition App.hpp:254
bool _parse_single_config(const ConfigItem &item, std::size_t level=0)
Fill in a single config option.
Definition App_inl.hpp:1493
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:1823
CLI11_NODISCARD const std::string & get_name() const
Get the name of the current app.
Definition App.hpp:1205
App * disabled(bool disable=true)
Disable the subcommand or option group.
Definition App.hpp:387
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 too many positionals or options are found.
Definition Error.hpp:308
Thrown when parsing an INI file and it is missing.
Definition Error.hpp:198
Definition Error.hpp:343
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
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:139
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:127
bool required_
True if this is a required option.
Definition Option.hpp:60
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:130
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:115
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
CLI11_NODISCARD bool check_name(const std::string &name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
Definition Option_inl.hpp:354
@ callback_run
the callback has been executed
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:297
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
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:300
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:340
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:715
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:249
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:576
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:671
CLI11_NODISCARD int get_items_expected_max() const
Get the maximum number of items expected to be returned and used for the callback.
Definition Option.hpp:579
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 bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:529
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:383
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:596
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:364
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:571
void clear()
Clear the parsed results (mostly for testing)
Definition Option.hpp:370
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:573
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
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:392
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:246
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 nonexistent 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
std::vector< std::string > inputs
Listing of inputs.
Definition ConfigFwd.hpp:36
std::string name
This is the name.
Definition ConfigFwd.hpp:34
CLI11_NODISCARD std::string fullname() const
The list of parents and name joined by ".".
Definition ConfigFwd.hpp:40
bool multiline
indicator if a multiline vector separator was inserted
Definition ConfigFwd.hpp:38
std::vector< std::string > parents
This is the list of parents.
Definition ConfigFwd.hpp:31