All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ParseOptions Class Reference

The class ParseOptions is for parsing command-line options; see Parsing command-line options for more documentation. More...

#include <parse-options.h>

Inheritance diagram for ParseOptions:
Collaboration diagram for ParseOptions:

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 (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...
 
OptionsItfother_parser_
 

Detailed Description

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.

Member Typedef Documentation

typedef std::map<std::string, DocInfo> DocMapType
private

Definition at line 206 of file parse-options.h.

Constructor & Destructor Documentation

ParseOptions ( const char *  usage)
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().

38  :
39  print_args_(true), help_(false), usage_(usage), argc_(0), argv_(NULL),
40  prefix_(""), other_parser_(NULL) {
41 #ifndef _MSC_VER // This is just a convenient place to set the stderr to line
42  setlinebuf(stderr); // buffering mode, since it's called at program start.
43 #endif // This helps ensure different programs' output is not mixed up.
44  RegisterStandard("config", &config_, "Configuration file to read (this "
45  "option may be repeated)");
46  RegisterStandard("print-args", &print_args_,
47  "Print the command line arguments (to stderr)");
48  RegisterStandard("help", &help_, "Print out usage message");
50  "Verbose level (higher->more logging)");
51  }
bool print_args_
variable for the implicit –print-args parameter
std::string config_
variable for the implicit –config parameter
bool help_
variable for the implicit –help parameter
const char *const * argv_
int32 g_kaldi_verbose_level
This is set by util/parse-options.{h, cc} if you set –verbose=? option.
Definition: kaldi-error.cc:40
const char * usage_
void RegisterStandard(const std::string &name, T *ptr, const std::string &doc)
This one is used for registering standard parameters of all the programs.
OptionsItf * other_parser_
std::string prefix_
These members are not normally used.
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_.

40  :
41  print_args_(false), help_(false), usage_(""), argc_(0), argv_(NULL) {
42  ParseOptions *po = dynamic_cast<ParseOptions*>(other);
43  if (po != NULL && po->other_parser_ != NULL) {
44  // we get here if this constructor is used twice, recursively.
45  other_parser_ = po->other_parser_;
46  } else {
47  other_parser_ = other;
48  }
49  if (po != NULL && po->prefix_ != "") {
50  prefix_ = po->prefix_ + std::string(".") + prefix;
51  } else {
52  prefix_ = prefix;
53  }
54 }
bool print_args_
variable for the implicit –print-args parameter
ParseOptions(const char *usage)
Definition: parse-options.h:38
bool help_
variable for the implicit –help parameter
const char *const * argv_
const char * usage_
OptionsItf * other_parser_
std::string prefix_
These members are not normally used.
~ParseOptions ( )
inline

Definition at line 71 of file parse-options.h.

71 {}

Member Function Documentation

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_.

183  {
184  if (argv_ != NULL)
185  KALDI_ERR << "DisableOption must not be called after calling Read().";
186  if (doc_map_.erase(name) == 0)
187  KALDI_ERR << "Option " << name
188  << " was not registered so cannot be disabled: ";
189  bool_map_.erase(name);
190  int_map_.erase(name);
191  uint_map_.erase(name);
192  float_map_.erase(name);
193  double_map_.erase(name);
194  string_map_.erase(name);
195 }
std::map< std::string, uint32 * > uint_map_
DocMapType doc_map_
map for the documentation
std::map< std::string, bool * > bool_map_
std::map< std::string, float * > float_map_
std::map< std::string, std::string * > string_map_
#define KALDI_ERR
Definition: kaldi-error.h:127
const char *const * argv_
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_
std::string Escape ( const std::string &  str)
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 kaldi::PrintableRxfilename(), kaldi::PrintableWxfilename(), ParseOptions::PrintUsage(), and ParseOptions::Read().

