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 #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:
68  CLIOption(const N defaultValue,
69  const std::string& identifier,
70  const std::string& description,
71  const std::string& alias,
72  const std::string& cppName,
73  const bool required = false,
74  const bool input = true,
75  const bool noTranspose = false,
76  const std::string& /*testName*/ = "")
77  {
78  // Create the ParamData object to give to CLI.
79  util::ParamData data;
80 
81  data.desc = description;
82  data.name = identifier;
83  data.tname = TYPENAME(N);
84  data.alias = alias[0];
85  data.wasPassed = false;
86  data.noTranspose = noTranspose;
87  data.required = required;
88  data.input = input;
89  data.loaded = false;
90  data.persistent = false; // All CLI parameters are not persistent.
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 boostName = MapParameterName<
108  typename std::remove_pointer<N>::type>(identifier);
109  std::string progOptId = (alias[0] != '\0') ? boostName + ","
110  + std::string(1, alias[0]) : boostName;
111 
112  // Do a check to ensure that the boost name isn't already in use.
113  const std::map<std::string, util::ParamData>& parameters =
114  CLI::Parameters();
115  if (parameters.count(boostName) > 0)
116  {
117  // Create a fake Log::Fatal since it may not yet be initialized.
118  // Temporarily define color code escape sequences.
119  #ifndef _WIN32
120  #define BASH_RED "\033[0;31m"
121  #define BASH_CLEAR "\033[0m"
122  #else
123  #define BASH_RED ""
124  #define BASH_CLEAR ""
125  #endif
126 
127  // Temporary outstream object for detecting duplicate identifiers.
128  util::PrefixedOutStream outstr(std::cerr,
129  BASH_RED "[FATAL] " BASH_CLEAR, false, true /* fatal */);
130 
131  #undef BASH_RED
132  #undef BASH_CLEAR
133 
134  outstr << "Parameter --" << boostName << " (" << data.alias << ") "
135  << "is defined multiple times with the same identifiers."
136  << std::endl;
137  }
138 
139  CLI::Add(std::move(data));
140 
141  // Set some function pointers that we need.
142  CLI::GetSingleton().functionMap[tname]["DefaultParam"] =
143  &DefaultParam<N>;
144  CLI::GetSingleton().functionMap[tname]["OutputParam"] =
145  &OutputParam<N>;
146  CLI::GetSingleton().functionMap[tname]["GetPrintableParam"] =
147  &GetPrintableParam<N>;
148  CLI::GetSingleton().functionMap[tname]["StringTypeParam"] =
149  &StringTypeParam<N>;
150  CLI::GetSingleton().functionMap[tname]["GetParam"] = &GetParam<N>;
151  CLI::GetSingleton().functionMap[tname]["GetRawParam"] = &GetRawParam<N>;
152  CLI::GetSingleton().functionMap[tname]["AddToPO"] = &AddToPO<N>;
153  CLI::GetSingleton().functionMap[tname]["MapParameterName"] =
154  &MapParameterName<N>;
155  CLI::GetSingleton().functionMap[tname]["SetParam"] = &SetParam<N>;
156  CLI::GetSingleton().functionMap[tname]["GetPrintableParamName"] =
157  &GetPrintableParamName<N>;
158  CLI::GetSingleton().functionMap[tname]["GetPrintableParamValue"] =
159  &GetPrintableParamValue<N>;
160  CLI::GetSingleton().functionMap[tname]["GetAllocatedMemory"] =
161  &GetAllocatedMemory<N>;
162  CLI::GetSingleton().functionMap[tname]["DeleteAllocatedMemory"] =
163  &DeleteAllocatedMemory<N>;
164  CLI::GetSingleton().functionMap[tname]["InPlaceCopy"] = &InPlaceCopy<N>;
165  }
166 };
167 
177 {
178  public:
188  ProgramDoc(const std::string& programName,
189  const std::string& documentation);
190 
192  std::string programName;
194  std::string documentation;
195 };
196 
197 } // namespace cli
198 } // namespace bindings
199 } // namespace mlpack
200 
201 #endif
#define BASH_CLEAR
boost::any value
The actual value that is held.
Definition: param_data.hpp:82
Linear algebra utility functions, generally performed on matrices or vectors.
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:68
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:48
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:176
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:192
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:194
FunctionMapType functionMap
Definition: cli.hpp:302
bool noTranspose
True if this is a matrix that should not be transposed.
Definition: param_data.hpp:69