cli11
Loading...
Searching...
No Matches
cli::validator_t Class Referenceexport
module cli11

A composable check applied to one command-line value. More...

Inheritance diagram for cli::validator_t:
cli::as_number_with_unit_t cli::bound_t cli::checked_transformer_t cli::detail::ipv4_validator_t cli::detail::permission_validator_t cli::file_on_default_path_t cli::file_size_validator_t cli::is_member_t cli::range_t cli::transformer_t cli::type_validator_t< desired_t >

Public Member Functions

 validator_t (std::string validator_desc)
 Constructs a validator that only carries a description.
 validator_t (std::function< std::string(std::string &)> op, std::string validator_desc, std::string validator_name="")
 Constructs a named validator.
auto operation (std::function< std::string(std::string &)> op) -> validator_t &
 Replaces the check.
auto operator() (std::string &str) const -> std::string
 Runs the check, possibly rewriting str.
auto operator() (const std::string &str) const -> std::string
 Runs the check against a copy, leaving str untouched.
auto description (std::string validator_desc) -> validator_t &
 Sets the description in place.
auto description (std::string validator_desc) const -> validator_t
 Returns a copy carrying a different description.
auto get_description () const -> std::string
 Returns the description shown in help output.
auto name (std::string validator_name) -> validator_t &
 Sets the name in place.
auto name (std::string validator_name) const -> validator_t
 Returns a copy carrying a different name.
auto get_name () const -> const std::string &
 Returns the validator's name.
auto active (bool active_val=true) -> validator_t &
 Enables or disables the check in place.
auto active (bool active_val=true) const -> validator_t
 Returns a copy that is enabled or disabled.
auto get_active () const -> bool
 Reports whether the check runs.
auto non_modifying (bool no_modify=true) -> validator_t &
 Forbids or permits the check to rewrite its argument.
auto get_modifying () const -> bool
 Reports whether the check may rewrite its argument.
auto application_index (int app_index) -> validator_t &
 Sets which value the check applies to, in place.
auto application_index (int app_index) const -> validator_t
 Returns a copy applying to a different value.
auto get_application_index () const -> int
 Returns which value the check applies to.
auto operator& (const validator_t &other) const -> validator_t
 Combines two validators so that both must pass.
auto operator| (const validator_t &other) const -> validator_t
 Combines two validators so that either may pass.
auto operator! () const -> validator_t
 Inverts a validator, so that passing becomes failing.

Protected Member Functions

 validator_t (std::string validator_desc, std::function< std::string(std::string &)> func)
 Constructs a validator with a fixed description.

Protected Attributes

std::function< std::string()> desc_function_ {[] { return std::string {}; }}
 Produces the description shown in help output.
std::function< std::string(std::string &)> func_
 The check itself.
std::string name_ {}
 The validator's name, used to find and replace it on an option.
int application_index_ = -1
 Which value this applies to, or -1 for all of them.
bool active_ {true}
 Whether the check runs at all.
bool non_modifying_ {false}
 Whether the check is forbidden from rewriting its argument.

Detailed Description

A composable check applied to one command-line value.

Constructor & Destructor Documentation

◆ validator_t() [1/3]

cli::validator_t::validator_t ( std::string validator_desc,
std::function< std::string(std::string &)> func )
inlineprotected

Constructs a validator with a fixed description.

Parameters
validator_descThe description shown in help output.
funcThe check to run.

◆ validator_t() [2/3]

cli::validator_t::validator_t ( std::string validator_desc)
inlineexplicit

Constructs a validator that only carries a description.

Parameters
validator_descThe description shown in help output.

◆ validator_t() [3/3]

cli::validator_t::validator_t ( std::function< std::string(std::string &)> op,
std::string validator_desc,
std::string validator_name = "" )
inline

Constructs a named validator.

Parameters
opThe check to run.
validator_descThe description shown in help output.
validator_nameThe name used to find this validator later.

Member Function Documentation

◆ active() [1/2]

auto cli::validator_t::active ( bool active_val = true) -> validator_t &
inline

Enables or disables the check in place.

Parameters
active_valWhether the check should run.
Returns
A reference to this validator, for chaining.

◆ active() [2/2]

auto cli::validator_t::active ( bool active_val = true) const -> validator_t
inlinenodiscard

Returns a copy that is enabled or disabled.

Parameters
active_valWhether the check should run.
Returns
The modified copy.

