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
This commit is contained in:
Michael Carlberg 2016-10-11 08:31:45 +02:00
parent bcb6894496
commit 39bfcb15e1
2 changed files with 10 additions and 3 deletions

View File

@ -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);

View File

@ -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() {