mlpack  master
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | List of all members
mlpack::CLI Class Reference

Parses the command line for parameters and holds user-specified parameters. More...

Public Member Functions

 ~CLI ()
 Destructor. More...
 

Static Public Member Functions

template<class T >
static void Add (const T &defaultValue, const std::string &identifier, const std::string &description, const char alias= '\0', const bool required=false, const bool input=true, const bool noTranspose=false)
 Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e. More...
 
static void Destroy ()
 Destroy the CLI object. More...
 
template<typename T >
static T & GetParam (const std::string &identifier)
 Grab the value of type T found while parsing. More...
 
template<typename T >
static T & GetRawParam (const std::string &identifier)
 Get the raw value of the parameter before the processing that GetParam() would normally do. More...
 
static CLIGetSingleton ()
 Retrieve the singleton. More...
 
template<typename T >
static util::ParameterType< T >::type & GetUnmappedParam (const std::string &identifier)
 Get the unmapped (i.e. More...
 
static bool HasParam (const std::string &identifier)
 See if the specified flag was found while parsing. More...
 
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 line. More...
 
static void ParseCommandLine (int argc, char **argv)
 Parses the commandline for arguments. More...
 
static void PrintHelp (const std::string &param="")
 Print out the help info for the given parameter (or all parameters if no argument is specified). More...
 
static void RegisterProgramDoc (util::ProgramDoc *doc)
 Registers a ProgramDoc object, which contains documentation about the program. More...
 

Public Attributes

util::ProgramDocdoc
 Pointer to the ProgramDoc object. More...
 

Private Member Functions

 CLI ()
 Make the constructor private, to preclude unauthorized instances. More...
 
 CLI (const CLI &other)
 Private copy constructor; we don't want copies floating around. More...
 
template<typename T >
void AddOption (const char *optId, const char *descr, const typename std::enable_if_t<!util::IsStdVector< T >::value > *=0)
 Add an option if it is not a vector type. More...
 
template<typename T >
void AddOption (const char *optId, const char *descr, const typename std::enable_if_t< util::IsStdVector< T >::value > *=0)
 Add an option if it is a vector type. More...
 

Static Private Member Functions

static void AddAlias (const std::string &alias, const std::string &original)
 Maps a given alias to a given parameter. More...
 
static char AliasReverseLookup (const std::string &value)
 Returns an alias, if given the name of the original. More...
 

Private Attributes

std::map< char, std::stringaliases
 Convenience map from alias values to names. More...
 
po::options_description desc
 The documentation and names of options. More...
 
bool didParse
 True, if CLI was used to parse command line options. More...
 
std::list< std::stringoutputOptions
 Convenience list of output options. More...
 
std::map< std::string, util::ParamDataparameters
 Map of parameters. More...
 
std::string programName
 Holds the name of the program for –version. More...
 
std::list< std::stringrequiredOptions
 Convenience list of required options. More...
 
Timers timer
 Holds the timer objects. More...
 
po::variables_map vmap
 Values of the options given by user. More...
 

Static Private Attributes

static CLIsingleton
 The singleton itself. More...
 

Detailed Description

Parses the command line for parameters and holds user-specified parameters.

The CLI class is a subsystem by which parameters for machine learning methods can be specified and accessed. In conjunction with the macros PARAM_DOUBLE, PARAM_INT, PARAM_STRING, PARAM_FLAG, and others, this class aims to make user configurability of mlpack methods very easy. There are only three methods in CLI that a user should need: CLI::ParseCommandLine(), CLI::GetParam(), and CLI::HasParam() (in addition to the PARAM_*() macros).

Adding parameters to a program

$ ./executable --bar=5
Note
The = is optional; a space can also be used.

A parameter is specified by using one of the following macros (this is not a complete list; see core/io/cli.hpp):

Parameters
IDName of the parameter.
DESCShort description of the parameter (one/two sentences).
ALIASAn alias for the parameter.
DEFDefault value of the parameter.

The flag (boolean) type automatically defaults to false; it is specified merely as a flag on the command line (no '=true' is required).

Here is an example of a few parameters being defined; this is for the KNN executable (methods/neighbor_search/knn_main.cpp):

PARAM_STRING_REQ("reference_file", "File containing the reference dataset.",
"r");
PARAM_STRING_REQ("distances_file", "File to output distances into.", "d");
PARAM_STRING_REQ("neighbors_file", "File to output neighbors into.", "n");
PARAM_INT_REQ("k", "Number of furthest neighbors to find.", "k");
PARAM_STRING("query_file", "File containing query points (optional).", "q",
"");
PARAM_INT("leaf_size", "Leaf size for tree building.", "l", 20);
PARAM_FLAG("naive", "If true, O(n^2) naive mode is used for computation.",
"N");
PARAM_FLAG("single_mode", "If true, single-tree search is used (as opposed "
"to dual-tree search.", "s");

More documentation is available on the PARAM_*() macros in the documentation for core/io/cli.hpp.

Documenting the program itself

In addition to allowing documentation for each individual parameter and module, the PROGRAM_INFO() macro provides support for documenting the program itself. There should only be one instance of the PROGRAM_INFO() macro. Below is an example:

PROGRAM_INFO("Maximum Variance Unfolding", "This program performs maximum "
"variance unfolding on the given dataset, writing a lower-dimensional "
"unfolded dataset to the given output file.");

This description should be verbose, and explain to a non-expert user what the program does and how to use it. If relevant, paper citations should be included.

Parsing the command line with CLI

To have CLI parse the command line at the beginning of code execution, only a call to ParseCommandLine() is necessary:

int main(int argc, char** argv)
{
CLI::ParseCommandLine(argc, argv);
...
}

CLI provides –help and –info options which give nicely formatted documentation of each option; the documentation is generated from the DESC arguments in the PARAM_*() macros.

Getting parameters with CLI

When the parameters have been defined, the next important thing is how to access them. For this, the HasParam() and GetParam() methods are used. For instance, to see if the user passed the flag (boolean) "naive":

if (CLI::HasParam("naive"))
{
Log::Info << "Naive has been passed!" << std::endl;
}

To get the value of a parameter, such as a string, use GetParam:

const std::string filename = CLI::GetParam<std::string>("filename");
Note
Options should only be defined in files which define main() (that is, main executables). If options are defined elsewhere, they may be spuriously included into other executables and confuse users. Similarly, if your executable has options which you did not define, it is probably because the option is defined somewhere else and included in your executable.
Bug:
The COUNTER variable is used in most cases to guarantee a unique global identifier for options declared using the PARAM_*() macros. However, not all compilers have this support–most notably, gcc < 4.3. In that case, the LINE macro is used as an attempt to get a unique global identifier, but collisions are still possible, and they produce bizarre error messages. See https://github.com/mlpack/mlpack/issues/100 for more information.

Definition at line 174 of file cli.hpp.

Constructor & Destructor Documentation

mlpack::CLI::~CLI ( )

Destructor.

mlpack::CLI::CLI ( )
private

Make the constructor private, to preclude unauthorized instances.

mlpack::CLI::CLI ( const CLI other)
private

Private copy constructor; we don't want copies floating around.

Member Function Documentation

template<class T >
static void mlpack::CLI::Add ( const T &  defaultValue,
const std::string identifier,
const std::string description,
const char  alias = '\0',
const bool  required = false,
const bool  input = true,
const bool  noTranspose = false 
)
static

Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e.

PARAM_INT()).

Parameters
identifierThe name of the parameter.
descriptionShort string description of the parameter.
aliasAn alias for the parameter, defaults to '\0' (no alias).
requiredIndicates if parameter must be set on command line.
inputIf true, the parameter is an input (not output) parameter.
noTransposeIf the parameter is a matrix and this is true, then the matrix will not be transposed on loading.
static void mlpack::CLI::AddAlias ( const std::string alias,
const std::string original 
)
staticprivate

Maps a given alias to a given parameter.

Parameters
aliasThe name of the alias to be mapped.
originalThe name of the parameter to be mapped.
template<typename T >
void mlpack::CLI::AddOption ( const char *  optId,
const char *  descr,
const typename std::enable_if_t<!util::IsStdVector< T >::value > *  = 0 
)
private

Add an option if it is not a vector type.

This is a utility function used by CLI::Add.

Template Parameters
Typeof parameter.
Parameters
optIdName of parameter.
descrDescription.
template<typename T >
void mlpack::CLI::AddOption ( const char *  optId,
const char *  descr,
const typename std::enable_if_t< util::IsStdVector< T >::value > *  = 0 
)
private

Add an option if it is a vector type.

This is a utility function used by CLI::Add.

Template Parameters
Typeof parameter.
Parameters
optIdName of parameter.
descrDescription.
static char mlpack::CLI::AliasReverseLookup ( const std::string value)
staticprivate

