SeqAn3  3.0.0
The Modern C++ library for sequence analysis.
seqan3::output_file_validator Class Reference

A validator that checks if a given path is a valid output file. More...

#include <seqan3/argument_parser/validators.hpp>

+ Inheritance diagram for seqan3::output_file_validator:

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.
 
template<std::ranges::ForwardRange range_type>
void operator() (range_type const &v) const
 Tests whether every path in list v passes validation. See operator()(value_type const & value) for further information. More...
 
virtual void operator() (std::filesystem::path const &file) const override
 Tests whether path is does not already exists and is writable. More...
 
Constructors, destructor and assignment
 output_file_validator ()=default
 Defaulted.
 
 output_file_validator (output_file_validator const &)=default
 Defaulted.
 
 output_file_validator (output_file_validator &&)=default
 Defaulted.
 
output_file_validatoroperator= (output_file_validator const &)=default
 Defaulted.
 
output_file_validatoroperator= (output_file_validator &&)=default
 Defaulted.
 
virtual ~output_file_validator ()=default
 Virtual Destructor.
 
 file_validator_base ()=default
 Defaulted.
 
 file_validator_base (file_validator_base const &)=default
 Defaulted.
 
 file_validator_base (file_validator_base &&)=default
 Defaulted.
 
 file_validator_base (std::vector< std::string > extensions)
 Constructs from a set of valid extensions. More...
 

Protected Member Functions

void validate_filename (std::filesystem::path const &path) const
 Validates the given filename path based on the specified extensions. More...
 
void validate_readability (std::filesystem::path const &path) const
 Checks if the given path is readable. More...
 
void validate_writeability (std::filesystem::path const &path) const
 Checks if the given path is writable. More...
 

Protected Attributes

std::vector< std::stringextensions {}
 Stores the extensions.
 

Related Functions

(Note that these are not member functions.)

Requirements for seqan3::Validator

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

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

Detailed Description

A validator that checks if a given path is a valid output file.

On construction, the validator can receive a list (std::vector over std::string) of valid file extensions. The struct acts as a functor that throws a seqan3::parser_invalid_argument exception whenever a given filename's extension (sts::string) is not in the given list of valid file extensions, if the file already exist, or if the parent path does not have the proper writer permissions.

int main(int argc, const char ** argv)
{
seqan3::argument_parser myparser{"Test", argc, argv}; // initialize
myparser.add_option(myfile,'f',"file","Output file containing the processed sequences.",
// an exception will be thrown if the user specifies a filename
// that does not have one of the extensions ["fa","fasta"],
// if the file already exists, or if the file is not writable.
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;
}
seqan3::debug_stream << "filename given by user passed validation: " << myfile << "\n";
return 0;
}
Note
The validator works on every type that can be implicitly converted to std::filesystem::path.

Member Function Documentation

◆ file_validator_base()

seqan3::file_validator_base::file_validator_base
inlineexplicit

Constructs from a set of valid extensions.

Parameters
[in]extensionsThe valid extensions to validate for.

◆ operator()() [1/2]

template<std::ranges::ForwardRange range_type>
void seqan3::file_validator_base::operator() ( range_type const &  v) const
inlineinherited

Tests whether every path in list v passes validation. See operator()(value_type const & value) for further information.

Template Parameters
range_typeThe type of range to check; must model std::ranges::ForwardRange and the value type must be convertible to std::filesystem::path.
Parameters
vThe input range to iterate over and check every element.
Exceptions
parser_invalid_argument

◆ operator()() [2/2]

virtual void seqan3::output_file_validator::operator() ( std::filesystem::path const &  file) const
inlineoverridevirtual

Tests whether path is does not already exists and is writable.

Parameters
fileThe input value to check.
Exceptions
parser_invalid_argumentif the validation process failed. Might be nested with std::filesystem::filesystem_error on unhandled OS API errors.

Implements seqan3::file_validator_base.

◆ validate_filename()

void seqan3::file_validator_base::validate_filename ( std::filesystem::path const &  path) const
inlineprotectedinherited

Validates the given filename path based on the specified extensions.

Parameters
pathThe filename path.
Exceptions
parser_invalid_argumentif the specified extensions don't match the given path, or std::filesystem::filesystem_error on underlying OS API errors.

◆ validate_readability()

void seqan3::file_validator_base::validate_readability ( std::filesystem::path const &  path) const
inlineprotectedinherited

Checks if the given path is readable.

Parameters
pathThe path to check.
Returns
true if readable, otherwise false.
Exceptions
seqan3::parser_invalid_argumentif the path is not readable, or std::filesystem::filesystem_error on underlying OS API errors.

◆ validate_writeability()

void seqan3::file_validator_base::validate_writeability ( std::filesystem::path const &  path) const
inlineprotectedinherited

Checks if the given path is writable.

Parameters
pathThe path to check.
Returns
true if writable, otherwise false.
Exceptions
seqan3::parser_invalid_argumentif the file could not be opened for writing, or std::filesystem::filesystem_error on underlying OS API errors.

Friends And Related Function Documentation

◆ operator()()

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

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

Template Parameters
value_typeThe type of the value to be validated.
Parameters
[in,out]cmpThe value to be validated.
Exceptions
seqan3::validation_failedif 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: