mlpack  master
cli.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_CORE_UTIL_CLI_HPP
15 #define MLPACK_CORE_UTIL_CLI_HPP
16 
17 #include <list>
18 #include <iostream>
19 #include <map>
20 #include <string>
21 
22 #include <boost/any.hpp>
23 #include <boost/program_options.hpp>
24 
25 #include "timers.hpp"
26 #include "cli_deleter.hpp" // To make sure we can delete the singleton.
27 #include "version.hpp"
28 #include "param.hpp"
29 
30 #include <mlpack/prereqs.hpp>
31 
32 #include "param_data.hpp"
33 #include "print_param.hpp"
34 #include "output_param.hpp"
35 #include "default_param.hpp"
36 #include "string_type_param.hpp"
37 
38 namespace po = boost::program_options;
39 
40 namespace mlpack {
41 namespace util {
42 
43 // Externally defined in option.hpp, this class holds information about the
44 // program being run.
45 class ProgramDoc;
46 
47 } // namespace util
48 
174 class CLI
175 {
176  public:
189  template<class T>
190  static void Add(const T& defaultValue,
191  const std::string& identifier,
192  const std::string& description,
193  const char alias = '\0',
194  const bool required = false,
195  const bool input = true,
196  const bool noTranspose = false);
197 
203  static void Destroy();
204 
211  template<typename T>
212  static T& GetParam(const std::string& identifier);
213 
223  template<typename T>
224  static typename util::ParameterType<T>::type& GetUnmappedParam(
225  const std::string& identifier);
226 
236  template<typename T>
237  static T& GetRawParam(const std::string& identifier);
238 
251  static CLI& GetSingleton();
252 
258  static bool HasParam(const std::string& identifier);
259 
267  static std::string HyphenateString(const std::string& str, int padding);
268 
275  static void ParseCommandLine(int argc, char** argv);
276 
281  static void PrintHelp(const std::string& param = "");
282 
290  static void RegisterProgramDoc(util::ProgramDoc* doc);
291 
295  ~CLI();
296 
297  private:
299  po::options_description desc;
300 
302  po::variables_map vmap;
303 
305  std::list<std::string> outputOptions;
307  std::list<std::string> requiredOptions;
309  std::map<char, std::string> aliases;
310 
312  std::map<std::string, util::ParamData> parameters;
313 
315  static CLI* singleton;
316 
318  bool didParse;
319 
323 
326 
328  friend class Timer;
329 
330  public:
333 
334  private:
341  static void AddAlias(const std::string& alias, const std::string& original);
342 
350  static char AliasReverseLookup(const std::string& value);
351 
355  CLI();
356 
358  CLI(const CLI& other);
359 
368  template<typename T>
369  void AddOption(
370  const char* optId,
371  const char* descr,
373  /* junk */ = 0);
374 
383  template<typename T>
384  void AddOption(
385  const char* optId,
386  const char* descr,
388  /* junk */ = 0);
389 };
390 
391 } // namespace mlpack
392 
393 // Include the actual definitions of templated methods
394 #include "cli_impl.hpp"
395 
396 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
std::list< std::string > requiredOptions
Convenience list of required options.
Definition: cli.hpp:307
The core includes that mlpack expects; standard C++ includes and Armadillo.
po::variables_map vmap
Values of the options given by user.
Definition: cli.hpp:302
std::map< std::string, util::ParamData > parameters
Map of parameters.
Definition: cli.hpp:312
Timers timer
Holds the timer objects.
Definition: cli.hpp:325
util::ProgramDoc * doc
Pointer to the ProgramDoc object.
Definition: cli.hpp:332
po::options_description desc
The documentation and names of options.
Definition: cli.hpp:299
The timer class provides a way for mlpack methods to be timed.
Definition: timers.hpp:38
Metaprogramming structure for vector detection.
Definition: param_data.hpp:25
std::list< std::string > outputOptions
Convenience list of output options.
Definition: cli.hpp:305
std::string programName
Holds the name of the program for –version.
Definition: cli.hpp:322
ParameterTypeDeducer< data::HasSerialize< T >::value, T >::type type
Definition: param_data.hpp:54
std::map< char, std::string > aliases
Convenience map from alias values to names.
Definition: cli.hpp:309
static CLI * singleton
The singleton itself.
Definition: cli.hpp:315
bool didParse
True, if CLI was used to parse command line options.
Definition: cli.hpp:318
test cpp RESULT_VARIABLE MEX_RESULT_TRASH OUTPUT_VARIABLE MEX_OUTPUT ERROR_VARIABLE MEX_ERROR_TRASH string(REGEX MATCH"Warning: You are using"MEX_WARNING"${MEX_OUTPUT}") if(MEX_WARNING) string(REGEX REPLACE".*using [a-zA-Z]* version \"([0-9.]*)[^\"]*\".*""\\1"OTHER_COMPILER_VERSION"$
Definition: CMakeLists.txt:18
A static object whose constructor registers program documentation with the CLI class.
Definition: option.hpp:71
Parses the command line for parameters and holds user-specified parameters.
Definition: cli.hpp:174
typename enable_if< B, T >::type enable_if_t
Definition: prereqs.hpp:59