From 46ada3886fa490e279274182aa8f5fc65740fc21 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Wed, 23 Sep 2015 16:20:07 +0200 Subject: [PATCH] fix wrong parsing of values containing '=' fixes #5074 --- modules/core/src/command_line_parser.cpp | 28 ++++++++++-------------- modules/core/test/test_utils.cpp | 15 +++++++++++++ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/modules/core/src/command_line_parser.cpp b/modules/core/src/command_line_parser.cpp index 658c3c700..542b231ff 100644 --- a/modules/core/src/command_line_parser.cpp +++ b/modules/core/src/command_line_parser.cpp @@ -234,25 +234,21 @@ CommandLineParser::CommandLineParser(int argc, const char* const argv[], const S jj = 0; for (int i = 1; i < argc; i++) { - String s = String(argv[i]); + String s(argv[i]); + bool hasSingleDash = s.length() > 1 && s[0] == '-'; - if (s.find('=') != String::npos && s.find('=') < s.length()) + if (hasSingleDash) { - std::vector k_v = impl->split_string(s, '=', true); - for (int h = 0; h < 2; h++) - { - if (k_v[0][0] == '-') - k_v[0] = k_v[0].substr(1, k_v[0].length() -1); + bool hasDoubleDash = s.length() > 2 && s[1] == '-'; + String key = s.substr(hasDoubleDash ? 2 : 1); + String value = "true"; + size_t equalsPos = key.find('='); + + if(equalsPos != String::npos) { + value = key.substr(equalsPos + 1); + key = key.substr(0, equalsPos); } - impl->apply_params(k_v[0], k_v[1]); - } - else if (s.length() > 2 && s[0] == '-' && s[1] == '-') - { - impl->apply_params(s.substr(2), "true"); - } - else if (s.length() > 1 && s[0] == '-') - { - impl->apply_params(s.substr(1), "true"); + impl->apply_params(key, value); } else { diff --git a/modules/core/test/test_utils.cpp b/modules/core/test/test_utils.cpp index 0f1188de0..18b0a5a7e 100644 --- a/modules/core/test/test_utils.cpp +++ b/modules/core/test/test_utils.cpp @@ -203,4 +203,19 @@ TEST(CommandLineParser, testEmptyStringValue) EXPECT_FALSE(parser.check()); } +TEST(CommandLineParser, positional_regression_5074_equal_sign) +{ + static const char * const keys3 = + "{ @eq0 | | }" + "{ eq1 | | }"; + + const char* argv[] = {"", "1=0", "--eq1=1=0"}; + const int argc = 3; + cv::CommandLineParser parser(argc, argv, keys3); + EXPECT_EQ("1=0", parser.get("@eq0")); + EXPECT_EQ("1=0", parser.get(0)); + EXPECT_EQ("1=0", parser.get("eq1")); + EXPECT_TRUE(parser.check()); +} + } // namespace