• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
operator <<(ostream & os,const vector<T> & v)19 ostream& operator<<(ostream& os, const vector<T>& v)
20 {
21     copy(v.begin(), v.end(), ostream_iterator<T>(os, " "));
22     return os;
23 }
24 
25 
main(int ac,char * av[])26 int main(int ac, char* av[])
27 {
28     try {
29         int opt;
30         string config_file;
31 
32         // Declare a group of options that will be
33         // allowed only on command line
34         po::options_description generic("Generic options");
35         generic.add_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.")
40             ;
41 
42         // Declare a group of options that will be
43         // allowed both on command line and in
44         // config file
45         po::options_description config("Configuration");
46         config.add_options()
47             ("optimization", po::value<int>(&opt)->default_value(10),
48                   "optimization level")
49             ("include-path,I",
50                  po::value< vector<string> >()->composing(),
51                  "include path")
52             ;
53 
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");
57         hidden.add_options()
58             ("input-file", po::value< vector<string> >(), "input file")
59             ;
60 
61 
62         po::options_description cmdline_options;
63         cmdline_options.add(generic).add(config).add(hidden);
64 
65         po::options_description config_file_options;
66         config_file_options.add(config).add(hidden);
67 
68         po::options_description visible("Allowed options");
69         visible.add(generic).add(config);
70 
71         po::positional_options_description p;
72         p.add("input-file", -1);
73 
74         po::variables_map vm;
75         store(po::command_line_parser(ac, av).
76               options(cmdline_options).positional(p).run(), vm);
77         notify(vm);
78 
79         ifstream ifs(config_file.c_str());
80         if (!ifs)
81         {
82             cout << "can not open config file: " << config_file << "\n";
83             return 0;
84         }
85         else
86         {
87             store(parse_config_file(ifs, config_file_options), vm);
88             notify(vm);
89         }
90 
91         if (vm.count("help")) {
92             cout << visible << "\n";
93             return 0;
94         }
95 
96         if (vm.count("version")) {
97             cout << "Multiple sources example, version 1.0\n";
98             return 0;
99         }
100 
101         if (vm.count("include-path"))
102         {
103             cout << "Include paths are: "
104                  << vm["include-path"].as< vector<string> >() << "\n";
105         }
106 
107         if (vm.count("input-file"))
108         {
109             cout << "Input files are: "
110                  << vm["input-file"].as< vector<string> >() << "\n";
111         }
112 
113         cout << "Optimization level is " << opt << "\n";
114     }
115     catch(exception& e)
116     {
117         cout << e.what() << "\n";
118         return 1;
119     }
120     return 0;
121 }
122