SeqAn3
seqan3::value_list_validator< option_value_type > Class Template Reference

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

#include <seqan3/argument_parser/validators.hpp>

Inheritance diagram for seqan3::value_list_validator< option_value_type >:
[legend]

Public Types

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

Public Member Functions

std::string get_help_page_message () const
 Returns a message that can be appended to the (positional) options help page info.
 
void operator() (option_value_type const &cmp) const
 Tests whether cmp lies inside values. More...
 
 value_list_validator (std::vector< option_value_type > const &v)
 Constructing from a vector. More...
 
 value_list_validator (std::initializer_list< option_value_type > const &v)
 Constructing from an initializer_list. More...
 

Related Functions

(Note that these are not member functions.)

Requirements for seqan3::validator_concept

You can expect these (meta-)functions on all types that implement seqan3::validator_concept.

void operator() (value_type const &cmp) const
 Validates the value 'cmp' and throws a seqan3::validation_error on failure. More...
 

Detailed Description

template<typename option_value_type>
class seqan3::value_list_validator< option_value_type >

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

On construction, the validator must receive a list (vector) of valid values. The struct than acts as a functor, that throws a seqan3::parser_invalid_argument exception whenever a given value is not in the given list.

int main(int argc, const char ** argv)
{
seqan3::argument_parser myparser("Test", argc, argv); // initialize
int myint;
seqan3::value_list_validator my_validator{2,4,6,8,10};
myparser.add_option(myint,'i',"integer","Give me a number.",
// 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 (seqan3::parser_invalid_argument const & ext) // the user did something wrong
{
std::cerr << "[PARSER ERROR] " << ext.what() << "\n"; // customize your error message
return -1;
}
catch (seqan3::parser_interruption const &) // expected behaviour on special requests (e.g. `--help`)
{
return 0;
}
seqan3::debug_stream << "integer given by user passed validation: " << myint << "\n";
return 0;
}

Constructor & Destructor Documentation

◆ value_list_validator() [1/2]

template<typename option_value_type >
seqan3::value_list_validator< option_value_type >::value_list_validator ( std::vector< option_value_type > const &  v)
inline

Constructing from a vector.

Parameters
[in]vThe vector of valid values to test.

◆ value_list_validator() [2/2]

template<typename option_value_type >
seqan3::value_list_validator< option_value_type >::value_list_validator ( std::initializer_list< option_value_type > const &  v)
inline

Constructing from an initializer_list.

Parameters
[in]vThe initializer_list of valid values to test.

Member Function Documentation

◆ operator()()

template<typename option_value_type >
void seqan3::value_list_validator< option_value_type >::operator() ( option_value_type const &  cmp) const
inline

Tests whether cmp lies inside values.

Parameters
cmpThe input value to check.
Exceptions
parser_invalid_argument

Friends And Related Function Documentation

◆ operator()()

void operator() ( value_type const &  cmp) const
related

Validates the value 'cmp' and throws a seqan3::validation_error on failure.

Template Parameters
value_typeThe type of the value to be validated.
Parameters
[in,out]cmpThe value to be validated.
Exceptions
seqan3::validation_errorif value 'cmp' does not pass validation.
Attention
This is a concept requirement, not an actual function (however types satisfying this concept will provide an implementation).

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