12#include "../Option.hpp"
26 other->required(required_);
27 other->ignore_case(ignore_case_);
28 other->ignore_underscore(ignore_underscore_);
29 other->configurable(configurable_);
30 other->disable_flag_override(disable_flag_override_);
31 other->delimiter(delimiter_);
32 other->always_capture_default(always_capture_default_);
33 other->multi_option_policy(multi_option_policy_);
44 }
else if(value == detail::expected_max_vector_size) {
59 value_min = -value_min;
63 value_max = detail::expected_max_vector_size;
65 if(value_max < value_min) {
77 validator.non_modifying();
79 if(!validator_name.empty())
85 std::string Validator_description,
86 std::string Validator_name) {
87 validators_.emplace_back(
Validator, std::move(Validator_description), std::move(Validator_name));
94 if(!Validator_name.empty())
100 std::string transform_description,
101 std::string transform_name) {
104 [func](std::string &val) {
106 return std::string{};
108 std::move(transform_description),
109 std::move(transform_name)));
116 [func](std::string &inout) {
118 return std::string{};
130 if((Validator_name.empty()) && (!
validators_.empty())) {
133 throw OptionNotFound(std::string{
"Validator "} + Validator_name +
" Not Found");
138 if(index >= 0 && index <
static_cast<int>(
validators_.size())) {
145 auto iterator = std::find(std::begin(
needs_), std::end(
needs_), opt);
147 if(iterator == std::end(
needs_)) {
182 auto *parent =
static_cast<T *
>(
parent_);
183 for(
const Option_p &opt : parent->options_) {
184 if(opt.get() ==
this) {
188 if(!omatch.empty()) {
203 auto *parent =
static_cast<T *
>(
parent_);
204 for(
const Option_p &opt : parent->options_) {
205 if(opt.get() ==
this) {
209 if(!omatch.empty()) {
211 throw OptionAlreadyAdded(
"adding ignore underscore caused a name conflict with " + omatch);
233CLI11_NODISCARD CLI11_INLINE std::string
Option::get_name(
bool positional,
bool all_options)
const {
239 std::vector<std::string> name_list;
243 name_list.push_back(
pname_);
246 for(
const std::string &sname :
snames_) {
247 name_list.push_back(
"-" + sname);
253 for(
const std::string &lname :
lnames_) {
254 name_list.push_back(
"--" + lname);
260 for(
const std::string &sname :
snames_)
261 name_list.push_back(
"-" + sname);
263 for(
const std::string &lname :
lnames_)
264 name_list.push_back(
"--" + lname);
267 return detail::join(name_list);
276 return std::string(2,
'-') +
lnames_[0];
280 return std::string(1,
'-') +
snames_[0];
305 bool local_result =
callback_(send_results);
313 static const std::string estring;
314 for(
const std::string &sname :
snames_) {
315 if(other.check_sname(sname))
317 if(other.check_lname(sname))
320 for(
const std::string &lname :
lnames_) {
321 if(other.check_lname(lname))
323 if(lname.size() == 1) {
324 if(other.check_sname(lname)) {
333 if(other.snames_.empty() && other.fnames_.empty() && !other.pname_.empty()) {
339 for(
const std::string &sname : other.snames_)
342 for(
const std::string &lname : other.lnames_)
351 if(name.length() > 2 && name[0] ==
'-' && name[1] ==
'-')
353 if(name.length() > 1 && name.front() ==
'-')
356 std::string local_pname =
pname_;
357 std::string local_name = name;
359 local_pname = detail::remove_underscore(local_pname);
360 local_name = detail::remove_underscore(local_name);
363 local_pname = detail::to_lower(local_pname);
364 local_name = detail::to_lower(local_name);
366 if(local_name == local_pname) {
379 std::string input_value)
const {
380 static const std::string trueString{
"true"};
381 static const std::string falseString{
"false"};
382 static const std::string emptyString{
"{}"};
385 if(!((input_value.empty()) || (input_value == emptyString))) {
387 if(default_ind >= 0) {
393 throw(ArgumentMismatch::FlagOverride(name));
396 if(input_value != trueString) {
397 throw(ArgumentMismatch::FlagOverride(name));
403 if((input_value.empty()) || (input_value == emptyString)) {
414 auto val = detail::to_flag_value(input_value);
419 return (val == 1) ? falseString : (val == (-1) ? trueString : std::to_string(-val));
425 _add_result(std::move(s),
results_);
431 results_added = _add_result(std::move(s),
results_);
439 _add_result(std::move(str),
results_);
449 _validate_results(res);
453 _reduce_results(extra, res);
455 res = std::move(extra);
463 if(option_type_size < 0) {
482 if(option_type_size_min < 0 || option_type_size_max < 0) {
485 option_type_size_min = (std::abs)(option_type_size_min);
486 option_type_size_max = (std::abs)(option_type_size_max);
489 if(option_type_size_min > option_type_size_max) {
511 full_type_name +=
":" + vtype;
515 return full_type_name;
518CLI11_INLINE
void Option::_validate_results(results_t &res)
const {
530 for(std::string &result : res) {
535 auto err_msg = _validate(result, (index >= 0) ? (index %
type_size_max_) : index);
537 throw ValidationError(
get_name(), err_msg);
548 for(std::string &result : res) {
549 auto err_msg = _validate(result, index);
552 throw ValidationError(
get_name(), err_msg);
558CLI11_INLINE
void Option::_reduce_results(results_t &out,
const results_t &original)
const {
566 case MultiOptionPolicy::TakeAll:
568 case MultiOptionPolicy::TakeLast: {
570 std::size_t trim_size = std::min<std::size_t>(
572 if(original.size() != trim_size) {
573 out.assign(original.end() -
static_cast<results_t::difference_type
>(trim_size), original.end());
576 case MultiOptionPolicy::Reverse: {
578 std::size_t trim_size = std::min<std::size_t>(
580 if(original.size() != trim_size || trim_size > 1) {
581 out.assign(original.end() -
static_cast<results_t::difference_type
>(trim_size), original.end());
583 std::reverse(out.begin(), out.end());
585 case MultiOptionPolicy::TakeFirst: {
586 std::size_t trim_size = std::min<std::size_t>(
588 if(original.size() != trim_size) {
589 out.assign(original.begin(), original.begin() +
static_cast<results_t::difference_type
>(trim_size));
592 case MultiOptionPolicy::Join:
594 out.push_back(detail::join(original, std::string(1, (
delimiter_ ==
'\0') ?
'\n' :
delimiter_)));
597 case MultiOptionPolicy::Sum:
598 out.push_back(detail::sum_string_vector(original));
600 case MultiOptionPolicy::Throw:
610 if(original.size() < num_min) {
611 throw ArgumentMismatch::AtLeast(
get_name(),
static_cast<int>(num_min), original.size());
613 if(original.size() > num_max) {
614 if(original.size() == 2 && num_max == 1 && original[1] ==
"%%" && original[0] ==
"{}") {
618 throw ArgumentMismatch::AtMost(
get_name(),
static_cast<int>(num_max), original.size());
628 out.emplace_back(
"{}");
629 out.emplace_back(
"%%");
632 out.emplace_back(
"%%");
636CLI11_INLINE std::string Option::_validate(std::string &result,
int index)
const {
643 auto v = vali.get_application_index();
644 if(v == -1 || v == index) {
646 err_msg = vali(result);
647 }
catch(
const ValidationError &err) {
648 err_msg = err.what();
658CLI11_INLINE
int Option::_add_result(std::string &&result, std::vector<std::string> &res)
const {
659 int result_count = 0;
661 result.back() ==
']') {
664 for(
auto &var : CLI::detail::split(result.substr(1),
',')) {
666 result_count += _add_result(std::move(var), res);
672 res.push_back(std::move(result));
675 if((result.find_first_of(
delimiter_) != std::string::npos)) {
676 for(
const auto &var : CLI::detail::split(result,
delimiter_)) {
683 res.push_back(std::move(result));
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:205
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition Error.hpp:96
Thrown when an option already exists.
Definition Error.hpp:144
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:81
bool ignore_case_
Ignore the case when matching (option, not value)
Definition Option.hpp:63
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:72
bool required_
True if this is a required option.
Definition Option.hpp:60
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:75
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition Option.hpp:66
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
Definition Option.hpp:231
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:462
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:36
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:265
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:349
std::function< std::string()> type_name_
Definition Option.hpp:273
@ reduced
a subset of results has been generated
@ callback_run
the callback has been executed
@ validated
the results have been validated
@ parsing
The option is currently collecting parsed results.
option_state current_option_state_
Whether the callback has run (needed for INI parsing)
Definition Option.hpp:328
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:286
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:444
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:505
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:646
std::string pname_
A positional name.
Definition Option.hpp:252
int expected_min_
The minimum number of expected values.
Definition Option.hpp:289
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:179
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 bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:636
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:332
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
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:572
CLI11_NODISCARD const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
Definition Option_inl.hpp:312
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:641
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:575
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:239
results_t proc_results_
results after reduction
Definition Option.hpp:319
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition Option_inl.hpp:169
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:220
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:154
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:307
int expected_max_
The maximum number of expected values.
Definition Option.hpp:291
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:378
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:580
Validator * get_validator(const std::string &Validator_name="")
Get a named Validator.
Definition Option_inl.hpp:124
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:336
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
Definition Option_inl.hpp:76
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:310
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:360
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:255
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:199
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:246
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:294
int type_size_max_
Definition Option.hpp:284
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:330
Option * transform(Validator Validator, const std::string &Validator_name="")
Adds a transforming Validator with a built in type name.
Definition Option_inl.hpp:92
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:242
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:424
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition Option_inl.hpp:144
Option * each(const std::function< void(std::string)> &func)
Adds a user supplied function to run on each item passed in (communicate though lambda capture)
Definition Option_inl.hpp:114
results_t results_
complete Results of parsing
Definition Option.hpp:317
Thrown when counting a non-existent option.
Definition Error.hpp:351
Some validators that are provided.
Definition Validators.hpp:55
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition Validators.hpp:108
CLI11_NODISCARD const std::string & get_name() const
Get the name of the Validator.
Definition Validators.hpp:126