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 (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, DocInfoDocMapType
 

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

◆ DocMapType

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

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

Constructor & Destructor Documentation

◆ ParseOptions() [1/2]

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 #if !defined(_MSC_VER) && !defined(__CYGWIN__) // 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.
Definition: kaldi-error.cc:46
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() [2/2]

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()

Member Function Documentation

◆ DisableOption()

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

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:147
const char *const * argv_
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_

◆ Escape()

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 ParseOptions::GetOptArg(), 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)

◆ GetArg()

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

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:147

◆ GetOptArg()

std::string GetOptArg ( int  param) const
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().

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

◆ NormalizeArgName()

void NormalizeArgName ( std::string *  str)
protected

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

References KALDI_ASSERT.

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

522  {
523  std::string out;
524  std::string::iterator it;
525 
526  for (it = str->begin(); it != str->end(); ++it) {
527  if (*it == '_')
528  out += '-'; // convert _ to -
529  else
530  out += std::tolower(*it);
531  }
532  *str = out;
533 
534  KALDI_ASSERT(str->length() > 0);
535 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NumArgs()

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

198  {
199  return positional_args_.size();
200 }
std::vector< std::string > positional_args_

◆ PrintConfig()

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

431  {
432  os << '\n' << "[[ Configuration of UI-Registered options ]]" << '\n';
433  std::string key;
434  DocMapType::iterator it;
435  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
436  key = it->first;
437  os << it->second.name_ << " = ";
438  if (bool_map_.end() != bool_map_.find(key)) {
439  os << (*bool_map_[key] ? "true" : "false");
440  } else if (int_map_.end() != int_map_.find(key)) {
441  os << (*int_map_[key]);
442  } else if (uint_map_.end() != uint_map_.find(key)) {
443  os << (*uint_map_[key]);
444  } else if (float_map_.end() != float_map_.find(key)) {
445  os << (*float_map_[key]);
446  } else if (double_map_.end() != double_map_.find(key)) {
447  os << (*double_map_[key]);
448  } else if (string_map_.end() != string_map_.find(key)) {
449  os << "'" << *string_map_[key] << "'";
450  } else {
451  KALDI_ERR << "PrintConfig: unrecognized option " << key << "[code error]";
452  }
453  os << '\n';
454  }
455  os << '\n';
456 }
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:147
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_

◆ PrintUsage()

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

393  {
394  std::cerr << '\n' << usage_ << '\n';
395  DocMapType::iterator it;
396  // first we print application-specific options
397  bool app_specific_header_printed = false;
398  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
399  if (it->second.is_standard_ == false) { // application-specific option
400  if (app_specific_header_printed == false) { // header was not yet printed
401  std::cerr << "Options:" << '\n';
402  app_specific_header_printed = true;
403  }
404  std::cerr << " --" << std::setw(25) << std::left << it->second.name_
405  << " : " << it->second.use_msg_ << '\n';
406  }
407  }
408  if (app_specific_header_printed == true) {
409  std::cerr << '\n';
410  }
411 
412  // then the standard options
413  std::cerr << "Standard options:" << '\n';
414  for (it = doc_map_.begin(); it != doc_map_.end(); ++it) {
415  if (it->second.is_standard_ == true) { // we have standard option
416  std::cerr << " --" << std::setw(25) << std::left << it->second.name_
417  << " : " << it->second.use_msg_ << '\n';
418  }
419  }
420  std::cerr << '\n';
421  if (print_command_line) {
422  std::ostringstream strm;
423  strm << "Command line was: ";
424  for (int j = 0; j < argc_; j++)
425  strm << Escape(argv_[j]) << " ";
426  strm << '\n';
427  std::cerr << strm.str() << std::flush;
428  }
429 }
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_

◆ Read()

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

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  SetProgramName(c == NULL ? argv[0] : c + 1);
327  }
328  // first pass: look for config parameter, look for priority
329  for (i = 1; i < argc; i++) {
330  if (std::strncmp(argv[i], "--", 2) == 0) {
331  if (std::strcmp(argv[i], "--") == 0) {
332  // a lone "--" marks the end of named options
333  break;
334  }
335  bool has_equal_sign;
336  SplitLongArg(argv[i], &key, &value, &has_equal_sign);
337  NormalizeArgName(&key);
338  Trim(&value);
339  if (key.compare("config") == 0) {
340  ReadConfigFile(value);
341  }
342  if (key.compare("help") == 0) {
343  PrintUsage();
344  exit(0);
345  }
346  }
347  }
348  bool double_dash_seen = false;
349  // second pass: add the command line options
350  for (i = 1; i < argc; i++) {
351  if (std::strncmp(argv[i], "--", 2) == 0) {
352  if (std::strcmp(argv[i], "--") == 0) {
353  // A lone "--" marks the end of named options.
354  // Skip that option and break the processing of named options
355  i += 1;
356  double_dash_seen = true;
357  break;
358  }
359  bool has_equal_sign;
360  SplitLongArg(argv[i], &key, &value, &has_equal_sign);
361  NormalizeArgName(&key);
362  Trim(&value);
363  if (!SetOption(key, value, has_equal_sign)) {
364  PrintUsage(true);
365  KALDI_ERR << "Invalid option " << argv[i];
366  }
367  } else {
368  break;
369  }
370  }
371 
372  // process remaining arguments as positional
373  for (; i < argc; i++) {
374  if ((std::strcmp(argv[i], "--") == 0) && !double_dash_seen) {
375  double_dash_seen = true;
376  } else {
377  positional_args_.push_back(std::string(argv[i]));
378  }
379  }
380 
381  // if the user did not suppress this with --print-args = false....
382  if (print_args_) {
383  std::ostringstream strm;
384  for (int j = 0; j < argc; j++)
385  strm << Escape(argv[j]) << " ";
386  strm << '\n';
387  std::cerr << strm.str() << std::flush;
388  }
389  return i;
390 }
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].
void SetProgramName(const char *basename)
Called by ParseOptions to set base name (no directory) of the executing program.
Definition: kaldi-error.cc:50
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.
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,.
std::vector< std::string > positional_args_
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&#39;t do it.
void ReadConfigFile(const std::string &filename)
Reads the options values from a config file.
#define KALDI_ERR
Definition: kaldi-error.h:147
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
Definition: text-utils.cc:92
const char *const * argv_

◆ ReadConfigFile()

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

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

◆ Register() [1/6]

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(), kaldi::UnitTestParseOptions(), and ParseOptions::~ParseOptions().

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.

◆ Register() [2/6]

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.

◆ Register() [3/6]

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.

◆ Register() [4/6]

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.

◆ Register() [5/6]

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.

◆ Register() [6/6]

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.

◆ RegisterCommon()

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::GetOptArg(), ParseOptions::RegisterStandard(), ParseOptions::RegisterTmpl(), and ParseOptions::ToDouble().

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:150
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:185

◆ RegisterSpecific() [1/6]

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::GetOptArg(), and 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_

◆ RegisterSpecific() [2/6]

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_

◆ RegisterSpecific() [3/6]

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

◆ RegisterSpecific() [4/6]

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_

◆ RegisterSpecific() [5/6]

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_

◆ RegisterSpecific() [6/6]

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_

◆ RegisterStandard()

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

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

◆ RegisterTmpl()

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::GetOptArg(), ParseOptions::Register(), and ParseOptions::ToDouble().

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:185
OptionsItf * other_parser_
std::string prefix_
These members are not normally used.

◆ SetOption()

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

