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)
6 /* Shows how to use both command line and config file. */
8 #include <boost/program_options.hpp>
9 namespace po
= boost::program_options
;
17 // A helper function to simplify the main part.
19 ostream
& operator<<(ostream
& os
, const vector
<T
>& v
)
21 copy(v
.begin(), v
.end(), ostream_iterator
<T
>(os
, " "));
26 int main(int ac
, char* av
[])
32 // Declare a group of options that will be
33 // allowed only on command line
34 po::options_description
generic("Generic options");
36 ("version,v", "print version string")
37 ("help", "produce help message")
38 ("config,c", po::value
<string
>(&config_file
)->default_value("multiple_sources.cfg"),
39 "name of a file of a configuration.")
42 // Declare a group of options that will be
43 // allowed both on command line and in
45 po::options_description
config("Configuration");
47 ("optimization", po::value
<int>(&opt
)->default_value(10),
50 po::value
< vector
<string
> >()->composing(),
54 // Hidden options, will be allowed both on command line and
55 // in config file, but will not be shown to the user.
56 po::options_description
hidden("Hidden options");
58 ("input-file", po::value
< vector
<string
> >(), "input file")
62 po::options_description cmdline_options
;
63 cmdline_options
.add(generic
).add(config
).add(hidden
);
65 po::options_description config_file_options
;
66 config_file_options
.add(config
).add(hidden
);
68 po::options_description
visible("Allowed options");
69 visible
.add(generic
).add(config
);
71 po::positional_options_description p
;
72 p
.add("input-file", -1);
75 store(po::command_line_parser(ac
, av
).
76 options(cmdline_options
).positional(p
).run(), vm
);
79 ifstream
ifs(config_file
.c_str());
82 cout
<< "can not open config file: " << config_file
<< "\n";
87 store(parse_config_file(ifs
, config_file_options
), vm
);
91 if (vm
.count("help")) {
92 cout
<< visible
<< "\n";
96 if (vm
.count("version")) {
97 cout
<< "Multiple sources example, version 1.0\n";
101 if (vm
.count("include-path"))
103 cout
<< "Include paths are: "
104 << vm
["include-path"].as
< vector
<string
> >() << "\n";
107 if (vm
.count("input-file"))
109 cout
<< "Input files are: "
110 << vm
["input-file"].as
< vector
<string
> >() << "\n";
113 cout
<< "Optimization level is " << opt
<< "\n";
117 cout
<< e
.what() << "\n";