306  {
307  return MustBeQuoted(str, kShellType) ? QuoteAndEscape(str, kShellType) : str;
308 }
static ShellType kShellType
static bool MustBeQuoted(const std::string &str, ShellType st)
static std::string QuoteAndEscape(const std::string &str, ShellType st)
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::TypeThreeUsage(), kaldi::TypeTwoUsage(), and kaldi::UnitTestParseOptions().

202  {
203  // use KALDI_ERR if code error
204  if (i < 1 || i > static_cast<int>(positional_args_.size()))
205  KALDI_ERR << "ParseOptions::GetArg, invalid index " << i;
206  return positional_args_[i - 1];
207 }
std::vector< std::string > positional_args_
#define KALDI_ERR
Definition: kaldi-error.h:127
std::string GetOptArg ( int  param) const
inline

Definition at line 127 of file parse-options.h.

References ParseOptions::GetArg(), and ParseOptions::NumArgs().

Referenced by main().

127  {
128  return (param <= NumArgs() ? GetArg(param) : "");
129  }
int NumArgs() const
Number of positional parameters (c.f. argc-1).
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
void NormalizeArgName ( std::string *  str)
protected

Definition at line 529 of file parse-options.cc.

References KALDI_ASSERT.

Referenced by ParseOptions::Read(), ParseOptions::ReadConfigFile(), and ParseOptions::RegisterCommon().

529  {
530  std::string out;
531  std::string::iterator it;
532 
533  for (it = str->begin(); it != str->end(); ++it) {
534  if (*it == '_')
535  out += '-'; // convert _ to -
536  else
537  out += std::tolower(*it);
538  }
539  *str = out;
540 
541  KALDI_ASSERT(str->length() > 0);
542 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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::TypeThreeUsage(), kaldi::TypeTwoUsage(), and kaldi::UnitTestParseOptions().

198  {
199  return positional_args_.size();
200 }
std::vector< std::string > positional_args_
void PrintConfig ( std::ostream &  os)

Prints the actual configuration of all the registered variables.

Definition at line 438 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_.

438  {
439  os << '\n' << "[[ Configuration of UI-Registered options ]]" << '\n';
440  std::string key;
441  DocMapType::iterator it;
442  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
443  key = it->first;
444  os << it->second.name_ << " = ";
445  if (bool_map_.end() != bool_map_.find(key)) {
446  os << (*bool_map_[key] ? "true" : "false");
447  } else if (int_map_.end() != int_map_.find(key)) {
448  os << (*int_map_[key]);
449  } else if (uint_map_.end() != uint_map_.find(key)) {
450  os << (*uint_map_[key]);
451  } else if (float_map_.end() != float_map_.find(key)) {
452  os << (*float_map_[key]);
453  } else if (double_map_.end() != double_map_.find(key)) {
454  os << (*double_map_[key]);
455  } else if (string_map_.end() != string_map_.find(key)) {
456  os << "'" << *string_map_[key] << "'";
457  } else {
458  KALDI_ERR << "PrintConfig: unrecognized option " << key << "[code error]";
459  }
460  os << '\n';
461  }
462  os << '\n';
463 }
std::map< std::string, uint32 * > uint_map_
DocMapType doc_map_
map for the documentation
std::map< std::string, bool * > bool_map_
std::map< std::string, float * > float_map_
std::map< std::string, std::string * > string_map_
#define KALDI_ERR
Definition: kaldi-error.h:127
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_
void PrintUsage ( bool  print_command_line = false)

Prints the usage documentation [provided in the constructor].

Definition at line 400 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(), and kaldi::UnitTestParseOptions().

