-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
55 lines (43 loc) · 1.45 KB
/
program.cpp
File metadata and controls
55 lines (43 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// this program show simple boost program_options usage
//
#include <iostream>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char* argv[])
{
// Declare the supported named options.
po::options_description name_desc("Options for [program]");
name_desc.add_options()
("help", "this help message")
("string,S", po::value<std::string>(), "pass string option to program")
("int,I", po::value<int>()->default_value(0), "pass int option to program")
;
// declare position options
po::positional_options_description pos_desc;
pos_desc.add("string", -1);
// define variables map
po::variables_map vmap;
// get variables form command line
// NOTE: it is a long function call, boost
po::store(
po::command_line_parser(argc, argv).
options(name_desc).
positional(pos_desc).
run(), vmap);
// update vmap so we can call vmap["name"].as<type>()
po::notify(vmap);
// print help message if request and exit
if (vmap.count("help"))
{
std::cout << name_desc << std::endl;
return 1;
}
//
// print option value if defined
//
if(vmap.count("string"))
std::cout << "string is " << vmap["string"].as<std::string>() << std::endl;
if(vmap.count("int"))
std::cout << "int is " << vmap["int"].as<int>() << std::endl;
return 0;
}