• 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  #include <boost/program_options.hpp>
7  
8  using namespace boost;
9  namespace po = boost::program_options;
10  
11  #include <iostream>
12  #include <algorithm>
13  #include <iterator>
14  using namespace std;
15  
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  
main(int ac,char * av[])25  int main(int ac, char* av[])
26  {
27      try {
28          int opt;
29          int portnum;
30          po::options_description desc("Allowed options");
31          desc.add_options()
32              ("help", "produce help message")
33              ("optimization", po::value<int>(&opt)->default_value(10),
34                    "optimization level")
35              ("verbose,v", po::value<int>()->implicit_value(1),
36                    "enable verbosity (optionally specify level)")
37              ("listen,l", po::value<int>(&portnum)->implicit_value(1001)
38                    ->default_value(0,"no"),
39                    "listen on a port.")
40              ("include-path,I", po::value< vector<string> >(),
41                    "include path")
42              ("input-file", po::value< vector<string> >(), "input file")
43          ;
44  
45          po::positional_options_description p;
46          p.add("input-file", -1);
47  
48          po::variables_map vm;
49          po::store(po::command_line_parser(ac, av).
50                    options(desc).positional(p).run(), vm);
51          po::notify(vm);
52  
53          if (vm.count("help")) {
54              cout << "Usage: options_description [options]\n";
55              cout << desc;
56              return 0;
57          }
58  
59          if (vm.count("include-path"))
60          {
61              cout << "Include paths are: "
62                   << vm["include-path"].as< vector<string> >() << "\n";
63          }
64  
65          if (vm.count("input-file"))
66          {
67              cout << "Input files are: "
68                   << vm["input-file"].as< vector<string> >() << "\n";
69          }
70  
71          if (vm.count("verbose")) {
72              cout << "Verbosity enabled.  Level is " << vm["verbose"].as<int>()
73                   << "\n";
74          }
75  
76          cout << "Optimization level is " << opt << "\n";
77  
78          cout << "Listen port is " << portnum << "\n";
79      }
80      catch(std::exception& e)
81      {
82          cout << e.what() << "\n";
83          return 1;
84      }
85      return 0;
86  }
87