• 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/custom_syntax.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 to support custom options syntax.
8

9
    It's possible to install 'custom_parser'. It will be invoked on all command
10
    line tokens and can return name/value pair, or nothing. If it returns
11
    nothing, usual processing will be done.
12
*/
13

14
#include <hpx/modules/program_options.hpp>
15

16
#include <exception>
17
#include <iostream>
18
#include <string>
19
#include <utility>
20

21
#include <hpx/config/warnings_prefix.hpp>
22

23
using namespace hpx::program_options;
24
using namespace std;
25

26
/*  This custom option parse function recognize gcc-style
27
    option "-fbar" / "-fno-bar".
×
28
*/
29
pair<string, string> reg_foo(string const& s)
×
30
{
31
    if (s.find("-f") == 0)
×
32
    {
×
33
        if (s.substr(2, 3) == "no-")
34
            return make_pair(s.substr(5), string("false"));
×
35
        else
36
            return make_pair(s.substr(2), string("true"));
37
    }
38
    else
×
39
    {
40
        return make_pair(string(), string());
41
    }
42
}
×
43

44
int main(int ac, char* av[])
45
{
46
    try
×
47
    {
48
        options_description desc("Allowed options");
×
49
        // clang-format off
×
50
        desc.add_options()
×
51
            ("help","produce a help message")
52
            ("foo", value<string>(), "just an option")
53
            ;
54
        // clang-format on
×
55

×
56
        variables_map vm;
57
        store(command_line_parser(ac, av)
×
58
                  .options(desc)
×
59
                  .extra_parser(reg_foo)
60
                  .run(),
61
            vm);
×
62

63
        if (vm.count("help"))
×
64
        {
×
65
            cout << desc;
66
            cout << "\nIn addition -ffoo and -fno-foo syntax are recognized.\n";
×
67
        }
68
        if (vm.count("foo"))
×
69
        {
×
70
            cout << "foo value with the value of " << vm["foo"].as<string>()
71
                 << "\n";
×
72
        }
×
73
    }
74
    catch (exception const& e)
×
75
    {
×
76
        cout << e.what() << "\n";
77
    }
78
    return 0;
79
}
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