SeqAn3
seqan3::regex_validator< std::string > Class Template Reference

A validator that checks if a matches a regular expression pattern. More...

#include <seqan3/argument_parser/validators.hpp>

Inheritance diagram for seqan3::regex_validator< std::string >:
[legend]

Public Types

using value_type = std::string
 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() (value_type const &cmp) const
 Tests whether cmp lies inside values. More...
 
 regex_validator (std::string const &pattern_)
 Constructing from a vector. 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<>
class seqan3::regex_validator< std::string >

A validator that checks if a matches a regular expression pattern.

On construction, the validator must receive a pattern for a regular expression. The pattern variable will be used for constructing an std::regex and the validator will call std::regex_match on the command line argument. Note: A regex_match will only return true if the strings matches the pattern completely (in contrast to regex_search which also matches substrings). The struct than acts as a functor, that throws a seqan3::parser_invalid_argument exception whenever a given filename (string) is not in the given extension list.

int main(int argc, const char ** argv)
{
seqan3::argument_parser myparser("Test", argc, argv); // initialize
std::string my_string;
seqan3::regex_validator my_validator{"[a-zA-Z]+@[a-zA-Z]+\\.com"};
myparser.add_option(my_string,'s',"str","Give me a string.",
// an exception will be thrown if the user specifies a string
// that is no email address ending on .com
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 << "email address given by user passed validation: " << my_string << "\n";
return 0;
}

Constructor & Destructor Documentation

◆ regex_validator()

seqan3::regex_validator< std::string >::regex_validator ( std::string const &  pattern_)
inline

Constructing from a vector.

Parameters
[in]pattern_The pattern to match.

Member Function Documentation

◆ operator()()

void seqan3::regex_validator< std::string >::operator() ( value_type const &  cmp) const
inline

Tests whether cmp lies inside values.

Parameters
[in]cmpThe value to validate.
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: