Electroneum
Loading...
Searching...
No Matches
command_line Namespace Reference

Classes

struct  arg_descriptor
struct  arg_descriptor< T, false >
struct  arg_descriptor< std::vector< T >, false >
struct  arg_descriptor< T, true >
struct  arg_descriptor< T, false, true >
struct  arg_descriptor< T, false, true, NUM_DEPS >

Functions

bool is_yes (const std::string &str)
bool is_no (const std::string &str)
template<typename T>
boost::program_options::typed_value< T, char > * make_semantic (const arg_descriptor< T, true > &)
template<typename T>
boost::program_options::typed_value< T, char > * make_semantic (const arg_descriptor< T, false > &arg)
template<typename T>
boost::program_options::typed_value< T, char > * make_semantic (const arg_descriptor< T, false, true > &arg)
template<typename T, int NUM_DEPS>
boost::program_options::typed_value< T, char > * make_semantic (const arg_descriptor< T, false, true, NUM_DEPS > &arg)
template<typename T>
boost::program_options::typed_value< T, char > * make_semantic (const arg_descriptor< T, false > &arg, const T &def)
template<typename T>
boost::program_options::typed_value< std::vector< T >, char > * make_semantic (const arg_descriptor< std::vector< T >, false > &)
template<typename T, bool required, bool dependent, int NUM_DEPS>
void add_arg (boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
template<typename T>
void add_arg (boost::program_options::options_description &description, const arg_descriptor< T, false > &arg, const T &def, bool unique=true)
template<>
void add_arg (boost::program_options::options_description &description, const arg_descriptor< bool, false > &arg, bool unique)
template<typename charT>
boost::program_options::basic_parsed_options< charT > parse_command_line (int argc, const charT *const argv[], const boost::program_options::options_description &desc, bool allow_unregistered=false)
template<typename F>
bool handle_error_helper (const boost::program_options::options_description &desc, F parser)
template<typename T, bool required, bool dependent, int NUM_DEPS>
std::enable_if<!std::is_same< T, bool >::value, bool >::type has_arg (const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
template<typename T, bool required, bool dependent, int NUM_DEPS>
bool is_arg_defaulted (const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
template<typename T>
T get_arg (const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
template<typename T, int NUM_DEPS>
T get_arg (const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true, NUM_DEPS > &arg)
template<typename T, bool required>
T get_arg (const boost::program_options::variables_map &vm, const arg_descriptor< T, required > &arg)
template<bool dependent, int NUM_DEPS>
bool has_arg (const boost::program_options::variables_map &vm, const arg_descriptor< bool, false, dependent, NUM_DEPS > &arg)

Variables

const arg_descriptor< bool > arg_help = {"help", "Produce help message"}
const arg_descriptor< bool > arg_version = {"version", "Output version information"}

Function Documentation

◆ add_arg() [1/3]

template<>
void command_line::add_arg ( boost::program_options::options_description & description,
const arg_descriptor< bool, false > & arg,
bool unique )
inline

Definition at line 212 of file command_line.h.

213 {
214 if (0 != description.find_nothrow(arg.name, false))
215 {
216 CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
217 return;
218 }
219
220 description.add_options()(arg.name, boost::program_options::bool_switch(), arg.description);
221 }
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)

◆ add_arg() [2/3]

template<typename T>
void command_line::add_arg ( boost::program_options::options_description & description,
const arg_descriptor< T, false > & arg,
const T & def,
bool unique = true )

Definition at line 200 of file command_line.h.

201 {
202 if (0 != description.find_nothrow(arg.name, false))
203 {
204 CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
205 return;
206 }
207
208 description.add_options()(arg.name, make_semantic(arg, def), arg.description);
209 }
boost::program_options::typed_value< T, char > * make_semantic(const arg_descriptor< T, true > &)
Here is the call graph for this function:

◆ add_arg() [3/3]

template<typename T, bool required, bool dependent, int NUM_DEPS>
void command_line::add_arg ( boost::program_options::options_description & description,
const arg_descriptor< T, required, dependent, NUM_DEPS > & arg,
bool unique = true )

Definition at line 188 of file command_line.h.

189 {
190 if (0 != description.find_nothrow(arg.name, false))
191 {
192 CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
193 return;
194 }
195
196 description.add_options()(arg.name, make_semantic(arg), arg.description);
197 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_arg() [1/3]

template<typename T>
T command_line::get_arg ( const boost::program_options::variables_map & vm,
const arg_descriptor< T, false, true > & arg )

Definition at line 271 of file command_line.h.

272 {
273 return arg.depf(get_arg(vm, arg.ref), is_arg_defaulted(vm, arg), vm[arg.name].template as<T>());
274 }
bool is_arg_defaulted(const boost::program_options::variables_map &vm, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg)
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
const arg_descriptor< bool, false > & ref
std::function< T(bool, bool, T)> depf
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_arg() [2/3]

template<typename T, int NUM_DEPS>
T command_line::get_arg ( const boost::program_options::variables_map & vm,
const arg_descriptor< T, false, true, NUM_DEPS > & arg )

Definition at line 277 of file command_line.h.

278 {
279 std::array<bool, NUM_DEPS> depval;
280 for (size_t i = 0; i < depval.size(); ++i)
281 depval[i] = get_arg(vm, *arg.ref[i]);
282 return arg.depf(depval, is_arg_defaulted(vm, arg), vm[arg.name].template as<T>());
283 }
std::array< const arg_descriptor< bool, false > *, NUM_DEPS > ref
std::function< T(std::array< bool, NUM_DEPS >, bool, T)> depf
Here is the call graph for this function:

◆ get_arg() [3/3]

template<typename T, bool required>
T command_line::get_arg ( const boost::program_options::variables_map & vm,
const arg_descriptor< T, required > & arg )

Definition at line 286 of file command_line.h.

287 {
288 return vm[arg.name].template as<T>();
289 }

◆ handle_error_helper()

template<typename F>
bool command_line::handle_error_helper ( const boost::program_options::options_description & desc,
F parser )

Definition at line 237 of file command_line.h.

238 {
239 try
240 {
241 return parser();
242 }
243 catch (const std::exception& e)
244 {
245 std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
246 std::cerr << desc << std::endl;
247 return false;
248 }
249 catch (...)
250 {
251 std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
252 std::cerr << desc << std::endl;
253 return false;
254 }
255 }
Here is the caller graph for this function:

◆ has_arg() [1/2]

template<bool dependent, int NUM_DEPS>
bool command_line::has_arg ( const boost::program_options::variables_map & vm,
const arg_descriptor< bool, false, dependent, NUM_DEPS > & arg )
inline

Definition at line 292 of file command_line.h.

293 {
294 return get_arg(vm, arg);
295 }
Here is the call graph for this function:

◆ has_arg() [2/2]

template<typename T, bool required, bool dependent, int NUM_DEPS>
std::enable_if<!std::is_same< T, bool >::value, bool >::type command_line::has_arg ( const boost::program_options::variables_map & vm,
const arg_descriptor< T, required, dependent, NUM_DEPS > & arg )

Definition at line 258 of file command_line.h.

259 {
260 auto value = vm[arg.name];
261 return !value.empty();
262 }
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1225
Here is the caller graph for this function:

◆ is_arg_defaulted()

template<typename T, bool required, bool dependent, int NUM_DEPS>
bool command_line::is_arg_defaulted ( const boost::program_options::variables_map & vm,
const arg_descriptor< T, required, dependent, NUM_DEPS > & arg )

Definition at line 265 of file command_line.h.

266 {
267 return vm[arg.name].defaulted();
268 }
Here is the caller graph for this function:

◆ is_no()

bool command_line::is_no ( const std::string & str)
Returns
True if str is is_iequal("n" || "no" || tr("no"))`.

Definition at line 62 of file command_line.cpp.

63 {
64 if (str == "n" || str == "N")
65 return true;
66
67 boost::algorithm::is_iequal ignore_case{};
68 if (boost::algorithm::equals("no", str, ignore_case))
69 return true;
70 if (boost::algorithm::equals(command_line::tr("no"), str, ignore_case))
71 return true;
72
73 return false;
74 }
Here is the caller graph for this function:

◆ is_yes()

bool command_line::is_yes ( const std::string & str)
Returns
True if str is is_iequal("y" || "yes" || tr("yes"))`.

Definition at line 48 of file command_line.cpp.

49 {
50 if (str == "y" || str == "Y")
51 return true;
52
53 boost::algorithm::is_iequal ignore_case{};
54 if (boost::algorithm::equals("yes", str, ignore_case))
55 return true;
56 if (boost::algorithm::equals(command_line::tr("yes"), str, ignore_case))
57 return true;
58
59 return false;
60 }
Here is the caller graph for this function:

◆ make_semantic() [1/6]

template<typename T>
boost::program_options::typed_value< std::vector< T >, char > * command_line::make_semantic ( const arg_descriptor< std::vector< T >, false > & )

Definition at line 180 of file command_line.h.

181 {
182 auto semantic = boost::program_options::value< std::vector<T> >();
183 semantic->default_value(std::vector<T>(), "");
184 return semantic;
185 }

◆ make_semantic() [2/6]

template<typename T>
boost::program_options::typed_value< T, char > * command_line::make_semantic ( const arg_descriptor< T, false > & arg)

Definition at line 126 of file command_line.h.

127 {
128 auto semantic = boost::program_options::value<T>();
129 if (!arg.not_use_default)
130 semantic->default_value(arg.default_value);
131 return semantic;
132 }

◆ make_semantic() [3/6]

template<typename T>
boost::program_options::typed_value< T, char > * command_line::make_semantic ( const arg_descriptor< T, false > & arg,
const T & def )

Definition at line 171 of file command_line.h.

172 {
173 auto semantic = boost::program_options::value<T>();
174 if (!arg.not_use_default)
175 semantic->default_value(def);
176 return semantic;
177 }

◆ make_semantic() [4/6]

template<typename T>
boost::program_options::typed_value< T, char > * command_line::make_semantic ( const arg_descriptor< T, false, true > & arg)

Definition at line 135 of file command_line.h.

136 {
137 auto semantic = boost::program_options::value<T>();
138 if (!arg.not_use_default) {
139 std::ostringstream format;
140 format << arg.depf(false, true, arg.default_value) << ", "
141 << arg.depf(true, true, arg.default_value) << " if '"
142 << arg.ref.name << "'";
143 semantic->default_value(arg.depf(arg.ref.default_value, true, arg.default_value), format.str());
144 }
145 return semantic;
146 }

◆ make_semantic() [5/6]

template<typename T, int NUM_DEPS>
boost::program_options::typed_value< T, char > * command_line::make_semantic ( const arg_descriptor< T, false, true, NUM_DEPS > & arg)

Definition at line 149 of file command_line.h.

150 {
151 auto semantic = boost::program_options::value<T>();
152 if (!arg.not_use_default) {
153 std::array<bool, NUM_DEPS> depval;
154 depval.fill(false);
155 std::ostringstream format;
156 format << arg.depf(depval, true, arg.default_value);
157 for (size_t i = 0; i < depval.size(); ++i)
158 {
159 depval.fill(false);
160 depval[i] = true;
161 format << ", " << arg.depf(depval, true, arg.default_value) << " if '" << arg.ref[i]->name << "'";
162 }
163 for (size_t i = 0; i < depval.size(); ++i)
164 depval[i] = arg.ref[i]->default_value;
165 semantic->default_value(arg.depf(depval, true, arg.default_value), format.str());
166 }
167 return semantic;
168 }

◆ make_semantic() [6/6]

template<typename T>
boost::program_options::typed_value< T, char > * command_line::make_semantic ( const arg_descriptor< T, true > & )

Definition at line 120 of file command_line.h.

121 {
122 return boost::program_options::value<T>()->required();
123 }
Here is the caller graph for this function:

◆ parse_command_line()

template<typename charT>
boost::program_options::basic_parsed_options< charT > command_line::parse_command_line ( int argc,
const charT *const argv[],
const boost::program_options::options_description & desc,
bool allow_unregistered = false )

Definition at line 224 of file command_line.h.

226 {
227 auto parser = boost::program_options::command_line_parser(argc, argv);
228 parser.options(desc);
229 if (allow_unregistered)
230 {
231 parser.allow_unregistered();
232 }
233 return parser.run();
234 }

Variable Documentation

◆ arg_help

const arg_descriptor< bool > command_line::arg_help = {"help", "Produce help message"}

Definition at line 76 of file command_line.cpp.

76{"help", "Produce help message"};

◆ arg_version

const arg_descriptor< bool > command_line::arg_version = {"version", "Output version information"}

Definition at line 77 of file command_line.cpp.

77{"version", "Output version information"};