94template <
typename val
idator_type>
95SEQAN3_CONCEPT
validator = std::copyable<std::remove_cvref_t<validator_type>> &&
119template <arithmetic option_value_t>
140 if (!((cmp <= max) && (cmp >= min)))
141 throw validation_error{detail::to_string(
"Value ", cmp,
" is not in range [", min,
",", max,
"].")};
150 template <std::ranges::forward_range range_type>
156 std::for_each(range.begin(), range.end(), [&] (
auto cmp) { (*this)(cmp); });
162 return detail::to_string(
"Value must be in range [", min,
",", max,
"].");
190template <
typename option_value_t>
212 template <std::ranges::forward_range range_type>
214 requires std::constructible_from<option_value_type, std::ranges::range_rvalue_reference_t<range_type>>
219 std::ranges::move(std::move(rng), std::cpp20::back_inserter(values));
227 template <
typename ...option_types>
229 requires ((std::constructible_from<option_value_type, option_types> && ...))
233 (values.emplace_back(std::forward<option_types>(opts)), ...);
243 if (!(
std::find(values.begin(), values.end(), cmp) != values.end()))
244 throw validation_error{detail::to_string(
"Value ", cmp,
" is not one of ", std::views::all(values),
".")};
252 template <std::ranges::forward_range range_type>
254 requires std::convertible_to<std::ranges::range_value_t<range_type>,
option_value_type>
258 std::for_each(std::ranges::begin(range), std::ranges::end(range), [&] (
auto cmp) { (*this)(cmp); });
264 return detail::to_string(
"Value must be one of ", std::views::all(values),
".");
278template <
typename option_type,
typename ...option_types>
280 requires (std::constructible_from<std::string, std::decay_t<option_types>> && ... &&
281 std::constructible_from<std::string, std::decay_t<option_type>>)
286template <
typename range_type>
288 requires (std::ranges::forward_range<std::decay_t<range_type>> &&
289 std::constructible_from<std::string, std::ranges::range_value_t<range_type>>)
294template <
typename option_type,
typename ...option_types>
298template <
typename range_type>
300 requires (std::ranges::forward_range<std::decay_t<range_type>>)
351 template <std::ranges::forward_range range_type>
354 && !std::convertible_to<range_type, std::filesystem::path const &>)
356 void operator()(range_type
const & v)
const
358 std::for_each(v.begin(), v.end(), [&] (
auto cmp) { this->operator()(cmp); });
375 throw validation_error{detail::to_string(
"The given filename ", path.
string(),
" has no extension. Expected"
376 " one of the following valid extensions:",
extensions,
"!")};
381 if (file_path.front() ==
'.')
382 file_path.erase(0, 1);
388 auto case_insensitive_ends_with = [&] (
std::string const & ext)
397 "! Got ", all_extensions,
" instead!")};
414 if (
static_cast<bool>(ec))
415 throw validation_error{detail::to_string(
"Cannot read the directory ", path ,
"!")};
421 throw validation_error{detail::to_string(
"Expected a regular file ", path,
"!")};
424 if (!file.is_open() || !file.good())
425 throw validation_error{detail::to_string(
"Cannot read the file ", path,
"!")};
438 detail::safe_filesystem_entry file_guard{path};
440 bool is_open = file.is_open();
441 bool is_good = file.good();
444 if (!is_good || !is_open)
466 size_t const suffix_length{suffix.
size()};
467 size_t const str_length{str.
size()};
468 return suffix_length > str_length ?
470 std::ranges::equal(str.
substr(str_length - suffix_length), suffix, [] (
char const chr1,
char const chr2)
472 return std::tolower(chr1) == std::tolower(chr2);
502template <
typename file_t =
void>
507 static_assert(std::same_as<file_t, void> || detail::has_type_valid_formats<file_t>,
508 "Expected either a template type with a static member called valid_formats (a file type) or void.");
527 if constexpr (!std::same_as<file_t, void>)
547 requires std::same_as<file_t, void>
559 using file_validator_base::operator();
571 throw validation_error{detail::to_string(
"The file ", file,
" does not exist!")};
594 return "The input file must exist and read permissions must be granted." +
634template <
typename file_t =
void>
638 static_assert(std::same_as<file_t, void> || detail::has_type_valid_formats<file_t>,
639 "Expected either a template type with a static member called valid_formats (a file type) or void.");
685 if constexpr (!std::same_as<file_t, void>)
686 return detail::valid_file_extensions<typename file_t::valid_formats>();
691 using file_validator_base::operator();
703 throw validation_error{detail::to_string(
"The file ", file,
" already exists!")};
728 return "The output file must not exist already and write permissions must be granted." +
772 using file_validator_base::operator();
784 throw validation_error{detail::to_string(
"The directory ", dir,
" does not exists!")};
787 throw validation_error{detail::to_string(
"The path ", dir,
" is not a directory!")};
807 return detail::to_string(
"An existing, readable path for the input directory.");
846 using file_validator_base::operator();
860 if (
static_cast<bool>(ec))
861 throw validation_error{detail::to_string(
"Cannot create directory: ", dir,
"!")};
867 detail::safe_filesystem_entry dir_guard{dir};
869 dir_guard.remove_all();
891 return detail::to_string(
"A valid path for the output directory.");
933 throw validation_error{detail::to_string(
"Value ", cmp,
" did not match the pattern ", pattern,
".")};
942 template <std::ranges::forward_range range_type>
944 requires std::convertible_to<std::ranges::range_reference_t<range_type>,
option_value_type const &>
948 for (
auto && file_name : v)
958 return detail::to_string(
"Value must match the pattern '", pattern,
"'.");
979template <
typename option_value_t>
980struct default_validator
983 using option_value_type = option_value_t;
986 void operator()(option_value_t
const & )
const noexcept
1007template <val
idator val
idator1_type, val
idator val
idator2_type>
1009 requires std::common_with<typename validator1_type::option_value_type, typename validator2_type::option_value_type>
1011class validator_chain_adaptor
1016 typename validator2_type::option_value_type>;
1021 validator_chain_adaptor() =
delete;
1022 validator_chain_adaptor(validator_chain_adaptor
const & pf) =
default;
1023 validator_chain_adaptor & operator=(validator_chain_adaptor
const & pf) =
default;
1024 validator_chain_adaptor(validator_chain_adaptor &&) =
default;
1025 validator_chain_adaptor & operator=(validator_chain_adaptor &&) =
default;
1031 validator_chain_adaptor(validator1_type vali1_, validator2_type vali2_) :
1032 vali1{
std::move(vali1_)}, vali2{
std::move(vali2_)}
1036 ~validator_chain_adaptor() =
default;
1047 template <
typename cmp_type>
1049 requires std::invocable<validator1_type, cmp_type const> && std::invocable<validator2_type, cmp_type const>
1060 return detail::to_string(vali1.get_help_page_message(),
" ", vali2.get_help_page_message());
1065 validator1_type vali1;
1067 validator2_type vali2;
1099template <val
idator val
idator1_type, val
idator val
idator2_type>
1101 requires std::common_with<typename std::remove_reference_t<validator1_type>::option_value_type,
1104auto operator|(validator1_type && vali1, validator2_type && vali2)
1106 return detail::validator_chain_adaptor{std::forward<validator1_type>(vali1),
1107 std::forward<validator2_type>(vali2)};
Adaptations of algorithms from the Ranges TS.
Provides various type traits on generic types.
A validator that checks whether a number is inside a given range.
Definition: validators.hpp:121
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside [min, max].
Definition: validators.hpp:138
option_value_t option_value_type
The type of value that this validator invoked upon.
Definition: validators.hpp:124
void operator()(range_type const &range) const
Tests whether every element in range lies inside [min, max].
Definition: validators.hpp:154
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition: validators.hpp:160
arithmetic_range_validator(option_value_type const min_, option_value_type const max_)
The constructor.
Definition: validators.hpp:130
An abstract base class for the file and directory validators.
Definition: validators.hpp:318
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.
Definition: validators.hpp:464
void validate_filename(std::filesystem::path const &path) const
Validates the given filename path based on the specified extensions.
Definition: validators.hpp:367
std::string valid_extensions_help_page_message() const
Returns the information of valid file extensions.
Definition: validators.hpp:451
virtual void operator()(std::filesystem::path const &path) const =0
Tests if the given path is a valid input, respectively output, file or directory.
std::string option_value_type
Type of values that are tested by validator.
Definition: validators.hpp:322
file_validator_base(file_validator_base &&)=default
Defaulted.
file_validator_base & operator=(file_validator_base &&)=default
Defaulted.
void validate_readability(std::filesystem::path const &path) const
Checks if the given path is readable.
Definition: validators.hpp:407
file_validator_base()=default
Defaulted.
file_validator_base(file_validator_base const &)=default
Defaulted.
std::vector< std::string > extensions
Stores the extensions.
Definition: validators.hpp:477
virtual ~file_validator_base()=default
file_validator_base & operator=(file_validator_base const &)=default
Defaulted.
void validate_writeability(std::filesystem::path const &path) const
Checks if the given path is writable.
Definition: validators.hpp:435
A validator that checks if a given path is a valid output directory.
Definition: validators.hpp:826
output_directory_validator()=default
Defaulted.
output_directory_validator & operator=(output_directory_validator const &)=default
Defaulted.
virtual ~output_directory_validator()=default
Virtual Destructor.
output_directory_validator(output_directory_validator &&)=default
Defaulted.
output_directory_validator(output_directory_validator const &)=default
Defaulted.
virtual void operator()(std::filesystem::path const &dir) const override
Tests whether path is writable.
Definition: validators.hpp:853
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition: validators.hpp:889
output_directory_validator & operator=(output_directory_validator &&)=default
Defaulted.
A validator that checks if a given path is a valid output file.
Definition: validators.hpp:636
static std::vector< std::string > default_extensions()
The default extensions of file_t.
Definition: validators.hpp:683
output_file_validator(output_file_validator &&)=default
Defaulted.
output_file_validator(output_file_validator const &)=default
Defaulted.
virtual void operator()(std::filesystem::path const &file) const override
Tests whether path is does not already exists and is writable.
Definition: validators.hpp:698
output_file_validator()
Default constructor.
Definition: validators.hpp:649
output_file_validator & operator=(output_file_validator const &)=default
Defaulted.
output_file_validator & operator=(output_file_validator &&)=default
Defaulted.
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition: validators.hpp:723
virtual ~output_file_validator()=default
Virtual Destructor.
output_file_validator(output_file_open_options const mode, std::vector< std::string > extensions=default_extensions())
Constructs from a given overwrite mode and a list of valid extensions.
Definition: validators.hpp:664
A validator that checks if a matches a regular expression pattern.
Definition: validators.hpp:913
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition: validators.hpp:956
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside values.
Definition: validators.hpp:929
void operator()(range_type const &v) const
Tests whether every filename in list v matches the pattern.
Definition: validators.hpp:946
std::string option_value_type
Type of values that are tested by validator.
Definition: validators.hpp:916
regex_validator(std::string const &pattern_)
Constructing from a vector.
Definition: validators.hpp:921
Argument parser exception thrown when an argument could not be casted to the according type.
Definition: exceptions.hpp:115
A validator that checks whether a value is inside a list of valid values.
Definition: validators.hpp:192
value_list_validator()=default
Defaulted.
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside values.
Definition: validators.hpp:241
value_list_validator(value_list_validator const &)=default
Defaulted.
value_list_validator & operator=(value_list_validator const &)=default
Defaulted.
option_value_t option_value_type
Type of values that are tested by validator.
Definition: validators.hpp:195
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition: validators.hpp:262
value_list_validator(option_type, option_types ...) -> value_list_validator< option_type >
Deduction guide for a parameter pack.
void operator()(range_type const &range) const
Tests whether every element in range lies inside values.
Definition: validators.hpp:256
value_list_validator(value_list_validator &&)=default
Defaulted.
~value_list_validator()=default
Defaulted.
value_list_validator(option_types &&...opts)
Constructing from a parameter pack.
Definition: validators.hpp:231
value_list_validator(range_type &&rng) -> value_list_validator< std::string >
Deduction guide for ranges over a value type convertible to std::string.
value_list_validator(option_type, option_types...) -> value_list_validator< std::string >
Type deduction guides.
value_list_validator & operator=(value_list_validator &&)=default
Defaulted.
value_list_validator(range_type &&rng) -> value_list_validator< std::ranges::range_value_t< range_type > >
Deduction guide for ranges.
value_list_validator(range_type rng)
Constructing from a range.
Definition: validators.hpp:216
T create_directory(T... args)
T current_exception(T... args)
Provides parser related exceptions.
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
This header includes C++17 filesystem support and imports it into namespace std::filesystem (independ...
auto operator|(validator1_type &&vali1, validator2_type &&vali2)
Enables the chaining of validators.
Definition: validators.hpp:1104
constexpr ptrdiff_t find_if
Get the index of the first type in a pack that satisfies the given predicate.
Definition: traits.hpp:210
constexpr auto join_with
A join view, please use std::views::join if you don't need a separator.
Definition: join_with.hpp:29
T has_extension(T... args)
A type that satisfies std::is_arithmetic_v<t>.
The concept for option validators passed to add_option/positional_option.
void operator()(option_value_type const &cmp) const
Validates the value 'cmp' and throws a seqan3::validation_error on failure.
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
using option_value_type
The type of value on which the validator is called on.
Provides various utility functions.
T is_directory(T... args)
T is_regular_file(T... args)
Provides seqan3::views::join_with.
The main SeqAn3 namespace.
Definition: cigar_operation_table.hpp:2
output_file_open_options
Mode of an output file: Determines whether an existing file can be (silently) overwritten.
Definition: validators.hpp:601
@ create_new
Forbid overwriting the output file.
@ open_or_create
Allow to overwrite the output file.
SeqAn specific customisations in the standard namespace.
Provides seqan3::debug_stream and related types.
Adaptations of concepts from the Ranges TS.
T rethrow_exception(T... args)
Provides seqan3::detail::safe_filesystem_entry.
T throw_with_nested(T... args)
Auxiliary for pretty printing of exception messages.
Provides traits for seqan3::type_list.
Provides various traits for template packs.