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 
17 #include "map_parameter_name.hpp"
18 
19 #include "third_party/CLI/CLI11.hpp"
20 
21 namespace mlpack {
22 namespace bindings {
23 namespace cli {
24 
32 template<typename T>
33 void 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,
43  std::tuple<mlpack::data::DatasetInfo,
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 
64 template<typename T>
65 void 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,
75  std::tuple<mlpack::data::DatasetInfo,
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 
96 template<typename T>
97 void 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 
126 template<typename T>
127 void 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 
156 template<typename T>
157 void 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 
187 template<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...
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
Linear algebra utility functions, generally performed on matrices or vectors.
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
std::string desc
Description of this parameter, if any.
Definition: param_data.hpp:58
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
char alias
Alias for this parameter.
Definition: param_data.hpp:63
std::string name
Name of this parameter.
Definition: param_data.hpp:56
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.