The class ParseOptions is for parsing command-line options; see Parsing command-line options for more documentation. More...
#include <parse-options.h>
Classes | |
struct | DocInfo |
Structure for options' documentation. More... | |
Public Member Functions | |
ParseOptions (const char *usage) | |
ParseOptions (const std::string &prefix, OptionsItf *other) | |
This is a constructor for the special case where some options are registered with a prefix to avoid conflicts. More... | |
~ParseOptions () | |
void | Register (const std::string &name, bool *ptr, const std::string &doc) |
void | Register (const std::string &name, int32 *ptr, const std::string &doc) |
void | Register (const std::string &name, uint32 *ptr, const std::string &doc) |
void | Register (const std::string &name, float *ptr, const std::string &doc) |
void | Register (const std::string &name, double *ptr, const std::string &doc) |
void | Register (const std::string &name, std::string *ptr, const std::string &doc) |
void | DisableOption (const std::string &name) |
If called after registering an option and before calling Read(), disables that option from being used. More... | |
template<typename T > | |
void | RegisterStandard (const std::string &name, T *ptr, const std::string &doc) |
This one is used for registering standard parameters of all the programs. More... | |
int | Read (int argc, const char *const *argv) |
Parses the command line options and fills the ParseOptions-registered variables. More... | |
void | PrintUsage (bool print_command_line=false) |
Prints the usage documentation [provided in the constructor]. More... | |
void | PrintConfig (std::ostream &os) |
Prints the actual configuration of all the registered variables. More... | |
void | ReadConfigFile (const std::string &filename) |
Reads the options values from a config file. More... | |
int | NumArgs () const |
Number of positional parameters (c.f. argc-1). More... | |
std::string | GetArg (int param) const |
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility. More... | |
std::string | GetOptArg (int param) const |
Public Member Functions inherited from OptionsItf | |
virtual | ~OptionsItf () |
Static Public Member Functions | |
static std::string | Escape (const std::string &str) |
The following function will return a possibly quoted and escaped version of "str", according to the current shell. More... | |
Protected Member Functions | |
void | SplitLongArg (const std::string &in, std::string *key, std::string *value, bool *has_equal_sign) |
SplitLongArg parses an argument of the form –a=b, –a=, or –a,. More... | |
void | NormalizeArgName (std::string *str) |
Private Types | |
typedef std::map< std::string, DocInfo > | DocMapType |
Private Member Functions | |
template<typename T > | |
void | RegisterTmpl (const std::string &name, T *ptr, const std::string &doc) |
Template to register various variable types, used for program-specific parameters. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, bool *b, const std::string &doc, bool is_standard) |
Register boolean variable. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, int32 *i, const std::string &doc, bool is_standard) |
Register int32 variable. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, uint32 *u, const std::string &doc, bool is_standard) |
Register unsinged int32 variable. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, float *f, const std::string &doc, bool is_standard) |
Register float variable. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, double *f, const std::string &doc, bool is_standard) |
Register double variable [useful as we change BaseFloat type]. More... | |
void | RegisterSpecific (const std::string &name, const std::string &idx, std::string *s, const std::string &doc, bool is_standard) |
Register string variable. More... | |
template<typename T > | |
void | RegisterCommon (const std::string &name, T *ptr, const std::string &doc, bool is_standard) |
Does the actual job for both kinds of parameters Does the common part of the job for all datatypes, then calls RegisterSpecific. More... | |
bool | SetOption (const std::string &key, const std::string &value, bool has_equal_sign) |
Set option with name "key" to "value"; will crash if can't do it. More... | |
bool | ToBool (std::string str) |
int32 | ToInt (const std::string &str) |
uint32 | ToUint (const std::string &str) |
float | ToFloat (const std::string &str) |
double | ToDouble (const std::string &str) |
Private Attributes | |
std::map< std::string, bool * > | bool_map_ |
std::map< std::string, int32 * > | int_map_ |
std::map< std::string, uint32 * > | uint_map_ |
std::map< std::string, float * > | float_map_ |
std::map< std::string, double * > | double_map_ |
std::map< std::string, std::string * > | string_map_ |
DocMapType | doc_map_ |
map for the documentation More... | |
bool | print_args_ |
variable for the implicit –print-args parameter More... | |
bool | help_ |
variable for the implicit –help parameter More... | |
std::string | config_ |
variable for the implicit –config parameter More... | |
std::vector< std::string > | positional_args_ |
const char * | usage_ |
int | argc_ |
const char *const * | argv_ |
std::string | prefix_ |
These members are not normally used. More... | |
OptionsItf * | other_parser_ |
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more documentation.
Definition at line 36 of file parse-options.h.
|
private |
Definition at line 206 of file parse-options.h.
|
inlineexplicit |
Definition at line 38 of file parse-options.h.
References ParseOptions::config_, kaldi::g_kaldi_verbose_level, ParseOptions::help_, ParseOptions::print_args_, and ParseOptions::RegisterStandard().
ParseOptions | ( | const std::string & | prefix, |
OptionsItf * | other | ||
) |
This is a constructor for the special case where some options are registered with a prefix to avoid conflicts.
The object thus created will only be used temporarily to register an options class with the original options parser (which is passed as the *other pointer) using the given prefix. It should not be used for any other purpose, and the prefix must not be the empty string. It seems to be the least bad way of implementing options with prefixes at this point. Example of usage is: ParseOptions po; // original ParseOptions object ParseOptions po_mfcc("mfcc", &po); // object with prefix. MfccOptions mfcc_opts; mfcc_opts.Register(&po_mfcc); The options will now get registered as, e.g., –mfcc.frame-shift=10.0 instead of just –frame-shift=10.0
Definition at line 39 of file parse-options.cc.
References ParseOptions::other_parser_, and ParseOptions::prefix_.
|
inline |
Definition at line 71 of file parse-options.h.
References ParseOptions::DisableOption(), ParseOptions::GetArg(), ParseOptions::NumArgs(), ParseOptions::PrintConfig(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::ReadConfigFile(), ParseOptions::Register(), and ParseOptions::RegisterStandard().
void DisableOption | ( | const std::string & | name | ) |
If called after registering an option and before calling Read(), disables that option from being used.
Will crash at runtime if that option had not been registered.
Definition at line 183 of file parse-options.cc.
References ParseOptions::argv_, ParseOptions::bool_map_, ParseOptions::doc_map_, ParseOptions::double_map_, ParseOptions::float_map_, ParseOptions::int_map_, KALDI_ERR, ParseOptions::string_map_, and ParseOptions::uint_map_.
Referenced by ParseOptions::~ParseOptions().
|
static |
The following function will return a possibly quoted and escaped version of "str", according to the current shell.
Currently this is just hardwired to bash. It's useful for debug output.
Definition at line 306 of file parse-options.cc.
References kaldi::MustBeQuoted(), and kaldi::QuoteAndEscape().
Referenced by ParseOptions::GetOptArg(), kaldi::PrintableRxfilename(), kaldi::PrintableWxfilename(), ParseOptions::PrintUsage(), and ParseOptions::Read().
std::string GetArg | ( | int | param | ) | const |
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
Will crash if param is not >=1 and <=NumArgs().
Definition at line 202 of file parse-options.cc.
References rnnlm::i, KALDI_ERR, and ParseOptions::positional_args_.
Referenced by ComputeLogPosteriors(), ComputeScores(), ParseOptions::GetOptArg(), main(), kaldi::TypeOneUsage(), kaldi::TypeOneUsageAverage(), kaldi::TypeThreeUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().
|
inline |
Definition at line 127 of file parse-options.h.
References ParseOptions::Escape(), ParseOptions::GetArg(), rnnlm::i, ParseOptions::NumArgs(), ParseOptions::RegisterCommon(), ParseOptions::RegisterSpecific(), ParseOptions::RegisterTmpl(), ParseOptions::SetOption(), ParseOptions::ToBool(), ParseOptions::ToDouble(), ParseOptions::ToFloat(), ParseOptions::ToInt(), and ParseOptions::ToUint().
Referenced by main().
|
protected |
Definition at line 522 of file parse-options.cc.
References KALDI_ASSERT.
Referenced by ParseOptions::Read(), ParseOptions::ReadConfigFile(), and ParseOptions::RegisterCommon().
int NumArgs | ( | ) | const |
Number of positional parameters (c.f. argc-1).
Definition at line 198 of file parse-options.cc.
References ParseOptions::positional_args_.
Referenced by ParseOptions::GetOptArg(), main(), kaldi::TypeOneUsage(), kaldi::TypeOneUsageAverage(), kaldi::TypeThreeUsage(), kaldi::TypeTwoUsage(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().
void PrintConfig | ( | std::ostream & | os | ) |
Prints the actual configuration of all the registered variables.
Definition at line 431 of file parse-options.cc.
References ParseOptions::bool_map_, ParseOptions::doc_map_, ParseOptions::double_map_, ParseOptions::float_map_, ParseOptions::int_map_, KALDI_ERR, ParseOptions::string_map_, and ParseOptions::uint_map_.
Referenced by ParseOptions::~ParseOptions().
void PrintUsage | ( | bool | print_command_line = false | ) |
Prints the usage documentation [provided in the constructor].
Definition at line 393 of file parse-options.cc.
References ParseOptions::argc_, ParseOptions::argv_, ParseOptions::doc_map_, ParseOptions::Escape(), rnnlm::j, and ParseOptions::usage_.
Referenced by main(), ParseOptions::Read(), ParseOptions::ReadConfigFile(), ParseOptions::SplitLongArg(), ParseOptions::ToBool(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().
int Read | ( | int | argc, |
const char *const * | argv | ||
) |
Parses the command line options and fills the ParseOptions-registered variables.
This must be called after all the variables were registered!!!
Initially the variables have implicit values, then the config file values are set-up, finally the command line values given. Returns the first position in argv that was not used. [typically not useful: use NumParams() and GetParam(). ]
Definition at line 311 of file parse-options.cc.
References ParseOptions::argc_, ParseOptions::argv_, ParseOptions::Escape(), rnnlm::i, rnnlm::j, KALDI_ERR, ParseOptions::NormalizeArgName(), ParseOptions::positional_args_, ParseOptions::print_args_, ParseOptions::PrintUsage(), ParseOptions::ReadConfigFile(), ParseOptions::SetOption(), kaldi::SetProgramName(), ParseOptions::SplitLongArg(), and kaldi::Trim().
Referenced by main(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().
void ReadConfigFile | ( | const std::string & | filename | ) |
Reads the options values from a config file.
Must be called after registering all options. This is usually used internally after the standard –config option is used, but it may also be called from a program.
Definition at line 459 of file parse-options.cc.
References KALDI_ERR, line_number, ParseOptions::NormalizeArgName(), ParseOptions::PrintUsage(), ParseOptions::SetOption(), ParseOptions::SplitLongArg(), and kaldi::Trim().
Referenced by ParseOptions::Read(), and ParseOptions::~ParseOptions().
|
virtual |
Implements OptionsItf.
Definition at line 56 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
Referenced by main(), kaldi::ReadConfigFromFile(), kaldi::ReadConfigsFromFile(), DummyOptions::Register(), ImageAugmentationConfig::Register(), OnlineCmvnOptions::Register(), ProcessPitchOptions::Register(), OnlineSpliceOptions::Register(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().
|
virtual |
Implements OptionsItf.
Definition at line 61 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
|
virtual |
Implements OptionsItf.
Definition at line 66 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
|
virtual |
Implements OptionsItf.
Definition at line 71 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
|
virtual |
Implements OptionsItf.
Definition at line 76 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
|
virtual |
Implements OptionsItf.
Definition at line 81 of file parse-options.cc.
References ParseOptions::RegisterTmpl().
|
private |
Does the actual job for both kinds of parameters Does the common part of the job for all datatypes, then calls RegisterSpecific.
Definition at line 102 of file parse-options.cc.
References ParseOptions::doc_map_, KALDI_ASSERT, KALDI_WARN, ParseOptions::NormalizeArgName(), and ParseOptions::RegisterSpecific().
Referenced by ParseOptions::GetOptArg(), ParseOptions::RegisterStandard(), ParseOptions::RegisterTmpl(), and ParseOptions::ToDouble().
|
private |
Register boolean variable.
Definition at line 120 of file parse-options.cc.
References ParseOptions::bool_map_, and ParseOptions::doc_map_.
Referenced by ParseOptions::GetOptArg(), and ParseOptions::RegisterCommon().
|
private |
Register int32 variable.
Definition at line 130 of file parse-options.cc.
References ParseOptions::doc_map_, rnnlm::i, and ParseOptions::int_map_.
|
private |
Register unsinged int32 variable.
Definition at line 141 of file parse-options.cc.
References ParseOptions::doc_map_, and ParseOptions::uint_map_.
|
private |
Register float variable.
Definition at line 152 of file parse-options.cc.
References ParseOptions::doc_map_, and ParseOptions::float_map_.
|
private |
Register double variable [useful as we change BaseFloat type].
Definition at line 163 of file parse-options.cc.
References ParseOptions::doc_map_, and ParseOptions::double_map_.
|
private |
Register string variable.
Definition at line 174 of file parse-options.cc.
References ParseOptions::doc_map_, and ParseOptions::string_map_.
template void RegisterStandard | ( | const std::string & | name, |
T * | ptr, | ||
const std::string & | doc | ||
) |
This one is used for registering standard parameters of all the programs.
Definition at line 115 of file parse-options.cc.
References ParseOptions::RegisterCommon().
Referenced by ParseOptions::ParseOptions(), ParseOptions::ToDouble(), and ParseOptions::~ParseOptions().
|
private |
Template to register various variable types, used for program-specific parameters.
Definition at line 88 of file parse-options.cc.
References KALDI_ASSERT, ParseOptions::other_parser_, ParseOptions::prefix_, OptionsItf::Register(), and ParseOptions::RegisterCommon().
Referenced by ParseOptions::GetOptArg(), ParseOptions::Register(), and ParseOptions::ToDouble().
Set option with name "key" to "value"; will crash if can't do it.
"has_equal_sign" is used to allow –x for a boolean option x, and –y=, for a string option y.
Definition at line 540 of file parse-options.cc.
References ParseOptions::bool_map_, ParseOptions::double_map_, ParseOptions::float_map_, ParseOptions::int_map_, KALDI_ERR, ParseOptions::string_map_, ParseOptions::ToBool(), ParseOptions::ToDouble(), ParseOptions::ToFloat(), ParseOptions::ToInt(), ParseOptions::ToUint(), and ParseOptions::uint_map_.
Referenced by ParseOptions::GetOptArg(), ParseOptions::Read(), and ParseOptions::ReadConfigFile().
|
protected |
SplitLongArg parses an argument of the form –a=b, –a=, or –a,.
and sets "has_equal_sign" to true if an equals-sign was parsed.. this is needed in order to correctly allow –x for a boolean option x, and –y= for a string option y, and to disallow –x= and –y.
Definition at line 500 of file parse-options.cc.
References KALDI_ASSERT, KALDI_ERR, and ParseOptions::PrintUsage().
Referenced by ParseOptions::Read(), and ParseOptions::ReadConfigFile().
|
private |
Definition at line 568 of file parse-options.cc.
References KALDI_ERR, and ParseOptions::PrintUsage().
Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().
|
private |
Definition at line 609 of file parse-options.cc.
References kaldi::ConvertStringToReal(), KALDI_ERR, ParseOptions::RegisterCommon(), ParseOptions::RegisterStandard(), and ParseOptions::RegisterTmpl().
Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().
|
private |
Definition at line 602 of file parse-options.cc.
References kaldi::ConvertStringToReal(), and KALDI_ERR.
Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().
|
private |
Definition at line 588 of file parse-options.cc.
References kaldi::ConvertStringToInteger(), and KALDI_ERR.
Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().
|
private |
Definition at line 595 of file parse-options.cc.
References kaldi::ConvertStringToInteger(), and KALDI_ERR.
Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().
|
private |
Definition at line 214 of file parse-options.h.
Referenced by ParseOptions::PrintUsage(), and ParseOptions::Read().
|
private |
Definition at line 215 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintUsage(), and ParseOptions::Read().
|
private |
Definition at line 184 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
variable for the implicit –config parameter
Definition at line 211 of file parse-options.h.
Referenced by ParseOptions::ParseOptions().
|
private |
map for the documentation
Definition at line 207 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::PrintUsage(), ParseOptions::RegisterCommon(), and ParseOptions::RegisterSpecific().
|
private |
Definition at line 188 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
Definition at line 187 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
variable for the implicit –help parameter
Definition at line 210 of file parse-options.h.
Referenced by ParseOptions::ParseOptions().
|
private |
Definition at line 185 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
Definition at line 220 of file parse-options.h.
Referenced by ParseOptions::ParseOptions(), and ParseOptions::RegisterTmpl().
|
private |
Definition at line 212 of file parse-options.h.
Referenced by ParseOptions::GetArg(), ParseOptions::NumArgs(), and ParseOptions::Read().
|
private |
These members are not normally used.
They are only used when the object is constructed with a prefix
Definition at line 219 of file parse-options.h.
Referenced by ParseOptions::ParseOptions(), and ParseOptions::RegisterTmpl().
|
private |
variable for the implicit –print-args parameter
Definition at line 209 of file parse-options.h.
Referenced by ParseOptions::ParseOptions(), and ParseOptions::Read().
|
private |
Definition at line 189 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
Definition at line 186 of file parse-options.h.
Referenced by ParseOptions::DisableOption(), ParseOptions::PrintConfig(), ParseOptions::RegisterSpecific(), and ParseOptions::SetOption().
|
private |
Definition at line 213 of file parse-options.h.
Referenced by ParseOptions::PrintUsage().