parse-options.cc
Go to the documentation of this file.
1 // util/parse-options.cc
2 
3 // Copyright 2009-2011 Karel Vesely; Microsoft Corporation;
4 // Saarland University (Author: Arnab Ghoshal);
5 // Copyright 2012-2013 Johns Hopkins University (Author: Daniel Povey);
6 // Frantisek Skala; Arnab Ghoshal
7 // Copyright 2013 Tanel Alumae
8 //
9 // See ../../COPYING for clarification regarding multiple authors
10 //
11 // Licensed under the Apache License, Version 2.0 (the "License");
12 // you may not use this file except in compliance with the License.
13 // You may obtain a copy of the License at
14 //
15 // http://www.apache.org/licenses/LICENSE-2.0
16 //
17 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
19 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
20 // MERCHANTABLITY OR NON-INFRINGEMENT.
21 // See the Apache 2 License for the specific language governing permissions and
22 // limitations under the License.
23 
24 #include <iostream>
25 #include <iomanip>
26 #include <fstream>
27 #include <algorithm>
28 #include <cstdlib>
29 #include <cassert>
30 #include <cstring>
31 
32 #include "util/parse-options.h"
33 #include "util/text-utils.h"
34 #include "base/kaldi-common.h"
35 
36 namespace kaldi {
37 
38 
39 ParseOptions::ParseOptions(const std::string &prefix,
40  OptionsItf *other):
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.
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 }
55 
56 void ParseOptions::Register(const std::string &name,
57  bool *ptr, const std::string &doc) {
58  RegisterTmpl(name, ptr, doc);
59 }
60 
61 void ParseOptions::Register(const std::string &name,
62  int32 *ptr, const std::string &doc) {
63  RegisterTmpl(name, ptr, doc);
64 }
65 
66 void ParseOptions::Register(const std::string &name,
67  uint32 *ptr, const std::string &doc) {
68  RegisterTmpl(name, ptr, doc);
69 }
70 
71 void ParseOptions::Register(const std::string &name,
72  float *ptr, const std::string &doc) {
73  RegisterTmpl(name, ptr, doc);
74 }
75 
76 void ParseOptions::Register(const std::string &name,
77  double *ptr, const std::string &doc) {
78  RegisterTmpl(name, ptr, doc);
79 }
80 
81 void ParseOptions::Register(const std::string &name,
82  std::string *ptr, const std::string &doc) {
83  RegisterTmpl(name, ptr, doc);
84 }
85 
86 // old-style, used for registering application-specific parameters
87 template<typename T>
88 void ParseOptions::RegisterTmpl(const std::string &name, T *ptr,
89  const std::string &doc) {
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 }
99 
100 // does the common part of the job of registering a parameter
101 template<typename T>
102 void ParseOptions::RegisterCommon(const std::string &name, T *ptr,
103  const std::string &doc, bool is_standard) {
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 }
111 
112 // used to register standard parameters (those that are present in all of the
113 // applications)
114 template<typename T>
115 void ParseOptions::RegisterStandard(const std::string &name, T *ptr,
116  const std::string &doc) {
117  this->RegisterCommon(name, ptr, doc, true);
118 }
119 
120 void ParseOptions::RegisterSpecific(const std::string &name,
121  const std::string &idx,
122  bool *b,
123  const std::string &doc,
124  bool is_standard) {
125  bool_map_[idx] = b;
126  doc_map_[idx] = DocInfo(name, doc + " (bool, default = "
127  + ((*b)? "true)" : "false)"), is_standard);
128 }
129 
130 void ParseOptions::RegisterSpecific(const std::string &name,
131  const std::string &idx,
132  int32 *i,
133  const std::string &doc,
134  bool is_standard) {
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 }
140 
141 void ParseOptions::RegisterSpecific(const std::string &name,
142  const std::string &idx,
143  uint32 *u,
144  const std::string &doc,
145  bool is_standard) {
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 }
151 
152 void ParseOptions::RegisterSpecific(const std::string &name,
153  const std::string &idx,
154  float *f,
155  const std::string &doc,
156  bool is_standard) {
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 }
162 
163 void ParseOptions::RegisterSpecific(const std::string &name,
164  const std::string &idx,
165  double *f,
166  const std::string &doc,
167  bool is_standard) {
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 }
173 
174 void ParseOptions::RegisterSpecific(const std::string &name,
175  const std::string &idx,
176  std::string *s,
177  const std::string &doc,
178  bool is_standard) {
179  string_map_[idx] = s;
180  doc_map_[idx] = DocInfo(name, doc + " (string, default = \"" + *s + "\")",
181  is_standard);
182 }
183 void ParseOptions::DisableOption(const std::string &name) {
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 }
196 
197 
199  return positional_args_.size();
200 }
201 
202 std::string ParseOptions::GetArg(int i) const {
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 }
208 
209 // We currently do not support any other options.
210 enum ShellType { kBash = 0 };
211 
212 // This can be changed in the code if it ever does need to be changed (as it's
213 // unlikely that one compilation of this tool-set would use both shells).
215 
216 // Returns true if we need to escape a string before putting it into
217 // a shell (mainly thinking of bash shell, but should work for others)
218 // This is for the convenience of the user so command-lines that are
219 // printed out by ParseOptions::Read (with --print-args=true) are
220 // paste-able into the shell and will run. If you use a different type of
221 // shell, it might be necessary to change this function.
222 // But it's mostly a cosmetic issue as it basically affects how
223 // the program echoes its command-line arguments to the screen.
224 static bool MustBeQuoted(const std::string &str, ShellType st) {
225  // Only Bash is supported (for the moment).
226  KALDI_ASSERT(st == kBash && "Invalid shell type.");
227 
228  const char *c = str.c_str();
229  if (*c == '\0') {
230  return true; // Must quote empty string
231  } else {
232  const char *ok_chars[2];
233 
234  // These seem not to be interpreted as long as there are no other "bad"
235  // characters involved (e.g. "," would be interpreted as part of something
236  // like a{b,c}, but not on its own.
237  ok_chars[kBash] = "[]~#^_-+=:.,/";
238 
239  // Just want to make sure that a space character doesn't get automatically
240  // inserted here via an automated style-checking script, like it did before.
241  KALDI_ASSERT(!strchr(ok_chars[kBash], ' '));
242 
243  for (; *c != '\0'; c++) {
244  // For non-alphanumeric characters we have a list of characters which
245  // are OK. All others are forbidden (this is easier since the shell
246  // interprets most non-alphanumeric characters).
247  if (!isalnum(*c)) {
248  const char *d;
249  for (d = ok_chars[st]; *d != '\0'; d++) if (*c == *d) break;
250  // If not alphanumeric or one of the "ok_chars", it must be escaped.
251  if (*d == '\0') return true;
252  }
253  }
254  return false; // The string was OK. No quoting or escaping.
255  }
256 }
257 
258 // Returns a quoted and escaped version of "str"
259 // which has previously been determined to need escaping.
260 // Our aim is to print out the command line in such a way that if it's
261 // pasted into a shell of ShellType "st" (only bash for now), it
262 // will get passed to the program in the same way.
263 static std::string QuoteAndEscape(const std::string &str, ShellType st) {
264  // Only Bash is supported (for the moment).
265  KALDI_ASSERT(st == kBash && "Invalid shell type.");
266 
267  // For now we use the following rules:
268  // In the normal case, we quote with single-quote "'", and to escape
269  // a single-quote we use the string: '\'' (interpreted as closing the
270  // single-quote, putting an escaped single-quote from the shell, and
271  // then reopening the single quote).
272  char quote_char = '\'';
273  const char *escape_str = "'\\''"; // e.g. echo 'a'\''b' returns a'b
274 
275  // If the string contains single-quotes that would need escaping this
276  // way, and we determine that the string could be safely double-quoted
277  // without requiring any escaping, then we double-quote the string.
278  // This is the case if the characters "`$\ do not appear in the string.
279  // e.g. see http://www.redhat.com/mirrors/LDP/LDP/abs/html/quotingvar.html
280  const char *c_str = str.c_str();
281  if (strchr(c_str, '\'') && !strpbrk(c_str, "\"`$\\")) {
282  quote_char = '"';
283  escape_str = "\\\""; // should never be accessed.
284  }
285 
286  char buf[2];
287  buf[1] = '\0';
288 
289  buf[0] = quote_char;
290  std::string ans = buf;
291  const char *c = str.c_str();
292  for (;*c != '\0'; c++) {
293  if (*c == quote_char) {
294  ans += escape_str;
295  } else {
296  buf[0] = *c;
297  ans += buf;
298  }
299  }
300  buf[0] = quote_char;
301  ans += buf;
302  return ans;
303 }
304 
305 // static function
306 std::string ParseOptions::Escape(const std::string &str) {
307  return MustBeQuoted(str, kShellType) ? QuoteAndEscape(str, kShellType) : str;
308 }
309 
310 
311 int ParseOptions::Read(int argc, const char *const argv[]) {
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 }
391 
392 
393 void ParseOptions::PrintUsage(bool print_command_line) {
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 }
430 
431 void ParseOptions::PrintConfig(std::ostream &os) {
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 }
457 
458 
459 void ParseOptions::ReadConfigFile(const std::string &filename) {
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 }
497 
498 
499 
500 void ParseOptions::SplitLongArg(const std::string &in,
501  std::string *key,
502  std::string *value,
503  bool *has_equal_sign) {
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 }
520 
521 
522 void ParseOptions::NormalizeArgName(std::string *str) {
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 }
536 
537 
538 
539 
540 bool ParseOptions::SetOption(const std::string &key,
541  const std::string &value,
542  bool has_equal_sign) {
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 }
565 
566 
567 
568 bool ParseOptions::ToBool(std::string str) {
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 }
586 
587 
588 int32 ParseOptions::ToInt(const std::string &str) {
589  int32 ret;
590  if (!ConvertStringToInteger(str, &ret))
591  KALDI_ERR << "Invalid integer option \"" << str << "\"";
592  return ret;
593 }
594 
595 uint32 ParseOptions::ToUint(const std::string &str) {
596  uint32 ret;
597  if (!ConvertStringToInteger(str, &ret))
598  KALDI_ERR << "Invalid integer option \"" << str << "\"";
599  return ret;
600 }
601 
602 float ParseOptions::ToFloat(const std::string &str) {
603  float ret;
604  if (!ConvertStringToReal(str, &ret))
605  KALDI_ERR << "Invalid floating-point option \"" << str << "\"";
606  return ret;
607 }
608 
609 double ParseOptions::ToDouble(const std::string &str) {
610  double ret;
611  if (!ConvertStringToReal(str, &ret))
612  KALDI_ERR << "Invalid floating-point option \"" << str << "\"";
613  return ret;
614 }
615 
616 // instantiate templates
617 template void ParseOptions::RegisterTmpl(const std::string &name, bool *ptr,
618  const std::string &doc);
619 template void ParseOptions::RegisterTmpl(const std::string &name, int32 *ptr,
620  const std::string &doc);
621 template void ParseOptions::RegisterTmpl(const std::string &name, uint32 *ptr,
622  const std::string &doc);
623 template void ParseOptions::RegisterTmpl(const std::string &name, float *ptr,
624  const std::string &doc);
625 template void ParseOptions::RegisterTmpl(const std::string &name, double *ptr,
626  const std::string &doc);
627 template void ParseOptions::RegisterTmpl(const std::string &name,
628  std::string *ptr, const std::string &doc);
629 
630 template void ParseOptions::RegisterStandard(const std::string &name,
631  bool *ptr,
632  const std::string &doc);
633 template void ParseOptions::RegisterStandard(const std::string &name,
634  int32 *ptr,
635  const std::string &doc);
636 template void ParseOptions::RegisterStandard(const std::string &name,
637  uint32 *ptr,
638  const std::string &doc);
639 template void ParseOptions::RegisterStandard(const std::string &name,
640  float *ptr,
641  const std::string &doc);
642 template void ParseOptions::RegisterStandard(const std::string &name,
643  double *ptr,
644  const std::string &doc);
645 template void ParseOptions::RegisterStandard(const std::string &name,
646  std::string *ptr,
647  const std::string &doc);
648 
649 template void ParseOptions::RegisterCommon(const std::string &name,
650  bool *ptr,
651  const std::string &doc, bool is_standard);
652 template void ParseOptions::RegisterCommon(const std::string &name,
653  int32 *ptr,
654  const std::string &doc, bool is_standard);
655 template void ParseOptions::RegisterCommon(const std::string &name,
656  uint32 *ptr,
657  const std::string &doc, bool is_standard);
658 template void ParseOptions::RegisterCommon(const std::string &name,
659  float *ptr,
660  const std::string &doc, bool is_standard);
661 template void ParseOptions::RegisterCommon(const std::string &name,
662  double *ptr,
663  const std::string &doc, bool is_standard);
664 template void ParseOptions::RegisterCommon(const std::string &name,
665  std::string *ptr,
666  const std::string &doc, bool is_standard);
667 
668 } // namespace kaldi
void DisableOption(const std::string &name)
If called after registering an option and before calling Read(), disables that option from being used...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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
bool print_args_
variable for the implicit –print-args parameter
std::map< std::string, uint32 * > uint_map_
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.
uint32 ToUint(const std::string &str)
static ShellType kShellType
void PrintConfig(std::ostream &os)
Prints the actual configuration of all the registered variables.
DocMapType doc_map_
map for the documentation
kaldi::int32 int32
std::map< std::string, bool * > bool_map_
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 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...
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::vector< std::string > positional_args_
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
double ToDouble(const std::string &str)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
Structure for options&#39; documentation.
void NormalizeArgName(std::string *str)
bool ToBool(std::string str)
std::map< std::string, float * > float_map_
float ToFloat(const 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.
std::map< std::string, std::string * > string_map_
ParseOptions(const char *usage)
Definition: parse-options.h:38
void ReadConfigFile(const std::string &filename)
Reads the options values from a config file.
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#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
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
#define KALDI_WARN
Definition: kaldi-error.h:150
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
Definition: text-utils.cc:92
void RegisterSpecific(const std::string &name, const std::string &idx, bool *b, const std::string &doc, bool is_standard)
Register boolean variable.
static bool MustBeQuoted(const std::string &str, ShellType st)
void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc)
Template to register various variable types, used for program-specific parameters.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
const char *const * argv_
std::map< std::string, int32 * > int_map_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::map< std::string, double * > double_map_
int32 ToInt(const std::string &str)
int32 line_number
const char * usage_
static std::string QuoteAndEscape(const std::string &str, ShellType st)
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.