Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
sharg::value_list_validator< option_value_t > Class Template Reference

A validator that checks whether a value is inside a list of valid values. More...

#include <sharg/validators.hpp>

+ Inheritance diagram for sharg::value_list_validator< option_value_t >:

Public Types

using option_value_type = option_value_t
 Type of values that are tested by validator.
 

Public Member Functions

void operator() (option_value_type const &cmp) const
 Tests whether cmp lies inside values.
 
template<std::ranges::forward_range range_type>
requires std::convertible_to<std::ranges::range_value_t<range_type>, option_value_type> && (!std::same_as<std::remove_cvref_t<range_type>, std::filesystem::path>)
void operator() (range_type const &range) const
 Tests whether every element in range lies inside values.
 
std::string get_help_page_message () const
 Returns a message that can be appended to the (positional) options help page info.
 
Constructors, destructor and assignment
 value_list_validator ()=default
 Defaulted.
 
 value_list_validator (value_list_validator const &)=default
 Defaulted.
 
 value_list_validator (value_list_validator &&)=default
 Defaulted.
 
value_list_validatoroperator= (value_list_validator const &)=default
 Defaulted.
 
value_list_validatoroperator= (value_list_validator &&)=default
 Defaulted.
 
 ~value_list_validator ()=default
 Defaulted.
 
template<std::ranges::forward_range range_type>
requires std::constructible_from<option_value_type, std::ranges::range_rvalue_reference_t<range_type>>
 value_list_validator (range_type rng)
 Constructing from a range.
 
template<typename... option_types>
requires ((std::constructible_from<option_value_type, option_types> && ...))
 value_list_validator (option_types &&... opts)
 Constructing from a parameter pack.
 

Related Symbols

(Note that these are not member symbols.)

Type deduction guides
template<typename option_type , typename... option_types>
 value_list_validator (option_type, option_types...) -> value_list_validator< std::string >
 Given a parameter pack of types that are convertible to std::string, delegate to value type std::string.
 
template<typename range_type >
 value_list_validator (range_type &&rng) -> value_list_validator< std::string >
 Deduction guide for ranges over a value type convertible to std::string.
 
template<typename option_type , typename... option_types>
 value_list_validator (option_type, option_types...) -> value_list_validator< option_type >
 Deduction guide for a parameter pack.
 
template<typename range_type >
 value_list_validator (range_type &&rng) -> value_list_validator< std::ranges::range_value_t< range_type > >
 Deduction guide for ranges.
 

Detailed Description

template<parsable option_value_t>
class sharg::value_list_validator< option_value_t >

A validator that checks whether a value is inside a list of valid values.

Template Parameters
option_value_tThe type the validator is called on. Must model sharg::parsable.

On construction, the validator must receive a range or parameter pack of valid values. The class then acts as a functor that throws a sharg::validation_error exception whenever a given value is not in the list.

Note
In order to simplify the chaining of validators, the option value type is deduced to std::string if the range's value type is convertible to it. Otherwise, the option value type is deduced to the value type of the range.
// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <sharg/all.hpp>
int main(int argc, char const ** argv)
{
sharg::parser myparser{"Test", argc, argv}; // initialize
int myint;
sharg::value_list_validator my_validator{2, 4, 6, 8, 10};
myparser.add_option(myint,
.long_id = "integer",
.description = "Give me a number.",
.validator = my_validator});
// an exception will be thrown if the user specifies an integer
// that is not one of [2,4,6,8,10] (e.g. "./test_app -i 3")
try
{
myparser.parse();
}
catch (sharg::parser_error const & ext) // the user did something wrong
{
std::cerr << "[PARSER ERROR] " << ext.what() << "\n"; // customize your error message
return -1;
}
std::cerr << "integer given by user passed validation: " << myint << "\n";
return 0;
}
Meta-header for the Parser module .
Parser exception that is thrown whenever there is an error while parsing the command line arguments.
Definition exceptions.hpp:40
The Sharg command line parser.
Definition parser.hpp:154
A validator that checks whether a value is inside a list of valid values.
Definition validators.hpp:175
Option struct that is passed to the sharg::parser::add_option() function.
Definition config.hpp:43
char short_id
The short identifier for the option (e.g. 'a', making the option callable via -a).
Definition config.hpp:53
T what(T... args)
Remarks
For a complete overview, take a look at Parser

This entity is stable. Since version 1.0.

Constructor & Destructor Documentation

◆ value_list_validator() [1/2]

template<parsable option_value_t>
template<std::ranges::forward_range range_type>
requires std::constructible_from<option_value_type, std::ranges::range_rvalue_reference_t<range_type>>
sharg::value_list_validator< option_value_t >::value_list_validator ( range_type rng)
inline

Constructing from a range.

Template Parameters
range_typeThe type of range; must model std::ranges::forward_range and value_list_validator::option_value_type must be constructible from the rvalue reference type of the given range.
Parameters
[in]rngThe range of valid values to test.

This entity is stable. Since version 1.0.

◆ value_list_validator() [2/2]

template<parsable option_value_t>
template<typename... option_types>
requires ((std::constructible_from<option_value_type, option_types> && ...))
sharg::value_list_validator< option_value_t >::value_list_validator ( option_types &&... opts)
inline

Constructing from a parameter pack.

Template Parameters
option_typesThe type of option values in the parameter pack; The value_list_validator::option_value_type must be constructible from each type in the parameter pack.
Parameters
[in]optsThe parameter pack values.

This entity is stable. Since version 1.0.

Member Function Documentation

◆ get_help_page_message()

template<parsable option_value_t>
std::string sharg::value_list_validator< option_value_t >::get_help_page_message ( ) const
inline

Returns a message that can be appended to the (positional) options help page info.

This entity is stable. Since version 1.0.

◆ operator()() [1/2]

template<parsable option_value_t>
void sharg::value_list_validator< option_value_t >::operator() ( option_value_type const & cmp) const
inline

Tests whether cmp lies inside values.

Parameters
cmpThe input value to check.
Exceptions
sharg::validation_error

This entity is stable. Since version 1.0.

◆ operator()() [2/2]

template<parsable option_value_t>
template<std::ranges::forward_range range_type>
requires std::convertible_to<std::ranges::range_value_t<range_type>, option_value_type> && (!std::same_as<std::remove_cvref_t<range_type>, std::filesystem::path>)
void sharg::value_list_validator< option_value_t >::operator() ( range_type const & range) const
inline

Tests whether every element in range lies inside values.

Template Parameters
range_typeThe type of range to check; must model std::ranges::forward_range.
Parameters
rangeThe input range to iterate over and check every element.
Exceptions
sharg::validation_error

This entity is stable. Since version 1.0.


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