◆ application_index() [1/2]

auto cli::validator_t::application_index ( int app_index) -> validator_t &
inline

Sets which value the check applies to, in place.

Parameters
app_indexThe value index, or -1 for all values.
Returns
A reference to this validator, for chaining.

◆ application_index() [2/2]

auto cli::validator_t::application_index ( int app_index) const -> validator_t
inlinenodiscard

Returns a copy applying to a different value.

Parameters
app_indexThe value index, or -1 for all values.
Returns
The modified copy.

◆ description() [1/2]

auto cli::validator_t::description ( std::string validator_desc) -> validator_t &
inline

Sets the description in place.

Parameters
validator_descThe new description.
Returns
A reference to this validator, for chaining.

◆ description() [2/2]

auto cli::validator_t::description ( std::string validator_desc) const -> validator_t
inlinenodiscard

Returns a copy carrying a different description.

Parameters
validator_descThe new description.
Returns
The modified copy.

◆ get_active()

auto cli::validator_t::get_active ( ) const -> bool
inlinenodiscard

Reports whether the check runs.

Returns
true if the validator is active.

◆ get_application_index()

auto cli::validator_t::get_application_index ( ) const -> int
inlinenodiscard

Returns which value the check applies to.

Returns
The value index, or -1 for all values.

◆ get_description()

auto cli::validator_t::get_description ( ) const -> std::string
inlinenodiscard

Returns the description shown in help output.

Returns
The description, or an empty string if the validator is inactive.

◆ get_modifying()

auto cli::validator_t::get_modifying ( ) const -> bool
inlinenodiscard

Reports whether the check may rewrite its argument.

Returns
true if rewriting is permitted.

◆ get_name()

auto cli::validator_t::get_name ( ) const -> const std::string &
inlinenodiscard

Returns the validator's name.

Returns
The name.

◆ name() [1/2]

auto cli::validator_t::name ( std::string validator_name) -> validator_t &
inline

Sets the name in place.

Parameters
validator_nameThe new name.
Returns
A reference to this validator, for chaining.

◆ name() [2/2]

auto cli::validator_t::name ( std::string validator_name) const -> validator_t
inlinenodiscard

Returns a copy carrying a different name.

Parameters
validator_nameThe new name.
Returns
The modified copy.

◆ non_modifying()

auto cli::validator_t::non_modifying ( bool no_modify = true) -> validator_t &
inline

Forbids or permits the check to rewrite its argument.

Parameters
no_modifyWhether to forbid rewriting.
Returns
A reference to this validator, for chaining.

◆ operation()

auto cli::validator_t::operation ( std::function< std::string(std::string &)> op) -> validator_t &
inline

Replaces the check.

Parameters
opThe new check.
Returns
A reference to this validator, for chaining.

◆ operator!()

auto cli::validator_t::operator! ( ) const -> validator_t
inline

Inverts a validator, so that passing becomes failing.

Returns
The inverted validator.

◆ operator&()

auto cli::validator_t::operator& ( const validator_t & other) const -> validator_t
inline

Combines two validators so that both must pass.

Parameters
otherThe validator to combine with.
Returns
The combined validator.

◆ operator()() [1/2]

auto cli::validator_t::operator() ( const std::string & str) const -> std::string
inline

Runs the check against a copy, leaving str untouched.

Parameters
strThe value to check.
Returns
An empty string on success, otherwise a failure message.

◆ operator()() [2/2]

auto cli::validator_t::operator() ( std::string & str) const -> std::string
inline

Runs the check, possibly rewriting str.

Parameters
[in,out]strThe value to check.
Returns
An empty string on success, otherwise a failure message.

◆ operator|()

auto cli::validator_t::operator| ( const validator_t & other) const -> validator_t
inline

Combines two validators so that either may pass.

Parameters
otherThe validator to combine with.
Returns
The combined validator.

Member Data Documentation

◆ desc_function_

std::function<std::string()> cli::validator_t::desc_function_ {[] { return std::string {}; }}
protected

Produces the description shown in help output.

A callable rather than a string so that a description can be built lazily from the descriptions of composed validators.

◆ func_

std::function<std::string(std::string &)> cli::validator_t::func_
protected
Initial value:
{[](std::string &)
{ return std::string {}; }}

The check itself.

Returns an empty string on success, or a failure message. May rewrite its argument unless non_modifying_ is set.


The documentation for this class was generated from the following file: