Sharg 1.1.2-rc.1
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>
requires(validator_type validator,
{
{validator.get_help_page_message()} -> std::same_as<std::string>;
}
The concept for option validators passed to add_option/positional_option.
Definition validators.hpp:49
T is_same_v

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.

// 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::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:40
The Sharg command line parser.
Definition parser.hpp:154
A validator that checks whether a value is inside a list of valid values.
Definition validators.hpp:175
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 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.

Hide me