Commit 70fb8495 authored by Arnab Ghoshal's avatar Arnab Ghoshal
Browse files

fixing ParseOptions to disallow empty values and keys (i.e. --option= and --=val)

git-svn-id: https://svn.code.sf.net/p/kaldi/code/trunk@2810 5e6a8d80-dfce-4ca6-a32a-6e07a63d50c8
parent 98fcb5ed
......@@ -98,6 +98,32 @@ void UnitTestParseOptions() {
KALDI_ASSERT(dummy_opts.my_bool == false);
KALDI_ASSERT(dummy_opts.my_string == "baz");
// test error with --option=
try {
int argc4 = 2;
const char *argv4[2] = { "program_name", "--option="};
ParseOptions po4("my usage msg");
bool val = false;
po4.Register("option", &val, "My boolean");
po4.Read(argc4, argv4);
KALDI_ASSERT(val == false);
} catch (std::exception e) {
KALDI_LOG << "Failed to read option (this is expected).";
}
// test error with --=
try {
int argc4 = 2;
const char *argv4[2] = { "program_name", "--=8"};
int32 num = 0;
ParseOptions po4("my usage msg");
po4.Register("num", &num, "My int32 variable");
po4.Read(argc4, argv4);
KALDI_ASSERT(num == 0);
} catch (std::exception e) {
KALDI_LOG << "Failed to read option (this is expected).";
}
// test "--" (no more options)
int argc4 = 5;
unum = 2;
......
......@@ -469,11 +469,17 @@ void ParseOptions::SplitLongArg(std::string in, std::string *key,
std::string *value) {
assert(in.substr(0, 2) == "--"); // precondition.
size_t pos = in.find_first_of('=', 0);
if (pos == std::string::npos) {
if (pos == std::string::npos) { // we allow --option for bools
// defaults to empty. We handle this differently in different cases.
*key = in.substr(2, in.size()-2); // 2 because starts with --.
*value = "";
} else {
} else if (pos == in.size()-1) { // we don't allow --option= without value
PrintUsage(true);
KALDI_ERR << "Invalid option (no value): " << in;
} else if (pos == 2) { // we also don't allow empty keys: --=value
PrintUsage(true);
KALDI_ERR << "Invalid option (no key): " << in;
} else { // normal case: --option=value
*key = in.substr(2, pos-2); // 2 because starts with --.
*value = in.substr(pos + 1);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment