Go to the documentation of this file.
50 std::string
group_ = std::string(
"Options");
77 template <
typename T>
void copy_to(T *other)
const {
93 CRTP *
group(
const std::string &name) {
95 return static_cast<CRTP *
>(
this);
101 return static_cast<CRTP *
>(
this);
109 return static_cast<CRTP *
>(
this);
145 auto self =
static_cast<CRTP *
>(
this);
152 auto self =
static_cast<CRTP *
>(
this);
159 auto self =
static_cast<CRTP *
>(
this);
166 auto self =
static_cast<CRTP *
>(
this);
173 auto self =
static_cast<CRTP *
>(
this);
182 return static_cast<CRTP *
>(
this);
188 return static_cast<CRTP *
>(
this);
270 std::function<std::string()>
type_name_{[]() {
return std::string(); }};
354 explicit operator bool()
const {
return !
empty(); }
391 value_min = -value_min;
397 if(value_max < value_min) {
429 if(!validator_name.empty())
436 std::string Validator_description =
"",
437 std::string Validator_name =
"") {
438 validators_.emplace_back(
Validator, std::move(Validator_description), std::move(Validator_name));
446 if(!Validator_name.empty())
453 std::string transform_description =
"",
454 std::string transform_name =
"") {
457 [func](std::string &val) {
459 return std::string{};
461 std::move(transform_description),
462 std::move(transform_name)));
470 [func](std::string &inout) {
472 return std::string{};
484 if((Validator_name.empty()) && (!
validators_.empty())) {
487 throw OptionNotFound(std::string{
"Validator "} + Validator_name +
" Not Found");
493 if(index >= 0 && index <
static_cast<int>(
validators_.size())) {
509 auto opt =
static_cast<T *
>(
parent_)->get_option_no_throw(opt_name);
511 throw IncorrectConstruction::MissingOption(opt_name);
517 template <
typename A,
typename B,
typename... ARG>
Option *
needs(A opt, B opt1, ARG... args) {
519 return needs(opt1, args...);
524 auto iterator = std::find(std::begin(
needs_), std::end(
needs_), opt);
526 if(iterator == std::end(
needs_)) {
551 auto opt =
static_cast<T *
>(
parent_)->get_option_no_throw(opt_name);
553 throw IncorrectConstruction::MissingOption(opt_name);
559 template <
typename A,
typename B,
typename... ARG>
Option *
excludes(A opt, B opt1, ARG... args) {
588 auto *parent =
static_cast<T *
>(
parent_);
589 for(
const Option_p &opt : parent->options_) {
590 if(opt.get() ==
this) {
593 auto &omatch = opt->matching_name(*
this);
594 if(!omatch.empty()) {
613 auto *parent =
static_cast<T *
>(
parent_);
614 for(
const Option_p &opt : parent->options_) {
615 if(opt.get() ==
this) {
618 auto &omatch = opt->matching_name(*
this);
619 if(!omatch.empty()) {
621 throw OptionAlreadyAdded(
"adding ignore underscore caused a name conflict with " + omatch);
731 bool all_options =
false
738 std::vector<std::string> name_list;
742 name_list.push_back(
pname_);
745 for(
const std::string &sname :
snames_) {
746 name_list.push_back(
"-" + sname);
752 for(
const std::string &lname :
lnames_) {
753 name_list.push_back(
"--" + lname);
759 for(
const std::string &sname :
snames_)
760 name_list.push_back(
"-" + sname);
762 for(
const std::string &lname :
lnames_)
763 name_list.push_back(
"--" + lname);
775 return std::string(2,
'-') +
lnames_[0];
779 return std::string(1,
'-') +
snames_[0];
807 bool local_result =
callback_(send_results);
816 static const std::string estring;
817 for(
const std::string &sname :
snames_)
818 if(other.check_sname(sname))
820 for(
const std::string &lname :
lnames_)
821 if(other.check_lname(lname))
826 for(
const std::string &sname : other.snames_)
829 for(
const std::string &lname : other.lnames_)
841 if(name.length() > 2 && name[0] ==
'-' && name[1] ==
'-')
843 if(name.length() > 1 && name.front() ==
'-')
846 std::string local_pname =
pname_;
855 return name == local_pname;
878 std::string
get_flag_value(
const std::string &name, std::string input_value)
const {
879 static const std::string trueString{
"true"};
880 static const std::string falseString{
"false"};
881 static const std::string emptyString{
"{}"};
884 if(!((input_value.empty()) || (input_value == emptyString))) {
886 if(default_ind >= 0) {
889 throw(ArgumentMismatch::FlagOverride(name));
892 if(input_value != trueString) {
893 throw(ArgumentMismatch::FlagOverride(name));
899 if((input_value.empty()) || (input_value == emptyString)) {
912 return (val == 1) ? falseString : (val == (-1) ? trueString :
std::to_string(-val));
913 }
catch(
const std::invalid_argument &) {
923 _add_result(std::move(s),
results_);
930 results_added = _add_result(std::move(s),
results_);
938 _add_result(std::move(str),
results_);
953 _validate_results(res);
957 _reduce_results(extra, res);
959 res = std::move(extra);
972 retval = detail::lexical_conversion<T, T>(res, output);
979 _validate_results(res);
981 _reduce_results(extra, res);
983 res = std::move(extra);
991 retval = detail::lexical_conversion<T, T>(res, output);
999 template <
typename T> T
as()
const {
1026 if(option_type_size < 0) {
1043 if(option_type_size_min < 0 || option_type_size_max < 0) {
1046 option_type_size_min = (std::abs)(option_type_size_min);
1047 option_type_size_max = (std::abs)(option_type_size_max);
1050 if(option_type_size_min > option_type_size_max) {
1116 if(!vtype.empty()) {
1117 full_type_name +=
":" + vtype;
1121 return full_type_name;
1126 void _validate_results(
results_t &res)
const {
1137 for(std::string &result : res) {
1142 auto err_msg = _validate(result, (index >= 0) ? (index %
type_size_max_) : index);
1143 if(!err_msg.empty())
1154 for(std::string &result : res) {
1155 auto err_msg = _validate(result, index);
1157 if(!err_msg.empty())
1179 std::size_t trim_size = std::min<std::size_t>(
1181 if(original.size() != trim_size) {
1182 res.assign(original.end() -
static_cast<results_t::difference_type
>(trim_size), original.end());
1186 std::size_t trim_size = std::min<std::size_t>(
1188 if(original.size() != trim_size) {
1189 res.assign(original.begin(), original.begin() +
static_cast<results_t::difference_type
>(trim_size));
1207 if(original.size() < num_min) {
1208 throw ArgumentMismatch::AtLeast(
get_name(),
static_cast<int>(num_min), original.size());
1210 if(original.size() > num_max) {
1211 throw ArgumentMismatch::AtMost(
get_name(),
static_cast<int>(num_max), original.size());
1219 std::string _validate(std::string &result,
int index)
const {
1220 std::string err_msg;
1226 auto v = vali.get_application_index();
1227 if(v == -1 || v == index) {
1229 err_msg = vali(result);
1230 }
catch(
const ValidationError &err) {
1231 err_msg = err.what();
1233 if(!err_msg.empty())
1242 int _add_result(std::string &&result, std::vector<std::string> &res)
const {
1243 int result_count = 0;
1245 result.back() ==
']') {
1250 result_count += _add_result(std::move(var), res);
1253 return result_count;
1256 res.push_back(std::move(result));
1259 if((result.find_first_of(
delimiter_) != std::string::npos)) {
1267 res.push_back(std::move(result));
1271 return result_count;
bool get_ignore_case() const
The status of ignore case.
Definition: Option.hpp:121
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional)
Definition: Option.hpp:836
Option * transform(Validator Validator, const std::string &Validator_name="")
Adds a transforming Validator with a built in type name.
Definition: Option.hpp:444
bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition: Option.hpp:136
CRTP * group(const std::string &name)
Changes the group membership.
Definition: Option.hpp:93
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition: Option.hpp:265
results_t reduced_results() const
Get a copy of the results.
Definition: Option.hpp:948
bool check_name(std::string name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
Definition: Option.hpp:839
Option * ignore_case(bool value=true)
Definition: Option.hpp:585
std::string get_description() const
Generate type description information for the Validator.
Definition: Validators.hpp:138
std::string get_envname() const
The environment variable associated to this value.
Definition: Option.hpp:662
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition: Option.hpp:645
std::string get_default_str() const
The default value (for help printing)
Definition: Option.hpp:671
bool get_required() const
True if this is a required option.
Definition: Option.hpp:118
bool get_disable_flag_override() const
The status of configurable.
Definition: Option.hpp:130
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition: Option.hpp:158
enabler
Simple empty scoped class.
Definition: TypeTools.hpp:26
char delimiter_
Specify a delimiter character for vector arguments.
Definition: Option.hpp:68
constexpr int expected_max_vector_size
Definition: StringTools.hpp:39
std::set< Option * > get_needs() const
The set of options needed.
Definition: Option.hpp:665
Option * add_result(std::vector< std::string > s)
Puts a result at the end.
Definition: Option.hpp:936
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition: Option.hpp:1064
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition: Option.hpp:172
std::string pname_
A positional name.
Definition: Option.hpp:252
Some validators that are provided.
Definition: Validators.hpp:74
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition: Option.hpp:213
const std::vector< std::string > & get_lnames() const
Get the long names.
Definition: Option.hpp:677
@ validated
the results have been validated
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition: Option.hpp:246
std::vector< std::string > split(const std::string &s, char delim)
Split a string by a delim.
Definition: StringTools.hpp:42
int expected_min_
The minimum number of expected values.
Definition: Option.hpp:286
Option * transform(const std::function< std::string(std::string)> &func, std::string transform_description="", std::string transform_name="")
Adds a Validator-like function that can change result.
Definition: Option.hpp:452
Option * description(std::string option_description)
Set the description.
Definition: Option.hpp:717
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition: Option.hpp:329
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition: Option.hpp:249
int get_type_size_max() const
The maximum number of arguments the option expects.
Definition: Option.hpp:659
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition: Option.hpp:239
results_t results() const
Get a copy of the results.
Definition: Option.hpp:945
int get_type_size_min() const
The minimum number of arguments the option expects.
Definition: Option.hpp:657
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.hpp:468
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition: Option.hpp:74
bool get_positional() const
True if the argument can be given directly.
Definition: Option.hpp:705
bool get_configurable() const
The status of configurable.
Definition: Option.hpp:127
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition: Option.hpp:331
int get_items_expected_max() const
Get the maximum number of items expected to be returned and used for the callback.
Definition: Option.hpp:697
@ TakeLast
take only the last Expected number of arguments
std::size_t count() const
Count the total number of times an option was passed.
Definition: Option.hpp:348
@ reduced
a subset of results has been generated
results_t results_
complete Results of parsing
Definition: Option.hpp:314
App * parent_
Remember the parent app.
Definition: Option.hpp:304
std::set< Option * > needs_
A list of options that are required with this option.
Definition: Option.hpp:294
Option * type_size(int option_type_size)
Set a custom option size.
Definition: Option.hpp:1025
std::string get_name(bool positional=false, bool all_options=false) const
Gets a comma separated list of names. Will include / prefer the positional name if positional is true...
Definition: Option.hpp:730
Option * expected(int value)
Set the number of expected arguments.
Definition: Option.hpp:367
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
bool has_description() const
True if option has description.
Definition: Option.hpp:711
int get_expected_max() const
The max number of times the option expects to be included.
Definition: Option.hpp:691
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition: Error.hpp:186
std::string join(const T &v, std::string delim=",")
Simple function to join a string.
Definition: StringTools.hpp:59
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition: Option.hpp:283
bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition: Option.hpp:869
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition: Option.hpp:576
CRTP * take_first()
Set the multi option policy to take last.
Definition: Option.hpp:151
const std::string & get_name() const
Get the name of the Validator.
Definition: Validators.hpp:156
@ parsing
The option is currently collecting parsed results.
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition: Option.hpp:297
Option * excludes(Option *opt)
Sets excluded options.
Definition: Option.hpp:534
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition: Option.hpp:35
bool always_capture_default_
Automatically capture default value.
Definition: Option.hpp:71
callback_t callback_
Options store a callback to do all the work.
Definition: Option.hpp:307
auto to_string(T &&value) -> decltype(std::forward< T >(value))
Convert an object to a string (directly forward if this can become a string)
Definition: TypeTools.hpp:226
std::string envname_
If given, check the environment for this option.
Definition: Option.hpp:255
Validator * get_validator(const std::string &Validator_name="")
Get a named Validator.
Definition: Option.hpp:478
@ TakeAll
just get all the passed argument regardless
bool nonpositional() const
True if option has at least one non-positional name.
Definition: Option.hpp:708
Option * needs(Option *opt)
Sets required options.
Definition: Option.hpp:500
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase)
Definition: Option.hpp:77
constexpr enabler dummy
An instance to use in EnableIf.
Definition: TypeTools.hpp:29
Option * needs(std::string opt_name)
Can find a string if needed.
Definition: Option.hpp:508
std::function< std::string()> type_name_
Definition: Option.hpp:270
Option * add_result(std::string s, int &results_added)
Puts a result at the end and get a count of the number of arguments actually added.
Definition: Option.hpp:929
const std::vector< std::string > & get_snames() const
Get the short names.
Definition: Option.hpp:680
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition: Option.hpp:186
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
Definition: Option.hpp:426
std::ptrdiff_t find_member(std::string name, const std::vector< std::string > names, bool ignore_case=false, bool ignore_underscore=false)
Check if a string is a member of a list of strings and optionally ignore case or ignore underscores.
Definition: StringTools.hpp:244
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition: Option.hpp:517
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition: Option.hpp:327
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition: Option.hpp:565
Definition: Option.hpp:45
int expected_max_
The maximum number of expected values.
Definition: Option.hpp:288
All errors derive from this one.
Definition: Error.hpp:67
bool configurable_
Allow this option to be given in a configuration file.
Definition: Option.hpp:62
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition: Option.hpp:201
Thrown when an option already exists.
Definition: Error.hpp:132
Option * default_val(const X &val)
Definition: Option.hpp:1085
Option * type_size(int option_type_size_min, int option_type_size_max)
Set a custom option type size range.
Definition: Option.hpp:1042
std::string get_flag_value(const std::string &name, std::string input_value) const
Definition: Option.hpp:878
CRTP * join()
Set the multi option policy to join.
Definition: Option.hpp:165
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition: Option.hpp:1013
std::tuple< std::vector< std::string >, std::vector< std::string >, std::string > get_names(const std::vector< std::string > &input)
Get a vector of short names, one of long names, and a single name.
Definition: Split.hpp:103
std::function< bool(const results_t &)> callback_t
callback function definition
Definition: Option.hpp:28
Definition: Option.hpp:231
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition: StringTools.hpp:199
std::vector< Validator > validators_
A list of Validators to run on each value parsed.
Definition: Option.hpp:291
int type_size_max_
Definition: Option.hpp:281
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition: Option.hpp:550
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition: Option.hpp:1019
@ TakeFirst
take only the first Expected number of arguments
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy)
Definition: Option.hpp:631
@ Throw
Throw an error if any extra arguments were given.
int get_items_expected() const
The total min number of expected string values to be used.
Definition: Option.hpp:702
std::string remove_underscore(std::string str)
remove underscores from a string
Definition: StringTools.hpp:207
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition: Option.hpp:559
std::int64_t to_flag_value(std::string val)
Convert a flag into an integer value typically binary flags.
Definition: TypeTools.hpp:564
Option * run_callback_for_default(bool value=true)
Definition: Option.hpp:418
results_t proc_results_
results after reduction
Definition: Option.hpp:316
Option * expected(int value_min, int value_max)
Set the range of expected arguments.
Definition: Option.hpp:389
MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition: Option.hpp:139
std::string group_
The group membership.
Definition: Option.hpp:50
const std::string & get_description() const
Get the description.
Definition: Option.hpp:714
bool get_callback_run() const
See if the callback has been run already.
Definition: Option.hpp:1006
CRTP * take_last()
Set the multi option policy to take last.
Definition: Option.hpp:144
void results(T &output) const
Get the results as a specified type.
Definition: Option.hpp:968
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition: Option.hpp:207
std::vector< std::string > results_t
Definition: Option.hpp:26
Option(std::string option_name, std::string option_description, callback_t callback, App *parent)
Making an option by hand is not defined, it must be made by the App class.
Definition: Option.hpp:335
Creates a command line program, with very few defaults.
Definition: App.hpp:66
int get_type_size() const
The number of arguments the option expects.
Definition: Option.hpp:654
const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
Definition: Option.hpp:815
bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition: Option.hpp:859
bool required_
True if this is a required option.
Definition: Option.hpp:53
Option * capture_default_str()
Capture the default value from the original value (if it can be captured)
Definition: Option.hpp:1070
T as() const
Return the results as the specified type.
Definition: Option.hpp:999
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty)
Definition: Option.hpp:273
Definition: Option.hpp:194
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition: Option.hpp:65
const std::string & get_group() const
Get the group of this option.
Definition: Option.hpp:115
const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition: Option.hpp:683
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition: Option.hpp:180
Option * ignore_underscore(bool value=true)
Definition: Option.hpp:609
option_state
enumeration for the option state machine
Definition: Option.hpp:318
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value)
Definition: Option.hpp:1078
std::unique_ptr< Option > Option_p
Definition: Option.hpp:33
std::string get_type_name() const
Get the full typename for this option.
Definition: Option.hpp:1111
CRTP * always_capture_default(bool value=true)
Definition: Option.hpp:107
Validator * get_validator(int index)
Get a Validator by index NOTE: this may not be the order of definition.
Definition: Option.hpp:491
bool empty() const
True if the option was not passed.
Definition: Option.hpp:351
int get_items_expected_min() const
The total min number of expected string values to be used.
Definition: Option.hpp:694
int get_expected() const
The number of times the option expects to be included.
Definition: Option.hpp:686
Validator & non_modifying(bool no_modify=true)
Specify whether the Validator can be modifying or not.
Definition: Validators.hpp:170
std::set< Option * > get_excludes() const
The set of options excluded.
Definition: Option.hpp:668
std::enable_if< std::is_integral< T >::value, bool >::type checked_multiply(T &a, T b)
Performs a *= b; if it doesn't cause integer overflow. Returns false otherwise.
Definition: Validators.hpp:669
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition: Option.hpp:225
bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition: Option.hpp:414
@ callback_run
the callback has been executed
option_state current_option_state_
Whether the callback has run (needed for INI parsing)
Definition: Option.hpp:325
Option * check(std::function< std::string(const std::string &)> Validator, std::string Validator_description="", std::string Validator_name="")
Adds a Validator. Takes a const string& and returns an error message (empty if conversion/check is ok...
Definition: Option.hpp:435
char get_delimiter() const
Get the current delimiter char.
Definition: Option.hpp:133
std::string description_
The description for help strings.
Definition: Option.hpp:262
Thrown when counting a non-existent option.
Definition: Error.hpp:330
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition: Option.hpp:105
int get_expected_min() const
The number of times the option expects to be included.
Definition: Option.hpp:689
CRTP * required(bool value=true)
Set the option as required.
Definition: Option.hpp:99
void clear()
Clear the parsed results (mostly for testing)
Definition: Option.hpp:357
std::vector< std::string > split_names(std::string current)
Definition: Split.hpp:63
Option & operator=(const Option &)=delete
Option * add_result(std::string s)
Puts a result at the end.
Definition: Option.hpp:922
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition: Option.hpp:219
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition: Option.hpp:242
bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition: Option.hpp:864
bool ignore_underscore_
Ignore underscores when matching (option, not value)
Definition: Option.hpp:59
bool ignore_case_
Ignore the case when matching (option, not value)
Definition: Option.hpp:56
void run_callback()
Process the callback.
Definition: Option.hpp:790
bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition: Option.hpp:423
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
Definition: Option.hpp:523
callback_t get_callback() const
Get the callback function.
Definition: Option.hpp:674
Thrown when an option is set to conflicting values (non-vector and multi args, for example)
Definition: Error.hpp:90
Option * allow_extra_args(bool value=true)
Definition: Option.hpp:409
bool get_ignore_underscore() const
The status of ignore_underscore.
Definition: Option.hpp:124