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.
int main(int argc, char const ** argv)
{
int myint;
myparser.add_option(myint,
.long_id = "integer",
.description = "Give me a number.",
.validator = my_validator});
try
{
myparser.parse();
}
{
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
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.