Returns an alias, if given the name of the original.

Parameters
valueThe value in a key:value pair where the key is an alias.
Returns
The alias associated with value.
static void mlpack::CLI::Destroy ( )
static

Destroy the CLI object.

This resets the pointer to the singleton, so in case someone tries to access it after destruction, a new one will be made (the program will not fail).

template<typename T >
static T& mlpack::CLI::GetParam ( const std::string identifier)
static

Grab the value of type T found while parsing.

You can set the value using this reference safely.

Parameters
identifierThe name of the parameter in question.
template<typename T >
static T& mlpack::CLI::GetRawParam ( const std::string identifier)
static

Get the raw value of the parameter before the processing that GetParam() would normally do.

Note that this does not perform any data loading or manipulation like GetParam() does. So if you want to access a matrix or model (or similar) parameter before it is loaded, this is the method to use.

Parameters
identifierThe name of the parameter in question.
static CLI& mlpack::CLI::GetSingleton ( )
static

Retrieve the singleton.

Not exposed to the outside, so as to spare users some ungainly x.GetSingleton().foo() syntax.

In this case, the singleton is used to store data for the static methods, as there is no point in defining static methods only to have users call private instance methods.

Returns
The singleton instance for use in the static methods.
template<typename T >
static util::ParameterType<T>::type& mlpack::CLI::GetUnmappedParam ( const std::string identifier)
static

Get the unmapped (i.e.

what the user specifies on the command-line) value of type ParameterType<T>::value found while parsing. You can set the value using this reference safely. You should not need to use this function unless you are doing something tricky (like getting the filename a user specified for a matrix parameter or something).

Parameters
identifierThe name of the parameter in question.
static bool mlpack::CLI::HasParam ( const std::string identifier)
static

See if the specified flag was found while parsing.

Parameters
identifierThe name of the parameter in question.
static std::string mlpack::CLI::HyphenateString ( const std::string str,
int  padding 
)
static

Hyphenate a string or split it onto multiple 80-character lines, with some amount of padding on each line.

This is ued for option output.

Parameters
strString to hyphenate (splits are on ' ').
paddingAmount of padding on the left for each new line.
static void mlpack::CLI::ParseCommandLine ( int  argc,
char **  argv 
)
static

Parses the commandline for arguments.

Parameters
argcThe number of arguments on the commandline.
argvThe array of arguments as strings.
static void mlpack::CLI::PrintHelp ( const std::string param = "")
static

Print out the help info for the given parameter (or all parameters if no argument is specified).

static void mlpack::CLI::RegisterProgramDoc ( util::ProgramDoc doc)
static

Registers a ProgramDoc object, which contains documentation about the program.

If this method has been called before (that is, if two ProgramDocs are instantiated in the program), a fatal error will occur.

Parameters
docPointer to the ProgramDoc object.

Member Data Documentation

std::map<char, std::string> mlpack::CLI::aliases
private

Convenience map from alias values to names.

Definition at line 309 of file cli.hpp.

po::options_description mlpack::CLI::desc
private

The documentation and names of options.

Definition at line 299 of file cli.hpp.

bool mlpack::CLI::didParse
private

True, if CLI was used to parse command line options.

Definition at line 318 of file cli.hpp.

util::ProgramDoc* mlpack::CLI::doc

Pointer to the ProgramDoc object.

Definition at line 332 of file cli.hpp.

std::list<std::string> mlpack::CLI::outputOptions
private

Convenience list of output options.

Definition at line 305 of file cli.hpp.

std::map<std::string, util::ParamData> mlpack::CLI::parameters
private

Map of parameters.

Definition at line 312 of file cli.hpp.

std::string mlpack::CLI::programName
private

Holds the name of the program for –version.

This is the true program name (argv[0]) not what is given in ProgramDoc.

Definition at line 322 of file cli.hpp.

std::list<std::string> mlpack::CLI::requiredOptions
private

Convenience list of required options.

Definition at line 307 of file cli.hpp.

CLI* mlpack::CLI::singleton
staticprivate

The singleton itself.

Definition at line 315 of file cli.hpp.

Timers mlpack::CLI::timer
private

Holds the timer objects.

Definition at line 325 of file cli.hpp.

po::variables_map mlpack::CLI::vmap
private

Values of the options given by user.

Definition at line 302 of file cli.hpp.


The documentation for this class was generated from the following file: