Loading...
Searching...
No Matches
Option.hpp
Go to the documentation of this file.
// Copyright (c) 2017-2025, University of Cincinnati, developed by Henry Schreiner
// under NSF AWARD 1414736 and by the respective contributors.
// All rights reserved.
//
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
// IWYU pragma: private, include "CLI/CLI.hpp"
// [CLI11:public_includes:set]
#include <algorithm>
#include <functional>
#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
// [CLI11:public_includes:end]
#include "Error.hpp"
#include "Macros.hpp"
#include "Split.hpp"
#include "StringTools.hpp"
#include "Validators.hpp"
namespace CLI {
// [CLI11:option_hpp:verbatim]
using results_t = std::vector<std::string>;
using callback_t = std::function<bool(const results_t &)>;
class Option;
class App;
class ConfigBase;
using Option_p = std::unique_ptr<Option>;
using Validator_p = std::shared_ptr<Validator>;
enum class MultiOptionPolicy : char {
Throw,
TakeLast,
TakeFirst,
Join,
TakeAll,
Sum,
Reverse,
};
enum class CallbackPriority : std::uint8_t {
FirstPreHelp = 0,
First = 1,
PreRequirementsCheckPreHelp = 2,
PreRequirementsCheck = 3,
NormalPreHelp = 4,
Normal = 5,
LastPreHelp = 6,
Last = 7
}; // namespace CLI
template <typename CRTP> class OptionBase {
friend App;
friend ConfigBase;
protected:
std::string group_ = std::string("OPTIONS");
bool required_{false};
bool ignore_case_{false};
bool ignore_underscore_{false};
bool configurable_{true};
bool disable_flag_override_{false};
char delimiter_{'\0'};
bool always_capture_default_{false};
MultiOptionPolicy multi_option_policy_{MultiOptionPolicy::Throw};
CallbackPriority callback_priority_{CallbackPriority::Normal};
template <typename T> void copy_to(T *other) const;
public:
// setters
CRTP *group(const std::string &name) {
if(!detail::valid_alias_name_string(name)) {
throw IncorrectConstruction("Group names may not contain newlines or null characters");
}
group_ = name;
return static_cast<CRTP *>(this);
}
CRTP *required(bool value = true) {
required_ = value;
return static_cast<CRTP *>(this);
}
CRTP *mandatory(bool value = true) { return required(value); }
CRTP *always_capture_default(bool value = true) {
always_capture_default_ = value;
return static_cast<CRTP *>(this);
}
// Getters
CLI11_NODISCARD const std::string &get_group() const { return group_; }
CLI11_NODISCARD bool get_required() const { return required_; }
CLI11_NODISCARD bool get_ignore_case() const { return ignore_case_; }
CLI11_NODISCARD bool get_ignore_underscore() const { return ignore_underscore_; }
CLI11_NODISCARD bool get_configurable() const { return configurable_; }
CLI11_NODISCARD bool get_disable_flag_override() const { return disable_flag_override_; }
CLI11_NODISCARD char get_delimiter() const { return delimiter_; }
CLI11_NODISCARD bool get_always_capture_default() const { return always_capture_default_; }
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
CLI11_NODISCARD CallbackPriority get_callback_priority() const { return callback_priority_; }
// Shortcuts for multi option policy
CRTP *take_last() {
auto *self = static_cast<CRTP *>(this);
self->multi_option_policy(MultiOptionPolicy::TakeLast);
return self;
}
CRTP *take_first() {
auto *self = static_cast<CRTP *>(this);
self->multi_option_policy(MultiOptionPolicy::TakeFirst);
return self;
}
CRTP *take_all() {
auto self = static_cast<CRTP *>(this);
self->multi_option_policy(MultiOptionPolicy::TakeAll);
return self;
}
CRTP *join() {
auto *self = static_cast<CRTP *>(this);
self->multi_option_policy(MultiOptionPolicy::Join);
return self;
}
CRTP *join(char delim) {
auto self = static_cast<CRTP *>(this);
self->delimiter_ = delim;
self->multi_option_policy(MultiOptionPolicy::Join);
return self;
}
CRTP *configurable(bool value = true) {
configurable_ = value;
return static_cast<CRTP *>(this);
}
CRTP *delimiter(char value = '\0') {
delimiter_ = value;
return static_cast<CRTP *>(this);
}
};
class OptionDefaults : public OptionBase<OptionDefaults> {
public:
OptionDefaults() = default;
// Methods here need a different implementation if they are Option vs. OptionDefault
OptionDefaults *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
callback_priority_ = value;
return this;
}
OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
multi_option_policy_ = value;
return this;
}
OptionDefaults *ignore_case(bool value = true) {
ignore_case_ = value;
return this;
}
OptionDefaults *ignore_underscore(bool value = true) {
ignore_underscore_ = value;
return this;
}
OptionDefaults *disable_flag_override(bool value = true) {
disable_flag_override_ = value;
return this;
}
OptionDefaults *delimiter(char value = '\0') {
delimiter_ = value;
return this;
}
};
class Option : public OptionBase<Option> {
friend App;
friend ConfigBase;
protected:
std::vector<std::string> snames_{};
std::vector<std::string> lnames_{};
std::vector<std::pair<std::string, std::string>> default_flag_values_{};
std::vector<std::string> fnames_{};
std::string pname_{};
std::string envname_{};
std::string description_{};
std::string default_str_{};
std::string option_text_{};
std::function<std::string()> type_name_{[]() { return std::string(); }};
std::function<std::string()> default_function_{};
int type_size_max_{1};
int type_size_min_{1};
int expected_min_{1};
int expected_max_{1};
std::vector<Validator_p> validators_{};
std::set<Option *> needs_{};
std::set<Option *> excludes_{};
App *parent_{nullptr};
callback_t callback_{};
results_t results_{};
mutable results_t proc_results_{};
enum class option_state : char {
parsing = 0,
validated = 2,
reduced = 4,
callback_run = 6,
};
option_state current_option_state_{option_state::parsing};
bool allow_extra_args_{false};
bool flag_like_{false};
bool run_callback_for_default_{false};
bool inject_separator_{false};
bool trigger_on_result_{false};
bool force_callback_{false};
Option(std::string option_name,
std::string option_description,
callback_t callback,
App *parent,
bool allow_non_standard = false)
: description_(std::move(option_description)), parent_(parent), callback_(std::move(callback)) {
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(option_name), allow_non_standard);
}
public:
Option(const Option &) = delete;
Option &operator=(const Option &) = delete;
CLI11_NODISCARD std::size_t count() const { return results_.size(); }
CLI11_NODISCARD bool empty() const { return results_.empty(); }
explicit operator bool() const { return !empty() || force_callback_; }
void clear() {
results_.clear();
current_option_state_ = option_state::parsing;
}
Option *expected(int value);
Option *expected(int value_min, int value_max);
Option *allow_extra_args(bool value = true) {
allow_extra_args_ = value;
return this;
}
CLI11_NODISCARD bool get_allow_extra_args() const { return allow_extra_args_; }
Option *trigger_on_parse(bool value = true) {
trigger_on_result_ = value;
return this;
}
CLI11_NODISCARD bool get_trigger_on_parse() const { return trigger_on_result_; }
Option *force_callback(bool value = true) {
force_callback_ = value;
return this;
}
CLI11_NODISCARD bool get_force_callback() const { return force_callback_; }
Option *run_callback_for_default(bool value = true) {
run_callback_for_default_ = value;
return this;
}
CLI11_NODISCARD bool get_run_callback_for_default() const { return run_callback_for_default_; }
Option *callback_priority(CallbackPriority value = CallbackPriority::Normal) {
callback_priority_ = value;
return this;
}
Option *check(Validator_p validator);
Option *check(Validator validator, const std::string &validator_name = "");
Option *check(std::function<std::string(const std::string &)> validator_func,
std::string validator_description = "",
std::string validator_name = "");
Option *transform(Validator_p validator);
Option *transform(Validator validator, const std::string &transform_name = "");
Option *transform(const std::function<std::string(std::string)> &transform_func,
std::string transform_description = "",
std::string transform_name = "");
Option *each(const std::function<void(std::string)> &func);
Validator *get_validator(const std::string &validator_name = "");
Validator *get_validator(int index);
Option *needs(Option *opt) {
if(opt != this) {
needs_.insert(opt);
}
return this;
}
template <typename T = App> Option *needs(std::string opt_name) {
auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
if(opt == nullptr) {
throw IncorrectConstruction::MissingOption(opt_name);
}
return needs(opt);
}
template <typename A, typename B, typename... ARG> Option *needs(A opt, B opt1, ARG... args) {
needs(opt);
return needs(opt1, args...); // NOLINT(readability-suspicious-call-argument)
}
bool remove_needs(Option *opt);
Option *excludes(Option *opt);
template <typename T = App> Option *excludes(std::string opt_name) {
auto opt = static_cast<T *>(parent_)->get_option_no_throw(opt_name);
if(opt == nullptr) {
throw IncorrectConstruction::MissingOption(opt_name);
}
return excludes(opt);
}
template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) {
excludes(opt);
return excludes(opt1, args...);
}
bool remove_excludes(Option *opt);
Option *envname(std::string name) {
envname_ = std::move(name);
return this;
}
template <typename T = App> Option *ignore_case(bool value = true);
template <typename T = App> Option *ignore_underscore(bool value = true);
Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw);
Option *disable_flag_override(bool value = true) {
disable_flag_override_ = value;
return this;
}
CLI11_NODISCARD int get_type_size() const { return type_size_min_; }
CLI11_NODISCARD int get_type_size_min() const { return type_size_min_; }
CLI11_NODISCARD int get_type_size_max() const { return type_size_max_; }
CLI11_NODISCARD bool get_inject_separator() const { return inject_separator_; }
CLI11_NODISCARD std::string get_envname() const { return envname_; }
CLI11_NODISCARD std::set<Option *> get_needs() const { return needs_; }
CLI11_NODISCARD std::set<Option *> get_excludes() const { return excludes_; }
CLI11_NODISCARD std::string get_default_str() const { return default_str_; }
CLI11_NODISCARD callback_t get_callback() const { return callback_; }
CLI11_NODISCARD const std::vector<std::string> &get_lnames() const { return lnames_; }
CLI11_NODISCARD const std::vector<std::string> &get_snames() const { return snames_; }
CLI11_NODISCARD const std::vector<std::string> &get_fnames() const { return fnames_; }
CLI11_NODISCARD const std::string &get_single_name() const {
if(!lnames_.empty()) {
return lnames_[0];
}
if(!snames_.empty()) {
return snames_[0];
}
if(!pname_.empty()) {
return pname_;
}
return envname_;
}
CLI11_NODISCARD int get_expected() const { return expected_min_; }
CLI11_NODISCARD int get_expected_min() const { return expected_min_; }
CLI11_NODISCARD int get_expected_max() const { return expected_max_; }
CLI11_NODISCARD int get_items_expected_min() const { return type_size_min_ * expected_min_; }
CLI11_NODISCARD int get_items_expected_max() const {
int t = type_size_max_;
return detail::checked_multiply(t, expected_max_) ? t : detail::expected_max_vector_size;
}
CLI11_NODISCARD int get_items_expected() const { return get_items_expected_min(); }
CLI11_NODISCARD bool get_positional() const { return !pname_.empty(); }
CLI11_NODISCARD bool nonpositional() const { return (!lnames_.empty() || !snames_.empty()); }
CLI11_NODISCARD bool has_description() const { return !description_.empty(); }
CLI11_NODISCARD const std::string &get_description() const { return description_; }
Option *description(std::string option_description) {
description_ = std::move(option_description);
return this;
}
Option *option_text(std::string text) {
option_text_ = std::move(text);
return this;
}
CLI11_NODISCARD const std::string &get_option_text() const { return option_text_; }
CLI11_NODISCARD std::string get_name(bool positional = false,
bool all_options = false
) const;
void run_callback();
CLI11_NODISCARD const std::string &matching_name(const Option &other) const;
bool operator==(const Option &other) const { return !matching_name(other).empty(); }
CLI11_NODISCARD bool check_name(const std::string &name) const;
CLI11_NODISCARD bool check_sname(std::string name) const {
return (detail::find_member(std::move(name), snames_, ignore_case_) >= 0);
}
CLI11_NODISCARD bool check_lname(std::string name) const {
return (detail::find_member(std::move(name), lnames_, ignore_case_, ignore_underscore_) >= 0);
}
CLI11_NODISCARD bool check_fname(std::string name) const {
if(fnames_.empty()) {
return false;
}
return (detail::find_member(std::move(name), fnames_, ignore_case_, ignore_underscore_) >= 0);
}
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const;
Option *add_result(std::string s);
Option *add_result(std::string s, int &results_added);
Option *add_result(std::vector<std::string> s);
CLI11_NODISCARD const results_t &results() const { return results_; }
CLI11_NODISCARD results_t reduced_results() const;
template <typename T> void results(T &output) const {
bool retval = false;
if(current_option_state_ >= option_state::reduced || (results_.size() == 1 && validators_.empty())) {
const results_t &res = (proc_results_.empty()) ? results_ : proc_results_;
if(!res.empty()) {
retval = detail::lexical_conversion<T, T>(res, output);
} else {
results_t res2;
res2.emplace_back();
proc_results_ = std::move(res2);
retval = detail::lexical_conversion<T, T>(proc_results_, output);
}
} else {
results_t res;
if(results_.empty()) {
if(!default_str_.empty()) {
// _add_results takes an rvalue only
_add_result(std::string(default_str_), res);
_validate_results(res);
results_t extra;
_reduce_results(extra, res);
if(!extra.empty()) {
res = std::move(extra);
}
} else {
res.emplace_back();
}
} else {
res = reduced_results();
}
// store the results in a stable location if the output is a view
proc_results_ = std::move(res);
retval = detail::lexical_conversion<T, T>(proc_results_, output);
}
if(!retval) {
throw ConversionError(get_name(), results_);
}
}
template <typename T> CLI11_NODISCARD T as() const {
T output;
results(output);
return output;
}
CLI11_NODISCARD bool get_callback_run() const { return (current_option_state_ == option_state::callback_run); }
Option *type_name_fn(std::function<std::string()> typefun) {
type_name_ = std::move(typefun);
return this;
}
Option *type_name(std::string typeval) {
type_name_fn([typeval]() { return typeval; });
return this;
}
Option *type_size(int option_type_size);
Option *type_size(int option_type_size_min, int option_type_size_max);
void inject_separator(bool value = true) { inject_separator_ = value; }
Option *default_function(const std::function<std::string()> &func) {
default_function_ = func;
return this;
}
Option *capture_default_str() {
if(default_function_) {
default_str_ = default_function_();
}
return this;
}
Option *default_str(std::string val) {
default_str_ = std::move(val);
return this;
}
template <typename X> Option *default_val(const X &val) {
std::string val_str = detail::to_string(val);
auto old_option_state = current_option_state_;
results_t old_results{std::move(results_)};
results_.clear();
try {
add_result(val_str);
// if trigger_on_result_ is set the callback already ran
if(run_callback_for_default_ && !trigger_on_result_) {
run_callback(); // run callback sets the state, we need to reset it again
current_option_state_ = option_state::parsing;
} else {
_validate_results(results_);
current_option_state_ = old_option_state;
}
} catch(const ConversionError &err) {
// this should be done
results_ = std::move(old_results);
current_option_state_ = old_option_state;
throw ConversionError(
get_name(), std::string("given default value(\"") + val_str + "\") produces an error : " + err.what());
} catch(const CLI::Error &) {
results_ = std::move(old_results);
current_option_state_ = old_option_state;
throw;
}
results_ = std::move(old_results);
default_str_ = std::move(val_str);
return this;
}
CLI11_NODISCARD std::string get_type_name() const;
private:
void _validate_results(results_t &res) const;
void _reduce_results(results_t &out, const results_t &original) const;
// Run a result through the Validators
std::string _validate(std::string &result, int index) const;
int _add_result(std::string &&result, std::vector<std::string> &res) const;
};
// [CLI11:option_hpp:end]
} // namespace CLI
#ifndef CLI11_COMPILE
#include "impl/Option_inl.hpp" // IWYU pragma: export
#endif
Error.hpp
Macros.hpp
CLI11_NODISCARD
#define CLI11_NODISCARD
Definition Macros.hpp:58
Split.hpp
StringTools.hpp
Validators.hpp
CLI::App
Creates a command line program, with very few defaults.
Definition App.hpp:98
CLI::ConfigBase
This converter works with INI/TOML files; to write INI files use ConfigINI.
Definition ConfigFwd.hpp:85
CLI::ConversionError
Thrown when conversion call back fails, such as when an int fails to coerce to a string.
Definition Error.hpp:205
CLI::Error
All errors derive from this one.
Definition Error.hpp:73
CLI::OptionBase
Definition Option.hpp:67
CLI::OptionBase::mandatory
CRTP * mandatory(bool value=true)
Support Plumbum term.
Definition Option.hpp:124
CLI::OptionBase::take_all
CRTP * take_all()
Set the multi option policy to take all arguments.
Definition Option.hpp:180
CLI::OptionBase::group
CRTP * group(const std::string &name)
Changes the group membership.
Definition Option.hpp:109
CLI::OptionBase::join
CRTP * join()
Set the multi option policy to join.
Definition Option.hpp:187
CLI::OptionBase::always_capture_default_
bool always_capture_default_
Automatically capture default value.
Definition Option.hpp:94
CLI::OptionBase::multi_option_policy_
MultiOptionPolicy multi_option_policy_
Policy for handling multiple arguments beyond the expected Max.
Definition Option.hpp:97
CLI::OptionBase::join
CRTP * join(char delim)
Set the multi option policy to join with a specific delimiter.
Definition Option.hpp:194
CLI::OptionBase::get_callback_priority
CLI11_NODISCARD CallbackPriority get_callback_priority() const
The priority of callback.
Definition Option.hpp:161
CLI::OptionBase::get_always_capture_default
CLI11_NODISCARD bool get_always_capture_default() const
Return true if this will automatically capture the default value for help printing.
Definition Option.hpp:155
CLI::OptionBase::get_delimiter
CLI11_NODISCARD char get_delimiter() const
Get the current delimiter char.
Definition Option.hpp:152
CLI::OptionBase::get_required
CLI11_NODISCARD bool get_required() const
True if this is a required option.
Definition Option.hpp:137
CLI::OptionBase::take_first
CRTP * take_first()
Set the multi option policy to take last.
Definition Option.hpp:173
CLI::OptionBase::get_ignore_case
CLI11_NODISCARD bool get_ignore_case() const
The status of ignore case.
Definition Option.hpp:140
CLI::OptionBase::ignore_case_
bool ignore_case_
Ignore the case when matching (option, not value).
Definition Option.hpp:79
CLI::OptionBase::configurable
CRTP * configurable(bool value=true)
Allow in a configuration file.
Definition Option.hpp:202
CLI::OptionBase::delimiter
CRTP * delimiter(char value='\0')
Allow in a configuration file.
Definition Option.hpp:208
CLI::OptionBase::get_multi_option_policy
CLI11_NODISCARD MultiOptionPolicy get_multi_option_policy() const
The status of the multi option policy.
Definition Option.hpp:158
CLI::OptionBase::get_configurable
CLI11_NODISCARD bool get_configurable() const
The status of configurable.
Definition Option.hpp:146
CLI::OptionBase::configurable_
bool configurable_
Allow this option to be given in a configuration file.
Definition Option.hpp:85
CLI::OptionBase::callback_priority_
CallbackPriority callback_priority_
Priority of callback.
Definition Option.hpp:100
CLI::OptionBase::disable_flag_override_
bool disable_flag_override_
Disable overriding flag values with '=value'.
Definition Option.hpp:88
CLI::OptionBase::required_
bool required_
True if this is a required option.
Definition Option.hpp:76
CLI::OptionBase::take_last
CRTP * take_last()
Set the multi option policy to take last.
Definition Option.hpp:166
CLI::OptionBase::delimiter_
char delimiter_
Specify a delimiter character for vector arguments.
Definition Option.hpp:91
CLI::OptionBase::always_capture_default
CRTP * always_capture_default(bool value=true)
Definition Option.hpp:126
CLI::OptionBase::group_
std::string group_
The group membership.
Definition Option.hpp:73
CLI::OptionBase::get_ignore_underscore
CLI11_NODISCARD bool get_ignore_underscore() const
The status of ignore_underscore.
Definition Option.hpp:143
CLI::OptionBase::ignore_underscore_
bool ignore_underscore_
Ignore underscores when matching (option, not value).
Definition Option.hpp:82
CLI::OptionBase::get_disable_flag_override
CLI11_NODISCARD bool get_disable_flag_override() const
The status of configurable.
Definition Option.hpp:149
CLI::OptionBase::get_group
CLI11_NODISCARD const std::string & get_group() const
Get the group of this option.
Definition Option.hpp:134
CLI::OptionBase::copy_to
void copy_to(T *other) const
Copy the contents to another similar class (one based on OptionBase).
CLI::OptionBase::required
CRTP * required(bool value=true)
Set the option as required.
Definition Option.hpp:118
CLI::OptionDefaults::multi_option_policy
OptionDefaults * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times.
Definition Option.hpp:229
CLI::OptionDefaults::ignore_case
OptionDefaults * ignore_case(bool value=true)
Ignore the case of the option name.
Definition Option.hpp:235
CLI::OptionDefaults::ignore_underscore
OptionDefaults * ignore_underscore(bool value=true)
Ignore underscores in the option name.
Definition Option.hpp:241
CLI::OptionDefaults::callback_priority
OptionDefaults * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Set the callback priority.
Definition Option.hpp:223
CLI::OptionDefaults::OptionDefaults
OptionDefaults()=default
CLI::OptionDefaults::delimiter
OptionDefaults * delimiter(char value='\0')
set a delimiter character to split up single arguments to treat as multiple inputs
Definition Option.hpp:253
CLI::OptionDefaults::disable_flag_override
OptionDefaults * disable_flag_override(bool value=true)
Disable overriding flag values with an '=' segment.
Definition Option.hpp:247
CLI::Option
Definition Option.hpp:259
CLI::Option::get_option_text
CLI11_NODISCARD const std::string & get_option_text() const
Definition Option.hpp:650
CLI::Option::get_positional
CLI11_NODISCARD bool get_positional() const
True if the argument can be given directly.
Definition Option.hpp:628
CLI::Option::default_str_
std::string default_str_
A human readable default value, either manually set, captured, or captured by default.
Definition Option.hpp:294
CLI::Option::run_callback_for_default_
bool run_callback_for_default_
Control option to run the callback to set the default.
Definition Option.hpp:363
CLI::Option::get_envname
CLI11_NODISCARD std::string get_envname() const
The environment variable associated to this value.
Definition Option.hpp:573
CLI::Option::excludes
Option * excludes(Option *opt)
Sets excluded options.
CLI::Option::type_name
Option * type_name(std::string typeval)
Set a custom option typestring.
Definition Option.hpp:779
CLI::Option::type_name_
std::function< std::string()> type_name_
Definition Option.hpp:302
CLI::Option::option_state
option_state
enumeration for the option state machine
Definition Option.hpp:350
CLI::Option::option_state::reduced
@ reduced
a subset of results has been generated
Definition Option.hpp:353
CLI::Option::option_state::callback_run
@ callback_run
the callback has been executed
Definition Option.hpp:354
CLI::Option::option_state::validated
@ validated
the results have been validated
Definition Option.hpp:352
CLI::Option::option_state::parsing
@ parsing
The option is currently collecting parsed results.
Definition Option.hpp:351
CLI::Option::transform
Option * transform(const std::function< std::string(std::string)> &transform_func, std::string transform_description="", std::string transform_name="")
Adds a Validator-like function that can change result.
CLI::Option::current_option_state_
option_state current_option_state_
Whether the callback has run (needed for INI parsing).
Definition Option.hpp:357
CLI::Option::option_text_
std::string option_text_
If given, replace the text that describes the option type and usage in the help text.
Definition Option.hpp:297
CLI::Option::type_size_min_
int type_size_min_
The minimum number of arguments an option should be expecting.
Definition Option.hpp:315
CLI::Option::callback_priority
Option * callback_priority(CallbackPriority value=CallbackPriority::Normal)
Definition Option.hpp:448
CLI::Option::check_fname
CLI11_NODISCARD bool check_fname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:691
CLI::Option::pname_
std::string pname_
A positional name.
Definition Option.hpp:281
CLI::Option::expected_min_
int expected_min_
The minimum number of expected values.
Definition Option.hpp:318
CLI::Option::expected
Option * expected(int value_min, int value_max)
Set the range of expected arguments.
CLI::Option::ignore_case
Option * ignore_case(bool value=true)
CLI::Option::needs_
std::set< Option * > needs_
A list of options that are required with this option.
Definition Option.hpp:326
CLI::Option::default_function
Option * default_function(const std::function< std::string()> &func)
Set a capture function for the default. Mostly used by App.
Definition Option.hpp:794
CLI::Option::get_type_size_min
CLI11_NODISCARD int get_type_size_min() const
The minimum number of arguments the option expects.
Definition Option.hpp:565
CLI::Option::remove_excludes
bool remove_excludes(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
CLI::Option::multi_option_policy
Option * multi_option_policy(MultiOptionPolicy value=MultiOptionPolicy::Throw)
Take the last argument if given multiple times (or another policy).
CLI::Option::check_sname
CLI11_NODISCARD bool check_sname(std::string name) const
Requires "-" to be removed from string.
Definition Option.hpp:681
CLI::Option::trigger_on_result_
bool trigger_on_result_
flag indicating that the option should trigger the validation and callback chain on each result when ...
Definition Option.hpp:367
CLI::Option::flag_like_
bool flag_like_
Specify that the option should act like a flag vs regular option.
Definition Option.hpp:361
CLI::Option::add_result
Option * add_result(std::string s)
Puts a result at the end.
CLI::Option::excludes_
std::set< Option * > excludes_
A list of options that are excluded with this option.
Definition Option.hpp:329
CLI::Option::force_callback_
bool force_callback_
flag indicating that the option should force the callback regardless if any results present
Definition Option.hpp:369
CLI::Option::get_callback_run
CLI11_NODISCARD bool get_callback_run() const
See if the callback has been run already.
Definition Option.hpp:766
CLI::Option::operator=
Option & operator=(const Option &)=delete
CLI::Option::get_force_callback
CLI11_NODISCARD bool get_force_callback() const
The status of force_callback.
Definition Option.hpp:435
CLI::Option::check
Option * check(Validator_p validator)
Adds a shared validator.
CLI::Option::fnames_
std::vector< std::string > fnames_
a list of flag names with specified default values;
Definition Option.hpp:278
CLI::Option::Option
Option(std::string option_name, std::string option_description, callback_t callback, App *parent, bool allow_non_standard=false)
Making an option by hand is not defined, it must be made by the App class.
Definition Option.hpp:372
CLI::Option::get_flag_value
CLI11_NODISCARD std::string get_flag_value(const std::string &name, std::string input_value) const
CLI::Option::each
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).
CLI::Option::option_text
Option * option_text(std::string text)
Definition Option.hpp:645
CLI::Option::get_run_callback_for_default
CLI11_NODISCARD bool get_run_callback_for_default() const
Get the current value of run_callback_for_default.
Definition Option.hpp:444
CLI::Option::get_default_str
CLI11_NODISCARD std::string get_default_str() const
The default value (for help printing).
Definition Option.hpp:582
CLI::Option::nonpositional
CLI11_NODISCARD bool nonpositional() const
True if option has at least one non-positional name.
Definition Option.hpp:631
CLI::Option::get_items_expected_min
CLI11_NODISCARD int get_items_expected_min() const
The total min number of expected string values to be used.
Definition Option.hpp:617
CLI::Option::expected
Option * expected(int value)
Set the number of expected arguments.
CLI::Option::transform
Option * transform(Validator validator, const std::string &transform_name="")
Adds a transforming Validator with a built in type name.
CLI::Option::get_type_name
CLI11_NODISCARD std::string get_type_name() const
Get the full typename for this option.
CLI::Option::check_lname
CLI11_NODISCARD bool check_lname(std::string name) const
Requires "--" to be removed from string.
Definition Option.hpp:686
CLI::Option::Option
Option(const Option &)=delete
CLI::Option::results
CLI11_NODISCARD const results_t & results() const
Get the current complete results set.
Definition Option.hpp:712
CLI::Option::disable_flag_override
Option * disable_flag_override(bool value=true)
Disable flag overrides values, e.g. –flag=is not allowed.
Definition Option.hpp:553
CLI::Option::get_items_expected_max
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:620
CLI::Option::snames_
std::vector< std::string > snames_
A list of the short names (-a) without the leading dashes.
Definition Option.hpp:268
CLI::Option::proc_results_
results_t proc_results_
results after reduction
Definition Option.hpp:348
CLI::Option::count
CLI11_NODISCARD std::size_t count() const
Count the total number of times an option was passed.
Definition Option.hpp:389
CLI::Option::run_callback_for_default
Option * run_callback_for_default(bool value=true)
Definition Option.hpp:439
CLI::Option::type_size
Option * type_size(int option_type_size_min, int option_type_size_max)
Set a custom option type size range.
CLI::Option::inject_separator
void inject_separator(bool value=true)
Set the value of the separator injection flag.
Definition Option.hpp:791
CLI::Option::allow_extra_args
Option * allow_extra_args(bool value=true)
Definition Option.hpp:415
CLI::Option::trigger_on_parse
Option * trigger_on_parse(bool value=true)
Set the value of trigger_on_parse which specifies that the option callback should be triggered on eve...
Definition Option.hpp:422
CLI::Option::transform
Option * transform(Validator_p validator)
Adds a shared Validator.
CLI::Option::get_callback
CLI11_NODISCARD callback_t get_callback() const
Get the callback function.
Definition Option.hpp:585
CLI::Option::get_inject_separator
CLI11_NODISCARD bool get_inject_separator() const
Return the inject_separator flag.
Definition Option.hpp:570
CLI::Option::validators_
std::vector< Validator_p > validators_
A list of Validators to run on each value parsed.
Definition Option.hpp:323
CLI::Option::type_size
Option * type_size(int option_type_size)
Set a custom option size.
CLI::Option::reduced_results
CLI11_NODISCARD results_t reduced_results() const
Get a copy of the results.
CLI::Option::parent_
App * parent_
link back up to the parent App for fallthrough
Definition Option.hpp:336
CLI::Option::get_trigger_on_parse
CLI11_NODISCARD bool get_trigger_on_parse() const
The status of trigger on parse.
Definition Option.hpp:427
CLI::Option::get_lnames
CLI11_NODISCARD const std::vector< std::string > & get_lnames() const
Get the long names.
Definition Option.hpp:588
CLI::Option::check_name
CLI11_NODISCARD bool check_name(const std::string &name) const
Check a name. Requires "-" or "--" for short / long, supports positional name.
CLI::Option::expected_max_
int expected_max_
The maximum number of expected values.
Definition Option.hpp:320
CLI::Option::get_excludes
CLI11_NODISCARD std::set< Option * > get_excludes() const
The set of options excluded.
Definition Option.hpp:579
CLI::Option::check
Option * check(std::function< std::string(const std::string &)> validator_func, 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...
CLI::Option::excludes
Option * excludes(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:514
CLI::Option::get_items_expected
CLI11_NODISCARD int get_items_expected() const
The total min number of expected string values to be used.
Definition Option.hpp:625
CLI::Option::get_needs
CLI11_NODISCARD std::set< Option * > get_needs() const
The set of options needed.
Definition Option.hpp:576
CLI::Option::description_
std::string description_
The description for help strings.
Definition Option.hpp:291
CLI::Option::get_snames
CLI11_NODISCARD const std::vector< std::string > & get_snames() const
Get the short names.
Definition Option.hpp:591
CLI::Option::get_type_size_max
CLI11_NODISCARD int get_type_size_max() const
The maximum number of arguments the option expects.
Definition Option.hpp:567
CLI::Option::inject_separator_
bool inject_separator_
flag indicating a separator needs to be injected after each argument call
Definition Option.hpp:365
CLI::Option::remove_needs
bool remove_needs(Option *opt)
Remove needs link from an option. Returns true if the option really was in the needs list.
CLI::Option::get_description
CLI11_NODISCARD const std::string & get_description() const
Get the description.
Definition Option.hpp:637
CLI::Option::run_callback
void run_callback()
Process the callback.
CLI::Option::get_single_name
CLI11_NODISCARD const std::string & get_single_name() const
Get a single name for the option, first of lname, sname, pname, envname.
Definition Option.hpp:596
CLI::Option::get_expected
CLI11_NODISCARD int get_expected() const
The number of times the option expects to be included.
Definition Option.hpp:609
CLI::Option::callback_
callback_t callback_
Options store a callback to do all the work.
Definition Option.hpp:339
CLI::Option::empty
CLI11_NODISCARD bool empty() const
True if the option was not passed.
Definition Option.hpp:392
CLI::Option::get_expected_min
CLI11_NODISCARD int get_expected_min() const
The number of times the option expects to be included.
Definition Option.hpp:612
CLI::Option::clear
void clear()
Clear the parsed results (mostly for testing).
Definition Option.hpp:398
CLI::Option::get_expected_max
CLI11_NODISCARD int get_expected_max() const
The max number of times the option expects to be included.
Definition Option.hpp:614
CLI::Option::add_result
Option * add_result(std::vector< std::string > s)
Puts a result at the end.
CLI::Option::matching_name
CLI11_NODISCARD const std::string & matching_name(const Option &other) const
If options share any of the same names, find it.
CLI::Option::capture_default_str
Option * capture_default_str()
Capture the default value from the original value (if it can be captured).
Definition Option.hpp:800
CLI::Option::results
void results(T &output) const
Get the results as a specified type.
Definition Option.hpp:718
CLI::Option::default_str
Option * default_str(std::string val)
Set the default value string representation (does not change the contained value).
Definition Option.hpp:808
CLI::Option::get_type_size
CLI11_NODISCARD int get_type_size() const
The number of arguments the option expects.
Definition Option.hpp:562
CLI::Option::envname_
std::string envname_
If given, check the environment for this option.
Definition Option.hpp:284
CLI::Option::default_function_
std::function< std::string()> default_function_
Run this function to capture a default (ignore if empty).
Definition Option.hpp:305
CLI::Option::get_allow_extra_args
CLI11_NODISCARD bool get_allow_extra_args() const
Get the current value of allow extra args.
Definition Option.hpp:420
CLI::Option::ignore_underscore
Option * ignore_underscore(bool value=true)
CLI::Option::default_flag_values_
std::vector< std::pair< std::string, std::string > > default_flag_values_
Definition Option.hpp:275
CLI::Option::envname
Option * envname(std::string name)
Sets environment variable to read if no option given.
Definition Option.hpp:532
CLI::Option::type_name_fn
Option * type_name_fn(std::function< std::string()> typefun)
Set the type function to run when displayed on this option.
Definition Option.hpp:773
CLI::Option::default_val
Option * default_val(const X &val)
Definition Option.hpp:815
CLI::Option::add_result
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.
CLI::Option::needs
Option * needs(Option *opt)
Sets required options.
Definition Option.hpp:485
CLI::Option::type_size_max_
int type_size_max_
Definition Option.hpp:313
CLI::Option::needs
Option * needs(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:502
CLI::Option::allow_extra_args_
bool allow_extra_args_
Specify that extra args beyond type_size_max should be allowed.
Definition Option.hpp:359
CLI::Option::description
Option * description(std::string option_description)
Set the description.
Definition Option.hpp:640
CLI::Option::lnames_
std::vector< std::string > lnames_
A list of the long names (--long) without the leading dashes.
Definition Option.hpp:271
CLI::Option::force_callback
Option * force_callback(bool value=true)
Set the value of force_callback.
Definition Option.hpp:430
CLI::Option::get_validator
Validator * get_validator(int index)
Get a Validator by index NOTE: this may not be the order of definition.
CLI::Option::operator==
bool operator==(const Option &other) const
If options share any of the same names, they are equal (not counting positional).
Definition Option.hpp:675
CLI::Option::check
Option * check(Validator validator, const std::string &validator_name="")
Adds a Validator with a built in type name.
CLI::Option::get_name
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...
CLI::Option::needs
Option * needs(std::string opt_name)
Can find a string if needed.
Definition Option.hpp:493
CLI::Option::get_fnames
CLI11_NODISCARD const std::vector< std::string > & get_fnames() const
Get the flag names with specified default values.
Definition Option.hpp:594
CLI::Option::has_description
CLI11_NODISCARD bool has_description() const
True if option has description.
Definition Option.hpp:634
CLI::Option::results_
results_t results_
complete Results of parsing
Definition Option.hpp:346
CLI::Option::get_validator
Validator * get_validator(const std::string &validator_name="")
Get a named Validator.
CLI::Option::as
CLI11_NODISCARD T as() const
Return the results as the specified type.
Definition Option.hpp:759
CLI::Option::excludes
Option * excludes(A opt, B opt1, ARG... args)
Any number supported, any mix of string and Opt.
Definition Option.hpp:523
CLI::Validator
Some validators that are provided.
Definition Validators.hpp:54
CLI::detail::to_string
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:337
CLI::detail::checked_multiply
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:306
CLI::detail::get_names
CLI11_INLINE std::tuple< std::vector< std::string >, std::vector< std::string >, std::string > get_names(const std::vector< std::string > &input, bool allow_non_standard=false)
Get a vector of short names, one of long names, and a single name.
CLI::detail::expected_max_vector_size
constexpr int expected_max_vector_size
Definition StringTools.hpp:48
CLI::detail::valid_alias_name_string
bool valid_alias_name_string(const std::string &str)
Verify an app name.
Definition StringTools.hpp:167
CLI::detail::split_names
CLI11_INLINE std::vector< std::string > split_names(std::string current)
CLI::detail::find_member
CLI11_INLINE 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.
CLI
Definition App.hpp:36
CLI::Validator_p
std::shared_ptr< Validator > Validator_p
Definition Option.hpp:40
CLI::CallbackPriority
CallbackPriority
enumeration for the callback priority
Definition Option.hpp:54
CLI::CallbackPriority::First
@ First
Definition Option.hpp:56
CLI::CallbackPriority::PreRequirementsCheckPreHelp
@ PreRequirementsCheckPreHelp
Definition Option.hpp:57
CLI::CallbackPriority::Normal
@ Normal
Definition Option.hpp:60
CLI::CallbackPriority::NormalPreHelp
@ NormalPreHelp
Definition Option.hpp:59
CLI::CallbackPriority::PreRequirementsCheck
@ PreRequirementsCheck
Definition Option.hpp:58
CLI::CallbackPriority::Last
@ Last
Definition Option.hpp:62
CLI::CallbackPriority::FirstPreHelp
@ FirstPreHelp
Definition Option.hpp:55
CLI::CallbackPriority::LastPreHelp
@ LastPreHelp
Definition Option.hpp:61
CLI::ExitCodes::ConversionError
@ ConversionError
Definition Error.hpp:50
CLI::ExitCodes::IncorrectConstruction
@ IncorrectConstruction
Definition Error.hpp:46
CLI::MultiOptionPolicy
MultiOptionPolicy
Enumeration of the multiOption Policy selection.
Definition Option.hpp:43
CLI::MultiOptionPolicy::TakeAll
@ TakeAll
just get all the passed argument regardless
Definition Option.hpp:48
CLI::MultiOptionPolicy::TakeFirst
@ TakeFirst
take only the first Expected number of arguments
Definition Option.hpp:46
CLI::MultiOptionPolicy::Reverse
@ Reverse
take only the last Expected number of arguments in reverse order
Definition Option.hpp:50
CLI::MultiOptionPolicy::Throw
@ Throw
Throw an error if any extra arguments were given.
Definition Option.hpp:44
CLI::MultiOptionPolicy::TakeLast
@ TakeLast
take only the last Expected number of arguments
Definition Option.hpp:45
CLI::MultiOptionPolicy::Sum
@ Sum
sum all the arguments together if numerical or concatenate directly without delimiter
Definition Option.hpp:49
CLI::MultiOptionPolicy::Join
@ Join
merge all the arguments together into a single string via the delimiter character default(' ')
Definition Option.hpp:47
CLI::callback_t
std::function< bool(const results_t &)> callback_t
callback function definition
Definition Option.hpp:33
CLI::AppFormatMode::Normal
@ Normal
The normal, detailed help.
Definition FormatterFwd.hpp:33
CLI::results_t
std::vector< std::string > results_t
Definition Option.hpp:31
CLI::Option_p
std::unique_ptr< Option > Option_p
Definition Option.hpp:39