12#include "../Option.hpp"
27 other->required(required_);
28 other->ignore_case(ignore_case_);
29 other->ignore_underscore(ignore_underscore_);
30 other->configurable(configurable_);
31 other->disable_flag_override(disable_flag_override_);
32 other->delimiter(delimiter_);
33 other->always_capture_default(always_capture_default_);
34 other->multi_option_policy(multi_option_policy_);
45 }
else if(value == detail::expected_max_vector_size) {
60 value_min = -value_min;
64 value_max = detail::expected_max_vector_size;
66 if(value_max < value_min) {
78 validator->non_modifying();
86 auto vp = std::make_shared<Validator>(std::move(validator));
87 if(!validator_name.empty()) {
88 vp->name(validator_name);
96 std::string validator_description,
97 std::string validator_name) {
99 auto vp = std::make_shared<Validator>(
100 std::move(validator_func), std::move(validator_description), std::move(validator_name));
113 auto vp = std::make_shared<Validator>(std::move(validator));
114 if(!transform_name.empty()) {
115 vp->name(transform_name);
122 std::string transform_description,
123 std::string transform_name) {
124 auto vp = std::make_shared<Validator>(
125 [transform_func](std::string &val) {
126 val = transform_func(val);
127 return std::string{};
129 std::move(transform_description),
130 std::move(transform_name));
137 auto vp = std::make_shared<Validator>(
138 [func](std::string &inout) {
140 return std::string{};
149 if(validator_name == validator->
get_name()) {
150 return validator.get();
153 if((validator_name.empty()) && (!
validators_.empty())) {
156 throw OptionNotFound(std::string{
"Validator "} + validator_name +
" Not Found");
161 if(index >= 0 && index <
static_cast<int>(
validators_.size())) {
168 auto iterator = std::find(std::begin(
needs_), std::end(
needs_), opt);
170 if(iterator == std::end(
needs_)) {
205 auto *parent =
static_cast<T *
>(
parent_);
206 for(
const Option_p &opt : parent->options_) {
207 if(opt.get() ==
this) {
210 const auto &omatch = opt->matching_name(*
this);
211 if(!omatch.empty()) {
226 auto *parent =
static_cast<T *
>(
parent_);
227 for(
const Option_p &opt : parent->options_) {
228 if(opt.get() ==
this) {
231 const auto &omatch = opt->matching_name(*
this);
232 if(!omatch.empty()) {
234 throw OptionAlreadyAdded(
"adding ignore underscore caused a name conflict with " + omatch);
256CLI11_NODISCARD CLI11_INLINE std::string
Option::get_name(
bool positional,
bool all_options)
const {
262 std::vector<std::string> name_list;
266 name_list.push_back(
pname_);
269 for(
const std::string &sname :
snames_) {
270 name_list.push_back(
"-" + sname);
276 for(
const std::string &lname :
lnames_) {
277 name_list.push_back(
"--" + lname);
283 for(
const std::string &sname :
snames_)
284 name_list.push_back(
"-" + sname);
286 for(
const std::string &lname :
lnames_)
287 name_list.push_back(
"--" + lname);
290 return detail::join(name_list);
299 return std::string(2,
'-') +
lnames_[0];
303 return std::string(1,
'-') +
snames_[0];
310 bool used_default_str =
false;
312 used_default_str =
true;
327 bool local_result =
callback_(send_results);
328 if(used_default_str) {
340 static const std::string estring;
342 for(
const std::string &sname :
snames_) {
348 for(
const std::string &lname :
lnames_) {
351 if(lname.size() == 1 && bothConfigurable) {
361 if(bothConfigurable && other.
snames_.empty() && other.
fnames_.empty() && !other.
pname_.empty()) {
367 for(
const std::string &sname : other.
snames_)
370 for(
const std::string &lname : other.
lnames_)
379 if(name.length() > 2 && name[0] ==
'-' && name[1] ==
'-')
381 if(name.length() > 1 && name.front() ==
'-')
384 std::string local_pname =
pname_;
385 std::string local_name = name;
387 local_pname = detail::remove_underscore(local_pname);
388 local_name = detail::remove_underscore(local_name);
391 local_pname = detail::to_lower(local_pname);
392 local_name = detail::to_lower(local_name);
394 if(local_name == local_pname) {
407 std::string input_value)
const {
408 static const std::string trueString{
"true"};
409 static const std::string falseString{
"false"};
410 static const std::string emptyString{
"{}"};
413 if(!((input_value.empty()) || (input_value == emptyString))) {
415 if(default_ind >= 0) {
421 throw(ArgumentMismatch::FlagOverride(name));
424 if(input_value != trueString) {
425 throw(ArgumentMismatch::FlagOverride(name));
431 if((input_value.empty()) || (input_value == emptyString)) {
442 auto val = detail::to_flag_value(input_value);
447 return (val == 1) ? falseString : (val == (-1) ? trueString : std::to_string(-val));
453 _add_result(std::move(s),
results_);
459 results_added = _add_result(std::move(s),
results_);
467 _add_result(std::move(str),
results_);
477 _validate_results(res);
481 _reduce_results(extra, res);
483 res = std::move(extra);
491 if(option_type_size < 0) {
510 if(option_type_size_min < 0 || option_type_size_max < 0) {
513 option_type_size_min = (std::abs)(option_type_size_min);
514 option_type_size_max = (std::abs)(option_type_size_max);
517 if(option_type_size_min > option_type_size_max) {
539 full_type_name +=
":" + vtype;
543 return full_type_name;
546CLI11_INLINE
void Option::_validate_results(results_t &res)
const {
558 for(std::string &result : res) {
563 auto err_msg = _validate(result, (index >= 0) ? (index %
type_size_max_) : index);
565 throw ValidationError(
get_name(), err_msg);
576 for(std::string &result : res) {
577 auto err_msg = _validate(result, index);
580 throw ValidationError(
get_name(), err_msg);
586CLI11_INLINE
void Option::_reduce_results(results_t &out,
const results_t &original)
const {
594 case MultiOptionPolicy::TakeAll:
596 case MultiOptionPolicy::TakeLast: {
598 std::size_t trim_size = std::min<std::size_t>(
600 if(original.size() != trim_size) {
601 out.assign(original.end() -
static_cast<results_t::difference_type
>(trim_size), original.end());
604 case MultiOptionPolicy::Reverse: {
606 std::size_t trim_size = std::min<std::size_t>(
608 if(original.size() != trim_size || trim_size > 1) {
609 out.assign(original.end() -
static_cast<results_t::difference_type
>(trim_size), original.end());
611 std::reverse(out.begin(), out.end());
613 case MultiOptionPolicy::TakeFirst: {
614 std::size_t trim_size = std::min<std::size_t>(
616 if(original.size() != trim_size) {
617 out.assign(original.begin(), original.begin() +
static_cast<results_t::difference_type
>(trim_size));
620 case MultiOptionPolicy::Join:
622 out.push_back(detail::join(original, std::string(1, (
delimiter_ ==
'\0') ?
'\n' :
delimiter_)));
625 case MultiOptionPolicy::Sum:
626 out.push_back(detail::sum_string_vector(original));
628 case MultiOptionPolicy::Throw:
638 if(original.size() < num_min) {
639 throw ArgumentMismatch::AtLeast(
get_name(),
static_cast<int>(num_min), original.size());
641 if(original.size() > num_max) {
642 if(original.size() == 2 && num_max == 1 && original[1] ==
"%%" && original[0] ==
"{}") {
646 throw ArgumentMismatch::AtMost(
get_name(),
static_cast<int>(num_max), original.size());
656 out.emplace_back(
"{}");
657 out.emplace_back(
"%%");
660 out.emplace_back(
"%%");
664CLI11_INLINE std::string Option::_validate(std::string &result,
int index)
const {
671 auto v = vali->get_application_index();
672 if(v == -1 || v == index) {
674 err_msg = (*vali)(result);
675 }
catch(
const ValidationError &err) {
676 err_msg = err.what();
686CLI11_INLINE
int Option::_add_result(std::string &&result, std::vector<std::string> &res)
const {
687 int result_count = 0;
691 if(result.size() >= 4 && result[0] ==
'[' && result[1] ==
'[' && result.back() ==
']' &&
692 (*(result.end() - 2) ==
']')) {
694 std::string nstrs{
'['};
695 bool duplicated{
true};
696 for(std::size_t ii = 2; ii < result.size() - 2; ii += 2) {
697 if(result[ii] == result[ii + 1]) {
698 nstrs.push_back(result[ii]);
705 nstrs.push_back(
']');
706 res.push_back(std::move(nstrs));
713 result.front() ==
'[' &&
714 result.back() ==
']') {
717 result.erase(result.begin());
718 bool skipSection{
false};
719 for(
auto &var : CLI::detail::split_up(result,
',')) {
721 result_count += _add_result(std::move(var), res);
729 res.push_back(std::move(result));
732 if((result.find_first_of(
delimiter_) != std::string::npos)) {
733 for(
const auto &var : CLI::detail::split(result,
delimiter_)) {
740 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_
Definition Option.hpp:85
bool ignore_case_
Definition Option.hpp:67
bool configurable_
Definition Option.hpp:73
bool disable_flag_override_
Definition Option.hpp:76
bool required_
Definition Option.hpp:64
char delimiter_
Definition Option.hpp:79
bool ignore_underscore_
Definition Option.hpp:70
CLI11_NODISCARD const std::string & get_group() const
Definition Option.hpp:119
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition Option_inl.hpp:25
Definition Option.hpp:235
Option * type_size(int option_type_size)
Set a custom option size.
Definition Option_inl.hpp:490
Option * expected(int value)
Set the number of expected arguments.
Definition Option_inl.hpp:37
Option * check(Validator_p validator)
Adds a shared validator.
Definition Option_inl.hpp:77
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:270
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:377
std::function< std::string()> type_name_
Definition Option.hpp:278
@ 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:333
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:291
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
Definition Option_inl.hpp:472
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
Definition Option_inl.hpp:533
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:661
std::string pname_
A positional name.
Definition Option.hpp:257
int expected_min_
The minimum number of expected values.
Definition Option.hpp:294
Option * transform(Validator_p validator)
Adds a shared Validator.
Definition Option_inl.hpp:106
Option * ignore_case(bool value=true)
Definition Option_inl.hpp:202
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:302
void run_callback()
Process the callback.
Definition Option_inl.hpp:309
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:651
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:337
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:256
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:305
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:345
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:254
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:587
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:339
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:656
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:590
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:244
results_t proc_results_
results after reduction
Definition Option.hpp:324
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:192
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition Option_inl.hpp:243
std::vector< Validator_p > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:299
Option * excludes(Option *opt)
Sets excluded options.
Definition Option_inl.hpp:177
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:312
int expected_max_
The maximum number of expected values.
Definition Option.hpp:296
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
Definition Option_inl.hpp:406
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:595
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:341
Validator * get_validator(const std::string &validator_name="")
Get a named Validator.
Definition Option_inl.hpp:147
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:315
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:369
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:584
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:532
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:260
Option * ignore_underscore(bool value=true)
Definition Option_inl.hpp:222
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:251
int type_size_max_
Definition Option.hpp:289
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:335
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:247
Option * add_result(std::string s)
Puts a result at the end.
Definition Option_inl.hpp:452
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:167
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:136
results_t results_
complete Results of parsing
Definition Option.hpp:322
Thrown when counting a nonexistent option.
Definition Error.hpp:351
Some validators that are provided.
Definition Validators.hpp:54
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition Validators.hpp:139
CLI11_NODISCARD std::string get_description() const
Generate type description information for the Validator.
Definition Validators.hpp:107
CLI11_NODISCARD const std::string & get_name() const
Get the name of the Validator.
Definition Validators.hpp:125