48template <
typename val
idator_type>
76template <
typename option_value_t>
106 if (!((cmp <=
max) && (cmp >=
min)))
119 template <std::ranges::forward_range range_type>
173template <parsable option_value_t>
198 template <std::ranges::forward_range range_type>
213 template <
typename... option_types>
242 template <std::ranges::forward_range range_type>
245 void operator()(range_type
const & range)
const
248 std::ranges::end(range),
275template <
typename option_type,
typename... option_types>
281template <
typename range_type>
282 requires (std::ranges::forward_range<std::decay_t<range_type>>
287template <
typename option_type,
typename... option_types>
291template <
typename range_type>
292 requires (std::ranges::forward_range<std::decay_t<range_type>>)
349 template <std::ranges::forward_range range_type>
352 void operator()(range_type
const & v)
const
358 this->operator()(cmp);
378 +
" has no extension. Expected one of the "
379 "following valid extensions:"
386 if (file_path.front() ==
'.')
387 file_path.erase(0, 1);
393 auto case_insensitive_ends_with = [&](
std::string const & ext)
402 + all_extensions +
" instead!"};
419 if (
static_cast<bool>(ec))
429 if (!file.is_open() || !file.good())
452 bool is_open = file.is_open();
453 bool is_good = file.good();
456 if (!is_good || !is_open)
478 size_t const suffix_length{suffix.
size()};
479 size_t const str_length{str.
size()};
481 if (suffix_length > str_length)
484 for (
size_t j = 0, s_start = str_length - suffix_length; j < suffix_length; ++j)
485 if (std::tolower(str[s_start + j]) != std::tolower(suffix[j]))
554 using file_validator_base::operator();
595 return "The input file must exist and read permissions must be granted."
708 using file_validator_base::file_validator_base;
712 using file_validator_base::operator();
733 validate_writeability(file);
735 validate_filename(file);
756 if (open_mode == output_file_open_options::open_or_create)
758 return "Write permissions must be granted."
760 + valid_extensions_help_page_message();
764 return "The output file must not exist already and write permissions must be granted."
766 + valid_extensions_help_page_message();
810 using file_validator_base::file_validator_base;
814 using file_validator_base::operator();
835 validate_readability(dir);
856 return "An existing, readable path for the input directory.";
895 using file_validator_base::file_validator_base;
899 using file_validator_base::operator();
916 if (
static_cast<bool>(ec))
924 validate_writeability(dir /
"dummy.txt");
925 dir_guard.remove_all();
929 validate_writeability(dir /
"dummy.txt");
951 return "A valid path for the output directory.";
1002 throw validation_error{
"Value " + cmp +
" did not match the pattern " + pattern +
"."};
1014 template <std::ranges::forward_range range_type>
1018 for (
auto && entry : v)
1032 return "Value must match the pattern '" + pattern +
"'.";
1060 template <
typename option_value_t>
1084template <val
idator val
idator1_type, val
idator val
idator2_type>
1107 vali1{
std::move(vali1_)},
1108 vali2{
std::move(vali2_)}
1123 template <
typename cmp_type>
1134 return vali1.get_help_page_message() +
" " + vali2.get_help_page_message();
1177template <val
idator val
idator1_type, val
idator val
idator2_type>
1180auto operator|(validator1_type && vali1, validator2_type && vali2)
T back_inserter(T... args)
A validator that checks whether a number is inside a given range.
Definition validators.hpp:79
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition validators.hpp:136
std::string valid_range_str
The range as string.
Definition validators.hpp:149
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside [min, max].
Definition validators.hpp:104
option_value_t option_value_type
The type of value that this validator invoked upon.
Definition validators.hpp:82
arithmetic_range_validator(option_value_type const min_, option_value_type const max_)
The constructor.
Definition validators.hpp:91
option_value_type max
Maximum of the range to test.
Definition validators.hpp:146
void operator()(range_type const &range) const
Tests whether every element in range lies inside [min, max].
Definition validators.hpp:121
option_value_type min
Minimum of the range to test.
Definition validators.hpp:143
A safe guard to manage a filesystem entry, e.g. a file or a directory.
Definition safe_filesystem_entry.hpp:35
A helper struct to chain validators recursively via the pipe operator.
Definition validators.hpp:1087
validator1_type vali1
The first validator in the chain.
Definition validators.hpp:1139
validator_chain_adaptor(validator_chain_adaptor const &pf)=default
Defaulted.
validator_chain_adaptor(validator1_type vali1_, validator2_type vali2_)
Constructing from two validators.
Definition validators.hpp:1106
validator2_type vali2
The second validator in the chain.
Definition validators.hpp:1141
validator_chain_adaptor & operator=(validator_chain_adaptor &&)=default
Defaulted.
validator_chain_adaptor & operator=(validator_chain_adaptor const &pf)=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:1132
void operator()(cmp_type const &cmp) const
Calls the operator() of each validator on the value cmp.
Definition validators.hpp:1125
~validator_chain_adaptor()=default
The destructor.
validator_chain_adaptor()=delete
Deleted.
validator_chain_adaptor(validator_chain_adaptor &&)=default
Defaulted.
An abstract base class for the file and directory validators.
Definition validators.hpp:312
file_validator_base()=default
Defaulted.
std::vector< std::string > extensions
Stores the extensions.
Definition validators.hpp:492
void validate_readability(std::filesystem::path const &path) const
Checks if the given path is readable.
Definition validators.hpp:411
void validate_filename(std::filesystem::path const &path) const
Validates the given filename path based on the specified extensions.
Definition validators.hpp:368
virtual void operator()(std::filesystem::path const &path) const =0
Tests if the given path is a valid input, respectively output, file or directory.
file_validator_base & operator=(file_validator_base const &)=default
Defaulted.
std::string extensions_str
The extension range as a std:;string for pretty printing.
Definition validators.hpp:495
virtual ~file_validator_base()=default
std::string valid_extensions_help_page_message() const
Returns the information of valid file extensions.
Definition validators.hpp:463
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:476
std::string option_value_type
Type of values that are tested by validator.
Definition validators.hpp:315
file_validator_base(file_validator_base const &)=default
Defaulted.
file_validator_base(file_validator_base &&)=default
Defaulted.
file_validator_base & operator=(file_validator_base &&)=default
Defaulted.
void validate_writeability(std::filesystem::path const &path) const
Checks if the given path is writable.
Definition validators.hpp:440
A validator that checks if a given path is a valid output directory.
Definition validators.hpp:879
output_directory_validator(output_directory_validator &&)=default
Defaulted.
output_directory_validator(output_directory_validator const &)=default
Defaulted.
output_directory_validator & operator=(output_directory_validator &&)=default
Defaulted.
virtual void operator()(std::filesystem::path const &dir) const override
Tests whether path is writable.
Definition validators.hpp:909
virtual ~output_directory_validator()=default
Virtual Destructor.
output_directory_validator & operator=(output_directory_validator const &)=default
Defaulted.
output_directory_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:949
A validator that checks if a given path is a valid output file.
Definition validators.hpp:641
output_file_validator()=default
Defaulted.
output_file_validator(auto &&... extensions)
Constructs from a parameter pack of valid extensions.
Definition validators.hpp:702
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition validators.hpp:754
virtual void operator()(std::filesystem::path const &file) const override
Tests whether path is does not already exists and is writable.
Definition validators.hpp:722
virtual ~output_file_validator()=default
Virtual Destructor.
output_file_validator(output_file_validator &&)=default
Defaulted.
output_file_validator(std::vector< std::string > const &extensions)
Constructs from a list of valid extensions.
Definition validators.hpp:691
output_file_validator(output_file_open_options const mode, std::vector< std::string > extensions)
Constructs from a given overwrite mode and a list of valid extensions.
Definition validators.hpp:664
output_file_validator(output_file_open_options const mode, auto &&... extensions)
Constructs from a given overwrite mode and a parameter pack of valid extensions.
Definition validators.hpp:680
output_file_open_options open_mode
Stores the current mode of whether it is valid to overwrite the output file.
Definition validators.hpp:772
output_file_validator & operator=(output_file_validator const &)=default
Defaulted.
output_file_validator(output_file_validator const &)=default
Defaulted.
output_file_validator & operator=(output_file_validator &&)=default
Defaulted.
A validator that checks if a matches a regular expression pattern.
Definition validators.hpp:977
void operator()(range_type const &v) const
Tests whether every entry in list v matches the pattern.
Definition validators.hpp:1016
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition validators.hpp:1030
std::string pattern
The pattern to match.
Definition validators.hpp:1037
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside values.
Definition validators.hpp:998
regex_validator(std::string const &pattern_)
Constructing from a vector.
Definition validators.hpp:988
Parser exception thrown when an argument could not be casted to the according type.
Definition exceptions.hpp:180
A validator that checks whether a value is inside a list of valid values.
Definition validators.hpp:175
value_list_validator(option_type, option_types...) -> value_list_validator< option_type >
Deduction guide for a parameter pack.
value_list_validator & operator=(value_list_validator const &)=default
Defaulted.
value_list_validator(value_list_validator const &)=default
Defaulted.
option_value_t option_value_type
Type of values that are tested by validator.
Definition validators.hpp:178
value_list_validator()=default
Defaulted.
value_list_validator(range_type rng)
Constructing from a range.
Definition validators.hpp:200
std::string get_help_page_message() const
Returns a message that can be appended to the (positional) options help page info.
Definition validators.hpp:260
value_list_validator(value_list_validator &&)=default
Defaulted.
void operator()(option_value_type const &cmp) const
Tests whether cmp lies inside values.
Definition validators.hpp:228
std::vector< option_value_type > values
Minimum of the range to test.
Definition validators.hpp:267
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 >
Given a parameter pack of types that are convertible to std::string, delegate to value type std::stri...
~value_list_validator()=default
Defaulted.
value_list_validator & operator=(value_list_validator &&)=default
Defaulted.
value_list_validator(option_types &&... opts)
Constructing from a parameter pack.
Definition validators.hpp:215
value_list_validator(range_type &&rng) -> value_list_validator< std::ranges::range_value_t< range_type > >
Deduction guide for ranges.
The concept for option validators passed to add_option/positional_option.
Definition validators.hpp:49
T create_directory(T... args)
T current_exception(T... args)
T emplace_back(T... args)
Provides parser related exceptions.
std::string to_string(value_types &&... values)
Streams all parameters via std::ostringstream and returns a concatenated string.
Definition to_string.hpp:40
auto operator|(validator1_type &&vali1, validator2_type &&vali2)
Enables the chaining of validators.
Definition validators.hpp:1180
T has_extension(T... args)
T is_directory(T... args)
T is_regular_file(T... args)
T rethrow_exception(T... args)
Provides sharg::detail::safe_filesystem_entry.
Validator that always returns true.
Definition validators.hpp:1055
void operator()(option_value_t const &) const noexcept
Value cmp always passes validation for any type and never throws.
Definition validators.hpp:1061
std::string get_help_page_message() const
Since no validation is happening the help message is empty.
Definition validators.hpp:1065
T throw_with_nested(T... args)
Provides sharg::detail::to_string.
output_file_open_options
Mode of an output file: Determines whether an existing file can be (silently) overwritten.
Definition validators.hpp:606
@ create_new
Forbid overwriting the output file.
@ open_or_create
Allow to overwrite the output file.