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 | /* Shows how to use both command line and config file. */ |
---|
7 | |
---|
8 | #include <boost/program_options.hpp> |
---|
9 | namespace po = boost::program_options; |
---|
10 | |
---|
11 | |
---|
12 | #include <iostream> |
---|
13 | #include <fstream> |
---|
14 | #include <iterator> |
---|
15 | using namespace std; |
---|
16 | |
---|
17 | // A helper function to simplify the main part. |
---|
18 | template<class T> |
---|
19 | ostream& operator<<(ostream& os, const vector<T>& v) |
---|
20 | { |
---|
21 | copy(v.begin(), v.end(), ostream_iterator<T>(cout, " ")); |
---|
22 | return os; |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | int main(int ac, char* av[]) |
---|
27 | { |
---|
28 | try { |
---|
29 | int opt; |
---|
30 | |
---|
31 | // Declare a group of options that will be |
---|
32 | // allowed only on command line |
---|
33 | po::options_description generic("Generic options"); |
---|
34 | generic.add_options() |
---|
35 | ("version,v", "print version string") |
---|
36 | ("help", "produce help message") |
---|
37 | ; |
---|
38 | |
---|
39 | // Declare a group of options that will be |
---|
40 | // allowed both on command line and in |
---|
41 | // config file |
---|
42 | po::options_description config("Configuration"); |
---|
43 | config.add_options() |
---|
44 | ("optimization", po::value<int>(&opt)->default_value(10), |
---|
45 | "optimization level") |
---|
46 | ("include-path,I", |
---|
47 | po::value< vector<string> >()->composing(), |
---|
48 | "include path") |
---|
49 | ; |
---|
50 | |
---|
51 | // Hidden options, will be allowed both on command line and |
---|
52 | // in config file, but will not be shown to the user. |
---|
53 | po::options_description hidden("Hidden options"); |
---|
54 | hidden.add_options() |
---|
55 | ("input-file", po::value< vector<string> >(), "input file") |
---|
56 | ; |
---|
57 | |
---|
58 | |
---|
59 | po::options_description cmdline_options; |
---|
60 | cmdline_options.add(generic).add(config).add(hidden); |
---|
61 | |
---|
62 | po::options_description config_file_options; |
---|
63 | config_file_options.add(config).add(hidden); |
---|
64 | |
---|
65 | po::options_description visible("Allowed options"); |
---|
66 | visible.add(generic).add(config); |
---|
67 | |
---|
68 | po::positional_options_description p; |
---|
69 | p.add("input-file", -1); |
---|
70 | |
---|
71 | po::variables_map vm; |
---|
72 | store(po::command_line_parser(ac, av). |
---|
73 | options(cmdline_options).positional(p).run(), vm); |
---|
74 | |
---|
75 | ifstream ifs("multiple_sources.cfg"); |
---|
76 | store(parse_config_file(ifs, config_file_options), vm); |
---|
77 | notify(vm); |
---|
78 | |
---|
79 | if (vm.count("help")) { |
---|
80 | cout << visible << "\n"; |
---|
81 | return 0; |
---|
82 | } |
---|
83 | |
---|
84 | if (vm.count("version")) { |
---|
85 | cout << "Multiple sources example, version 1.0\n"; |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | if (vm.count("include-path")) |
---|
90 | { |
---|
91 | cout << "Include paths are: " |
---|
92 | << vm["include-path"].as< vector<string> >() << "\n"; |
---|
93 | } |
---|
94 | |
---|
95 | if (vm.count("input-file")) |
---|
96 | { |
---|
97 | cout << "Input files are: " |
---|
98 | << vm["input-file"].as< vector<string> >() << "\n"; |
---|
99 | } |
---|
100 | |
---|
101 | cout << "Optimization level is " << opt << "\n"; |
---|
102 | } |
---|
103 | catch(exception& e) |
---|
104 | { |
---|
105 | cout << e.what() << "\n"; |
---|
106 | return 1; |
---|
107 | } |
---|
108 | return 0; |
---|
109 | } |
---|