Ticket #8588: program_options_eat_param.cpp

File program_options_eat_param.cpp, 1.2 KB (added by Adam Nielsen <a.nielsen@…>, 9 years ago)

Example to demonstrate the issue

Line 
1#include <boost/program_options.hpp>
2
3namespace po = boost::program_options;
4
5int main(int argc, char *argv[])
6{
7 // Declare the supported options.
8 po::options_description poActions("Actions");
9 poActions.add_options()
10 ("dump-firmware,d", po::value<std::string>(),
11 "copy firmware from device's flash into this file")
12 ;
13
14 po::options_description poHidden("Hidden parameters");
15 poHidden.add_options()
16 ("help", "produce help message")
17 ;
18
19 po::options_description poComplete("Parameters");
20 poComplete.add(poActions).add(poHidden);
21 po::variables_map mpArgs;
22
23 std::string strType, strHost, strSerial;
24
25 try {
26 po::parsed_options pa = po::parse_command_line(argc, argv, poComplete);
27
28 for (std::vector<po::option>::iterator i = pa.options.begin(); i != pa.options.end(); i++) {
29 if (i->string_key.compare("help") == 0) {
30 std::cout << "help text" << std::endl;
31 return 0;
32
33 } else if (
34 (i->string_key.compare("dump-firmware") == 0)
35 ) {
36 std::cout << "dumping firmware to file: " << i->value[0] << std::endl;
37 strSerial = i->value[0];
38
39 }
40 }
41
42 } catch (const po::invalid_command_line_syntax& e) {
43 std::cerr << "Invalid command line syntax: " << e.what()
44 << ". Use --help for help." << std::endl;
45 return 1;
46 }
47
48 return 0;
49}