mlpack  2.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 <mlpack/prereqs.hpp>
26 
27 #include "timers.hpp"
28 #include "cli_deleter.hpp" // To make sure we can delete the singleton.
29 #include "version.hpp"
30 #include "param.hpp"
31 
35 #define TYPENAME(x) (std::string(typeid(x).name()))
36 
37 namespace po = boost::program_options;
38 
39 namespace mlpack {
40 
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 
53 struct ParamData
54 {
56  std::string name;
58  std::string desc;
60  std::string tname;
62  boost::any value;
64  bool wasPassed;
66  bool isFlag;
67 };
68 
194 class CLI
195 {
196  public:
209  static void Add(const std::string& path,
210  const std::string& description,
211  const std::string& alias = "",
212  const bool required = false,
213  const bool input = true);
214 
227  template<class T>
228  static void Add(const std::string& identifier,
229  const std::string& description,
230  const std::string& alias = "",
231  const bool required = false,
232  const bool input = true);
233 
241  static void AddFlag(const std::string& identifier,
242  const std::string& description,
243  const std::string& alias = "");
244 
249  static void DefaultMessages();
250 
256  static void Destroy();
257 
264  template<typename T>
265  static T& GetParam(const std::string& identifier);
266 
273  static std::string GetDescription(const std::string& identifier);
274 
287  static CLI& GetSingleton();
288 
294  static bool HasParam(const std::string& identifier);
295 
303  static std::string HyphenateString(const std::string& str, int padding);
304 
311  static void ParseCommandLine(int argc, char** argv);
312 
318  static void RemoveDuplicateFlags(po::basic_parsed_options<char>& bpo);
319 
323  static void PrintOutput();
324 
328  static void Print();
329 
333  static void PrintHelp(const std::string& param = "");
334 
343 
347  ~CLI();
348 
349  private:
351  po::options_description desc;
352 
354  po::variables_map vmap;
355 
357  std::list<std::string> requiredOptions;
358 
360  std::list<std::string> inputOptions;
362  std::list<std::string> outputOptions;
363 
365  typedef std::map<std::string, ParamData> gmap_t;
366  gmap_t globalValues;
367 
369  typedef std::map<std::string, std::string> amap_t;
370  amap_t aliasValues;
371 
373  static CLI* singleton;
374 
376  bool didParse;
377 
379  std::string programName;
380 
382  Timers timer;
383 
385  friend class Timer;
386 
387  public:
390 
391  private:
398  static void AddAlias(const std::string& alias, const std::string& original);
399 
407  static std::string AliasReverseLookup(const std::string& value);
408 
414  static void RequiredOptions();
415 
419  static void UpdateGmap();
420 
424  CLI();
425 
431  CLI(const std::string& optionsName);
432 
434  CLI(const CLI& other);
435 
437  template<typename T>
438  struct IsStdVector { const static bool value = false; };
439 
441  template<typename eT>
442  struct IsStdVector<std::vector<eT>> { const static bool value = true; };
443 
452  template<typename T>
453  void AddOption(
454  const char* optId,
455  const char* descr,
456  const typename boost::disable_if<IsStdVector<T>>::type* /* junk */ = 0);
457 
466  template<typename T>
467  void AddOption(
468  const char* optId,
469  const char* descr,
470  const typename boost::enable_if<IsStdVector<T>>::type* /* junk */ = 0);
471 };
472 
473 } // namespace mlpack
474 
475 // Include the actual definitions of templated methods
476 #include "cli_impl.hpp"
477 
478 #endif
~CLI()
Destructor.
static CLI & GetSingleton()
Retrieve the singleton.
static void RegisterProgramDoc(util::ProgramDoc *doc)
Registers a ProgramDoc object, which contains documentation about the program.
static void PrintHelp(const std::string &param="")
Print out the help info of the hierarchy.
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool isFlag
True if the wasPassed value should not be ignored.
Definition: cli.hpp:66
static std::string GetDescription(const std::string &identifier)
Get the description of the specified node.
bool wasPassed
True if this parameter was passed in via command line or file.
Definition: cli.hpp:64
std::string name
Name of this parameter.
Definition: cli.hpp:56
The timer class provides a way for mlpack methods to be timed.
Definition: timers.hpp:38
static void Print()
Print out the current hierarchy.
static void RemoveDuplicateFlags(po::basic_parsed_options< char > &bpo)
Removes duplicate flags.
static void Destroy()
Destroy the CLI object.
util::ProgramDoc * doc
Pointer to the ProgramDoc object.
Definition: cli.hpp:389
std::string tname
Type information of this parameter.
Definition: cli.hpp:60
static T & GetParam(const std::string &identifier)
Grab the value of type T found while parsing.
Aids in the extensibility of CLI by focusing potential changes into one structure.
Definition: cli.hpp:53
static void PrintOutput()
Print the value of any output options on stdout.
static void DefaultMessages()
Parses the parameters for &#39;help&#39; and &#39;info&#39;.
static std::string HyphenateString(const std::string &str, int padding)
Hyphenate a string or split it onto multiple 80-character lines, with some amount of padding on each ...
static void Add(const std::string &path, const std::string &description, const std::string &alias="", const bool required=false, const bool input=true)
Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. ...
static void AddFlag(const std::string &identifier, const std::string &description, const std::string &alias="")
Adds a flag parameter to the hierarchy; use PARAM_FLAG() instead of this.
A static object whose constructor registers program documentation with the CLI class.
Definition: option.hpp:80
std::string desc
Description of this parameter, if any.
Definition: cli.hpp:58
Parses the command line for parameters and holds user-specified parameters.
Definition: cli.hpp:194
static void ParseCommandLine(int argc, char **argv)
Parses the commandline for arguments.
boost::any value
The actual value of this parameter.
Definition: cli.hpp:62
static bool HasParam(const std::string &identifier)
See if the specified flag was found while parsing.