Loading...
Searching...
No Matches
seqan3::input_file_validator< file_t > Class Template Reference
Argument Parser

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

`#include <seqan3/argument_parser/validators.hpp>`

Inheritance diagram for seqan3::input_file_validator< file_t >:
[legend]

## Public Types

using | option_value_type
| Type of values that are tested by validator.
Public Types inherited from seqan3::file_validator_base
using | option_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::forward_range range_type> requires (std::convertible_to<std::ranges::range_value_t<range_type>, std::filesystem::path const &> && !std::convertible_to<range_type, std::filesystem::path const &>)
void | operator() (range_type const &v) const
| Tests whether every path in list `v` passes validation. See operator()(option_value_type const & value) for further information.
virtual void | operator() (std::filesystem::path const &file) const override
| Tests whether path is an existing regular file and is readable.
Constructors, destructor and assignment
| input_file_validator ()
| Default constructor.
| input_file_validator (input_file_validator const &)=default
| Defaulted.
| input_file_validator (input_file_validator &&)=default
| Defaulted.
input_file_validator & | operator= (input_file_validator const &)=default
| Defaulted.
input_file_validator & | operator= (input_file_validator &&)=default
| Defaulted.
virtual | ~input_file_validator ()=default
| Virtual destructor.
| input_file_validator (std::vector< std::string > extensions)
| Constructs from a given collection of valid extensions.
| file_validator_base ()=default
| Defaulted.
| file_validator_base (file_validator_base const &)=default
| Defaulted.
| file_validator_base (file_validator_base &&)=default
| Defaulted.
Public Member Functions inherited from seqan3::file_validator_base
template<std::ranges::forward_range range_type> requires (std::convertible_to<std::ranges::range_value_t<range_type>, std::filesystem::path const &> && !std::convertible_to<range_type, std::filesystem::path const &>)
void | operator() (range_type const &v) const
| Tests whether every path in list `v` passes validation. See operator()(option_value_type const & value) for further information.
| 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 & | operator= (file_validator_base const &)=default
| Defaulted.
file_validator_base & | operator= (file_validator_base &&)=default
| Defaulted.
virtual | ~file_validator_base ()=default
| Virtual destructor.

## Additional Inherited Members

Protected Member Functions inherited from seqan3::file_validator_base
bool | case_insensitive_string_ends_with (std::string_view str, std::string_view suffix) const
| Helper function that checks if a string is a suffix of another string. Case insensitive.
std::string | valid_extensions_help_page_message () const
| Returns the information of valid file extensions.
void | validate_filename (std::filesystem::path const &path) const
| Validates the given filename path based on the specified extensions.
void | validate_readability (std::filesystem::path const &path) const
| Checks if the given path is readable.
void | validate_writeability (std::filesystem::path const &path) const
| Checks if the given path is writable.
Protected Attributes inherited from seqan3::file_validator_base
std::vector< std::string > | extensions {}
| Stores the extensions.
Related Symbols inherited from validator
using | option_value_type
| The type of value on which the validator is called on.
void | operator() (option_value_type const &cmp) const
| Validates the value 'cmp' and throws a seqan3::validation_error on failure.

## Detailed Description

template<typename file_t = void>
class seqan3::input_file_validator< file_t >

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

Template Parameters

  file_t | The type of the file to get the valid extensions for; void on default.

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

// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <filesystem>
#include <seqan3/argument_parser/all.hpp>
#include <seqan3/core/debug_stream.hpp>
int main(int argc, char const ** argv)
{
seqan3::argument_parser myparser{"Test", argc, argv}; // initialize
std::filesystem::path myfile{};
myparser.add_option(myfile,
'f',
"file",
"The input file containing the sequences.",
seqan3::option_spec::standard,
seqan3::input_file_validator{{"fa", "fasta"}});
// an exception will be thrown if the user specifies a filename
// that does not have one of the extensions ["fa","fasta"] or if the file does not exist/is not readable.
try
{
myparser.parse();
}
catch (seqan3::argument_parser_error 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;
}
all.hpp
Meta-header for the Argument Parser module .
std::cerr
seqan3::argument_parser_error
Argument parser exception that is thrown whenever there is an error while parsing the command line ar...
Definition exceptions.hpp:39
seqan3::argument_parser
The SeqAn command line parser.
Definition argument_parser.hpp:147
seqan3::argument_parser::add_option
void add_option(option_type &value, char const short_id, std::string const &long_id, std::string const &desc, option_spec const spec=option_spec::standard, validator_type option_validator=validator_type{})
Adds an option to the seqan3::argument_parser.
Definition argument_parser.hpp:238
seqan3::argument_parser::parse
void parse()
Initiates the actual command line parsing.
Definition argument_parser.hpp:404
seqan3::input_file_validator
A validator that checks if a given path is a valid input file.
Definition validators.hpp:517
debug_stream.hpp
Provides seqan3::debug_stream and related types.
filesystem
seqan3::standard
@ standard
The default were no checking or special displaying is happening.
Definition auxiliary.hpp:250
seqan3::debug_stream
debug_stream_type debug_stream
A global instance of seqan3::debug_stream_type.
Definition debug_stream.hpp:38
std::filesystem::path
std::runtime_error::what
T what(T... args)

The valid extensions can also be obtained from a seqan3 formatted file type, e.g. seqan3::sequence_input_file, if it is given as template argument to this class. The following snippet demonstrates the different ways to instantiate the seqan3::input_file_validator.

// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <seqan3/argument_parser/validators.hpp>
#include <seqan3/core/debug_stream.hpp>
#include <seqan3/io/sequence_file/input.hpp>
int main()
{
// Default constructed validator has an empty extension list.
seqan3::input_file_validator validator1{};
seqan3::debug_stream << validator1.get_help_page_message() << '\n';
// Specify your own extensions for the input file.
seqan3::input_file_validator validator2{std::vector{std::string{"exe"}, std::string{"fasta"}}};
seqan3::debug_stream << validator2.get_help_page_message() << '\n';
// Give the seqan3 file type as a template argument to get all valid extensions for this file.
seqan3::input_file_validator<seqan3::sequence_file_input<>> validator3{};
seqan3::debug_stream << validator3.get_help_page_message() << '\n';
return 0;
}
std::string
seqan3::input_file_validator::get_help_page_message
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition validators.hpp:602
input.hpp
Provides seqan3::sequence_file_input and corresponding traits classes.
validators.hpp
Provides some standard validators for (positional) options.
std::vector

Note
  The validator works on every type that can be implicitly converted to std::filesystem::path.

Remarks
  For a complete overview, take a look at Argument Parser

## Constructor & Destructor Documentation

## input_file_validator() [1/2]

template<typename file_t = void>

seqan3::input_file_validator< file_t >::input_file_validator | ( |  | ) |

inline

Default constructor.

If the class' template argument file_t names a valid seqan3 file type that contains a static member valid_formats, e.g. seqan3::sequence_input_file::valid_formats, then it generates the list of valid extensions from this file. Otherwise the extensions list is empty.

## input_file_validator() [2/2]

template<typename file_t = void>

seqan3::input_file_validator< file_t >::input_file_validator | ( | std::vector< std::string > | extensions | ) |

inline explicit

Constructs from a given collection of valid extensions.

Parameters

  [in] | extensions | The valid extensions to validate for.

This constructor is only available if file_t does not name a valid seqan3 file type that contains a static member valid_formats.

## Member Function Documentation

## get_help_page_message()

template<typename file_t = void>

std::string seqan3::input_file_validator< file_t >::get_help_page_message | ( |  | ) | const

inline

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

Implements validator.

## operator()() [1/2]

template<typename file_t = void>
template<std::ranges::forward_range range_type>
requires (std::convertible_to<std::ranges::range_value_t<range_type>, std::filesystem::path const &> && !std::convertible_to<range_type, std::filesystem::path const &>)

void seqan3::file_validator_base::operator() | ( | range_type const & | v | ) | const

inline

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

Template Parameters

  range_type | The type of range to check; must model std::ranges::forward_range and the value type must be convertible to std::filesystem::path.

Parameters

  v | The input range to iterate over and check every element.

Exceptions

  seqan3::validation_error |

## operator()() [2/2]

template<typename file_t = void>

virtual void seqan3::input_file_validator< file_t >::operator() | ( | std::filesystem::path const & | file | ) | const

inline override virtual

Tests whether path is an existing regular file and is readable.

Parameters

  file | The input value to check.

Exceptions

  seqan3::validation_error | if the validation process failed. Might be nested with std::filesystem::filesystem_error on unhandled OS API errors.

Implements seqan3::file_validator_base.

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

- validators.hpp

Hide me