• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libs/core/program_options/examples/regex.cpp
1
// Copyright Vladimir Prus 2002-2004.
2
//  SPDX-License-Identifier: BSL-1.0
3
// Distributed under the Boost Software License, Version 1.0.
4
// (See accompanying file LICENSE_1_0.txt
5
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
// This example shows how a user-defined class can be parsed using
8
// specific mechanism -- not the iostream operations used by default.
9
//
10
// A new class 'magic_number' is defined and the 'validate' method is overloaded
11
// to validate the values of that class using Boost.Regex.
12
// To test, run
13
//
14
//    regex -m 123-456
15
//    regex -m 123-4567
16
//
17
// The first invocation should output:
18
//
19
//   The magic is "456"
20
//
21
// and the second invocation should issue an error message.
22

23
#include <hpx/modules/datastructures.hpp>
24
#include <hpx/modules/format.hpp>
25
#include <hpx/modules/program_options.hpp>
26

27
#include <iostream>
28
#include <regex>
29
#include <string>
30
#include <vector>
31

32
#include <hpx/config/warnings_prefix.hpp>
33

34
using namespace hpx::program_options;
35

36
/* Define a completely non-sensical class. */
37
struct magic_number
38
{
39
public:
40
    magic_number(int n)
41
      : n(n)
42
    {
43
    }
44
    int n;
45
};
×
46

47
bool operator==(magic_number const& lhs, magic_number const& rhs)
×
48
{
49
    return lhs.n == rhs.n;
50
}
51

52
/* Overload the 'validate' function for the user-defined class.
53
   It makes sure that value is of form XXX-XXX
54
   where X are digits and converts the second group to an integer.
55
   This has no practical meaning, meant only to show how
56
   regex can be used to validate values.
×
57
*/
58
void validate(
59
    any& v, std::vector<std::string> const& values, magic_number*, int)
×
60
{
61
    static std::regex r(R"(\d\d\d-(\d\d\d))");
62

×
63
    // Make sure no previous assignment to 'a' was made.
64
    validators::check_first_occurrence(v);
65

66
    // Extract the first string from 'values'. If there is more than
×
67
    // one string, it's an error, and exception will be thrown.
68
    std::string const& s = validators::get_single_string(values);
69

70
    // Do regex match and convert the interesting part to
71
    // int.
×
72
    std::smatch match;
73
    if (regex_match(s, match, r))
×
74
    {
75
        v = any(magic_number(hpx::util::from_string<int>(match[1])));
76
    }
77
    else
×
78
    {
79
        throw validation_error(validation_error::invalid_option_value);
×
80
    }
81
}
×
82

83
int main(int ac, char* av[])
84
{
85
    try
×
86
    {
87
        options_description desc("Allowed options");
×
88
        // clang-format off
×
89
        desc.add_options()
×
90
            ("help","produce a help screen")
×
91
            ("version,v", "print the version number")
92
            ("magic,m", value<magic_number>(),
93
                "magic value (in NNN-NNN format)")
94
            ;
95
        // clang-format on
×
96

×
97
        variables_map vm;
98
        store(parse_command_line(ac, av, desc), vm);
×
99

100
        if (vm.count("help"))
×
101
        {
×
102
            std::cout << "Usage: regex [options]\n";
103
            std::cout << desc;
104
            return 0;
×
105
        }
106
        if (vm.count("version"))
×
107
        {
108
            std::cout << "Version 1.\n";
109
            return 0;
×
110
        }
111
        if (vm.count("magic"))
×
112
        {
×
113
            std::cout << "The magic is \"" << vm["magic"].as<magic_number>().n
114
                      << "\"\n";
×
115
        }
×
116
    }
117
    catch (std::exception const& e)
×
118
    {
×
119
        std::cout << e.what() << "\n";
120
    }
121
    return 0;
122
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc