cli11
Loading...
Searching...
No Matches
extra_validators.cpp File Reference
module cli11

The heavier validators: type checks, set membership, and unit parsing. More...

import cli11:encoding;
import std;

Classes

class  cli::detail::ipv4_validator_t
 Requires the value to be a legal IPv4 address. More...
class  cli::type_validator_t< desired_t >
 Requires the value to parse as a particular type. More...
class  cli::bound_t
 Clamps the value into a closed interval instead of rejecting it. More...
class  cli::is_member_t
 Requires the value to appear in a set. More...
class  cli::transformer_t
 Rewrites a value through a lookup table, passing unknown values through. More...
class  cli::checked_transformer_t
 Rewrites a value through a lookup table, rejecting unknown values. More...
class  cli::as_number_with_unit_t
 Multiplies a value by a unit factor drawn from a mapping. More...
class  cli::as_size_value_t
 Converts a human-readable size to a byte count. More...
class  cli::detail::permission_validator_t
 Requires the path to exist and carry a given permission. More...
class  cli::file_size_validator_t
 Requires the path to name a file within a size range. More...

Concepts

concept  cli::detail::has_find
 Matches containers offering their own find.

Typedefs

template<typename T>
using cli::transform_pairs_t = std::vector<std::pair<std::string, T>>
 The default mapping type for the transformers.

Enumerations

enum class  cli::detail::permission_t : std::uint8_t { none = 0 , read = 1 , write = 2 , exec = 4 }
 A filesystem permission to check for. More...

Functions

template<typename T>
requires copyable_ptr<std::remove_reference_t<T>>
auto cli::detail::smart_deref (T value) -> decltype(*value)
 Dereferences a pointer-like value.
template<typename T>
requires (!copyable_ptr<std::remove_reference_t<T>>)
auto cli::detail::smart_deref (T &value) -> std::remove_reference_t< T > &
 Passes a non-pointer value through unchanged.
template<typename T>
auto cli::detail::generate_set (const T &set) -> std::string
 Renders a set as {a,b,c} for help output.
template<typename T>
auto cli::detail::generate_map (const T &map, bool key_only=false) -> std::string
 Renders a map as {k->v,k->v} for help output.
template<typename T, typename V>
requires (!has_find<T, V>)
auto cli::detail::search (const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
 Finds a value in a container by scanning it.
template<typename T, typename V>
requires has_find<T, V>
auto cli::detail::search (const T &set, const V &val) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
 Finds a value in a container using its own find.
template<typename T, typename V>
auto cli::detail::search (const T &set, const V &val, const std::function< V(V)> &filter_function) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
 Finds a value, comparing through a filter when the direct lookup fails.
auto cli::ignore_case (std::string item) -> std::string
 Filter that makes a comparison case-insensitive.
auto cli::ignore_underscore (std::string item) -> std::string
 Filter that makes a comparison ignore underscores.
auto cli::ignore_space (std::string item) -> std::string
 Filter that makes a comparison ignore spaces and tabs.

Variables

const type_validator_t< double > cli::number ("NUMBER")
 Requires the value to parse as a number.
const detail::ipv4_validator_t cli::valid_ipv4
 Requires the value to be a legal IPv4 address.
const detail::permission_validator_t cli::read_permissions (detail::permission_t::read)
 Requires the path to exist and be readable.
const detail::permission_validator_t cli::write_permissions (detail::permission_t::write)
 Requires the path to exist and be writable.
const detail::permission_validator_t cli::exec_permissions (detail::permission_t::exec)
 Requires the path to exist and be executable.
const file_size_validator_t cli::non_empty_file (1, 0)
 Requires the path to name a file that is not empty.

Detailed Description

The heavier validators: type checks, set membership, and unit parsing.

These sit apart from the validators partition because they are templated on the option's type or on a container of allowed values, and because most carry enough machinery to be worth isolating:

Include this partition only if you need them; cli11.cpp re-exports it.

Typedef Documentation

◆ transform_pairs_t

template<typename T>
using cli::transform_pairs_t = std::vector<std::pair<std::string, T>>
export

The default mapping type for the transformers.

Template Parameters
TThe mapped-to type.

Enumeration Type Documentation

◆ permission_t

enum class cli::detail::permission_t : std::uint8_t
exportstrong

A filesystem permission to check for.

Enumerator
none 

No permission check.

read 

Readable.

write 

Writable.

exec 

Executable.

Function Documentation

◆ generate_map()

template<typename T>
auto cli::detail::generate_map ( const T & map,
bool key_only = false ) -> std::string
export

Renders a map as {k->v,k->v} for help output.

Parameters
mapThe map to render.
key_onlyRender only the keys, omitting the ->value part.
Returns
The rendered map.

◆ generate_set()

template<typename T>
auto cli::detail::generate_set ( const T & set) -> std::string
export

Renders a set as {a,b,c} for help output.

Parameters
setThe set to render.
Returns
The rendered set.

◆ ignore_case()

auto cli::ignore_case ( std::string item) -> std::string
export

Filter that makes a comparison case-insensitive.

Pass to cli::is_member_t or one of the transformers.

Parameters
itemThe value to fold.
Returns
The lowercased value.

◆ ignore_space()

auto cli::ignore_space ( std::string item) -> std::string
export

Filter that makes a comparison ignore spaces and tabs.

Parameters
itemThe value to fold.
Returns
The value with spaces and tabs removed.

◆ ignore_underscore()

auto cli::ignore_underscore ( std::string item) -> std::string
export

Filter that makes a comparison ignore underscores.

Parameters
itemThe value to fold.
Returns
The value with underscores removed.

◆ search() [1/3]

template<typename T, typename V>
requires has_find<T, V>
auto cli::detail::search ( const T & set,
const V & val ) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
export

Finds a value in a container using its own find.

Parameters
setThe container to search.
valThe value to look for.
Returns
Whether the value was found, and an iterator to it.

◆ search() [2/3]

template<typename T, typename V>
requires (!has_find<T, V>)
auto cli::detail::search ( const T & set,
const V & val ) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
export

Finds a value in a container by scanning it.

Parameters
setThe container to search.
valThe value to look for.
Returns
Whether the value was found, and an iterator to it.

◆ search() [3/3]

template<typename T, typename V>
auto cli::detail::search ( const T & set,
const V & val,
const std::function< V(V)> & filter_function ) -> std::pair< bool, decltype(std::begin(detail::smart_deref(set)))>
export

Finds a value, comparing through a filter when the direct lookup fails.

Tries the plain lookup first, since it may be the associative one, and only falls back to scanning with the filter applied to each element.

Parameters
setThe container to search.
valThe value to look for.
filter_functionApplied to each element before comparison.
Returns
Whether the value was found, and an iterator to it.

◆ smart_deref() [1/2]

template<typename T>
requires (!copyable_ptr<std::remove_reference_t<T>>)
auto cli::detail::smart_deref ( T & value) -> std::remove_reference_t< T > &
export

Passes a non-pointer value through unchanged.

Parameters
valueThe value to return.
Returns
A reference to value.

◆ smart_deref() [2/2]

template<typename T>
requires copyable_ptr<std::remove_reference_t<T>>
auto cli::detail::smart_deref ( T value) -> decltype(*value)
export

Dereferences a pointer-like value.

Lets the set-membership validators accept either a container or a pointer to one without branching at every use.

Parameters
valueThe pointer to dereference.
Returns
A reference to the pointed-at object.