Commit a10bd5233b9607ad236ee24a46475a65ec9e98c7

Authored by jarro2783
Committed by GitHub
1 parent 17b2c910

Fix values attached to short options (#360)

Fixes #357.
include/cxxopts.hpp
... ... @@ -754,7 +754,7 @@ std::basic_regex<char> falsy_pattern
754 754 ("(f|F)(alse)?|0");
755 755  
756 756 std::basic_regex<char> option_matcher
757   - ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)");
  757 + ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]].*)");
758 758 std::basic_regex<char> option_specifier
759 759 ("([[:alnum:]][-_[:alnum:]]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*");
760 760 std::basic_regex<char> option_specifier_separator(", *");
... ...
test/options.cpp
... ... @@ -118,9 +118,11 @@ TEST_CASE(&quot;Short options&quot;, &quot;[options]&quot;)
118 118 cxxopts::Options options("test_short", " - test short options");
119 119  
120 120 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>());
122 124  
123   - Argv argv({"test_short", "-a", "value"});
  125 + Argv argv({"test_short", "-a", "value", "-bcfoo=something"});
124 126  
125 127 auto actual_argv = argv.argv();
126 128 auto argc = argv.argc();
... ... @@ -131,10 +133,15 @@ TEST_CASE(&quot;Short options&quot;, &quot;[options]&quot;)
131 133 CHECK(result["a"].as<std::string>() == "value");
132 134  
133 135 auto& arguments = result.arguments();
134   - REQUIRE(arguments.size() == 1);
  136 + REQUIRE(arguments.size() == 3);
135 137 CHECK(arguments[0].key() == "a");
136 138 CHECK(arguments[0].value() == "value");
137 139  
  140 + CHECK(result.count("b") == 1);
  141 + CHECK(result.count("c") == 1);
  142 +
  143 + CHECK(result["c"].as<std::string>() == "foo=something");
  144 +
138 145 REQUIRE_THROWS_AS(options.add_options()("", "nothing option"),
139 146 cxxopts::exceptions::invalid_option_format&);
140 147 }
... ... @@ -703,8 +710,8 @@ TEST_CASE(&quot;Allow bad short syntax&quot;, &quot;[options]&quot;) {
703 710 ("s,short", "a short option");
704 711  
705 712 Argv av({
706   - "unknown_options",
707   - "-some_bad_short",
  713 + "--ab?",
  714 + "-?b?#@"
708 715 });
709 716  
710 717 auto** argv = av.argv();
... ... @@ -718,7 +725,7 @@ TEST_CASE(&quot;Allow bad short syntax&quot;, &quot;[options]&quot;) {
718 725 options.allow_unrecognised_options();
719 726 CHECK_NOTHROW(options.parse(argc, argv));
720 727 REQUIRE(argc == 2);
721   - CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
  728 + CHECK_THAT(argv[1], Catch::Equals("-?b?#@"));
722 729 }
723 730 }
724 731  
... ...