| 1 | #include <iostream>
|
|---|
| 2 | #include <iomanip>
|
|---|
| 3 | #include <vector>
|
|---|
| 4 | #include <boost/program_options.hpp>
|
|---|
| 5 |
|
|---|
| 6 | using namespace std;
|
|---|
| 7 | using namespace boost::program_options;
|
|---|
| 8 |
|
|---|
| 9 | template<typename T>
|
|---|
| 10 | ostream& operator<<(ostream& os, const vector<T>& v)
|
|---|
| 11 | {
|
|---|
| 12 | copy(v.begin(), v.end(), ostream_iterator<T>(cout, " "));
|
|---|
| 13 | return os;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | int main(int avArgc, char* apArgv[])
|
|---|
| 17 | {
|
|---|
| 18 | try
|
|---|
| 19 | {
|
|---|
| 20 | // Define the allowed options.
|
|---|
| 21 | bool vBoolOpt;
|
|---|
| 22 | options_description vVisibleOpts("Visible options");
|
|---|
| 23 | vVisibleOpts.add_options()
|
|---|
| 24 | (
|
|---|
| 25 | "help,?", "produce help message"
|
|---|
| 26 | )
|
|---|
| 27 | (
|
|---|
| 28 | "bool-opt,a", value<bool>(&vBoolOpt)->implicit_value(true)->default_value(false), "a boolean option"
|
|---|
| 29 | );
|
|---|
| 30 | vector<string> vVectorOpt;
|
|---|
| 31 | options_description vHiddenOpts("Hidden options");
|
|---|
| 32 | vHiddenOpts.add_options()
|
|---|
| 33 | (
|
|---|
| 34 | "input-file", value< vector<string> >(&vVectorOpt), "input file"
|
|---|
| 35 | );
|
|---|
| 36 | positional_options_description vPositional;
|
|---|
| 37 | vPositional.add("input-file", -1);
|
|---|
| 38 | options_description vAllOpts;
|
|---|
| 39 | vAllOpts.add(vVisibleOpts).add(vHiddenOpts);
|
|---|
| 40 | options_description vAllowOpts("Allowed options");
|
|---|
| 41 | vAllowOpts.add(vVisibleOpts);
|
|---|
| 42 |
|
|---|
| 43 | // Parse and store the options.
|
|---|
| 44 | variables_map vVarMap;
|
|---|
| 45 | store(command_line_parser(avArgc, apArgv).options(vAllOpts).positional(vPositional).run(), vVarMap);
|
|---|
| 46 | notify(vVarMap);
|
|---|
| 47 |
|
|---|
| 48 | // Display help or the value of the options.
|
|---|
| 49 | if (vVarMap.count("help") != 0)
|
|---|
| 50 | {
|
|---|
| 51 | cout << vAllowOpts << endl;
|
|---|
| 52 | }
|
|---|
| 53 | else
|
|---|
| 54 | {
|
|---|
| 55 | cout << "vBoolOpt = " << boolalpha << vBoolOpt << endl;
|
|---|
| 56 | cout << "vVectorOpt = " << vVectorOpt << endl;
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 | catch (exception& e)
|
|---|
| 60 | {
|
|---|
| 61 | cout << e.what() << endl;
|
|---|
| 62 | return 1;
|
|---|
| 63 | }
|
|---|
| 64 | return 0;
|
|---|
| 65 | }
|
|---|