summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/program_options/example/option_groups.cpp
blob: 63e1fca48bed33915a80f9e37ce251a80f72e4f6 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Vladimir Prus 2002-2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

/** This example shows how to handle options groups.

    For a test, run:

    option_groups --help
    option_groups --num-threads 10
    option_groups --help-module backend

    The first invocation would show to option groups, and will not show the
    '--num-threads' options. The second invocation will still get the value of
    the hidden '--num-threads' option. Finally, the third invocation will show
    the options for the 'backend' module, including the '--num-threads' option.

*/


#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
#include <boost/tokenizer.hpp>
#include <boost/token_functions.hpp>
using namespace boost;
using namespace boost::program_options;

#include <iostream>
#include <fstream>
#include <exception>
using namespace std;

int main(int ac, char* av[])
{
    try {
        // Declare three groups of options.
        options_description general("General options");
        general.add_options()
            ("help", "produce a help message")
            ("help-module", value<string>(),
                "produce a help for a given module")
            ("version", "output the version number")
            ;

        options_description gui("GUI options");
        gui.add_options()
            ("display", value<string>(), "display to use")
            ;

        options_description backend("Backend options");
        backend.add_options()
            ("num-threads", value<int>(), "the initial number of threads")
            ;
            
        // Declare an options description instance which will include
        // all the options
        options_description all("Allowed options");
        all.add(general).add(gui).add(backend);

        // Declare an options description instance which will be shown
        // to the user
        options_description visible("Allowed options");
        visible.add(general).add(gui);
           

        variables_map vm;
        store(parse_command_line(ac, av, all), vm);

        if (vm.count("help")) 
        {
            cout << visible;
            return 0;
        }
        if (vm.count("help-module")) {
            const string& s = vm["help-module"].as<string>();
            if (s == "gui") {
                cout << gui;
            } else if (s == "backend") {
                cout << backend;
            } else {
                cout << "Unknown module '" 
                     << s << "' in the --help-module option\n";
                return 1;
            }
            return 0;
        }
        if (vm.count("num-threads")) {
            cout << "The 'num-threads' options was set to "
                 << vm["num-threads"].as<int>() << "\n";            
        }                           
    }
    catch(std::exception& e) {
        cout << e.what() << "\n";
    }
}