Sharg 1.1.0
The argument parser for bio-c++ tools.
 
Loading...
Searching...
No Matches
sharg::validator Concept Reference

The concept for option validators passed to add_option/positional_option. More...

#include <sharg/validators.hpp>

Concept definition

template<typename validator_type>
concept sharg::validator = std::copyable<std::remove_cvref_t<validator_type>> &&
requires(validator_type validator,
{
{validator(value)} -> std::same_as<void>;
{validator.get_help_page_message()} -> std::same_as<std::string>;
}
The concept for option validators passed to add_option/positional_option.
Definition: validators.hpp:50

Detailed Description

The concept for option validators passed to add_option/positional_option.

When adding (positional) options to the sharg::parser you may pass a function object that models sharg::validator which checks the option value provided by the user for some constraint.

Sharg provides several common-use-case validators, e.g. the sharg::arithmetic_range_validator.

#include <sharg/all.hpp>
int main(int argc, char const ** argv)
{
sharg::parser myparser{"Test", argc, argv}; // initialize
int myint;
sharg::value_list_validator my_validator{2, 4, 6, 8, 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 one of [2,4,6,8,10] (e.g. "./test_app -i 3")
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 .
Parser exception that is thrown whenever there is an error while parsing the command line arguments.
Definition: exceptions.hpp:43
The Sharg command line parser.
Definition: parser.hpp:157
A validator that checks whether a value is inside a list of valid values.
Definition: validators.hpp:176
Option struct that is passed to the sharg::parser::add_option() function.
Definition: config.hpp:46
char short_id
The short identifier for the option (e.g. 'a', making the option callable via -a).
Definition: config.hpp:56
T what(T... args)

You can learn more about Sharg validators in our tutorial Validation of (positional) option values.

To implement your own validator please refer to the detailed concept description below.

This entity is stable. Since version 1.0.