Public Member Functions |
| ~CLI () |
| Destructor.
|
Static Public Member Functions |
static void | Add (const std::string &path, const std::string &description, const std::string &alias="", bool required=false) |
| Adds a parameter to the hierarchy; use the PARAM_*() macros instead of this (i.e.
|
template<class T > |
static void | Add (const std::string &identifier, const std::string &description, const std::string &alias="", bool required=false) |
| 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.
|
static void | DefaultMessages () |
| Parses the parameters for 'help' and 'info'.
|
static void | Destroy () |
| Destroy the CLI object.
|
static std::string | GetDescription (const std::string &identifier) |
| Get the description of the specified node.
|
template<typename T > |
static T & | GetParam (const std::string &identifier) |
| Grab the value of type T found while parsing.
|
static CLI & | GetSingleton () |
| Retrieve the singleton.
|
static bool | HasParam (const std::string &identifier) |
| See if the specified flag was found while parsing.
|
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.
|
static void | ParseCommandLine (int argc, char **argv) |
| Parses the commandline for arguments.
|
static void | ParseStream (std::istream &stream) |
| Parses a stream for arguments.
|
static void | Print () |
| Print out the current hierarchy.
|
static void | PrintHelp (const std::string ¶m="") |
| Print out the help info of the hierarchy.
|
static void | RegisterProgramDoc (util::ProgramDoc *doc) |
| Registers a ProgramDoc object, which contains documentation about the program.
|
static void | RemoveDuplicateFlags (po::basic_parsed_options< char > &bpo) |
| Removes duplicate flags.
|
Public Attributes |
util::ProgramDoc * | doc |
| Pointer to the ProgramDoc object.
|
Private Types |
typedef std::map< std::string,
std::string > | amap_t |
| Map for aliases, from alias to actual name.
|
typedef std::map< std::string,
ParamData > | gmap_t |
| Map of global values.
|
Private Member Functions |
| CLI () |
| Make the constructor private, to preclude unauthorized instances.
|
| CLI (const std::string &optionsName) |
| Initialize desc with a particular name.
|
| CLI (const CLI &other) |
| Private copy constructor; we don't want copies floating around.
|
Static Private Member Functions |
static void | AddAlias (const std::string &alias, const std::string &original) |
| Maps a given alias to a given parameter.
|
static std::string | AliasReverseLookup (const std::string &value) |
| Returns an alias, if given the name of the original.
|
static void | RequiredOptions () |
| Checks that all required parameters have been specified on the command line.
|
static std::string | SanitizeString (const std::string &str) |
| Cleans up input pathnames, rendering strings such as /foo/bar and foo/bar/ equivalent inputs.
|
static void | UpdateGmap () |
| Parses the values given on the command line, overriding any default values.
|
Private Attributes |
amap_t | aliasValues |
po::options_description | desc |
| The documentation and names of options.
|
bool | didParse |
| True, if CLI was used to parse command line options.
|
gmap_t | globalValues |
std::list< std::string > | requiredOptions |
| Pathnames of required options.
|
Timers | timer |
| Holds the timer objects.
|
po::variables_map | vmap |
| Values of the options given by user.
|
Static Private Attributes |
static CLI * | singleton |
| The singleton itself.
|
Friends |
class | Timer |
| So that Timer::Start() and Timer::Stop() can access the timer variable.
|
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
- 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):
- PARAM_FLAG(ID, DESC, ALIAS)
- PARAM_DOUBLE(ID, DESC, ALIAS, DEF)
- PARAM_INT(ID, DESC, ALIAS, DEF)
- PARAM_STRING(ID, DESC, ALIAS, DEF)
- Parameters:
-
ID | Name of the parameter. |
DESC | Short description of the parameter (one/two sentences). |
ALIAS | An alias for the parameter. |
DEF | Default 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 AllkNN executable (methods/neighbor_search/allknn_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:
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":
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 http://mlpack.org/trac/ticket/74 for more information.
Definition at line 530 of file cli.hpp.