-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramOptions.cpp
80 lines (65 loc) · 1.77 KB
/
ProgramOptions.cpp
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
#include "ProgramOptions.h"
#include <iostream>
#include <cassert>
#include <vector>
namespace po = boost::program_options;
namespace cc_demo1
{
namespace client
{
namespace
{
const std::string HelpStr("help");
const std::string FullHelpStr(HelpStr + ",h");
const std::string ServerStr("server");
const std::string FullServerStr(ServerStr + ",s");
const std::string PortStr("port");
const std::string FullPortStr(PortStr + ",p");
const std::uint16_t DefaultPort = 20000;
po::options_description createDescription()
{
po::options_description desc("Options");
desc.add_options()
(FullHelpStr.c_str(), "This help.")
(FullServerStr.c_str(), po::value<std::string>()->default_value(std::string()),
"IP address of the server. Empty means localhost.")
(FullPortStr.c_str(), po::value<std::uint16_t>()->default_value(DefaultPort),
"TCP port of the server.")
;
return desc;
}
const po::options_description& getDescription()
{
static const auto Desc = createDescription();
return Desc;
}
} // namespace
void ProgramOptions::parse(int argc, const char* argv[])
{
po::options_description allOptions;
allOptions.add(getDescription());
auto parseResult =
po::command_line_parser(argc, argv)
.options(allOptions)
.run();
po::store(parseResult, m_vm);
po::notify(m_vm);
}
void ProgramOptions::printHelp(std::ostream& out)
{
out << getDescription() << std::endl;
}
bool ProgramOptions::helpRequested() const
{
return 0 < m_vm.count(HelpStr);
}
std::string ProgramOptions::server() const
{
return m_vm[ServerStr].as<std::string>();
}
std::uint16_t ProgramOptions::port() const
{
return m_vm[PortStr].as<std::uint16_t>();
}
} // namespace client
} // namespace cc_demo1