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> |
---|
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 | int main(int ac, char* av[]) |
---|
26 | { |
---|
27 | try { |
---|
28 | int opt; |
---|
29 | po::options_description desc("Allowed options"); |
---|
30 | desc.add_options() |
---|
31 | ("help", "produce help message") |
---|
32 | ("optimization", po::value<int>(&opt)->default_value(10), |
---|
33 | "optimization level") |
---|
34 | ("include-path,I", po::value< vector<string> >(), |
---|
35 | "include path") |
---|
36 | ("input-file", po::value< vector<string> >(), "input file") |
---|
37 | ; |
---|
38 | |
---|
39 | po::positional_options_description p; |
---|
40 | p.add("input-file", -1); |
---|
41 | |
---|
42 | po::variables_map vm; |
---|
43 | po::store(po::command_line_parser(ac, av). |
---|
44 | options(desc).positional(p).run(), vm); |
---|
45 | po::notify(vm); |
---|
46 | |
---|
47 | if (vm.count("help")) { |
---|
48 | cout << "Usage: options_description [options]\n"; |
---|
49 | cout << desc; |
---|
50 | return 0; |
---|
51 | } |
---|
52 | |
---|
53 | if (vm.count("include-path")) |
---|
54 | { |
---|
55 | cout << "Include paths are: " |
---|
56 | << vm["include-path"].as< vector<string> >() << "\n"; |
---|
57 | } |
---|
58 | |
---|
59 | if (vm.count("input-file")) |
---|
60 | { |
---|
61 | cout << "Input files are: " |
---|
62 | << vm["input-file"].as< vector<string> >() << "\n"; |
---|
63 | } |
---|
64 | |
---|
65 | cout << "Optimization level is " << opt << "\n"; |
---|
66 | } |
---|
67 | catch(exception& e) |
---|
68 | { |
---|
69 | cout << e.what() << "\n"; |
---|
70 | return 1; |
---|
71 | } |
---|
72 | return 0; |
---|
73 | } |
---|