Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
Validators

The Validators Module. More...

Concepts

concept  sharg::validator
 The concept for option validators passed to add_option/positional_option.
 

Classes

class  sharg::arithmetic_range_validator< option_value_t >
 A validator that checks whether a number is inside a given range. More...
 
class  sharg::value_list_validator< option_value_t >
 A validator that checks whether a value is inside a list of valid values. More...
 
class  sharg::file_validator_base
 An abstract base class for the file and directory validators. More...
 
class  sharg::input_file_validator
 A validator that checks if a given path is a valid input file. More...
 
class  sharg::output_file_validator
 A validator that checks if a given path is a valid output file. More...
 
class  sharg::input_directory_validator
 A validator that checks if a given path is a valid input directory. More...
 
class  sharg::output_directory_validator
 A validator that checks if a given path is a valid output directory. More...
 
class  sharg::regex_validator
 A validator that checks if a matches a regular expression pattern. More...
 
struct  sharg::detail::default_validator
 Validator that always returns true. More...
 
class  sharg::detail::validator_chain_adaptor< validator1_type, validator2_type >
 A helper struct to chain validators recursively via the pipe operator. More...
 

Functions

template<validator validator1_type, validator validator2_type>
requires std::common_with<typename std::remove_reference_t<validator1_type>::option_value_type, typename std::remove_reference_t<validator2_type>::option_value_type>
auto sharg::operator| (validator1_type &&vali1, validator2_type &&vali2)
 Enables the chaining of validators.
 

Detailed Description

The Validators Module.

Function Documentation

◆ operator|()

template<validator validator1_type, validator validator2_type>
requires std::common_with<typename std::remove_reference_t<validator1_type>::option_value_type, typename std::remove_reference_t<validator2_type>::option_value_type>
auto sharg::operator| ( validator1_type && vali1,
validator2_type && vali2 )

Enables the chaining of validators.

Template Parameters
validator1_typeThe type of the fist validator; Must satisfy the sharg::validator and the same option_value_type as the second validator type.
validator2_typeThe type of the second validator; Must satisfy the sharg::validator and the same option_value_type as the fist validator type.
Parameters
[in]vali1The first validator to chain.
[in]vali2The second validator to chain.
Returns
A new validator that tests a value for both vali1 and vali2.

The pipe operator is the AND operation for two validators, which means that a value must pass both validators in order to be accepted by the new validator.

For example you may want a file name that only accepts absolute paths but also must have one out of some given file extensions. For this purpose you can chain a sharg::regex_validator to a sharg::input_file_validator like this:

// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: CC0-1.0
#include <sharg/all.hpp>
int main(int argc, char const ** argv)
{
sharg::parser myparser{"Test", argc, argv}; // initialize
std::string file_name;
sharg::regex_validator absolute_path_validator{"(/[^/]+)+/.*\\.[^/\\.]+$"};
sharg::input_file_validator my_file_ext_validator{{"sa", "so"}};
myparser.add_option(file_name,
.long_id = "file",
.description = "Give me a file name with an absolute path.",
.validator = absolute_path_validator | my_file_ext_validator});
// an exception will be thrown if the user specifies a file name
// that is not an absolute path or does not have one of the file extension [sa,so]
try
{
myparser.parse();
}
catch (sharg::parser_error const & ext) // the user did something wrong
{
std::cerr << "[PARSER ERROR] " << ext.what() << "\n"; // customize your error message
return -1;
}
std::cout << "filename given by user passed validation: " << file_name << "\n";
return 0;
}
Meta-header for the Parser module .
A validator that checks if a given path is a valid input file.
Definition validators.hpp:521
Parser exception that is thrown whenever there is an error while parsing the command line arguments.
Definition exceptions.hpp:40
The Sharg command line parser.
Definition parser.hpp:154
A validator that checks if a matches a regular expression pattern.
Definition validators.hpp:976
Option struct that is passed to the sharg::parser::add_option() function.
Definition config.hpp:43
char short_id
The short identifier for the option (e.g. 'a', making the option callable via -a).
Definition config.hpp:53
T what(T... args)

You can chain as many validators as you want which will be evaluated one after the other from left to right (first to last).

Remarks
For a complete overview, take a look at Parser

This entity is stable. Since version 1.0.

Hide me