Skip to content

Commit a893b14

Browse files
committed
Merge pull request opencv#9428 from csukuangfj:fix-commandline-parser
2 parents b67c64e + 97ec91a commit a893b14

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

modules/core/include/opencv2/core/utility.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ class CV_EXPORTS CommandLineParser
822822
823823
This method returns the path to the executable from the command line (`argv[0]`).
824824
825-
For example, if the application has been started with such command:
825+
For example, if the application has been started with such a command:
826826
@code{.sh}
827827
$ ./bin/my-executable
828828
@endcode
@@ -928,7 +928,7 @@ class CV_EXPORTS CommandLineParser
928928
*/
929929
void printMessage() const;
930930

931-
/** @brief Print list of errors occured
931+
/** @brief Print list of errors occurred
932932
933933
@sa check
934934
*/

modules/core/src/command_line_parser.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ static const char* get_type_name(int type)
6969
return "unknown";
7070
}
7171

72+
static bool parse_bool(std::string str)
73+
{
74+
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
75+
std::istringstream is(str);
76+
bool b;
77+
is >> (str.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
78+
return b;
79+
}
80+
7281
static void from_str(const String& str, int type, void* dst)
7382
{
7483
std::stringstream ss(str.c_str());
@@ -78,7 +87,7 @@ static void from_str(const String& str, int type, void* dst)
7887
{
7988
std::string temp;
8089
ss >> temp;
81-
*(bool*) dst = temp == "true";
90+
*(bool*) dst = parse_bool(temp);
8291
}
8392
else if( type == Param::UNSIGNED_INT )
8493
ss >> *(unsigned*)dst;
@@ -113,7 +122,7 @@ void CommandLineParser::getByName(const String& name, bool space_delete, int typ
113122
if (space_delete)
114123
v = cat_string(v);
115124

116-
// the key was neither specified nor has it a default value
125+
// the key was neither specified nor has a default value
117126
if((v.empty() && type != Param::STRING) || v == noneValue) {
118127
impl->error = true;
119128
impl->error_message = impl->error_message + "Missing parameter: '" + name + "'\n";
@@ -148,7 +157,7 @@ void CommandLineParser::getByIndex(int index, bool space_delete, int type, void*
148157
String v = impl->data[i].def_value;
149158
if (space_delete == true) v = cat_string(v);
150159

151-
// the key was neither specified nor has it a default value
160+
// the key was neither specified nor has a default value
152161
if((v.empty() && type != Param::STRING) || v == noneValue) {
153162
impl->error = true;
154163
impl->error_message = impl->error_message + format("Missing parameter #%d\n", index);

modules/core/test/test_utils.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ TEST(CommandLineParser, testHas_noValues)
3535
cv::CommandLineParser parser(argc, argv, keys);
3636
EXPECT_TRUE(parser.has("help"));
3737
EXPECT_TRUE(parser.has("h"));
38+
EXPECT_TRUE(parser.get<bool>("help"));
39+
EXPECT_TRUE(parser.get<bool>("h"));
3840
EXPECT_TRUE(parser.has("info"));
3941
EXPECT_TRUE(parser.has("i"));
42+
EXPECT_TRUE(parser.get<bool>("info"));
43+
EXPECT_TRUE(parser.get<bool>("i"));
44+
EXPECT_TRUE(parser.get<bool>("true"));
45+
EXPECT_TRUE(parser.get<bool>("t"));
4046
EXPECT_FALSE(parser.has("n"));
4147
EXPECT_FALSE(parser.has("unused"));
4248
}
@@ -47,8 +53,14 @@ TEST(CommandLineParser, testHas_TrueValues)
4753
cv::CommandLineParser parser(argc, argv, keys);
4854
EXPECT_TRUE(parser.has("help"));
4955
EXPECT_TRUE(parser.has("h"));
56+
EXPECT_TRUE(parser.get<bool>("help"));
57+
EXPECT_TRUE(parser.get<bool>("h"));
5058
EXPECT_TRUE(parser.has("info"));
5159
EXPECT_TRUE(parser.has("i"));
60+
EXPECT_TRUE(parser.get<bool>("info"));
61+
EXPECT_TRUE(parser.get<bool>("i"));
62+
EXPECT_TRUE(parser.get<bool>("true"));
63+
EXPECT_TRUE(parser.get<bool>("t"));
5264
EXPECT_FALSE(parser.has("n"));
5365
EXPECT_FALSE(parser.has("unused"));
5466
}
@@ -59,8 +71,14 @@ TEST(CommandLineParser, testHas_TrueValues1)
5971
cv::CommandLineParser parser(argc, argv, keys);
6072
EXPECT_TRUE(parser.has("help"));
6173
EXPECT_TRUE(parser.has("h"));
74+
EXPECT_TRUE(parser.get<bool>("help"));
75+
EXPECT_TRUE(parser.get<bool>("h"));
6276
EXPECT_TRUE(parser.has("info"));
6377
EXPECT_TRUE(parser.has("i"));
78+
EXPECT_TRUE(parser.get<bool>("info"));
79+
EXPECT_TRUE(parser.get<bool>("i"));
80+
EXPECT_TRUE(parser.get<bool>("true"));
81+
EXPECT_TRUE(parser.get<bool>("t"));
6482
EXPECT_FALSE(parser.has("n"));
6583
EXPECT_FALSE(parser.has("unused"));
6684
}
@@ -71,8 +89,14 @@ TEST(CommandLineParser, testHas_FalseValues0)
7189
cv::CommandLineParser parser(argc, argv, keys);
7290
EXPECT_TRUE(parser.has("help"));
7391
EXPECT_TRUE(parser.has("h"));
92+
EXPECT_FALSE(parser.get<bool>("help"));
93+
EXPECT_FALSE(parser.get<bool>("h"));
7494
EXPECT_TRUE(parser.has("info"));
7595
EXPECT_TRUE(parser.has("i"));
96+
EXPECT_FALSE(parser.get<bool>("info"));
97+
EXPECT_FALSE(parser.get<bool>("i"));
98+
EXPECT_TRUE(parser.get<bool>("true"));
99+
EXPECT_TRUE(parser.get<bool>("t"));
76100
EXPECT_FALSE(parser.has("n"));
77101
EXPECT_FALSE(parser.has("unused"));
78102
}
@@ -99,30 +123,38 @@ TEST(CommandLineParser, testBoolOption_noValues)
99123
EXPECT_TRUE(parser.get<bool>("h"));
100124
EXPECT_TRUE(parser.get<bool>("info"));
101125
EXPECT_TRUE(parser.get<bool>("i"));
126+
EXPECT_TRUE(parser.get<bool>("true"));
127+
EXPECT_TRUE(parser.get<bool>("t"));
102128
}
103129

104130
TEST(CommandLineParser, testBoolOption_TrueValues)
105131
{
106-
const char* argv[] = {"<bin>", "-h=TRUE", "--info=true"};
107-
const int argc = 3;
132+
const char* argv[] = {"<bin>", "-h=TrUe", "-t=1", "--info=true", "-n=truE"};
133+
const int argc = 5;
108134
cv::CommandLineParser parser(argc, argv, keys);
109-
//EXPECT_TRUE(parser.get<bool>("help"));
110-
//EXPECT_TRUE(parser.get<bool>("h"));
135+
EXPECT_TRUE(parser.get<bool>("help"));
136+
EXPECT_TRUE(parser.get<bool>("h"));
111137
EXPECT_TRUE(parser.get<bool>("info"));
112138
EXPECT_TRUE(parser.get<bool>("i"));
113-
EXPECT_FALSE(parser.get<bool>("unused"));
114-
EXPECT_FALSE(parser.get<bool>("n"));
139+
EXPECT_TRUE(parser.get<bool>("true"));
140+
EXPECT_TRUE(parser.get<bool>("t"));
141+
EXPECT_TRUE(parser.get<bool>("unused"));
142+
EXPECT_TRUE(parser.get<bool>("n"));
115143
}
116144

117145
TEST(CommandLineParser, testBoolOption_FalseValues)
118146
{
119-
const char* argv[] = {"<bin>", "--help=FALSE", "-i=false"};
120-
const int argc = 3;
147+
const char* argv[] = {"<bin>", "--help=FALSE", "-t=FaLsE", "-i=false", "-n=0"};
148+
const int argc = 5;
121149
cv::CommandLineParser parser(argc, argv, keys);
122150
EXPECT_FALSE(parser.get<bool>("help"));
123151
EXPECT_FALSE(parser.get<bool>("h"));
124152
EXPECT_FALSE(parser.get<bool>("info"));
125153
EXPECT_FALSE(parser.get<bool>("i"));
154+
EXPECT_FALSE(parser.get<bool>("true"));
155+
EXPECT_FALSE(parser.get<bool>("t"));
156+
EXPECT_FALSE(parser.get<bool>("unused"));
157+
EXPECT_FALSE(parser.get<bool>("n"));
126158
}
127159

128160

0 commit comments

Comments
 (0)