You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
11 KiB

  1. // util/parse-options.h
  2. // Copyright 2009-2011 Karel Vesely; Microsoft Corporation;
  3. // Saarland University (Author: Arnab Ghoshal);
  4. // Copyright 2012-2013 Frantisek Skala; Arnab Ghoshal
  5. // See ../../COPYING for clarification regarding multiple authors
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  15. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  16. // MERCHANTABLITY OR NON-INFRINGEMENT.
  17. // See the Apache 2 License for the specific language governing permissions and
  18. // limitations under the License.
  19. #ifndef KALDI_UTIL_PARSE_OPTIONS_H_
  20. #define KALDI_UTIL_PARSE_OPTIONS_H_
  21. #include <map>
  22. #include <string>
  23. #include <vector>
  24. #include "base/kaldi-common.h"
  25. #include "itf/options-itf.h"
  26. namespace kaldi {
  27. /// The class ParseOptions is for parsing command-line options; see
  28. /// \ref parse_options for more documentation.
  29. class ParseOptions : public OptionsItf {
  30. public:
  31. explicit ParseOptions(const char* usage)
  32. : print_args_(true),
  33. help_(false),
  34. usage_(usage),
  35. argc_(0),
  36. argv_(NULL),
  37. prefix_(""),
  38. other_parser_(NULL) {
  39. #if !defined(_MSC_VER) && \
  40. !defined(__CYGWIN__) // This is just a convenient place to set the stderr
  41. // 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_,
  45. "Configuration file to read (this "
  46. "option may be repeated)");
  47. RegisterStandard("print-args", &print_args_,
  48. "Print the command line arguments (to stderr)");
  49. RegisterStandard("help", &help_, "Print out usage message");
  50. RegisterStandard("verbose", &g_kaldi_verbose_level,
  51. "Verbose level (higher->more logging)");
  52. }
  53. /**
  54. This is a constructor for the special case where some options are
  55. registered with a prefix to avoid conflicts. The object thus created will
  56. only be used temporarily to register an options class with the original
  57. options parser (which is passed as the *other pointer) using the given
  58. prefix. It should not be used for any other purpose, and the prefix must
  59. not be the empty string. It seems to be the least bad way of implementing
  60. options with prefixes at this point.
  61. Example of usage is:
  62. ParseOptions po; // original ParseOptions object
  63. ParseOptions po_mfcc("mfcc", &po); // object with prefix.
  64. MfccOptions mfcc_opts;
  65. mfcc_opts.Register(&po_mfcc);
  66. The options will now get registered as, e.g., --mfcc.frame-shift=10.0
  67. instead of just --frame-shift=10.0
  68. */
  69. ParseOptions(const std::string& prefix, OptionsItf* other);
  70. ~ParseOptions() {}
  71. // Methods from the interface
  72. void Register(const std::string& name, bool* ptr, const std::string& doc);
  73. void Register(const std::string& name, int32* ptr, const std::string& doc);
  74. void Register(const std::string& name, uint32* ptr, const std::string& doc);
  75. void Register(const std::string& name, float* ptr, const std::string& doc);
  76. void Register(const std::string& name, double* ptr, const std::string& doc);
  77. void Register(const std::string& name, std::string* ptr,
  78. const std::string& doc);
  79. /// If called after registering an option and before calling
  80. /// Read(), disables that option from being used. Will crash
  81. /// at runtime if that option had not been registered.
  82. void DisableOption(const std::string& name);
  83. /// This one is used for registering standard parameters of all the programs
  84. template <typename T>
  85. void RegisterStandard(const std::string& name, T* ptr,
  86. const std::string& doc);
  87. /**
  88. Parses the command line options and fills the ParseOptions-registered
  89. variables. This must be called after all the variables were registered!!!
  90. Initially the variables have implicit values,
  91. then the config file values are set-up,
  92. finally the command line values given.
  93. Returns the first position in argv that was not used.
  94. [typically not useful: use NumParams() and GetParam(). ]
  95. */
  96. int Read(int argc, const char* const* argv);
  97. /// Prints the usage documentation [provided in the constructor].
  98. void PrintUsage(bool print_command_line = false);
  99. /// Prints the actual configuration of all the registered variables
  100. void PrintConfig(std::ostream& os);
  101. /// Reads the options values from a config file. Must be called after
  102. /// registering all options. This is usually used internally after the
  103. /// standard --config option is used, but it may also be called from a
  104. /// program.
  105. void ReadConfigFile(const std::string& filename);
  106. /// Number of positional parameters (c.f. argc-1).
  107. int NumArgs() const;
  108. /// Returns one of the positional parameters; 1-based indexing for argc/argv
  109. /// compatibility. Will crash if param is not >=1 and <=NumArgs().
  110. std::string GetArg(int param) const;
  111. std::string GetOptArg(int param) const {
  112. return (param <= NumArgs() ? GetArg(param) : "");
  113. }
  114. /// The following function will return a possibly quoted and escaped
  115. /// version of "str", according to the current shell. Currently
  116. /// this is just hardwired to bash. It's useful for debug output.
  117. static std::string Escape(const std::string& str);
  118. private:
  119. /// Template to register various variable types,
  120. /// used for program-specific parameters
  121. template <typename T>
  122. void RegisterTmpl(const std::string& name, T* ptr, const std::string& doc);
  123. // Following functions do just the datatype-specific part of the job
  124. /// Register boolean variable
  125. void RegisterSpecific(const std::string& name, const std::string& idx,
  126. bool* b, const std::string& doc, bool is_standard);
  127. /// Register int32 variable
  128. void RegisterSpecific(const std::string& name, const std::string& idx,
  129. int32* i, const std::string& doc, bool is_standard);
  130. /// Register unsinged int32 variable
  131. void RegisterSpecific(const std::string& name, const std::string& idx,
  132. uint32* u, const std::string& doc, bool is_standard);
  133. /// Register float variable
  134. void RegisterSpecific(const std::string& name, const std::string& idx,
  135. float* f, const std::string& doc, bool is_standard);
  136. /// Register double variable [useful as we change BaseFloat type].
  137. void RegisterSpecific(const std::string& name, const std::string& idx,
  138. double* f, const std::string& doc, bool is_standard);
  139. /// Register string variable
  140. void RegisterSpecific(const std::string& name, const std::string& idx,
  141. std::string* s, const std::string& doc,
  142. bool is_standard);
  143. /// Does the actual job for both kinds of parameters
  144. /// Does the common part of the job for all datatypes,
  145. /// then calls RegisterSpecific
  146. template <typename T>
  147. void RegisterCommon(const std::string& name, T* ptr, const std::string& doc,
  148. bool is_standard);
  149. /// Set option with name "key" to "value"; will crash if can't do it.
  150. /// "has_equal_sign" is used to allow --x for a boolean option x,
  151. /// and --y=, for a string option y.
  152. bool SetOption(const std::string& key, const std::string& value,
  153. bool has_equal_sign);
  154. bool ToBool(std::string str);
  155. int32 ToInt(const std::string& str);
  156. uint32 ToUint(const std::string& str);
  157. float ToFloat(const std::string& str);
  158. double ToDouble(const std::string& str);
  159. // maps for option variables
  160. std::map<std::string, bool*> bool_map_;
  161. std::map<std::string, int32*> int_map_;
  162. std::map<std::string, uint32*> uint_map_;
  163. std::map<std::string, float*> float_map_;
  164. std::map<std::string, double*> double_map_;
  165. std::map<std::string, std::string*> string_map_;
  166. /**
  167. Structure for options' documentation
  168. */
  169. struct DocInfo {
  170. DocInfo() {}
  171. DocInfo(const std::string& name, const std::string& usemsg)
  172. : name_(name), use_msg_(usemsg), is_standard_(false) {}
  173. DocInfo(const std::string& name, const std::string& usemsg,
  174. bool is_standard)
  175. : name_(name), use_msg_(usemsg), is_standard_(is_standard) {}
  176. std::string name_;
  177. std::string use_msg_;
  178. bool is_standard_;
  179. };
  180. typedef std::map<std::string, DocInfo> DocMapType;
  181. DocMapType doc_map_; ///< map for the documentation
  182. bool print_args_; ///< variable for the implicit --print-args parameter
  183. bool help_; ///< variable for the implicit --help parameter
  184. std::string config_; ///< variable for the implicit --config parameter
  185. std::vector<std::string> positional_args_;
  186. const char* usage_;
  187. int argc_;
  188. const char* const* argv_;
  189. /// These members are not normally used. They are only used when the object
  190. /// is constructed with a prefix
  191. std::string prefix_;
  192. OptionsItf* other_parser_;
  193. protected:
  194. /// SplitLongArg parses an argument of the form --a=b, --a=, or --a,
  195. /// and sets "has_equal_sign" to true if an equals-sign was parsed..
  196. /// this is needed in order to correctly allow --x for a boolean option
  197. /// x, and --y= for a string option y, and to disallow --x= and --y.
  198. void SplitLongArg(const std::string& in, std::string* key, std::string* value,
  199. bool* has_equal_sign);
  200. void NormalizeArgName(std::string* str);
  201. };
  202. /// This template is provided for convenience in reading config classes from
  203. /// files; this is not the standard way to read configuration options, but may
  204. /// occasionally be needed. This function assumes the config has a function
  205. /// "void Register(OptionsItf *opts)" which it can call to register the
  206. /// ParseOptions object.
  207. template <class C>
  208. void ReadConfigFromFile(const std::string& config_filename, C* c) {
  209. std::ostringstream usage_str;
  210. usage_str << "Parsing config from "
  211. << "from '" << config_filename << "'";
  212. ParseOptions po(usage_str.str().c_str());
  213. c->Register(&po);
  214. po.ReadConfigFile(config_filename);
  215. }
  216. /// This variant of the template ReadConfigFromFile is for if you need to read
  217. /// two config classes from the same file.
  218. template <class C1, class C2>
  219. void ReadConfigsFromFile(const std::string& conf, C1* c1, C2* c2) {
  220. std::ostringstream usage_str;
  221. usage_str << "Parsing config from "
  222. << "from '" << conf << "'";
  223. ParseOptions po(usage_str.str().c_str());
  224. c1->Register(&po);
  225. c2->Register(&po);
  226. po.ReadConfigFile(conf);
  227. }
  228. } // namespace kaldi
  229. #endif // KALDI_UTIL_PARSE_OPTIONS_H_