Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
sharg::file_validator_base Class Referenceabstract

An abstract base class for the file and directory validators. More...

#include <sharg/validators.hpp>

+ Inheritance diagram for sharg::file_validator_base:

Public Types

using option_value_type = std::string
 Type of values that are tested by validator.
 

Public Member Functions

virtual void operator() (std::filesystem::path const &path) const =0
 Tests if the given path is a valid input, respectively output, file or directory.
 
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.
 
Constructors, destructor and assignment
 file_validator_base ()=default
 Defaulted.
 
 file_validator_base (file_validator_base const &)=default
 Defaulted.
 
 file_validator_base (file_validator_base &&)=default
 Defaulted.
 
file_validator_baseoperator= (file_validator_base const &)=default
 Defaulted.
 
file_validator_baseoperator= (file_validator_base &&)=default
 Defaulted.
 
virtual ~file_validator_base ()=default
 

Protected Member Functions

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.
 
std::string valid_extensions_help_page_message () const
 Returns the information of valid file extensions.
 
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.
 

Protected Attributes

std::vector< std::stringextensions {}
 Stores the extensions.
 
std::string extensions_str {}
 The extension range as a std:;string for pretty printing.
 

Detailed Description

An abstract base class for the file and directory validators.

This class provides a common interface for sharg::input_file_validator and the sharg::output_file_validator as well as the sharg::input_directory_validator and sharg::output_directory_validator.

The type can be further specialised for the sharg::input_file_validator and the sharg::output_file_validator using the template argument to determine the valid extensions from the given file type.

Remarks
For a complete overview, take a look at Parser

This entity is experimental and subject to change in the future. Experimental since version 1.0.

Constructor & Destructor Documentation

◆ ~file_validator_base()

virtual sharg::file_validator_base::~file_validator_base ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ case_insensitive_string_ends_with()

bool sharg::file_validator_base::case_insensitive_string_ends_with ( std::string_view str,
std::string_view suffix ) const
inlineprotected

Helper function that checks if a string is a suffix of another string. Case insensitive.

Parameters
strThe string to be searched.
suffixThe suffix to be searched for.
Returns
true if suffix is a suffix of str, otherwise false.

◆ operator()() [1/2]

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 sharg::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_typeThe type of range to check; must model std::ranges::forward_range and the value type must be convertible to std::filesystem::path.
Parameters
vThe input range to iterate over and check every element.
Exceptions
sharg::validation_error

This entity is experimental and subject to change in the future. Experimental since version 1.0.

◆ operator()() [2/2]

virtual void sharg::file_validator_base::operator() ( std::filesystem::path const & path) const
pure virtual

Tests if the given path is a valid input, respectively output, file or directory.

Parameters
pathThe path to validate.

This is a pure virtual function and must be overloaded by the derived class.

This entity is experimental and subject to change in the future. Experimental since version 1.0.

Implemented in sharg::input_directory_validator, sharg::output_directory_validator, sharg::input_file_validator, and sharg::output_file_validator.

◆ validate_filename()

void sharg::file_validator_base::validate_filename ( std::filesystem::path const & path) const
inlineprotected

Validates the given filename path based on the specified extensions.

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

◆ validate_readability()

void sharg::file_validator_base::validate_readability ( std::filesystem::path const & path) const
inlineprotected

Checks if the given path is readable.

Parameters
pathThe path to check.
Exceptions
sharg::validation_errorif the path is not readable, or std::filesystem::filesystem_error on underlying OS API errors.

◆ validate_writeability()

void sharg::file_validator_base::validate_writeability ( std::filesystem::path const & path) const
inlineprotected

Checks if the given path is writable.

Parameters
pathThe path to check.
Exceptions
sharg::validation_errorif the given path is a directory.
sharg::validation_errorif the file could not be opened for writing.
std::filesystem::filesystem_erroron underlying OS API errors.

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