| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/program_options/options_description.hpp>
|
|---|
| 3 | #include <boost/program_options/positional_options.hpp>
|
|---|
| 4 | #include <boost/program_options/variables_map.hpp>
|
|---|
| 5 | #include <boost/program_options/parsers.hpp>
|
|---|
| 6 |
|
|---|
| 7 | using namespace std;
|
|---|
| 8 | using namespace boost::program_options;
|
|---|
| 9 |
|
|---|
| 10 | struct ProgramOptions {
|
|---|
| 11 | string configFile;
|
|---|
| 12 | string configString;
|
|---|
| 13 | };
|
|---|
| 14 |
|
|---|
| 15 | int prepareOptions(ProgramOptions &po, int argc, char **argv) {
|
|---|
| 16 | options_description desc("Project Usage:\n\n"
|
|---|
| 17 | "\tproject <options>\n\n"
|
|---|
| 18 | "Options");
|
|---|
| 19 | desc.add_options()
|
|---|
| 20 | ("help", "display this message")
|
|---|
| 21 | ("config", value<string>(&po.configFile), "config file name")
|
|---|
| 22 | ("config-value", value<string>(&po.configString), "single config value")
|
|---|
| 23 | ;
|
|---|
| 24 |
|
|---|
| 25 | variables_map vm;
|
|---|
| 26 |
|
|---|
| 27 | try {
|
|---|
| 28 | store(command_line_parser(argc, argv).options(desc).run(), vm);
|
|---|
| 29 | notify(vm);
|
|---|
| 30 | } catch (unknown_option e) {
|
|---|
| 31 | cerr << "Error in command line: " << e.what() << "\n\n";
|
|---|
| 32 | cout << desc << "\n";
|
|---|
| 33 | return 1;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | if (argc <= 1 || vm.count("help")) {
|
|---|
| 37 | cout << desc << "\n";
|
|---|
| 38 | return 1;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | if (!po.configFile.empty()) {
|
|---|
| 42 | printf("config: %s\n", po.configFile.c_str());
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | if (!po.configString.empty()) {
|
|---|
| 46 | printf("config-value: %s\n", po.configString.c_str());
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | return 0;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | int main(int argc, char **argv) {
|
|---|
| 53 | ProgramOptions po;
|
|---|
| 54 | return prepareOptions(po, argc, argv);
|
|---|
| 55 | }
|
|---|