From 39bfcb15e1427aafdf20a2e855d0678ee477a1d8 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 11 Oct 2016 08:31:45 +0200 Subject: [PATCH] fix(command_line): Correct handling of long argument value Use correct format for passing values using the long version of the option, e.g: --arg=value --- include/components/command_line.hpp | 6 +++--- tests/components/test_command_line.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/components/command_line.hpp b/include/components/command_line.hpp index 13c7ac75..53732765 100644 --- a/include/components/command_line.hpp +++ b/include/components/command_line.hpp @@ -174,10 +174,10 @@ namespace command_line { if (input_next.empty() && opt.compare(0, 2, "--") != 0) throw value_error("Missing value for " + opt); - else if (!input_next.empty()) - value = input_next; - else if ((pos = opt.find("=")) == string::npos) + else if ((pos = opt.find("=")) == string::npos && opt.compare(0, 2, "--") == 0) throw value_error("Missing value for " + opt); + else if (pos == string::npos && !input_next.empty()) + value = input_next; else { value = opt.substr(pos + 1); opt = opt.substr(0, pos); diff --git a/tests/components/test_command_line.cpp b/tests/components/test_command_line.cpp index 40f8bd39..78b5f712 100644 --- a/tests/components/test_command_line.cpp +++ b/tests/components/test_command_line.cpp @@ -53,6 +53,11 @@ class test_command_line : public unit_test { CPPUNIT_ASSERT_EQUAL(true, cli.has("flag")); CPPUNIT_ASSERT_EQUAL(true, cli.has("option")); + cli = get_instance(); + cli.process_input(string_util::split("--option=foo --flag", ' ')); + CPPUNIT_ASSERT_EQUAL(true, cli.has("flag")); + CPPUNIT_ASSERT_EQUAL(true, cli.has("option")); + cli = get_instance(); cli.process_input(string_util::split("--option=baz", ' ')); CPPUNIT_ASSERT_EQUAL(false, cli.has("flag")); @@ -82,11 +87,13 @@ class test_command_line : public unit_test { void test_missing_value() { auto input1 = string_util::split("--option", ' '); auto input2 = string_util::split("-o", ' '); + auto input3 = string_util::split("--option baz", ' '); using command_line::value_error; CPPUNIT_ASSERT_THROW(get_instance().process_input(input1), value_error); CPPUNIT_ASSERT_THROW(get_instance().process_input(input2), value_error); + CPPUNIT_ASSERT_THROW(get_instance().process_input(input3), value_error); } void test_invalid_value() {