parse-options-test.cc
Go to the documentation of this file.
1 // util/parse-options-test.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // Copyright 2012-2013 Frantisek Skala; Arnab Ghoshal
5 // Copyright 2013 Tanel Alumae
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 #include "util/parse-options.h"
22 
23 namespace kaldi {
24 
25 struct DummyOptions {
27  bool my_bool;
28  std::string my_string;
29 
31  my_int = 0;
32  my_bool = true;
33  my_string = "default dummy string";
34  }
35 
36  void Register(ParseOptions *po) {
37  po->Register("my-int", &my_int,
38  "An int32 variable in DummyOptions.");
39  po->Register("my-bool", &my_bool,
40  "A Boolean varaible in DummyOptions.");
41  po->Register("my-str", &my_string,
42  "A string varaible in DummyOptions.");
43  }
44 };
45 
47  int argc = 7;
48  std::string str="default_for_str";
49  int32 num = 1;
50  uint32 unum = 2;
51  const char *argv[7] = { "program_name", "--unum=5", "--num=3", "--i=boo",
52  "a", "b", "c" };
53  ParseOptions po("my usage msg");
54  po.Register("i", &str, "My variable");
55  po.Register("num", &num, "My int32 variable");
56  po.Register("unum", &unum, "My uint32 variable");
57  po.Read(argc, argv);
58  KALDI_ASSERT(po.NumArgs() == 3);
59  KALDI_ASSERT(po.GetArg(1) == "a");
60  KALDI_ASSERT(po.GetArg(2) == "b");
61  KALDI_ASSERT(po.GetArg(3) == "c");
62  KALDI_ASSERT(unum == 5);
63  KALDI_ASSERT(num == 3);
64  KALDI_ASSERT(str == "boo");
65 
66  ParseOptions po2("my another msg");
67  int argc2 = 4;
68  const char *argv2[4] = { "program_name", "--i=foo",
69  "--to-be-NORMALIZED=test", "c" };
70  std::string str2 = "default_for_str2";
71  po2.Register("To_Be_Normalized", &str2,
72  "My variable (name has to be normalized)");
73  po2.Register("i", &str, "My variable");
74  po2.Read(argc2, argv2);
75  KALDI_ASSERT(po2.NumArgs() == 1);
76  KALDI_ASSERT(po2.GetArg(1) == "c");
77  KALDI_ASSERT(str2 == "test");
78  KALDI_ASSERT(str == "foo");
79 
80  ParseOptions po3("now checking options with prefix");
81  ParseOptions ro3("prefix", &po3); // to register with prefix
82  ParseOptions so3("prefix2", &ro3); // to register with prefix, recursively.
83  DummyOptions dummy_opts;
84  po3.Register("str", &str, "My string variable");
85  po3.Register("num", &num, "My int32 variable");
86  // Now register with prefix
87  ro3.Register("unum", &unum, "My uint32 variable");
88  ro3.Register("str", &str2, "My other string variable");
89  uint32 unum2 = 0;
90  so3.Register("unum", &unum2, "Another uint32 variable");
91 
92  int argc3 = 10;
93  const char *argv3[10] = {
94  "program_name", "--prefix.unum=5", "--num=3",
95  "--prefix.str=foo", "--str=bar", "--prefix.my-bool=false",
96  "--prefix.my-str=baz", "--prefix.prefix2.unum=42", "a", "b" };
97 
98  dummy_opts.Register(&ro3);
99  po3.PrintUsage(false);
100 
101  po3.Read(argc3, argv3);
102  KALDI_ASSERT(po3.NumArgs() == 2);
103  KALDI_ASSERT(po3.GetArg(1) == "a");
104  KALDI_ASSERT(po3.GetArg(2) == "b");
105  KALDI_ASSERT(unum == 5);
106  KALDI_ASSERT(unum2 == 42);
107  KALDI_ASSERT(num == 3);
108  KALDI_ASSERT(str2 == "foo");
109  KALDI_ASSERT(str == "bar");
110  KALDI_ASSERT(dummy_opts.my_bool == false);
111  KALDI_ASSERT(dummy_opts.my_string == "baz");
112 
113 
114  try { // test error with --option=, which is not a valid way to set
115  // boolean options.
116  int argc4 = 2;
117  const char *argv4[2] = { "program_name", "--option="};
118  ParseOptions po4("my usage msg");
119  bool val = false;
120  po4.Register("option", &val, "My boolean");
121  po4.Read(argc4, argv4);
122  assert(false); // Should not reach this part of code.
123  } catch(std::exception e) {
124  KALDI_LOG << "Failed to read option (this is expected).";
125  }
126 
127  { // test that --option sets "option" to true, if bool.
128  int argc4 = 2;
129  const char *argv4[2] = { "program_name", "--option"};
130  ParseOptions po4("my usage msg");
131  bool val = false;
132  po4.Register("option", &val, "My boolean");
133  po4.Read(argc4, argv4);
134  KALDI_ASSERT(val == true);
135  }
136 
137 
138  try { // test error with --option, which is not a valid way to set
139  // string-valued options.
140  int argc4 = 2;
141  const char *argv4[2] = { "program_name", "--option"};
142  ParseOptions po4("my usage msg");
143  std::string val;
144  po4.Register("option", &val, "My string");
145  po4.Read(argc4, argv4);
146  assert(false); // Should not reach this part of code.
147  } catch(std::exception e) {
148  KALDI_LOG << "Failed to read option (this is expected).";
149  }
150 
151  { // test that --option= sets "option" to empty, if string.
152  int argc4 = 2;
153  const char *argv4[2] = { "program_name", "--option="};
154  ParseOptions po4("my usage msg");
155  std::string val = "foo";
156  po4.Register("option", &val, "My boolean");
157  po4.Read(argc4, argv4);
158  KALDI_ASSERT(val.empty());
159  }
160 
161  { // integer options test
162  int argc4 = 2;
163  const char *argv4[2] = { "program_name", "--option=8"};
164  ParseOptions po4("my usage msg");
165  int32 val = 32;
166  po4.Register("option", &val, "My int");
167  po4.Read(argc4, argv4);
168  KALDI_ASSERT(val == 8);
169  }
170 
171  { // float
172  int argc4 = 2;
173  const char *argv4[2] = { "program_name", "--option=8.5"};
174  ParseOptions po4("my usage msg");
175  BaseFloat val = 32.0;
176  po4.Register("option", &val, "My float");
177  po4.Read(argc4, argv4);
178  KALDI_ASSERT(val == 8.5);
179  }
180  { // string options test
181  int argc4 = 2;
182  const char *argv4[2] = { "program_name", "--option=bar"};
183  ParseOptions po4("my usage msg");
184  std::string val = "foo";
185  po4.Register("option", &val, "My string");
186  po4.Read(argc4, argv4);
187  KALDI_ASSERT(val == "bar");
188  }
189 
190  try { // test error with --float=string
191  int argc4 = 2;
192  const char *argv4[2] = { "program_name", "--option=foo"};
193  ParseOptions po4("my usage msg");
194  BaseFloat val = 32.0;
195  po4.Register("option", &val, "My float");
196  po4.Read(argc4, argv4);
197  assert(false); // Should not reach this part of code.
198  } catch(std::exception e) {
199  KALDI_LOG << "Failed to read option (this is expected).";
200  }
201 
202 
203  try { // test error with --int=string
204  int argc4 = 2;
205  const char *argv4[2] = { "program_name", "--option=foo"};
206  ParseOptions po4("my usage msg");
207  int32 val = 32;
208  po4.Register("option", &val, "My int");
209  po4.Read(argc4, argv4);
210  assert(false); // Should not reach this part of code.
211  } catch(std::exception e) {
212  KALDI_LOG << "Failed to read option (this is expected).";
213  }
214 
215  try { // test error with --int=int+garbage
216  int argc4 = 2;
217  const char *argv4[2] = { "program_name", "--option=12xyz"};
218  ParseOptions po4("my usage msg");
219  int32 val = 32;
220  po4.Register("option", &val, "My int");
221  po4.Read(argc4, argv4);
222  assert(false); // Should not reach this part of code.
223  } catch(std::exception e) {
224  KALDI_LOG << "Failed to read option (this is expected).";
225  }
226 
227  try { // test error with --unsigned-int=negative-number.
228  int argc4 = 2;
229  const char *argv4[2] = { "program_name", "--option=-13"};
230  ParseOptions po4("my usage msg");
231  uint32 val = 32;
232  po4.Register("option", &val, "My int");
233  po4.Read(argc4, argv4);
234  assert(false); // Should not reach this part of code.
235  } catch(std::exception e) {
236  KALDI_LOG << "Failed to read option (this is expected)xxx.";
237  }
238 
239  try { // test error with --bool=string
240  int argc4 = 2;
241  const char *argv4[2] = { "program_name", "--option=foo"};
242  ParseOptions po4("my usage msg");
243  bool val = false;
244  po4.Register("option", &val, "My bool");
245  po4.Read(argc4, argv4);
246  assert(false); // Should not reach this part of code.
247  } catch(std::exception e) {
248  KALDI_LOG << "Failed to read option (this is expected).";
249  }
250 
251 
252  // test error with --=
253  try {
254  int argc4 = 2;
255  const char *argv4[2] = { "program_name", "--=8"};
256  int32 num = 0;
257  ParseOptions po4("my usage msg");
258  po4.Register("num", &num, "My int32 variable");
259  po4.Read(argc4, argv4);
260  KALDI_ASSERT(num == 0);
261  } catch(std::exception e) {
262  KALDI_LOG << "Failed to read option (this is expected).";
263  }
264 
265  // test "--" (no more options)
266  int argc4 = 5;
267  unum = 2;
268  const char *argv4[5] = { "program_name", "--unum=6", "--", "a", "b" };
269  ParseOptions po4("my usage msg");
270  po4.Register("unum", &unum, "My uint32 variable");
271  po4.Read(argc4, argv4);
272  KALDI_ASSERT(po4.NumArgs() == 2);
273  KALDI_ASSERT(po4.GetArg(1) == "a");
274  KALDI_ASSERT(po4.GetArg(2) == "b");
275  KALDI_ASSERT(unum == 6);
276 
277  // test obsolete "--" (no more options)
278  int argc5 = 3;
279  unum = 2;
280  const char *argv5[3] = { "program_name", "--unum=7", "--" };
281  ParseOptions po5("my usage msg");
282  po5.Register("unum", &unum, "My uint32 variable");
283  po5.Read(argc5, argv5);
284  KALDI_ASSERT(po5.NumArgs() == 0);
285  KALDI_ASSERT(unum == 7);
286 
287  // test that "--foo=bar" after "--" is interpreted as argument
288  int argc6 = 4;
289  unum = 2;
290  const char *argv6[5] = { "program_name", "--unum=8", "--", "--foo=8" };
291  ParseOptions po6("my usage msg");
292  po6.Register("unum", &unum, "My uint32 variable");
293  po6.Read(argc6, argv6);
294  KALDI_ASSERT(po6.NumArgs() == 1);
295  KALDI_ASSERT(po6.GetArg(1) == "--foo=8");
296 }
297 
298 
299 } // end namespace kaldi.
300 
301 int main() {
302  using namespace kaldi;
304  return 0;
305 }
306 
307 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void UnitTestParseOptions()
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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.
int main()
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void Register(ParseOptions *po)
#define KALDI_LOG
Definition: kaldi-error.h:153