-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramOptions.cpp
More file actions
100 lines (84 loc) · 2.49 KB
/
ProgramOptions.cpp
File metadata and controls
100 lines (84 loc) · 2.49 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* \file ProgramOptions.cpp
* \brief boost::program_options
*
* https://theboostcpplibraries.com/boost.program_options
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#if BOOST_VERSION > 0
#include <boost/program_options.hpp>
//-------------------------------------------------------------------------------------------------
void
toCout(const std::vector<std::string> &v)
{
std::copy(v.cbegin(), v.cend(), std::ostream_iterator<std::string>{std::cout, "\n"});
}
//-------------------------------------------------------------------------------------------------
#endif
//-------------------------------------------------------------------------------------------------
int main(int argc, const char *argv[])
{
#if BOOST_VERSION > 0
namespace po = boost::program_options;
try {
int age {};
po::options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("pi", po::value<float>()->implicit_value(3.14f), "Pi")
("age", po::value<int>(&age), "Age")
("phone", po::value<std::vector<std::string>>()
->multitoken()
->zero_tokens()
->composing(),
"Phone")
("unreg", "Unrecognized options");
po::command_line_parser parser{argc, argv};
parser
.options(desc)
.allow_unregistered().style(
po::command_line_style::default_style |
po::command_line_style::allow_slash_for_short);
po::parsed_options parsed_options = parser.run();
po::variables_map vm;
std::cout << STD_TRACE_VAR(vm.size()) << std::endl;
po::store(parsed_options, vm);
po::notify(vm);
if (vm.count("help")) {
std::cout << desc << std::endl;
}
else if (vm.count("age")) {
std::cout << "Age: " << age << std::endl;
}
else if (vm.count("phone")) {
::toCout(vm["phone"].as<std::vector<std::string>>());
}
else if (vm.count("unreg")) {
::toCout(collect_unrecognized(parsed_options.options, po::exclude_positional));
}
else if (vm.count("pi")) {
std::cout << "Pi: " << vm["pi"].as<float>() << std::endl;
}
}
catch (const po::error &ex) {
std::cout << ex.what() << std::endl;
}
catch (const std::exception &ex) {
std::cout << ex.what() << std::endl;
}
catch (...) {
std::cout << "Unknown exception" << std::endl;
}
#else
STD_UNUSED(argc);
STD_UNUSED(argv);
std::cout << "Boost - not instaled, skip" << std::endl;
#endif
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
vm.size(): 0
#endif