| 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 |
#pragma once |
| 32 |
|
| 33 |
#include <functional> |
| 34 |
#include <iostream> |
| 35 |
#include <sstream> |
| 36 |
#include <array> |
| 37 |
#include <type_traits> |
| 38 |
|
| 39 |
#include <boost/program_options/parsers.hpp> |
| 40 |
#include <boost/program_options/options_description.hpp> |
| 41 |
#include <boost/program_options/variables_map.hpp> |
| 42 |
#include "include_base_utils.h" |
| 43 |
|
| 44 |
namespace command_line |
| 45 |
{ |
| 46 |
|
| 47 |
|
| 48 |
bool is_yes(const std::string& str); |
| 49 |
|
| 50 |
bool is_no(const std::string& str); |
| 51 |
|
| 52 |
template<typename T, bool required = false, bool dependent = false, int NUM_DEPS = 1> |
| 53 |
struct arg_descriptor; |
| 54 |
|
| 55 |
template<typename T> |
| 56 |
struct arg_descriptor<T, false> |
| 57 |
{ |
| 58 |
typedef T value_type; |
| 59 |
|
| 60 |
const char* name; |
| 61 |
const char* description; |
| 62 |
T default_value; |
| 63 |
bool not_use_default; |
| 64 |
}; |
| 65 |
|
| 66 |
template<typename T> |
| 67 |
struct arg_descriptor<std::vector<T>, false> |
| 68 |
{ |
| 69 |
typedef std::vector<T> value_type; |
| 70 |
|
| 71 |
const char* name; |
| 72 |
const char* description; |
| 73 |
}; |
| 74 |
|
| 75 |
template<typename T> |
| 76 |
struct arg_descriptor<T, true> |
| 77 |
{ |
| 78 |
static_assert(!std::is_same<T, bool>::value, "Boolean switch can't be required"); |
| 79 |
|
| 80 |
typedef T value_type; |
| 81 |
|
| 82 |
const char* name; |
| 83 |
const char* description; |
| 84 |
}; |
| 85 |
|
| 86 |
template<typename T> |
| 87 |
struct arg_descriptor<T, false, true> |
| 88 |
{ |
| 89 |
typedef T value_type; |
| 90 |
|
| 91 |
const char* name; |
| 92 |
const char* description; |
| 93 |
|
| 94 |
T default_value; |
| 95 |
|
| 96 |
const arg_descriptor<bool, false>& ref; |
| 97 |
std::function<T(bool, bool, T)> depf; |
| 98 |
|
| 99 |
bool not_use_default; |
| 100 |
}; |
| 101 |
|
| 102 |
template<typename T, int NUM_DEPS> |
| 103 |
struct arg_descriptor<T, false, true, NUM_DEPS> |
| 104 |
{ |
| 105 |
typedef T value_type; |
| 106 |
|
| 107 |
const char* name; |
| 108 |
const char* description; |
| 109 |
|
| 110 |
T default_value; |
| 111 |
|
| 112 |
std::array<const arg_descriptor<bool, false> *, NUM_DEPS> ref; |
| 113 |
std::function<T(std::array<bool, NUM_DEPS>, bool, T)> depf; |
| 114 |
|
| 115 |
bool not_use_default; |
| 116 |
}; |
| 117 |
|
| 118 |
template<typename T> |
| 119 |
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, true>& ) |
| 120 |
{ |
| 121 |
return boost::program_options::value<T>()->required(); |
| 122 |
} |
| 123 |
|
| 124 |
template<typename T> |
| 125 |
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg) |
| 126 |
{ |
| 127 |
auto semantic = boost::program_options::value<T>(); |
| 128 |
if (!arg.not_use_default) |
| 129 |
semantic->default_value(arg.default_value); |
| 130 |
return semantic; |
| 131 |
} |
| 132 |
|
| 133 |
template<typename T> |
| 134 |
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false, true>& arg) |
| 135 |
{ |
| 136 |
auto semantic = boost::program_options::value<T>(); |
| 137 |
if (!arg.not_use_default) { |
| 138 |
std::ostringstream format; |
| 139 |
format << arg.depf(false, true, arg.default_value) << ", " |
| 140 |
<< arg.depf(true, true, arg.default_value) << " if '" |
| 141 |
<< arg.ref.name << "'"; |
| 142 |
semantic->default_value(arg.depf(arg.ref.default_value, true, arg.default_value), format.str()); |
| 143 |
} |
| 144 |
return semantic; |
| 145 |
} |
| 146 |
|
| 147 |
template<typename T, int NUM_DEPS> |
| 148 |
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false, true, NUM_DEPS>& arg) |
| 149 |
{ |
| 150 |
auto semantic = boost::program_options::value<T>(); |
| 151 |
if (!arg.not_use_default) { |
| 152 |
std::array<bool, NUM_DEPS> depval; |
| 153 |
depval.fill(false); |
| 154 |
std::ostringstream format; |
| 155 |
format << arg.depf(depval, true, arg.default_value); |
| 156 |
for (size_t i = 0; i < depval.size(); ++i) |
| 157 |
{ |
| 158 |
depval.fill(false); |
| 159 |
depval[i] = true; |
| 160 |
format << ", " << arg.depf(depval, true, arg.default_value) << " if '" << arg.ref[i]->name << "'"; |
| 161 |
} |
| 162 |
for (size_t i = 0; i < depval.size(); ++i) |
| 163 |
depval[i] = arg.ref[i]->default_value; |
| 164 |
semantic->default_value(arg.depf(depval, true, arg.default_value), format.str()); |
| 165 |
} |
| 166 |
return semantic; |
| 167 |
} |
| 168 |
|
| 169 |
template<typename T> |
| 170 |
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg, const T& def) |
| 171 |
{ |
| 172 |
auto semantic = boost::program_options::value<T>(); |
| 173 |
if (!arg.not_use_default) |
| 174 |
semantic->default_value(def); |
| 175 |
return semantic; |
| 176 |
} |
| 177 |
|
| 178 |
template<typename T> |
| 179 |
boost::program_options::typed_value<std::vector<T>, char>* make_semantic(const arg_descriptor<std::vector<T>, false>& ) |
| 180 |
{ |
| 181 |
auto semantic = boost::program_options::value< std::vector<T> >(); |
| 182 |
semantic->default_value(std::vector<T>(), ""); |
| 183 |
return semantic; |
| 184 |
} |
| 185 |
|
| 186 |
template<typename T, bool required, bool dependent, int NUM_DEPS> |
| 187 |
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required, dependent, NUM_DEPS>& arg, bool unique = true) |
| 188 |
{ |
| 189 |
if (0 != description.find_nothrow(arg.name, false)) |
| 190 |
{ |
| 191 |
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
description.add_options()(arg.name, make_semantic(arg), arg.description); |
| 196 |
} |
| 197 |
|
| 198 |
template<typename T> |
| 199 |
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, false>& arg, const T& def, bool unique = true) |
| 200 |
{ |
| 201 |
if (0 != description.find_nothrow(arg.name, false)) |
| 202 |
{ |
| 203 |
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
description.add_options()(arg.name, make_semantic(arg, def), arg.description); |
| 208 |
} |
| 209 |
|
| 210 |
inline void add_arg(boost::program_options::options_description& description, const arg_descriptor<bool, false>& arg, bool unique = true) |
| 211 |
{ |
| 212 |
if (0 != description.find_nothrow(arg.name, false)) |
| 213 |
{ |
| 214 |
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name); |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
description.add_options()(arg.name, boost::program_options::bool_switch(), arg.description); |
| 219 |
} |
| 220 |
|
| 221 |
template<typename charT> |
| 222 |
boost::program_options::basic_parsed_options<charT> parse_command_line(int argc, const charT* const argv[], |
| 223 |
const boost::program_options::options_description& desc, bool allow_unregistered = false) |
| 224 |
{ |
| 225 |
auto parser = boost::program_options::command_line_parser(argc, argv); |
| 226 |
parser.options(desc); |
| 227 |
if (allow_unregistered) |
| 228 |
{ |
| 229 |
parser.allow_unregistered(); |
| 230 |
} |
| 231 |
return parser.run(); |
| 232 |
} |
| 233 |
|
| 234 |
template<typename F> |
| 235 |
bool handle_error_helper(const boost::program_options::options_description& desc, F parser) |
| 236 |
{ |
| 237 |
try |
| 238 |
{ |
| 239 |
return parser(); |
| 240 |
} |
| 241 |
catch (const std::exception& e) |
| 242 |
{ |
| 243 |
std::cerr << "Failed to parse arguments: " << e.what() << std::endl; |
| 244 |
std::cerr << desc << std::endl; |
| 245 |
return false; |
| 246 |
} |
| 247 |
catch (...) |
| 248 |
{ |
| 249 |
std::cerr << "Failed to parse arguments: unknown exception" << std::endl; |
| 250 |
std::cerr << desc << std::endl; |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
template<typename T, bool required, bool dependent, int NUM_DEPS> |
| 256 |
typename 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) |
| 257 |
{ |
| 258 |
auto value = vm[arg.name]; |
| 259 |
return !value.empty(); |
| 260 |
} |
| 261 |
|
| 262 |
template<typename T, bool required, bool dependent, int NUM_DEPS> |
| 263 |
bool is_arg_defaulted(const boost::program_options::variables_map& vm, const arg_descriptor<T, required, dependent, NUM_DEPS>& arg) |
| 264 |
{ |
| 265 |
return vm[arg.name].defaulted(); |
| 266 |
} |
| 267 |
|
| 268 |
template<typename T> |
| 269 |
T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, false, true>& arg) |
| 270 |
{ |
| 271 |
return arg.depf(get_arg(vm, arg.ref), is_arg_defaulted(vm, arg), vm[arg.name].template as<T>()); |
| 272 |
} |
| 273 |
|
| 274 |
template<typename T, int NUM_DEPS> |
| 275 |
T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, false, true, NUM_DEPS>& arg) |
| 276 |
{ |
| 277 |
std::array<bool, NUM_DEPS> depval; |
| 278 |
for (size_t i = 0; i < depval.size(); ++i) |
| 279 |
depval[i] = get_arg(vm, *arg.ref[i]); |
| 280 |
return arg.depf(depval, is_arg_defaulted(vm, arg), vm[arg.name].template as<T>()); |
| 281 |
} |
| 282 |
|
| 283 |
template<typename T, bool required> |
| 284 |
T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg) |
| 285 |
{ |
| 286 |
return vm[arg.name].template as<T>(); |
| 287 |
} |
| 288 |
|
| 289 |
template<bool dependent, int NUM_DEPS> |
| 290 |
inline bool has_arg(const boost::program_options::variables_map& vm, const arg_descriptor<bool, false, dependent, NUM_DEPS>& arg) |
| 291 |
{ |
| 292 |
return get_arg(vm, arg); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
extern const arg_descriptor<bool> arg_help; |
| 297 |
extern const arg_descriptor<bool> arg_version; |
| 298 |
} |