400  {
401  std::cerr << '\n' << usage_ << '\n';
402  DocMapType::iterator it;
403  // first we print application-specific options
404  bool app_specific_header_printed = false;
405  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
406  if (it->second.is_standard_ == false) { // application-specific option
407  if (app_specific_header_printed == false) { // header was not yet printed
408  std::cerr << "Options:" << '\n';
409  app_specific_header_printed = true;
410  }
411  std::cerr << " --" << std::setw(25) << std::left << it->second.name_
412  << " : " << it->second.use_msg_ << '\n';
413  }
414  }
415  if (app_specific_header_printed == true) {
416  std::cerr << '\n';
417  }
418 
419  // then the standard options
420  std::cerr << "Standard options:" << '\n';
421  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
422  if (it->second.is_standard_ == true) { // we have standard option
423  std::cerr << " --" << std::setw(25) << std::left << it->second.name_
424  << " : " << it->second.use_msg_ << '\n';
425  }
426  }
427  std::cerr << '\n';
428  if (print_command_line) {
429  std::ostringstream strm;
430  strm << "Command line was: ";
431  for (int j = 0; j < argc_; j++)
432  strm << Escape(argv_[j]) << " ";
433  strm << '\n';
434  std::cerr << strm.str() << std::flush;
435  }
436 }
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.
DocMapType doc_map_
map for the documentation
const char *const * argv_
const char * usage_
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 vaues 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(), kaldi::g_program_name, rnnlm::i, rnnlm::j, KALDI_ERR, ParseOptions::NormalizeArgName(), ParseOptions::positional_args_, ParseOptions::print_args_, ParseOptions::PrintUsage(), ParseOptions::ReadConfigFile(), ParseOptions::SetOption(), ParseOptions::SplitLongArg(), and kaldi::Trim().

Referenced by main(), and kaldi::UnitTestParseOptions().

