parse-options.h
Go to the documentation of this file.
1 // util/parse-options.h
2 
3 // Copyright 2009-2011 Karel Vesely; Microsoft Corporation;
4 // Saarland University (Author: Arnab Ghoshal);
5 // Copyright 2012-2013 Frantisek Skala; Arnab Ghoshal
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #ifndef KALDI_UTIL_PARSE_OPTIONS_H_
23 #define KALDI_UTIL_PARSE_OPTIONS_H_
24 
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 #include "base/kaldi-common.h"
30 #include "itf/options-itf.h"
31 
32 namespace kaldi {
33 
36 class ParseOptions : public OptionsItf {
37  public:
38  explicit ParseOptions(const char *usage) :
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  }
52 
69  ParseOptions(const std::string &prefix, OptionsItf *other);
70 
72 
73  // Methods from the interface
74  void Register(const std::string &name,
75  bool *ptr, const std::string &doc);
76  void Register(const std::string &name,
77  int32 *ptr, const std::string &doc);
78  void Register(const std::string &name,
79  uint32 *ptr, const std::string &doc);
80  void Register(const std::string &name,
81  float *ptr, const std::string &doc);
82  void Register(const std::string &name,
83  double *ptr, const std::string &doc);
84  void Register(const std::string &name,
85  std::string *ptr, const std::string &doc);
86 
90  void DisableOption(const std::string &name);
91 
93  template<typename T>
94  void RegisterStandard(const std::string &name,
95  T *ptr, const std::string &doc);
96 
107  int Read(int argc, const char *const *argv);
108 
110  void PrintUsage(bool print_command_line = false);
112  void PrintConfig(std::ostream &os);
113 
118  void ReadConfigFile(const std::string &filename);
119 
121  int NumArgs() const;
122 
125  std::string GetArg(int param) const;
126 
127  std::string GetOptArg(int param) const {
128  return (param <= NumArgs() ? GetArg(param) : "");
129  }
130 
134  static std::string Escape(const std::string &str);
135 
136  private:
139  template<typename T>
140  void RegisterTmpl(const std::string &name, T *ptr, const std::string &doc);
141 
142  // Following functions do just the datatype-specific part of the job
144  void RegisterSpecific(const std::string &name, const std::string &idx,
145  bool *b, const std::string &doc, bool is_standard);
147  void RegisterSpecific(const std::string &name, const std::string &idx,
148  int32 *i, const std::string &doc, bool is_standard);
150  void RegisterSpecific(const std::string &name, const std::string &idx,
151  uint32 *u,
152  const std::string &doc, bool is_standard);
154  void RegisterSpecific(const std::string &name, const std::string &idx,
155  float *f, const std::string &doc, bool is_standard);
157  void RegisterSpecific(const std::string &name, const std::string &idx,
158  double *f, const std::string &doc, bool is_standard);
160  void RegisterSpecific(const std::string &name, const std::string &idx,
161  std::string *s, const std::string &doc,
162  bool is_standard);
163 
167  template<typename T>
168  void RegisterCommon(const std::string &name,
169  T *ptr, const std::string &doc, bool is_standard);
170 
174  bool SetOption(const std::string &key, const std::string &value,
175  bool has_equal_sign);
176 
177  bool ToBool(std::string str);
178  int32 ToInt(const std::string &str);
179  uint32 ToUint(const std::string &str);
180  float ToFloat(const std::string &str);
181  double ToDouble(const std::string &str);
182 
183  // maps for option variables
184  std::map<std::string, bool*> bool_map_;
185  std::map<std::string, int32*> int_map_;
186  std::map<std::string, uint32*> uint_map_;
187  std::map<std::string, float*> float_map_;
188  std::map<std::string, double*> double_map_;
189  std::map<std::string, std::string*> string_map_;
190 
194  struct DocInfo {
195  DocInfo() {}
196  DocInfo(const std::string &name, const std::string &usemsg)
197  : name_(name), use_msg_(usemsg), is_standard_(false) {}
198  DocInfo(const std::string &name, const std::string &usemsg,
199  bool is_standard)
200  : name_(name), use_msg_(usemsg), is_standard_(is_standard) {}
201 
202  std::string name_;
203  std::string use_msg_;
205  };
206  typedef std::map<std::string, DocInfo> DocMapType;
207  DocMapType doc_map_;
208 
209  bool print_args_;
210  bool help_;
211  std::string config_;
212  std::vector<std::string> positional_args_;
213  const char *usage_;
214  int argc_;
215  const char *const *argv_;
216 
219  std::string prefix_;
221  protected:
226  void SplitLongArg(const std::string &in, std::string *key,
227  std::string *value, bool *has_equal_sign);
228 
229  void NormalizeArgName(std::string *str);
230 };
231 
237 template<class C> void ReadConfigFromFile(const std::string &config_filename,
238  C *c) {
239  std::ostringstream usage_str;
240  usage_str << "Parsing config from "
241  << "from '" << config_filename << "'";
242  ParseOptions po(usage_str.str().c_str());
243  c->Register(&po);
244  po.ReadConfigFile(config_filename);
245 }
246 
249 template<class C1, class C2> void ReadConfigsFromFile(const std::string &conf,
250  C1 *c1, C2 *c2) {
251  std::ostringstream usage_str;
252  usage_str << "Parsing config from "
253  << "from '" << conf << "'";
254  ParseOptions po(usage_str.str().c_str());
255  c1->Register(&po);
256  c2->Register(&po);
257  po.ReadConfigFile(conf);
258 }
259 
260 
261 
262 } // namespace kaldi
263 
264 #endif // KALDI_UTIL_PARSE_OPTIONS_H_
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
void ReadConfigFromFile(const std::string &config_filename, C *c)
This template is provided for convenience in reading config classes from files; this is not the stand...
bool print_args_
variable for the implicit –print-args parameter
std::map< std::string, uint32 * > uint_map_
std::string config_
variable for the implicit –config 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.
uint32 ToUint(const std::string &str)
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_
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.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
std::map< std::string, DocInfo > DocMapType
bool help_
variable for the implicit –help parameter
void RegisterSpecific(const std::string &name, const std::string &idx, bool *b, const std::string &doc, bool is_standard)
Register boolean variable.
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_
DocInfo(const std::string &name, const std::string &usemsg, bool is_standard)
std::map< std::string, int32 * > int_map_
void ReadConfigsFromFile(const std::string &conf, C1 *c1, C2 *c2)
This variant of the template ReadConfigFromFile is for if you need to read two config classes from th...
int32 g_kaldi_verbose_level
This is set by util/parse-options.
Definition: kaldi-error.cc:46
DocInfo(const std::string &name, const std::string &usemsg)
std::map< std::string, double * > double_map_
int32 ToInt(const std::string &str)
const char * usage_
std::string GetOptArg(int param) const
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.