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/cli.hpp>
19 #include "parameter_type.hpp"
20 #include "add_to_po.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 
34 namespace mlpack {
35 namespace bindings {
36 namespace cli {
37 
46 template<typename N>
47 class CLIOption
48 {
49  public:
67  CLIOption(const N defaultValue,
68  const std::string& identifier,
69  const std::string& description,
70  const std::string& alias,
71  const std::string& cppName,
72  const bool required = false,
73  const bool input = true,
74  const bool noTranspose = false,
75  const std::string& /*testName*/ = "")
76  {
77  // Create the ParamData object to give to CLI.
78  util::ParamData data;
79 
80  data.desc = description;
81  data.name = identifier;
82  data.tname = TYPENAME(N);
83  data.alias = alias[0];
84  data.wasPassed = false;
85  data.noTranspose = noTranspose;
86  data.required = required;
87  data.input = input;
88  data.loaded = false;
89  data.persistent = false; // All CLI parameters are not persistent.
90  data.cppType = cppName;
91 
92  // Apply default value.
93  if (std::is_same<typename std::remove_pointer<N>::type,
94  typename ParameterType<typename
95  std::remove_pointer<N>::type>::type>::value)
96  {
97  data.value = boost::any(defaultValue);
98  }
99  else
100  {
102  data.value = boost::any(std::tuple<N, decltype(tmp)>(defaultValue, tmp));
103  }
104 
105  const std::string tname = data.tname;
106  const std::string boostName = MapParameterName<
107  typename std::remove_pointer<N>::type>(identifier);
108  std::string progOptId = (alias[0] != '\0') ? boostName + ","
109  + std::string(1, alias[0]) : boostName;
110 
111  // Do a check to ensure that the boost name isn't already in use.
112  const std::map<std::string, util::ParamData>& parameters =
113  CLI::Parameters();
114  if (parameters.count(boostName) > 0)
115  {
116  // Create a fake Log::Fatal since it may not yet be initialized.
117  // Temporarily define color code escape sequences.
118  #ifndef _WIN32
119  #define BASH_RED "\033[0;31m"
120  #define BASH_CLEAR "\033[0m"
121  #else
122  #define BASH_RED ""
123  #define BASH_CLEAR ""
124  #endif
125 
126  // Temporary outstream object for detecting duplicate identifiers.
127  util::PrefixedOutStream outstr(std::cerr,
128  BASH_RED "[FATAL] " BASH_CLEAR, false, true /* fatal */);
129 
130  #undef BASH_RED
131  #undef BASH_CLEAR
132 
133  outstr << "Parameter --" << boostName << " (" << data.alias << ") "
134  << "is defined multiple times with the same identifiers."
135  << std::endl;
136  }
137 
138  CLI::Add(std::move(data));
139 
140  // Set some function pointers that we need.
141  CLI::GetSingleton().functionMap[tname]["DefaultParam"] =
142  &DefaultParam<N>;
143  CLI::GetSingleton().functionMap[tname]["OutputParam"] =
144  &OutputParam<N>;
145  CLI::GetSingleton().functionMap[tname]["GetPrintableParam"] =
146  &GetPrintableParam<N>;
147  CLI::GetSingleton().functionMap[tname]["StringTypeParam"] =
148  &StringTypeParam<N>;
149  CLI::GetSingleton().functionMap[tname]["GetParam"] = &GetParam<N>;
150  CLI::GetSingleton().functionMap[tname]["GetRawParam"] = &GetRawParam<N>;
151  CLI::GetSingleton().functionMap[tname]["AddToPO"] = &AddToPO<N>;
152  CLI::GetSingleton().functionMap[tname]["MapParameterName"] =
153  &MapParameterName<N>;
154  CLI::GetSingleton().functionMap[tname]["SetParam"] = &SetParam<N>;
155  CLI::GetSingleton().functionMap[tname]["GetPrintableParamName"] =
156  &GetPrintableParamName<N>;
157  CLI::GetSingleton().functionMap[tname]["GetPrintableParamValue"] =
158  &GetPrintableParamValue<N>;
159  CLI::GetSingleton().functionMap[tname]["GetAllocatedMemory"] =
160  &GetAllocatedMemory<N>;
161  CLI::GetSingleton().functionMap[tname]["DeleteAllocatedMemory"] =
162  &DeleteAllocatedMemory<N>;
163  }
164 };
165 
175 {
176  public:
186  ProgramDoc(const std::string& programName,
187  const std::string& documentation);
188 
190  std::string programName;
192  std::string documentation;
193 };
194 
195 } // namespace cli
196 } // namespace bindings
197 } // namespace mlpack
198 
199 #endif
#define BASH_CLEAR
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
strip_type.hpp
Definition: add_to_po.hpp:21
bool wasPassed
True if the option was passed to the program.
Definition: param_data.hpp:66
static CLI & GetSingleton()
Retrieve the singleton.
bool persistent
If this should be preserved across different settings (i.e.
Definition: param_data.hpp:79
static std::map< std::string, util::ParamData > & Parameters()
Return a modifiable list of parameters that CLI knows about.
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
std::string programName
#define BASH_RED
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
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 &="")
Construct an Option object.
Definition: cli_option.hpp:67
char alias
Alias for this parameter.
Definition: param_data.hpp:63
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
std::string MapParameterName(const std::string &identifier, 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::disable_if< std::is_same< T, std::tuple< mlpack::data::DatasetInfo, arma::mat >>>::type *=0)
If needed, map the parameter name to the name that is used by boost::program_options.
A static object whose constructor registers a parameter with the CLI class.
Definition: cli_option.hpp:47
std::string name
Name of this parameter.
Definition: param_data.hpp:56
bool required
True if this option is required.
Definition: param_data.hpp:71
A static object whose constructor registers program documentation with the CLI class.
Definition: cli_option.hpp:174
static void Add(util::ParamData &&d)
Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. ...
std::string programName
The name of the program.
Definition: cli_option.hpp:190
Utility struct to return the type that boost::program_options 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:84
Allows us to output to an ostream with a prefix at the beginning of each line, in the same way we wou...
std::string documentation
Documentation for what the program does.
Definition: cli_option.hpp:192
FunctionMapType functionMap
Definition: cli.hpp:290
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69