generated from seqan/app-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOC] Copy over the cookbook entry about the custom validator (#62)
* [DOC] Copy over the cookbook entry about "how to build your own validator" Signed-off-by: Lydia Buntrock <[email protected]> * Apply suggestions from code review Co-authored-by: Enrico Seiler <[email protected]>
- Loading branch information
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <sharg/argument_parser.hpp> | ||
//![validator] | ||
#include <cmath> | ||
|
||
struct custom_validator | ||
{ | ||
using option_value_type = double; // used for all arithmetic types | ||
|
||
void operator() (option_value_type const & val) const | ||
{ | ||
if ((std::round(val) != val) || // not an integer | ||
(std::pow(std::round(std::sqrt(val)), 2) != val)) // not a square | ||
{ | ||
throw sharg::validation_error{"The provided number is not an arithmetic square."}; | ||
} | ||
} | ||
|
||
std::string get_help_page_message () const | ||
{ | ||
return "Value must be the square of an integral number."; | ||
} | ||
}; | ||
//![validator] | ||
|
||
static_assert(sharg::validator<custom_validator>); | ||
|
||
//![main] | ||
int main(int argc, char ** argv) | ||
{ | ||
sharg::argument_parser myparser("Test-Parser", argc, argv); | ||
|
||
int32_t variable{}; | ||
int16_t variable2{}; | ||
|
||
myparser.add_option(variable, 'i', "", "An int that is a square", sharg::option_spec::standard, | ||
custom_validator{}); // ← your validator is used! | ||
|
||
myparser.add_option(variable2, 'j', "", "An int that is a square and within [0,20].", sharg::option_spec::standard, | ||
custom_validator{} | sharg::arithmetic_range_validator{0, 20}); // ← now it's chained | ||
|
||
try | ||
{ | ||
myparser.parse(); // trigger command line parsing | ||
} | ||
catch (sharg::argument_parser_error const & ext) | ||
{ | ||
std::cout << ext.what() << '\n'; | ||
return -1; | ||
} | ||
std::cout << "Yeah!\n"; | ||
|
||
return 0; | ||
} | ||
//![main] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Test-Parser | ||
=========== | ||
Try -h or --help for more information. |
fbaf3e4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: