mlpack 3.4.2
add_to_cli11.hpp
Go to the documentation of this file.
1
12#ifndef MLPACK_BINDINGS_CLI_ADD_TO_CLI11_HPP
13#define MLPACK_BINDINGS_CLI_ADD_TO_CLI11_HPP
14
18
19#include "third_party/CLI/CLI11.hpp"
20
21namespace mlpack {
22namespace bindings {
23namespace cli {
24
32template<typename T>
33void AddToCLI11(const std::string& cliName,
34 util::ParamData& param,
35 CLI::App& app,
36 const typename boost::disable_if<std::is_same<T,
37 bool>>::type* = 0,
38 const typename boost::disable_if<
39 arma::is_arma_type<T>>::type* = 0,
40 const typename boost::disable_if<
41 data::HasSerialize<T>>::type* = 0,
42 const typename boost::enable_if<std::is_same<T,
44 arma::mat>>>::type* = 0)
45{
46 app.add_option_function<std::string>(cliName.c_str(),
47 [&param](const std::string& value)
48 {
49 using TupleType = std::tuple<T, typename ParameterType<T>::type>;
50 TupleType& tuple = *boost::any_cast<TupleType>(&param.value);
51 std::get<1>(tuple) = boost::any_cast<std::string>(value);
52 param.wasPassed = true;
53 },
54 param.desc.c_str());
55}
56
64template<typename T>
65void AddToCLI11(const std::string& cliName,
66 util::ParamData& param,
67 CLI::App& app,
68 const typename boost::disable_if<std::is_same<T,
69 bool>>::type* = 0,
70 const typename boost::disable_if<
71 arma::is_arma_type<T>>::type* = 0,
72 const typename boost::enable_if<
73 data::HasSerialize<T>>::type* = 0,
74 const typename boost::disable_if<std::is_same<T,
76 arma::mat>>>::type* = 0)
77{
78 app.add_option_function<std::string>(cliName.c_str(),
79 [&param](const std::string& value)
80 {
81 using TupleType = std::tuple<T*, typename ParameterType<T>::type>;
82 TupleType& tuple = *boost::any_cast<TupleType>(&param.value);
83 std::get<1>(tuple) = boost::any_cast<std::string>(value);
84 param.wasPassed = true;
85 },
86 param.desc.c_str());
87}
88
96template<typename T>
97void AddToCLI11(const std::string& cliName,
98 util::ParamData& param,
99 CLI::App& app,
100 const typename boost::disable_if<
101 std::is_same<T, bool>>::type* = 0,
102 const typename boost::enable_if<
103 arma::is_arma_type<T>>::type* = 0,
104 const typename boost::disable_if<std::is_same<T,
105 std::tuple<mlpack::data::DatasetInfo,
106 arma::mat>>>::type* = 0)
107{
108 app.add_option_function<std::string>(cliName.c_str(),
109 [&param](const std::string& value)
110 {
111 using TupleType = std::tuple<T, typename ParameterType<T>::type>;
112 TupleType& tuple = *boost::any_cast<TupleType>(&param.value);
113 std::get<1>(tuple) = boost::any_cast<std::string>(value);
114 param.wasPassed = true;
115 },
116 param.desc.c_str());
117}
118
126template<typename T>
127void AddToCLI11(const std::string& cliName,
128 util::ParamData& param,
129 CLI::App& app,
130 const typename boost::disable_if<
131 std::is_same<T, bool>>::type* = 0,
132 const typename boost::disable_if<
133 arma::is_arma_type<T>>::type* = 0,
134 const typename boost::disable_if<
135 data::HasSerialize<T>>::type* = 0,
136 const typename boost::disable_if<std::is_same<T,
137 std::tuple<mlpack::data::DatasetInfo,
138 arma::mat>>>::type* = 0)
139{
140 app.add_option_function<T>(cliName.c_str(),
141 [&param](const T& value)
142 {
143 param.value = value;
144 param.wasPassed = true;
145 },
146 param.desc.c_str());
147}
148
156template<typename T>
157void AddToCLI11(const std::string& cliName,
158 util::ParamData& param,
159 CLI::App& app,
160 const typename boost::enable_if<
161 std::is_same<T, bool>>::type* = 0,
162 const typename boost::disable_if<
163 arma::is_arma_type<T>>::type* = 0,
164 const typename boost::disable_if<
165 data::HasSerialize<T>>::type* = 0,
166 const typename boost::disable_if<std::is_same<T,
167 std::tuple<mlpack::data::DatasetInfo,
168 arma::mat>>>::type* = 0)
169{
170 app.add_flag_function(cliName.c_str(),
171 [&param](const T& value)
172 {
173 param.value = value;
174 param.wasPassed = true;
175 },
176 param.desc.c_str());
177}
178
187template<typename T>
189 const void* /* input */,
190 void* output)
191{
192 // Cast CLI::App object.
193 CLI::App* app = (CLI::App*) output;
194
195 // Generate the name to be given to CLI11.
196 const std::string mappedName =
197 MapParameterName<typename std::remove_pointer<T>::type>(param.name);
198 std::string cliName = (param.alias != '\0') ?
199 "-" + std::string(1, param.alias) + ",--" + mappedName :
200 "--" + mappedName;
201
202 // Note that we have to add the option as type equal to the mapped type, not
203 // the true type of the option.
204 AddToCLI11<typename std::remove_pointer<T>::type>(
205 cliName, param, *app);
206}
207
208} // namespace cli
209} // namespace bindings
210} // namespace mlpack
211
212#endif
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
void AddToCLI11(const std::string &cliName, util::ParamData &param, CLI::App &app, const typename boost::disable_if< std::is_same< T, bool > >::type *=0, const typename boost::disable_if< arma::is_arma_type< T > >::type *=0, const typename boost::disable_if< data::HasSerialize< T > >::type *=0, const typename boost::enable_if< std::is_same< T, std::tuple< mlpack::data::DatasetInfo, arma::mat > > >::type *=0)
Add a tuple option to CLI11.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:53
char alias
Alias for this parameter.
Definition: param_data.hpp:63
std::string desc
Description of this parameter, if any.
Definition: param_data.hpp:58
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
std::string name
Name of this parameter.
Definition: param_data.hpp:56