Sharg 1.1.2-rc.1
The argument parser for bio-c++ tools.
Loading...
Searching...
No Matches
sharg::arithmetic_range_validator< option_value_t > Class Template Reference

A validator that checks whether a number is inside a given range. More...

#include <sharg/validators.hpp>

+ Inheritance diagram for sharg::arithmetic_range_validator< option_value_t >:

Public Types

using option_value_type = option_value_t
 The type of value that this validator invoked upon.
 

Public Member Functions

 arithmetic_range_validator (option_value_type const min_, option_value_type const max_)
 The constructor.
 
void operator() (option_value_type const &cmp) const
 Tests whether cmp lies inside [min, max].
 
template<std::ranges::forward_range range_type>
requires std::is_arithmetic_v<std::ranges::range_value_t<range_type>>
void operator() (range_type const &range) const
 Tests whether every element in range lies inside [min, max].
 
std::string get_help_page_message () const
 Returns a message that can be appended to the (positional) options help page info.
 

Private Attributes

option_value_type min {}
 Minimum of the range to test.
 
option_value_type max {}
 Maximum of the range to test.
 
std::string valid_range_str {}
 The range as string.
 

Detailed Description

template<typename option_value_t>
requires std::is_arithmetic_v<option_value_t>
class sharg::arithmetic_range_validator< option_value_t >

A validator that checks whether a number is inside a given range.

Template Parameters
option_value_tThe value type of the range; must model std::is_arithmetic_v.

On construction, the validator must receive a maximum and a minimum number. The class than acts as a functor, that throws a sharg::validation_error exception whenever a given value does not lie inside the given min/max range.

// 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
int myint;
sharg::arithmetic_range_validator my_validator{2, 10};
myparser.add_option(myint,
.long_id = "integer",
.description = "Give me a number.",
.validator = my_validator});
// an exception will be thrown if the user specifies an integer
// that is not in range [2,10] (e.g. "./test_app -i 15")
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::cerr << "integer given by user passed validation: " << myint << "\n";
return 0;
}
Meta-header for the Parser module .
A validator that checks whether a number is inside a given range.
Definition validators.hpp:79
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
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)
Remarks
For a complete overview, take a look at Parser

This entity is stable. Since version 1.0.

Constructor & Destructor Documentation

◆ arithmetic_range_validator()

template<typename option_value_t >
sharg::arithmetic_range_validator< option_value_t >::arithmetic_range_validator ( option_value_type const min_,
option_value_type const max_ )
inline

The constructor.

Parameters
[in]min_Minimum set for the range to test.
[in]max_Maximum set for the range to test.

This entity is stable. Since version 1.0.

Member Function Documentation

◆ get_help_page_message()

template<typename option_value_t >
std::string sharg::arithmetic_range_validator< option_value_t >::get_help_page_message ( ) const
inline

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

This entity is stable. Since version 1.0.

◆ operator()() [1/2]

template<typename option_value_t >
void sharg::arithmetic_range_validator< option_value_t >::operator() ( option_value_type const & cmp) const
inline

Tests whether cmp lies inside [min, max].

Parameters
cmpThe input value to check.
Exceptions
sharg::validation_error

This entity is stable. Since version 1.0.

◆ operator()() [2/2]

template<typename option_value_t >
template<std::ranges::forward_range range_type>
requires std::is_arithmetic_v<std::ranges::range_value_t<range_type>>
void sharg::arithmetic_range_validator< option_value_t >::operator() ( range_type const & range) const
inline

Tests whether every element in range lies inside [min, max].

Template Parameters
range_typeThe type of range to check; must model std::ranges::forward_range. The value type must model std::is_arithmetic_v.
Parameters
rangeThe input range to iterate over and check every element.
Exceptions
sharg::validation_error

This entity is stable. Since version 1.0.


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