File: custom_validator_solution2.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (55 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <seqan3/argument_parser/all.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 seqan3::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(seqan3::validator<custom_validator>);

//![main]
int main(int argc, char ** argv)
{
    seqan3::argument_parser myparser("Test-Parser", argc, argv);

    int32_t variable{};
    int16_t variable2{};

    myparser.add_option(variable, 'i', "", "An int that is a square", seqan3::option_spec::DEFAULT,
                        custom_validator{}); // ← your validator is used!

    myparser.add_option(variable2, 'j', "", "An int that is a square and within [0,20].", seqan3::option_spec::DEFAULT,
                        custom_validator{} | seqan3::arithmetic_range_validator{0, 20}); // ← now it's chained

    try
    {
         myparser.parse(); // trigger command line parsing
    }
    catch (seqan3::argument_parser_error const & ext)
    {
        seqan3::debug_stream << ext.what() << '\n';
        return -1;
    }

    seqan3::debug_stream << "Yeah!\n";

    return 0;
}
//![main]