- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramOptions.h
More file actions
Latest commit
188 lines (159 loc) · 5.46 KB
/
Copy pathProgramOptions.h
File metadata and controls
188 lines (159 loc) · 5.46 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2022, Antoine Basset
// SPDX-License-Identifier: LGPL-3.0-or-later
#ifndef _PROGRAMOPTIONS_H
#define_PROGRAMOPTIONS_H
#include<boost/program_options.hpp>
#include<sstream>
namespaceKast {
/**
* @brief Helper class to declare positional, named and flag options, as well as some help message.
* @details
* Here is an example use case for the following command line:
* \verbatim Program <positional> --named1 <value1> --flag --named2 <value2> \endverbatim
*
* Here's an example program to handle it:
* \code
* std::pair<OptionsDescription, PositionalOptionsDescription> defineProgramArguments() override {
* auto options = ProgramOptions::fromAuxdir("help.txt");
* options.positional("positional", value<std::string>(), "Positional option");
* options.named("named1", value<int>(), "Named option 1");
* options.named("named2", value<int>(), "Named option 2");
* return options.asPair();
* }
* \endcode
*/
classProgramOptions {
public:
using OptionsDescription = boost::program_options::options_description;
using PositionalOptionsDescription = boost::program_options::positional_options_description;
using ValueSemantics = boost::program_options::value_semantic;
/**
* @brief Make a `ProgramOptions` with optional description string.
*/
ProgramOptions(const std::string& description = "") :
m_named(makeDesc(description)),
m_add(m_named.add_options()),
m_positional(),
m_variables() {}
/**
* @brief Declare a positional option.
*/
template <typename T>
voidpositional(constchar* name, constchar* description) {
positional(name, boost::program_options::value<T>(), description);
}
/**
* @brief Declare a positional option with default value.
*/
template <typename T>
voidpositional(constchar* name, constchar* description, T defaultValue) {
positional(name, boost::program_options::value<T>()->default_value(defaultValue), description);
}
/**
* @brief Declare a positional option with custom semantics.
*/
voidpositional(constchar* name, const ValueSemantics* value, constchar* description) {
m_add(name, value, description);
constint maxArgs = value->max_tokens();
m_positional.add(name, maxArgs);
}
/**
* @brief Declare a named option.
* @details
* A short form (1-character) of the option can be provided, separated by a comma.
*/
template <typename T>
voidnamed(constchar* name, constchar* description) {
named(name, boost::program_options::value<T>(), description);
}
/**
* @brief Declare a named option with default value.
* @details
* A short form (1-character) of the option can be provided, separated by a comma.
*/
template <typename T>
voidnamed(constchar* name, constchar* description, T defaultValue) {
named(name, boost::program_options::value<T>()->default_value(defaultValue), description);
}
/**
* @brief Declare a named option with custom semantics.
* @details
* A short form (1-character) of the option can be provided, separated by a comma.
*/
voidnamed(constchar* name, const ValueSemantics* value, constchar* description) {
m_add(name, value, description);
}
/**
* @brief Declare a flag option.
*/
voidflag(constchar* name, constchar* description) {
named(name, boost::program_options::value<bool>()->default_value(false)->implicit_value(true), description);
}
/**
* @brief Get the named (flags included) and positional options as a pair.
*/
std::pair<OptionsDescription, PositionalOptionsDescription> asPair() const {
returnstd::make_pair(m_named, m_positional);
}
/**
* @brief Parse a command line (`main`-like).
*/
voidparse(int argc, constchar* const argv[]) {
boost::program_options::store(
boost::program_options::command_line_parser(argc, argv).options(m_named).positional(m_positional).run(),
m_variables);
boost::program_options::notify(m_variables);
}
/**
* @brief Parse a command line (vector of `const char*` arguments).
*/
voidparse(const std::vector<constchar*>& args) {
parse(args.size(), args.data());
}
/**
* @brief Parse a command line (vector of string arguments).
*/
voidparse(const std::vector<std::string>& args) {
std::vector<constchar*> cstr(args.size());
std::transform(args.begin(), args.end(), cstr.begin(), [](constauto& a) { return a.c_str(); });
parse(cstr);
}
/**
* @brief Parse a command line (space-separated arguments as a single string).
*/
voidparse(const std::string& args) {
std::istringstream iss(args);
parse(std::vector<std::string>(
std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>()));
}
/**
* @brief Check whether a given option is set.
*/
boolhas(constchar* name) const {
return m_variables.count(name);
}
/**
* @brief Get the value of a given option.
* @details
* Throws if the option is not set.
*/
template <typename T>
T as(constchar* name) const {
return m_variables[name].as<T>();
}
private:
static std::string makeDesc(const std::string& description) {
const std::string optionsGroup = "Options:";
if (description.length() > 0) {
return description + "\n\n" + optionsGroup;
}
return optionsGroup;
}
OptionsDescription m_named;
boost::program_options::options_description_easy_init m_add;
PositionalOptionsDescription m_positional;
boost::program_options::variables_map m_variables;
};
}
#endif