311  {
312  argc_ = argc;
313  argv_ = argv;
314  std::string key, value;
315  int i;
316  if (argc > 0) {
317  // set global "const char*" g_program_name (name of the program)
318  // so it can be printed out in error messages;
319  // it's useful because often the stderr of different programs will
320  // be mixed together in the same log file.
321 #ifdef _MSC_VER
322  const char *c = strrchr(argv[0], '\\');
323 #else
324  const char *c = strrchr(argv[0], '/');
325 #endif
326  if (c == NULL)
327  c = argv[0];
328  else
329  c++;
330  char *program_name = new char[strlen(c)+1];
331  strcpy(program_name, c);
332  delete [] g_program_name;
333  g_program_name = program_name;
334  }
335  // first pass: look for config parameter, look for priority
336  for (i = 1; i < argc; i++) {
337  if (std::strncmp(argv[i], "--", 2) == 0) {
338  if (std::strcmp(argv[i], "--") == 0) {
339  // a lone "--" marks the end of named options
340  break;
341  }
342  bool has_equal_sign;
343  SplitLongArg(argv[i], &key, &value, &has_equal_sign);
344  NormalizeArgName(&key);
345  Trim(&value);
346  if (key.compare("config") == 0) {
347  ReadConfigFile(value);
348  }
349  if (key.compare("help") == 0) {
350  PrintUsage();
351  exit(0);
352  }
353  }
354  }
355  bool double_dash_seen = false;
356  // second pass: add the command line options
357  for (i = 1; i < argc; i++) {
358  if (std::strncmp(argv[i], "--", 2) == 0) {
359  if (std::strcmp(argv[i], "--") == 0) {
360  // A lone "--" marks the end of named options.
361  // Skip that option and break the processing of named options
362  i += 1;
363  double_dash_seen = true;
364  break;
365  }
366  bool has_equal_sign;
367  SplitLongArg(argv[i], &key, &value, &has_equal_sign);
368  NormalizeArgName(&key);
369  Trim(&value);
370  if (!SetOption(key, value, has_equal_sign)) {
371  PrintUsage(true);
372  KALDI_ERR << "Invalid option " << argv[i];
373  }
374  } else {
375  break;
376  }
377  }
378 
379  // process remaining arguments as positional
380  for (; i < argc; i++) {
381  if ((std::strcmp(argv[i], "--") == 0) && !double_dash_seen) {
382  double_dash_seen = true;
383  } else {
384  positional_args_.push_back(std::string(argv[i]));
385  }
386  }
387 
388  // if the user did not suppress this with --print-args = false....
389  if (print_args_) {
390  std::ostringstream strm;
391  for (int j = 0; j < argc; j++)
392  strm << Escape(argv[j]) << " ";
393  strm << '\n';
394  std::cerr << strm.str() << std::flush;
395  }
396  return i;
397 }
bool print_args_
variable for the implicit –print-args parameter
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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.
std::vector< std::string > positional_args_
const char * g_program_name
This is set by util/parse-options.
Definition: kaldi-error.cc:41
void SplitLongArg(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,.
void NormalizeArgName(std::string *str)
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.
void ReadConfigFile(const std::string &filename)
Reads the options values from a config file.
#define KALDI_ERR
Definition: kaldi-error.h:127
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
Definition: text-utils.cc:92
const char *const * argv_
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 466 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().

466  {
467  std::ifstream is(filename.c_str(), std::ifstream::in);
468  if (!is.good()) {
469  KALDI_ERR << "Cannot open config file: " << filename;
470  }
471 
472  std::string line, key, value;
473  int32 line_number = 0;
474  while (std::getline(is, line)) {
475  line_number++;
476  // trim out the comments
477  size_t pos;
478  if ((pos = line.find_first_of('#')) != std::string::npos) {
479  line.erase(pos);
480  }
481  // skip empty lines
482  Trim(&line);
483  if (line.length() == 0) continue;
484 
485  if (line.substr(0, 2) != "--") {
486  KALDI_ERR << "Reading config file " << filename
487  << ": line " << line_number << " does not look like a line "
488  << "from a Kaldi command-line program's config file: should "
489  << "be of the form --x=y. Note: config files intended to "
490  << "be sourced by shell scripts lack the '--'.";
491  }
492 
493  // parse option
494  bool has_equal_sign;
495  SplitLongArg(line, &key, &value, &has_equal_sign);
496  NormalizeArgName(&key);
497  Trim(&value);
498  if (!SetOption(key, value, has_equal_sign)) {
499  PrintUsage(true);
500  KALDI_ERR << "Invalid option " << line << " in config file " << filename;
501  }
502  }
503 }
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void SplitLongArg(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,.
void NormalizeArgName(std::string *str)
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.
#define KALDI_ERR
Definition: kaldi-error.h:127
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
Definition: text-utils.cc:92
int32 line_number
void Register ( const std::string &  name,
bool *  ptr,
const std::string &  doc 
)
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(), and kaldi::UnitTestParseOptions().

57  {
58  RegisterTmpl(name, ptr, doc);
59 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
void Register ( const std::string &  name,
int32 *  ptr,
const std::string &  doc 
)
virtual

Implements OptionsItf.

Definition at line 61 of file parse-options.cc.

References ParseOptions::RegisterTmpl().

62  {
63  RegisterTmpl(name, ptr, doc);
64 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
void Register ( const std::string &  name,
uint32 *  ptr,
const std::string &  doc 
)
virtual

Implements OptionsItf.

Definition at line 66 of file parse-options.cc.

References ParseOptions::RegisterTmpl().

67  {
68  RegisterTmpl(name, ptr, doc);
69 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
void Register ( const std::string &  name,
float *  ptr,
const std::string &  doc 
)
virtual

Implements OptionsItf.

Definition at line 71 of file parse-options.cc.

References ParseOptions::RegisterTmpl().

72  {
73  RegisterTmpl(name, ptr, doc);
74 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
void Register ( const std::string &  name,
double *  ptr,
const std::string &  doc 
)
virtual

Implements OptionsItf.

Definition at line 76 of file parse-options.cc.

References ParseOptions::RegisterTmpl().

77  {
78  RegisterTmpl(name, ptr, doc);
79 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
void Register ( const std::string &  name,
std::string *  ptr,
const std::string &  doc 
)
virtual

Implements OptionsItf.

Definition at line 81 of file parse-options.cc.

References ParseOptions::RegisterTmpl().

82  {
83  RegisterTmpl(name, ptr, doc);
84 }
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
template void RegisterCommon ( const std::string &  name,
T *  ptr,
const std::string &  doc,
bool  is_standard 
)
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::RegisterStandard(), and ParseOptions::RegisterTmpl().

103  {
104  KALDI_ASSERT(ptr != NULL);
105  std::string idx = name;
106  NormalizeArgName(&idx);
107  if (doc_map_.find(idx) != doc_map_.end())
108  KALDI_WARN << "Registering option twice, ignoring second time: " << name;
109  this->RegisterSpecific(name, idx, ptr, doc, is_standard);
110 }
DocMapType doc_map_
map for the documentation
void NormalizeArgName(std::string *str)
#define KALDI_WARN
Definition: kaldi-error.h:130
void RegisterSpecific(const std::string &name, const std::string &idx, bool *b, const std::string &doc, bool is_standard)
Register boolean variable.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
bool *  b,
const std::string &  doc,
bool  is_standard 
)
private

Register boolean variable.

Definition at line 120 of file parse-options.cc.

References ParseOptions::bool_map_, and ParseOptions::doc_map_.

Referenced by ParseOptions::RegisterCommon().

124  {
125  bool_map_[idx] = b;
126  doc_map_[idx] = DocInfo(name, doc + " (bool, default = "
127  + ((*b)? "true)" : "false)"), is_standard);
128 }
DocMapType doc_map_
map for the documentation
std::map< std::string, bool * > bool_map_
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
int32 *  i,
const std::string &  doc,
bool  is_standard 
)
private

Register int32 variable.

Definition at line 130 of file parse-options.cc.

References ParseOptions::doc_map_, rnnlm::i, and ParseOptions::int_map_.

134  {
135  int_map_[idx] = i;
136  std::ostringstream ss;
137  ss << doc << " (int, default = " << *i << ")";
138  doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
139 }
DocMapType doc_map_
map for the documentation
std::map< std::string, int32 * > int_map_
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
uint32 *  u,
const std::string &  doc,
bool  is_standard 
)
private

Register unsinged int32 variable.

Definition at line 141 of file parse-options.cc.

References ParseOptions::doc_map_, and ParseOptions::uint_map_.

145  {
146  uint_map_[idx] = u;
147  std::ostringstream ss;
148  ss << doc << " (uint, default = " << *u << ")";
149  doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
150 }
std::map< std::string, uint32 * > uint_map_
DocMapType doc_map_
map for the documentation
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
float *  f,
const std::string &  doc,
bool  is_standard 
)
private

Register float variable.

Definition at line 152 of file parse-options.cc.

References ParseOptions::doc_map_, and ParseOptions::float_map_.

156  {
157  float_map_[idx] = f;
158  std::ostringstream ss;
159  ss << doc << " (float, default = " << *f << ")";
160  doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
161 }
DocMapType doc_map_
map for the documentation
std::map< std::string, float * > float_map_
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
double *  f,
const std::string &  doc,
bool  is_standard 
)
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_.

167  {
168  double_map_[idx] = f;
169  std::ostringstream ss;
170  ss << doc << " (double, default = " << *f << ")";
171  doc_map_[idx] = DocInfo(name, ss.str(), is_standard);
172 }
DocMapType doc_map_
map for the documentation
std::map< std::string, double * > double_map_
void RegisterSpecific ( const std::string &  name,
const std::string &  idx,
std::string *  s,
const std::string &  doc,
bool  is_standard 
)
private

Register string variable.

Definition at line 174 of file parse-options.cc.

References ParseOptions::doc_map_, and ParseOptions::string_map_.

178  {
179  string_map_[idx] = s;
180  doc_map_[idx] = DocInfo(name, doc + " (string, default = \"" + *s + "\")",
181  is_standard);
182 }
DocMapType doc_map_
map for the documentation
std::map< std::string, std::string * > 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().

116  {
117  this->RegisterCommon(name, ptr, doc, true);
118 }
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...
template void RegisterTmpl ( const std::string &  name,
T *  ptr,
const std::string &  doc 
)
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::Register().

89  {
90  if (other_parser_ == NULL) {
91  this->RegisterCommon(name, ptr, doc, false);
92  } else {
93  KALDI_ASSERT(prefix_ != "" &&
94  "Cannot use empty prefix when registering with prefix.");
95  std::string new_name = prefix_ + '.' + name; // name becomes prefix.name
96  other_parser_->Register(new_name, ptr, doc);
97  }
98 }
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...
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
OptionsItf * other_parser_
std::string prefix_
These members are not normally used.
bool SetOption ( const std::string &  key,
const std::string &  value,
bool  has_equal_sign 
)
private

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 547 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::Read(), and ParseOptions::ReadConfigFile().

549  {
550  if (bool_map_.end() != bool_map_.find(key)) {
551  if (has_equal_sign && value == "")
552  KALDI_ERR << "Invalid option --" << key << "=";
553  *(bool_map_[key]) = ToBool(value);
554  } else if (int_map_.end() != int_map_.find(key)) {
555  *(int_map_[key]) = ToInt(value);
556  } else if (uint_map_.end() != uint_map_.find(key)) {
557  *(uint_map_[key]) = ToUint(value);
558  } else if (float_map_.end() != float_map_.find(key)) {
559  *(float_map_[key]) = ToFloat(value);
560  } else if (double_map_.end() != double_map_.find(key)) {
561  *(double_map_[key]) = ToDouble(value);
562  } else if (string_map_.end() != string_map_.find(key)) {
563  if (!has_equal_sign)
564  KALDI_ERR << "Invalid option --" << key
565  << " (option format is --x=y).";
566  *(string_map_[key]) = value;
567  } else {
568  return false;
569  }
570  return true;
571 }
std::map< std::string, uint32 * > uint_map_
uint32 ToUint(const std::string &str)
std::map< std::string, bool * > bool_map_
double ToDouble(const std::string &str)
bool ToBool(std::string str)
std::map< std::string, float * > float_map_
float ToFloat(const std::string &str)
std::map< std::string, std::string * > string_map_
#define KALDI_ERR
Definition: kaldi-error.h:127
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_
int32 ToInt(const std::string &str)
void SplitLongArg ( std::string  in,
std::string *  key,
std::string *  value,
bool *  has_equal_sign 
)
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 507 of file parse-options.cc.

References KALDI_ASSERT, KALDI_ERR, and ParseOptions::PrintUsage().

Referenced by ParseOptions::Read(), and ParseOptions::ReadConfigFile().

510  {
511  KALDI_ASSERT(in.substr(0, 2) == "--"); // precondition.
512  size_t pos = in.find_first_of('=', 0);
513  if (pos == std::string::npos) { // we allow --option for bools
514  // defaults to empty. We handle this differently in different cases.
515  *key = in.substr(2, in.size()-2); // 2 because starts with --.
516  *value = "";
517  *has_equal_sign = false;
518  } else if (pos == 2) { // we also don't allow empty keys: --=value
519  PrintUsage(true);
520  KALDI_ERR << "Invalid option (no key): " << in;
521  } else { // normal case: --option=value
522  *key = in.substr(2, pos-2); // 2 because starts with --.
523  *value = in.substr(pos + 1);
524  *has_equal_sign = true;
525  }
526 }
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool ToBool ( std::string  str)
private

Definition at line 575 of file parse-options.cc.

References KALDI_ERR, and ParseOptions::PrintUsage().

Referenced by ParseOptions::SetOption().

575  {
576  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
577 
578  // allow "" as a valid option for "true", so that --x is the same as --x=true
579  if ((str.compare("true") == 0) || (str.compare("t") == 0)
580  || (str.compare("1") == 0) || (str.compare("") == 0)) {
581  return true;
582  }
583  if ((str.compare("false") == 0) || (str.compare("f") == 0)
584  || (str.compare("0") == 0)) {
585  return false;
586  }
587  // if it is neither true nor false:
588  PrintUsage(true);
589  KALDI_ERR << "Invalid format for boolean argument [expected true or false]: "
590  << str;
591  return false; // never reached
592 }
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
#define KALDI_ERR
Definition: kaldi-error.h:127
double ToDouble ( const std::string &  str)
private

Definition at line 616 of file parse-options.cc.

References kaldi::ConvertStringToReal(), and KALDI_ERR.

Referenced by ParseOptions::SetOption().

616  {
617  double ret;
618  if (!ConvertStringToReal(str, &ret))
619  KALDI_ERR << "Invalid floating-point option \"" << str << "\"";
620  return ret;
621 }
#define KALDI_ERR
Definition: kaldi-error.h:127
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
float ToFloat ( const std::string &  str)
private

Definition at line 609 of file parse-options.cc.

References kaldi::ConvertStringToReal(), and KALDI_ERR.

Referenced by ParseOptions::SetOption().

609  {
610  float ret;
611  if (!ConvertStringToReal(str, &ret))
612  KALDI_ERR << "Invalid floating-point option \"" << str << "\"";
613  return ret;
614 }
#define KALDI_ERR
Definition: kaldi-error.h:127
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
int32 ToInt ( const std::string &  str)
private

Definition at line 595 of file parse-options.cc.

References kaldi::ConvertStringToInteger(), and KALDI_ERR.

Referenced by ParseOptions::SetOption().

595  {
596  int32 ret;
597  if (!ConvertStringToInteger(str, &ret))
598  KALDI_ERR << "Invalid integer option \"" << str << "\"";
599  return ret;
600 }
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
#define KALDI_ERR
Definition: kaldi-error.h:127
uint32 ToUint ( const std::string &  str)
private

Definition at line 602 of file parse-options.cc.

References kaldi::ConvertStringToInteger(), and KALDI_ERR.

Referenced by ParseOptions::SetOption().

602  {
603  uint32 ret;
604  if (!ConvertStringToInteger(str, &ret))
605  KALDI_ERR << "Invalid integer option \"" << str << "\"";
606  return ret;
607 }
bool ConvertStringToInteger(const std::string &str, Int *out)
Converts a string into an integer via strtoll and returns false if there was any kind of problem (i...
Definition: text-utils.h:118
#define KALDI_ERR
Definition: kaldi-error.h:127

Member Data Documentation

int argc_
private

Definition at line 214 of file parse-options.h.

Referenced by ParseOptions::PrintUsage(), and ParseOptions::Read().

const char* const* argv_
private
std::map<std::string, bool*> bool_map_
private
std::string config_
private

variable for the implicit –config parameter

Definition at line 211 of file parse-options.h.

Referenced by ParseOptions::ParseOptions().

std::map<std::string, double*> double_map_
private
std::map<std::string, float*> float_map_
private
bool help_
private

variable for the implicit –help parameter

Definition at line 210 of file parse-options.h.

Referenced by ParseOptions::ParseOptions().

std::map<std::string, int32*> int_map_
private
OptionsItf* other_parser_
private

Definition at line 220 of file parse-options.h.

Referenced by ParseOptions::ParseOptions(), and ParseOptions::RegisterTmpl().

std::vector<std::string> positional_args_
private
std::string prefix_
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().

bool print_args_
private

variable for the implicit –print-args parameter

Definition at line 209 of file parse-options.h.

Referenced by ParseOptions::ParseOptions(), and ParseOptions::Read().

std::map<std::string, std::string*> string_map_
private
std::map<std::string, uint32*> uint_map_
private
const char* usage_
private

Definition at line 213 of file parse-options.h.

Referenced by ParseOptions::PrintUsage().


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