542  {
543  if (bool_map_.end() != bool_map_.find(key)) {
544  if (has_equal_sign && value == "")
545  KALDI_ERR << "Invalid option --" << key << "=";
546  *(bool_map_[key]) = ToBool(value);
547  } else if (int_map_.end() != int_map_.find(key)) {
548  *(int_map_[key]) = ToInt(value);
549  } else if (uint_map_.end() != uint_map_.find(key)) {
550  *(uint_map_[key]) = ToUint(value);
551  } else if (float_map_.end() != float_map_.find(key)) {
552  *(float_map_[key]) = ToFloat(value);
553  } else if (double_map_.end() != double_map_.find(key)) {
554  *(double_map_[key]) = ToDouble(value);
555  } else if (string_map_.end() != string_map_.find(key)) {
556  if (!has_equal_sign)
557  KALDI_ERR << "Invalid option --" << key
558  << " (option format is --x=y).";
559  *(string_map_[key]) = value;
560  } else {
561  return false;
562  }
563  return true;
564 }
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:147
std::map< std::string, int32 * > int_map_
std::map< std::string, double * > double_map_
int32 ToInt(const std::string &str)

◆ SplitLongArg()

void SplitLongArg ( const 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 500 of file parse-options.cc.

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

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

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

◆ ToBool()

bool ToBool ( std::string  str)
private

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

References KALDI_ERR, and ParseOptions::PrintUsage().

Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().

568  {
569  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
570 
571  // allow "" as a valid option for "true", so that --x is the same as --x=true
572  if ((str.compare("true") == 0) || (str.compare("t") == 0)
573  || (str.compare("1") == 0) || (str.compare("") == 0)) {
574  return true;
575  }
576  if ((str.compare("false") == 0) || (str.compare("f") == 0)
577  || (str.compare("0") == 0)) {
578  return false;
579  }
580  // if it is neither true nor false:
581  PrintUsage(true);
582  KALDI_ERR << "Invalid format for boolean argument [expected true or false]: "
583  << str;
584  return false; // never reached
585 }
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ToDouble()

double ToDouble ( const std::string &  str)
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().

609  {
610  double 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:147
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

◆ ToFloat()

float ToFloat ( const std::string &  str)
private

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

References kaldi::ConvertStringToReal(), and KALDI_ERR.

Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().

602  {
603  float ret;
604  if (!ConvertStringToReal(str, &ret))
605  KALDI_ERR << "Invalid floating-point option \"" << str << "\"";
606  return ret;
607 }
#define KALDI_ERR
Definition: kaldi-error.h:147
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

◆ ToInt()

int32 ToInt ( const std::string &  str)
private

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

References kaldi::ConvertStringToInteger(), and KALDI_ERR.

Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().

588  {
589  int32 ret;
590  if (!ConvertStringToInteger(str, &ret))
591  KALDI_ERR << "Invalid integer option \"" << str << "\"";
592  return ret;
593 }
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
kaldi::int32 int32
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ ToUint()

uint32 ToUint ( const std::string &  str)
private

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

References kaldi::ConvertStringToInteger(), and KALDI_ERR.

Referenced by ParseOptions::GetOptArg(), and ParseOptions::SetOption().

595  {
596  uint32 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:147

Member Data Documentation

◆ argc_

int argc_
private

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

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

◆ argv_

const char* const* argv_
private

◆ bool_map_

std::map<std::string, bool*> bool_map_
private

◆ config_

std::string config_
private

variable for the implicit –config parameter

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

Referenced by ParseOptions::ParseOptions().

◆ doc_map_

◆ double_map_

std::map<std::string, double*> double_map_
private

◆ float_map_

std::map<std::string, float*> float_map_
private

◆ help_

bool help_
private

variable for the implicit –help parameter

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

Referenced by ParseOptions::ParseOptions().

◆ int_map_

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

◆ other_parser_

OptionsItf* other_parser_
private

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

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

◆ positional_args_

std::vector<std::string> positional_args_
private

◆ prefix_

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

◆ print_args_

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

◆ string_map_

std::map<std::string, std::string*> string_map_
private

◆ uint_map_

std::map<std::string, uint32*> uint_map_
private

◆ usage_

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: