Skip to content

Commit a10bd52

Browse files
authored
Fix values attached to short options (#360)
Fixes #357.
1 parent 17b2c91 commit a10bd52

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

include/cxxopts.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ std::basic_regex<char> falsy_pattern
754754
("(f|F)(alse)?|0");
755755

756756
std::basic_regex<char> option_matcher
757-
("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)");
757+
("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]].*)");
758758
std::basic_regex<char> option_specifier
759759
("([[:alnum:]][-_[:alnum:]]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*");
760760
std::basic_regex<char> option_specifier_separator(", *");

test/options.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ TEST_CASE("Short options", "[options]")
118118
cxxopts::Options options("test_short", " - test short options");
119119

120120
options.add_options()
121-
("a", "a short option", cxxopts::value<std::string>());
121+
("a", "a short option", cxxopts::value<std::string>())
122+
("b", "b option")
123+
("c", "c option", cxxopts::value<std::string>());
122124

123-
Argv argv({"test_short", "-a", "value"});
125+
Argv argv({"test_short", "-a", "value", "-bcfoo=something"});
124126

125127
auto actual_argv = argv.argv();
126128
auto argc = argv.argc();
@@ -131,10 +133,15 @@ TEST_CASE("Short options", "[options]")
131133
CHECK(result["a"].as<std::string>() == "value");
132134

133135
auto& arguments = result.arguments();
134-
REQUIRE(arguments.size() == 1);
136+
REQUIRE(arguments.size() == 3);
135137
CHECK(arguments[0].key() == "a");
136138
CHECK(arguments[0].value() == "value");
137139

140+
CHECK(result.count("b") == 1);
141+
CHECK(result.count("c") == 1);
142+
143+
CHECK(result["c"].as<std::string>() == "foo=something");
144+
138145
REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
139146
cxxopts::exceptions::invalid_option_format&);
140147
}
@@ -703,8 +710,8 @@ TEST_CASE("Allow bad short syntax", "[options]") {
703710
("s,short", "a short option");
704711

705712
Argv av({
706-
"unknown_options",
707-
"-some_bad_short",
713+
"--ab?",
714+
"-?b?#@"
708715
});
709716

710717
auto** argv = av.argv();
@@ -718,7 +725,7 @@ TEST_CASE("Allow bad short syntax", "[options]") {
718725
options.allow_unrecognised_options();
719726
CHECK_NOTHROW(options.parse(argc, argv));
720727
REQUIRE(argc == 2);
721-
CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
728+
CHECK_THAT(argv[1], Catch::Equals("-?b?#@"));
722729
}
723730
}
724731

0 commit comments

Comments
 (0)