cli_option.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_BINDINGS_CLI_CLI_OPTION_HPP
14 #define MLPACK_CORE_BINDINGS_CLI_CLI_OPTION_HPP
15 
16 #include <string>
17 
18 #include <mlpack/core/util/io.hpp>
19 #include "parameter_type.hpp"
20 #include "add_to_cli11.hpp"
21 #include "default_param.hpp"
22 #include "output_param.hpp"
23 #include "get_printable_param.hpp"
24 #include "string_type_param.hpp"
25 #include "get_param.hpp"
26 #include "get_raw_param.hpp"
27 #include "map_parameter_name.hpp"
28 #include "set_param.hpp"
31 #include "get_allocated_memory.hpp"
33 #include "in_place_copy.hpp"
34 
35 namespace mlpack {
36 namespace bindings {
37 namespace cli {
38 
47 template<typename N>
48 class CLIOption
49 {
50  public:
69  CLIOption(const N defaultValue,
70  const std::string& identifier,
71  const std::string& description,
72  const std::string& alias,
73  const std::string& cppName,
74  const bool required = false,
75  const bool input = true,
76  const bool noTranspose = false,
77  const std::string& bindingName = "")
78  {
79  // Create the ParamData object to give to CLI.
80  util::ParamData data;
81 
82  data.desc = description;
83  data.name = identifier;
84  data.tname = TYPENAME(N);
85  data.alias = alias[0];
86  data.wasPassed = false;
87  data.noTranspose = noTranspose;
88  data.required = required;
89  data.input = input;
90  data.loaded = false;
91  data.cppType = cppName;
92 
93  // Apply default value.
94  if (std::is_same<typename std::remove_pointer<N>::type,
95  typename ParameterType<typename
96  std::remove_pointer<N>::type>::type>::value)
97  {
98  data.value = boost::any(defaultValue);
99  }
100  else
101  {
103  data.value = boost::any(std::tuple<N, decltype(tmp)>(defaultValue, tmp));
104  }
105 
106  const std::string tname = data.tname;
107  const std::string cliName = MapParameterName<
108  typename std::remove_pointer<N>::type>(identifier);
109  std::string progOptId = (alias[0] != '\0') ?
110  "-" + std::string(1, alias[0]) + ",--" + cliName : "--" + cliName;
111 
112  // Set some function pointers that we need.
113  IO::AddFunction(tname, "DefaultParam", &DefaultParam<N>);
114  IO::AddFunction(tname, "OutputParam", &OutputParam<N>);
115  IO::AddFunction(tname, "GetPrintableParam", &GetPrintableParam<N>);
116  IO::AddFunction(tname, "StringTypeParam", &StringTypeParam<N>);
117  IO::AddFunction(tname, "GetParam", &GetParam<N>);
118  IO::AddFunction(tname, "GetRawParam", &GetRawParam<N>);
119  IO::AddFunction(tname, "AddToCLI11", &AddToCLI11<N>);
120  IO::AddFunction(tname, "MapParameterName", &MapParameterName<N>);
121  IO::AddFunction(tname, "GetPrintableParamName", &GetPrintableParamName<N>);
122  IO::AddFunction(tname, "GetPrintableParamValue",
123  &GetPrintableParamValue<N>);
124  IO::AddFunction(tname, "GetAllocatedMemory", &GetAllocatedMemory<N>);
125  IO::AddFunction(tname, "DeleteAllocatedMemory", &DeleteAllocatedMemory<N>);
126  IO::AddFunction(tname, "InPlaceCopy", &InPlaceCopy<N>);
127 
128  IO::AddParameter(bindingName, std::move(data));
129  }
130 };
131 
132 } // namespace cli
133 } // namespace bindings
134 } // namespace mlpack
135 
136 #endif
boost::any value
The actual value that is held.
Definition: param_data.hpp:79
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
bool input
True if this option is an input option (otherwise, it is output).
Definition: param_data.hpp:73
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
bool loaded
If this is an input parameter that needs extra loading, this indicates whether or not it has been loa...
Definition: param_data.hpp:76
#define TYPENAME(x)
The TYPENAME macro is used internally to convert a type into a string.
Definition: param_data.hpp:22
char alias
Alias for this parameter.
Definition: param_data.hpp:63
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
A static object whose constructor registers a parameter with the IO class.
Definition: cli_option.hpp:48
std::string MapParameterName(const std::string &identifier, const typename std::enable_if<!arma::is_arma_type< T >::value >::type *=0, const typename std::enable_if<!data::HasSerialize< T >::value >::type *=0, const typename std::enable_if<!std::is_same< T, std::tuple< mlpack::data::DatasetInfo, arma::mat >>::value >::type *=0)
If needed, map the parameter name to the name that is used by CLI11.
std::string name
Name of this parameter.
Definition: param_data.hpp:56
static void AddFunction(const std::string &type, const std::string &name, void(*func)(util::ParamData &, const void *, void *))
Add a function to the function map.
bool required
True if this option is required.
Definition: param_data.hpp:71
CLIOption(const N defaultValue, const std::string &identifier, const std::string &description, const std::string &alias, const std::string &cppName, const bool required=false, const bool input=true, const bool noTranspose=false, const std::string &bindingName="")
Construct an Option object.
Definition: cli_option.hpp:69
static void AddParameter(const std::string &bindingName, util::ParamData &&d)
Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. ...
Utility struct to return the type that CLI11 should accept for a given input type.
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:81
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69