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
|
// Copyright (c) 2012-2013 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include <iostream>
#include <type_traits>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp>
#include "include_base_utils.h"
namespace command_line
{
template<typename T, bool required = false>
struct arg_descriptor;
template<typename T>
struct arg_descriptor<T, false>
{
typedef T value_type;
const char* name;
const char* description;
T default_value;
bool not_use_default;
};
template<typename T>
struct arg_descriptor<std::vector<T>, false>
{
typedef std::vector<T> value_type;
const char* name;
const char* description;
};
template<typename T>
struct arg_descriptor<T, true>
{
static_assert(!std::is_same<T, bool>::value, "Boolean switch can't be required");
typedef T value_type;
const char* name;
const char* description;
};
template<typename T>
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, true>& /*arg*/)
{
return boost::program_options::value<T>()->required();
}
template<typename T>
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg)
{
auto semantic = boost::program_options::value<T>();
if (!arg.not_use_default)
semantic->default_value(arg.default_value);
return semantic;
}
template<typename T>
boost::program_options::typed_value<T, char>* make_semantic(const arg_descriptor<T, false>& arg, const T& def)
{
auto semantic = boost::program_options::value<T>();
if (!arg.not_use_default)
semantic->default_value(def);
return semantic;
}
template<typename T>
boost::program_options::typed_value<std::vector<T>, char>* make_semantic(const arg_descriptor<std::vector<T>, false>& /*arg*/)
{
auto semantic = boost::program_options::value< std::vector<T> >();
semantic->default_value(std::vector<T>(), "");
return semantic;
}
template<typename T, bool required>
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, required>& arg, bool unique = true)
{
if (0 != description.find_nothrow(arg.name, false))
{
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
return;
}
description.add_options()(arg.name, make_semantic(arg), arg.description);
}
template<typename T>
void add_arg(boost::program_options::options_description& description, const arg_descriptor<T, false>& arg, const T& def, bool unique = true)
{
if (0 != description.find_nothrow(arg.name, false))
{
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
return;
}
description.add_options()(arg.name, make_semantic(arg, def), arg.description);
}
template<>
inline void add_arg(boost::program_options::options_description& description, const arg_descriptor<bool, false>& arg, bool unique)
{
if (0 != description.find_nothrow(arg.name, false))
{
CHECK_AND_ASSERT_MES(!unique, void(), "Argument already exists: " << arg.name);
return;
}
description.add_options()(arg.name, boost::program_options::bool_switch(), arg.description);
}
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)
{
auto parser = boost::program_options::command_line_parser(argc, argv);
parser.options(desc);
if (allow_unregistered)
{
parser.allow_unregistered();
}
return parser.run();
}
template<typename F>
bool handle_error_helper(const boost::program_options::options_description& desc, F parser)
{
try
{
return parser();
}
catch (std::exception& e)
{
std::cerr << "Failed to parse arguments: " << e.what() << std::endl;
std::cerr << desc << std::endl;
return false;
}
catch (...)
{
std::cerr << "Failed to parse arguments: unknown exception" << std::endl;
std::cerr << desc << std::endl;
return false;
}
}
template<typename T, bool required>
bool has_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
{
auto value = vm[arg.name];
return !value.empty();
}
template<typename T, bool required>
T get_arg(const boost::program_options::variables_map& vm, const arg_descriptor<T, required>& arg)
{
return vm[arg.name].template as<T>();
}
template<>
inline bool has_arg<bool, false>(const boost::program_options::variables_map& vm, const arg_descriptor<bool, false>& arg)
{
return get_arg<bool, false>(vm, arg);
}
extern const arg_descriptor<bool> arg_help;
extern const arg_descriptor<bool> arg_version;
extern const arg_descriptor<std::string> arg_data_dir;
}
|