| 1 | #include <boost/program_options.hpp>
|
|---|
| 2 | #include <string>
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <fstream>
|
|---|
| 5 |
|
|---|
| 6 | using namespace boost::program_options;
|
|---|
| 7 | using namespace std;
|
|---|
| 8 |
|
|---|
| 9 | int main(int argc, char *argv[])
|
|---|
| 10 | {
|
|---|
| 11 | options_description opts;
|
|---|
| 12 | opts.add_options()
|
|---|
| 13 | ("cfgfile,c", value<string>()->required(), "the configfile")
|
|---|
| 14 | ("fritz,f", value<string>()->default_value("/other/file"), "the output file")
|
|---|
| 15 | ;
|
|---|
| 16 |
|
|---|
| 17 | variables_map vm;
|
|---|
| 18 | ifstream strm("options.ini");
|
|---|
| 19 |
|
|---|
| 20 | try {
|
|---|
| 21 | store(parse_command_line(argc, argv, opts), vm);
|
|---|
| 22 | if (strm) {
|
|---|
| 23 | store(parse_config_file(strm, opts), vm);
|
|---|
| 24 | }
|
|---|
| 25 | notify(vm);
|
|---|
| 26 | }
|
|---|
| 27 | catch (required_option& e) { // new exception type
|
|---|
| 28 | cout << "required option: " << e.what() << endl;
|
|---|
| 29 | return -1;
|
|---|
| 30 | }
|
|---|
| 31 | catch (...) {
|
|---|
| 32 | cout << "other exception: " << endl;
|
|---|
| 33 | return -1;
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | cout << "count (cfgfile): " << vm.count("cfgfile") << endl;
|
|---|
| 37 | if (vm.count("cfgfile")) {
|
|---|
| 38 | cout << "is empty (cfgfile): " << (vm["cfgfile"].empty() ? "true": "false") << endl;
|
|---|
| 39 | cout << "value: " << vm["cfgfile"].as< string >() << endl;
|
|---|
| 40 | }
|
|---|
| 41 | return 0;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|