Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
CLI::AsNumberWithUnit Class Reference

#include <CLI11.hpp>

Inheritance diagram for CLI::AsNumberWithUnit:
Collaboration diagram for CLI::AsNumberWithUnit:

Public Types

enum  Options {
  CASE_SENSITIVE = 0 , CASE_INSENSITIVE = 1 , UNIT_OPTIONAL = 0 , UNIT_REQUIRED = 2 ,
  DEFAULT = CASE_INSENSITIVE | UNIT_OPTIONAL
}
 

Public Member Functions

template<typename Number >
 AsNumberWithUnit (std::map< std::string, Number > mapping, Options opts=DEFAULT, const std::string &unit_name="UNIT")
 
- Public Member Functions inherited from CLI::Validator
 Validator ()=default
 
 Validator (std::string validator_desc)
 Construct a Validator with just the description string.
 
 Validator (std::function< std::string(std::string &)> op, std::string validator_desc, std::string validator_name="")
 Construct Validator from basic information.
 
Validatoroperation (std::function< std::string(std::string &)> op)
 Set the Validator operation function.
 
std::string operator() (std::string &str) const
 
std::string operator() (const std::string &str) const
 
Validatordescription (std::string validator_desc)
 Specify the type string.
 
Validator description (std::string validator_desc) const
 Specify the type string.
 
std::string get_description () const
 Generate type description information for the Validator.
 
Validatorname (std::string validator_name)
 Specify the type string.
 
Validator name (std::string validator_name) const
 Specify the type string.
 
const std::string & get_name () const
 Get the name of the Validator.
 
Validatoractive (bool active_val=true)
 Specify whether the Validator is active or not.
 
Validator active (bool active_val=true) const
 Specify whether the Validator is active or not.
 
Validatornon_modifying (bool no_modify=true)
 Specify whether the Validator can be modifying or not.
 
Validatorapplication_index (int app_index)
 Specify the application index of a validator.
 
Validator application_index (int app_index) const
 Specify the application index of a validator.
 
int get_application_index () const
 Get the current value of the application index.
 
bool get_active () const
 Get a boolean if the validator is active.
 
bool get_modifying () const
 Get a boolean if the validator is allowed to modify the input returns true if it can modify the input.
 
Validator operator& (const Validator &other) const
 
Validator operator| (const Validator &other) const
 
Validator operator! () const
 Create a validator that fails when a given validator succeeds.
 

Additional Inherited Members

- Protected Attributes inherited from CLI::Validator
std::function< std::string()> desc_function_ {[]() { return std::string{}; }}
 This is the description function, if empty the description_ will be used.
 
std::function< std::string(std::string &)> func_ {[](std::string &) { return std::string{}; }}
 
std::string name_ {}
 The name for search purposes of the Validator.
 
int application_index_ = -1
 A Validator will only apply to an indexed value (-1 is all elements)
 
bool active_ {true}
 Enable for Validator to allow it to be disabled if need be.
 
bool non_modifying_ {false}
 specify that a validator should not modify the input
 

Detailed Description

Multiply a number by a factor using given mapping. Can be used to write transforms for SIZE or DURATION inputs.

Example: With mapping = {"b"->1, "kb"->1024, "mb"->1024*1024} one can recognize inputs like "100", "12kb", "100 MB", that will be automatically transformed to 100, 14448, 104857600.

Output number type matches the type in the provided mapping. Therefore, if it is required to interpret real inputs like "0.42 s", the mapping should be of a type <string, float> or <string, double>.

Definition at line 2941 of file CLI11.hpp.

Member Enumeration Documentation

◆ Options

Adjust AsNumberWithUnit behavior. CASE_SENSITIVE/CASE_INSENSITIVE controls how units are matched. UNIT_OPTIONAL/UNIT_REQUIRED throws ValidationError if UNIT_REQUIRED is set and unit literal is not found.

Enumerator
CASE_SENSITIVE 
CASE_INSENSITIVE 
UNIT_OPTIONAL 
UNIT_REQUIRED 
DEFAULT 

Definition at line 2947 of file CLI11.hpp.

Constructor & Destructor Documentation

◆ AsNumberWithUnit()

template<typename Number >
CLI::AsNumberWithUnit::AsNumberWithUnit ( std::map< std::string, Number > mapping,
Options opts = DEFAULT,
const std::string & unit_name = "UNIT" )
inlineexplicit

Definition at line 2956 of file CLI11.hpp.

2958 {
2959 description(generate_description<Number>(unit_name, opts));
2960 validate_mapping(mapping, opts);
2961
2962 // transform function
2963 func_ = [mapping, opts](std::string &input) -> std::string {
2964 Number num;
2965
2966 detail::rtrim(input);
2967 if(input.empty()) {
2968 throw ValidationError("Input is empty");
2969 }
2970
2971 // Find split position between number and prefix
2972 auto unit_begin = input.end();
2973 while(unit_begin > input.begin() && std::isalpha(*(unit_begin - 1), std::locale())) {
2974 --unit_begin;
2975 }
2976
2977 std::string unit{unit_begin, input.end()};
2978 input.resize(static_cast<std::size_t>(std::distance(input.begin(), unit_begin)));
2979 detail::trim(input);
2980
2981 if(opts & UNIT_REQUIRED && unit.empty()) {
2982 throw ValidationError("Missing mandatory unit");
2983 }
2984 if(opts & CASE_INSENSITIVE) {
2985 unit = detail::to_lower(unit);
2986 }
2987
2988 bool converted = detail::lexical_cast(input, num);
2989 if(!converted) {
2990 throw ValidationError(std::string("Value ") + input + " could not be converted to " +
2992 }
2993
2994 if(unit.empty()) {
2995 // No need to modify input if no unit passed
2996 return {};
2997 }
2998
2999 // find corresponding factor
3000 auto it = mapping.find(unit);
3001 if(it == mapping.end()) {
3002 throw ValidationError(unit +
3003 " unit not recognized. "
3004 "Allowed values: " +
3005 detail::generate_map(mapping, true));
3006 }
3007
3008 // perform safe multiplication
3009 bool ok = detail::checked_multiply(num, it->second);
3010 if(!ok) {
3011 throw ValidationError(detail::to_string(num) + " multiplied by " + unit +
3012 " factor would cause number overflow. Use smaller value.");
3013 }
3014 input = detail::to_string(num);
3015
3016 return {};
3017 };
3018 }
Validator & description(std::string validator_desc)
Specify the type string.
Definition CLI11.hpp:2142
std::function< std::string(std::string &)> func_
Definition CLI11.hpp:2096
constexpr const char * type_name()
Print name for enumeration types.
Definition CLI11.hpp:1299
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 CLI11.hpp:1053
std::string generate_map(const T &map, bool key_only=false)
Generate a string representation of a map.
Definition CLI11.hpp:2597
std::string & rtrim(std::string &str)
Trim whitespace from right of string.
Definition CLI11.hpp:252
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 CLI11.hpp:2684
std::string & trim(std::string &str)
Trim whitespace from string.
Definition CLI11.hpp:267
std::string to_lower(std::string str)
Return a lower case version of a string.
Definition CLI11.hpp:336
const detail::Number Number
Check for a number.
Definition CLI11.hpp:2510
Here is the call graph for this function:

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