41 print_args_(false), help_(false), usage_(
""), argc_(0), argv_(NULL) {
49 if (po != NULL && po->
prefix_ !=
"") {
57 bool *ptr,
const std::string &doc) {
62 int32 *ptr,
const std::string &doc) {
67 uint32 *ptr,
const std::string &doc) {
72 float *ptr,
const std::string &doc) {
77 double *ptr,
const std::string &doc) {
82 std::string *ptr,
const std::string &doc) {
89 const std::string &doc) {
94 "Cannot use empty prefix when registering with prefix.");
95 std::string new_name =
prefix_ +
'.' + name;
103 const std::string &doc,
bool is_standard) {
105 std::string idx = name;
108 KALDI_WARN <<
"Registering option twice, ignoring second time: " << name;
116 const std::string &doc) {
121 const std::string &idx,
123 const std::string &doc,
127 + ((*b)?
"true)" :
"false)"), is_standard);
131 const std::string &idx,
133 const std::string &doc,
136 std::ostringstream ss;
137 ss << doc <<
" (int, default = " << *i <<
")";
142 const std::string &idx,
144 const std::string &doc,
147 std::ostringstream ss;
148 ss << doc <<
" (uint, default = " << *u <<
")";
153 const std::string &idx,
155 const std::string &doc,
158 std::ostringstream ss;
159 ss << doc <<
" (float, default = " << *f <<
")";
164 const std::string &idx,
166 const std::string &doc,
169 std::ostringstream ss;
170 ss << doc <<
" (double, default = " << *f <<
")";
175 const std::string &idx,
177 const std::string &doc,
180 doc_map_[idx] =
DocInfo(name, doc +
" (string, default = \"" + *s +
"\")",
185 KALDI_ERR <<
"DisableOption must not be called after calling Read().";
188 <<
" was not registered so cannot be disabled: ";
205 KALDI_ERR <<
"ParseOptions::GetArg, invalid index " <<
i;
228 const char *c = str.c_str();
232 const char *ok_chars[2];
237 ok_chars[
kBash] =
"[]~#^_-+=:.,/";
243 for (; *c !=
'\0'; c++) {
249 for (d = ok_chars[st]; *d !=
'\0'; d++)
if (*c == *d)
break;
251 if (*d ==
'\0')
return true;
272 char quote_char =
'\'';
273 const char *escape_str =
"'\\''";
280 const char *c_str = str.c_str();
281 if (strchr(c_str,
'\'') && !strpbrk(c_str,
"\"`$\\")) {
290 std::string ans = buf;
291 const char *c = str.c_str();
292 for (;*c !=
'\0'; c++) {
293 if (*c == quote_char) {
314 std::string key, value;
322 const char *c = strrchr(argv[0],
'\\');
324 const char *c = strrchr(argv[0],
'/');
329 for (i = 1; i < argc; i++) {
330 if (std::strncmp(argv[i],
"--", 2) == 0) {
331 if (std::strcmp(argv[i],
"--") == 0) {
339 if (key.compare(
"config") == 0) {
342 if (key.compare(
"help") == 0) {
348 bool double_dash_seen =
false;
350 for (i = 1; i < argc; i++) {
351 if (std::strncmp(argv[i],
"--", 2) == 0) {
352 if (std::strcmp(argv[i],
"--") == 0) {
356 double_dash_seen =
true;
363 if (!
SetOption(key, value, has_equal_sign)) {
373 for (; i < argc; i++) {
374 if ((std::strcmp(argv[i],
"--") == 0) && !double_dash_seen) {
375 double_dash_seen =
true;
383 std::ostringstream strm;
384 for (
int j = 0;
j < argc;
j++)
385 strm <<
Escape(argv[
j]) <<
" ";
387 std::cerr << strm.str() << std::flush;
394 std::cerr <<
'\n' <<
usage_ <<
'\n';
395 DocMapType::iterator it;
397 bool app_specific_header_printed =
false;
399 if (it->second.is_standard_ ==
false) {
400 if (app_specific_header_printed ==
false) {
401 std::cerr <<
"Options:" <<
'\n';
402 app_specific_header_printed =
true;
404 std::cerr <<
" --" << std::setw(25) << std::left << it->second.name_
405 <<
" : " << it->second.use_msg_ <<
'\n';
408 if (app_specific_header_printed ==
true) {
413 std::cerr <<
"Standard options:" <<
'\n';
415 if (it->second.is_standard_ ==
true) {
416 std::cerr <<
" --" << std::setw(25) << std::left << it->second.name_
417 <<
" : " << it->second.use_msg_ <<
'\n';
421 if (print_command_line) {
422 std::ostringstream strm;
423 strm <<
"Command line was: ";
427 std::cerr << strm.str() << std::flush;
432 os <<
'\n' <<
"[[ Configuration of UI-Registered options ]]" <<
'\n';
434 DocMapType::iterator it;
437 os << it->second.name_ <<
" = ";
439 os << (*
bool_map_[key] ?
"true" :
"false");
451 KALDI_ERR <<
"PrintConfig: unrecognized option " << key <<
"[code error]";
460 std::ifstream is(filename.c_str(), std::ifstream::in);
462 KALDI_ERR <<
"Cannot open config file: " << filename;
465 std::string line, key, value;
467 while (std::getline(is, line)) {
471 if ((pos = line.find_first_of(
'#')) != std::string::npos) {
476 if (line.length() == 0)
continue;
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 '--'.";
491 if (!
SetOption(key, value, has_equal_sign)) {
493 KALDI_ERR <<
"Invalid option " << line <<
" in config file " << filename;
503 bool *has_equal_sign) {
505 size_t pos = in.find_first_of(
'=', 0);
506 if (pos == std::string::npos) {
508 *key = in.substr(2, in.size()-2);
510 *has_equal_sign =
false;
511 }
else if (pos == 2) {
513 KALDI_ERR <<
"Invalid option (no key): " << in;
515 *key = in.substr(2, pos-2);
516 *value = in.substr(pos + 1);
517 *has_equal_sign =
true;
524 std::string::iterator it;
526 for (it = str->begin(); it != str->end(); ++it) {
530 out += std::tolower(*it);
541 const std::string &value,
542 bool has_equal_sign) {
544 if (has_equal_sign && value ==
"")
545 KALDI_ERR <<
"Invalid option --" << key <<
"=";
558 <<
" (option format is --x=y).";
569 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
572 if ((str.compare(
"true") == 0) || (str.compare(
"t") == 0)
573 || (str.compare(
"1") == 0) || (str.compare(
"") == 0)) {
576 if ((str.compare(
"false") == 0) || (str.compare(
"f") == 0)
577 || (str.compare(
"0") == 0)) {
582 KALDI_ERR <<
"Invalid format for boolean argument [expected true or false]: " 591 KALDI_ERR <<
"Invalid integer option \"" << str <<
"\"";
598 KALDI_ERR <<
"Invalid integer option \"" << str <<
"\"";
605 KALDI_ERR <<
"Invalid floating-point option \"" << str <<
"\"";
612 KALDI_ERR <<
"Invalid floating-point option \"" << str <<
"\"";
618 const std::string &doc);
620 const std::string &doc);
622 const std::string &doc);
624 const std::string &doc);
626 const std::string &doc);
628 std::string *ptr,
const std::string &doc);
632 const std::string &doc);
635 const std::string &doc);
638 const std::string &doc);
641 const std::string &doc);
644 const std::string &doc);
647 const std::string &doc);
651 const std::string &doc,
bool is_standard);
654 const std::string &doc,
bool is_standard);
657 const std::string &doc,
bool is_standard);
660 const std::string &doc,
bool is_standard);
663 const std::string &doc,
bool is_standard);
666 const std::string &doc,
bool is_standard);
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...
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...
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.
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
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...
Structure for options' 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't do it.
std::map< std::string, std::string * > string_map_
ParseOptions(const char *usage)
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.
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
void Trim(std::string *str)
Removes the beginning and trailing whitespaces from a string.
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)
std::map< std::string, double * > double_map_
int32 ToInt(const std::string &str)
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.