1 | // Copyright Vladimir Prus 2002-2004. |
---|
2 | // Distributed under the Boost Software License, Version 1.0. |
---|
3 | // (See accompanying file LICENSE_1_0.txt |
---|
4 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | /** This example shows how to handle options groups. |
---|
7 | |
---|
8 | For a test, run: |
---|
9 | |
---|
10 | option_groups --help |
---|
11 | option_groups --num-threads 10 |
---|
12 | option_groups --help-module backend |
---|
13 | |
---|
14 | The first invocation would show to option groups, and will not show the |
---|
15 | '--num-threads' options. The second invocation will still get the value of |
---|
16 | the hidden '--num-threads' option. Finally, the third invocation will show |
---|
17 | the options for the 'backend' module, including the '--num-threads' option. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | #include <boost/program_options/options_description.hpp> |
---|
23 | #include <boost/program_options/parsers.hpp> |
---|
24 | #include <boost/program_options/variables_map.hpp> |
---|
25 | #include <boost/tokenizer.hpp> |
---|
26 | #include <boost/token_functions.hpp> |
---|
27 | using namespace boost; |
---|
28 | using namespace boost::program_options; |
---|
29 | |
---|
30 | #include <iostream> |
---|
31 | #include <fstream> |
---|
32 | using namespace std; |
---|
33 | |
---|
34 | |
---|
35 | int main(int ac, char* av[]) |
---|
36 | { |
---|
37 | try { |
---|
38 | // Declare three groups of options. |
---|
39 | options_description general("General options"); |
---|
40 | general.add_options() |
---|
41 | ("help", "produce a help message") |
---|
42 | ("help-module", value<string>()->implicit(), |
---|
43 | "produce a help for a given module") |
---|
44 | ("version", "output the version number") |
---|
45 | ; |
---|
46 | |
---|
47 | options_description gui("GUI options"); |
---|
48 | gui.add_options() |
---|
49 | ("display", value<string>(), "display to use") |
---|
50 | ; |
---|
51 | |
---|
52 | options_description backend("Backend options"); |
---|
53 | backend.add_options() |
---|
54 | ("num-threads", value<int>(), "the initial number of threads") |
---|
55 | ; |
---|
56 | |
---|
57 | // Declare an options description instance which will include |
---|
58 | // all the options |
---|
59 | options_description all("Allowed options"); |
---|
60 | all.add(general).add(gui).add(backend); |
---|
61 | |
---|
62 | // Declare an options description instance which will be shown |
---|
63 | // to the user |
---|
64 | options_description visible("Allowed options"); |
---|
65 | visible.add(general).add(gui); |
---|
66 | |
---|
67 | |
---|
68 | variables_map vm; |
---|
69 | store(parse_command_line(ac, av, all), vm); |
---|
70 | |
---|
71 | if (vm.count("help")) |
---|
72 | { |
---|
73 | cout << visible; |
---|
74 | return 0; |
---|
75 | } |
---|
76 | if (vm.count("help-module")) { |
---|
77 | const string& s = vm["help-module"].as<string>(); |
---|
78 | if (s == "gui") { |
---|
79 | cout << gui; |
---|
80 | } else if (s == "backend") { |
---|
81 | cout << backend; |
---|
82 | } else { |
---|
83 | cout << "Unknown module '" |
---|
84 | << s << "' in the --help-module option\n"; |
---|
85 | return 1; |
---|
86 | } |
---|
87 | return 0; |
---|
88 | } |
---|
89 | if (vm.count("num-threads")) { |
---|
90 | cout << "The 'num-threads' options was set to " |
---|
91 | << vm["num-threads"].as<int>() << "\n"; |
---|
92 | } |
---|
93 | } |
---|
94 | catch(exception& e) { |
---|
95 | cout << e.what() << "\n"; |
---|
96 | } |
---|
97 | } |
---|