mlpack
print_doc_functions_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_BINDINGS_CLI_PRINT_DOC_FUNCTIONS_IMPL_HPP
15 #define MLPACK_BINDINGS_CLI_PRINT_DOC_FUNCTIONS_IMPL_HPP
16 
18 
19 namespace mlpack {
20 namespace bindings {
21 namespace cli {
22 
27 inline std::string GetBindingName(const std::string& bindingName)
28 {
29  return "mlpack_" + bindingName;
30 }
31 
35 inline std::string PrintImport(const std::string& /* bindingName */)
36 {
37  return "";
38 }
39 
43 inline std::string PrintInputOptionInfo()
44 {
45  return "";
46 }
50 inline std::string PrintOutputOptionInfo()
51 {
52  return "";
53 }
54 
58 template<typename T>
59 inline std::string PrintValue(const T& value, bool quotes)
60 {
61  std::ostringstream oss;
62  if (quotes)
63  oss << "'";
64  oss << value;
65  if (quotes)
66  oss << "'";
67  return oss.str();
68 }
69 
73 template<typename T>
74 inline std::string PrintValue(const std::vector<T>& value, bool quotes)
75 {
76  std::ostringstream oss;
77  if (quotes)
78  oss << "'";
79  if (value.size() > 0)
80  {
81  oss << value[0];
82  for (size_t i = 1; i < value.size(); ++i)
83  oss << ", " << value[i];
84  }
85  if (quotes)
86  oss << "'";
87  return oss.str();
88 }
89 
93 inline std::string PrintDefault(const std::string& paramName)
94 {
95  if (IO::Parameters().count(paramName) == 0)
96  throw std::invalid_argument("unknown parameter " + paramName + "!");
97 
98  util::ParamData& d = IO::Parameters()[paramName];
99 
100  std::string defaultValue;
101  IO::GetSingleton().functionMap[d.tname]["DefaultParam"](d, NULL,
102  (void*) &defaultValue);
103 
104  return defaultValue;
105 }
106 
110 inline std::string PrintDataset(const std::string& dataset)
111 {
112  return "'" + dataset + ".csv'";
113 }
114 
118 inline std::string PrintModel(const std::string& model)
119 {
120  return "'" + model + ".bin'";
121 }
122 
123 // Base case for recursion.
124 inline std::string ProcessOptions() { return ""; }
125 
129 template<typename T, typename... Args>
130 std::string ProcessOptions(const std::string& paramName,
131  const T& value,
132  Args... args)
133 {
134  // See if it is part of the program.
135  std::string result = "";
136  if (IO::Parameters().count(paramName) > 0)
137  {
138  util::ParamData& d = IO::Parameters()[paramName];
139 
140  std::string name;
141  IO::GetSingleton().functionMap[d.tname]["GetPrintableParamName"](d, NULL,
142  (void*) &name);
143 
144  std::ostringstream ossValue;
145  ossValue << value;
146  std::string rawValue = ossValue.str();
147  std::string fullValue;
148  IO::GetSingleton().functionMap[d.tname]["GetPrintableParamValue"](d,
149  (void*) &rawValue, (void*) &fullValue);
150 
151  std::ostringstream oss;
152  if (d.tname != TYPENAME(bool))
153  oss << name << " " << fullValue;
154  else
155  oss << name;
156  result = oss.str();
157  }
158  else
159  {
160  throw std::runtime_error("Unknown parameter '" + paramName + "' " +
161  "encountered while assembling documentation! Check BINDING_LONG_DESC()"
162  + " and BINDING_EXAMPLE() declaration.");
163  }
164 
165  std::string rest = ProcessOptions(args...);
166  if (rest != "")
167  result += " " + rest;
168 
169  return result;
170 }
171 
176 template<typename... Args>
177 std::string ProgramCall(const std::string& programName, Args... args)
178 {
179  return util::HyphenateString("$ " + GetBindingName(programName) + " " +
180  ProcessOptions(args...), 2);
181 }
182 
187 inline std::string ProgramCall(const std::string& programName)
188 {
189  std::ostringstream oss;
190  oss << "$ " << GetBindingName(programName);
191 
192  // Handle all options---first input options, then output options.
193  std::map<std::string, util::ParamData>& parameters = IO::Parameters();
194 
195  for (auto& it : parameters)
196  {
197  if (!it.second.input || it.second.persistent)
198  continue;
199 
200  // Otherwise, print the name and the default value.
201  std::string name;
202  IO::GetSingleton().functionMap[it.second.tname]["GetPrintableParamName"](
203  it.second, NULL, (void*) &name);
204 
205  std::string value;
206  IO::GetSingleton().functionMap[it.second.tname]["DefaultParam"](
207  it.second, NULL, (void*) &value);
208  if (value == "''")
209  value = "<string>";
210 
211  oss << " ";
212  if (!it.second.required)
213  oss << "[";
214 
215  oss << name;
216  if (it.second.cppType != "bool")
217  oss << " " << value;
218 
219  if (!it.second.required)
220  oss << "]";
221  }
222 
223  // Now get the output options.
224  for (auto& it : parameters)
225  {
226  if (it.second.input)
227  continue;
228 
229  // Otherwise, print the name and the default value.
230  std::string name;
231  IO::GetSingleton().functionMap[it.second.tname]["GetPrintableParamName"](
232  it.second, NULL, (void*) &name);
233 
234  std::string value;
235  IO::GetSingleton().functionMap[it.second.tname]["DefaultParam"](
236  it.second, NULL, (void*) &value);
237  if (value == "''")
238  value = "<string>";
239 
240  oss << " [" << name;
241  if (it.second.cppType != "bool")
242  oss << " " << value;
243  oss << "]";
244  }
245 
246  return util::HyphenateString(oss.str(), 8);
247 }
248 
256 inline std::string ParamString(const std::string& paramName)
257 {
258  // Return the correct parameter name.
259  if (IO::Parameters().count(paramName) > 0)
260  {
261  util::ParamData& d = IO::Parameters()[paramName];
262 
263  std::string output;
264  IO::GetSingleton().functionMap[d.tname]["GetPrintableParamName"](d, NULL,
265  (void*) &output);
266  // Is there an alias?
267  std::string alias = "";
268  if (d.alias != '\0')
269  alias = " (-" + std::string(1, d.alias) + ")";
270 
271  return "'" + output + alias + "'";
272  }
273  else
274  {
275  throw std::runtime_error("Parameter '" + paramName + "' not known! Check "
276  "BINDING_LONG_DESC() and BINDING_EXAMPLE() definition.");
277  }
278 }
279 
280 } // namespace cli
281 } // namespace bindings
282 } // namespace mlpack
283 
284 #endif
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 ProgramCall(const std::string &programName, Args... args)
Given a program name and arguments for it, print what its invocation would be.
Definition: print_doc_functions_impl.hpp:177
std::string GetBindingName(const std::string &bindingName)
Given the name of a binding, print its command-line name (this returns "mlpack_<bindingName>".
Definition: print_doc_functions_impl.hpp:27
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
std::string PrintValue(const T &value, bool quotes)
Given a parameter type, print the corresponding value.
Definition: print_doc_functions_impl.hpp:59
static IO & GetSingleton()
Retrieve the singleton.
Definition: io.cpp:147
std::string PrintInputOptionInfo()
Print any special information about input options.
Definition: print_doc_functions_impl.hpp:43
std::string ProcessOptions()
Base case for recursion.
Definition: print_doc_functions_impl.hpp:124
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
std::string PrintDefault(const std::string &paramName)
Given a parameter name, print its corresponding default value.
Definition: print_doc_functions_impl.hpp:93
#define TYPENAME(x)
The TYPENAME macro is used internally to convert a type into a string.
Definition: param_data.hpp:22
std::string PrintImport(const std::string &bindingName)
Print any imports for CLI (there are none, so this returns an empty string).
Definition: print_doc_functions_impl.hpp:35
std::string PrintModel(const std::string &model)
Print a model type parameter (add .bin and return).
Definition: print_doc_functions_impl.hpp:118
static std::map< std::string, util::ParamData > & Parameters()
Return a modifiable list of parameters that IO knows about.
Definition: io.cpp:154
std::string PrintDataset(const std::string &dataset)
Print a dataset type parameter (add .csv and return).
Definition: print_doc_functions_impl.hpp:110
std::string ParamString(const std::string &paramName)
Print what a user would type to invoke the given option name.
Definition: print_doc_functions_impl.hpp:256
std::string PrintOutputOptionInfo()
Print any special information about output options.
Definition: print_doc_functions_impl.hpp:50