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

Multiplies a value by a unit factor drawn from a mapping. More...

Inheritance diagram for cli::as_number_with_unit_t:
cli::validator_t cli::as_size_value_t

Public Types

enum class  options_t : std::uint8_t {
  case_sensitive = 0 , case_insensitive = 1 , unit_optional = 0 , unit_required = 2 ,
  default_mode = 1
}
 Flags controlling how units are matched. More...

Public Member Functions

template<typename number_t>
 as_number_with_unit_t (std::map< std::string, number_t > mapping, options_t opts=options_t::default_mode, const std::string &unit_name="UNIT")
 Constructs the validator from a unit mapping.
Public Member Functions inherited from cli::validator_t
 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.

Friends

constexpr auto operator| (options_t a, options_t b) -> options_t
 Combines two option flags.
constexpr auto operator& (options_t a, options_t b) -> options_t
 Intersects two option flags.
constexpr auto operator|= (options_t &a, options_t b) -> options_t &
 Adds flags in place.
constexpr auto operator&= (options_t &a, options_t b) -> options_t &
 Removes flags in place.
constexpr auto has_flag (options_t value, options_t flag) -> bool
 Tests whether a flag is set.

Additional Inherited Members

Protected Member Functions inherited from cli::validator_t
 validator_t (std::string validator_desc, std::function< std::string(std::string &)> func)
 Constructs a validator with a fixed description.
Protected Attributes inherited from cli::validator_t
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

Multiplies a value by a unit factor drawn from a mapping.

Given a mapping of {"b" -> 1, "kb" -> 1024, "mb" -> 1024*1024}, inputs such as "100", "12kb", and "100 MB" become 100, 12288, and 104857600.

The result type follows the mapping's value type, so to accept fractional inputs such as "0.42 s" the mapping must map to float or double.

Member Enumeration Documentation

◆ options_t

enum class cli::as_number_with_unit_t::options_t : std::uint8_t
strong

Flags controlling how units are matched.

A bitmask. Combine with | and test with has_flag.

Note
default_mode rather than default, which is a keyword. Its value is case_insensitive | unit_optional, spelled numerically because the enumerators are not yet usable with | at this point.
Enumerator
case_sensitive 

Units must match exactly.

case_insensitive 

Units match without regard to case.

unit_optional 

A value with no unit is accepted.

unit_required 

A value with no unit is an error.

default_mode 

case_insensitive | unit_optional.

Constructor & Destructor Documentation

◆ as_number_with_unit_t()

template<typename number_t>
cli::as_number_with_unit_t::as_number_with_unit_t ( std::map< std::string, number_t > mapping,
options_t opts = options_t::default_mode,
const std::string & unit_name = "UNIT" )
inlineexplicit

Constructs the validator from a unit mapping.

Template Parameters
number_tThe numeric type the result is produced in.
Parameters
mappingUnit names mapped to their multipliers.
optsHow units are matched.
unit_nameThe label used for the unit in help output.
Exceptions
cli::validation_error_tAt parse time, if the value is empty, the unit is missing or unrecognised, or the multiplication would overflow.

◆ has_flag

auto has_flag ( options_t value,
options_t flag ) -> bool
friend

Tests whether a flag is set.

Replaces the implicit conversion to bool that the unscoped enum used to allow.

Parameters
valueThe flags to inspect.
flagThe flag to look for.
Returns
true if flag is set in value.

◆ operator&

auto operator& ( options_t a,
options_t b ) -> options_t
friend

Intersects two option flags.

Parameters
aThe left operand.
bThe right operand.
Returns
The common flags.

◆ operator&=

auto operator&= ( options_t & a,
options_t b ) -> options_t &
friend

Removes flags in place.

Parameters
[in,out]aThe flags to modify.
[in]bThe mask to apply.
Returns
A reference to a.

◆ operator|

auto operator| ( options_t a,
options_t b ) -> options_t
friend

Combines two option flags.

Parameters
aThe left operand.
bThe right operand.
Returns
The combined flags.

◆ operator|=

auto operator|= ( options_t & a,
options_t b ) -> options_t &
friend

Adds flags in place.

Parameters
[in,out]aThe flags to modify.
[in]bThe flags to add.
Returns
A reference to a.

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