Commit a10bd5233b9607ad236ee24a46475a65ec9e98c7
Committed by
GitHub
1 parent
17b2c910
Fix values attached to short options (#360)
Fixes #357.
Showing
2 changed files
with
14 additions
and
7 deletions
include/cxxopts.hpp
| @@ -754,7 +754,7 @@ std::basic_regex<char> falsy_pattern | @@ -754,7 +754,7 @@ std::basic_regex<char> falsy_pattern | ||
| 754 | ("(f|F)(alse)?|0"); | 754 | ("(f|F)(alse)?|0"); |
| 755 | 755 | ||
| 756 | std::basic_regex<char> option_matcher | 756 | std::basic_regex<char> option_matcher |
| 757 | - ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)"); | 757 | + ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]].*)"); |
| 758 | std::basic_regex<char> option_specifier | 758 | std::basic_regex<char> option_specifier |
| 759 | ("([[:alnum:]][-_[:alnum:]]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*"); | 759 | ("([[:alnum:]][-_[:alnum:]]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*"); |
| 760 | std::basic_regex<char> option_specifier_separator(", *"); | 760 | std::basic_regex<char> option_specifier_separator(", *"); |
test/options.cpp
| @@ -118,9 +118,11 @@ TEST_CASE("Short options", "[options]") | @@ -118,9 +118,11 @@ TEST_CASE("Short options", "[options]") | ||
| 118 | cxxopts::Options options("test_short", " - test short options"); | 118 | cxxopts::Options options("test_short", " - test short options"); |
| 119 | 119 | ||
| 120 | options.add_options() | 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 | auto actual_argv = argv.argv(); | 127 | auto actual_argv = argv.argv(); |
| 126 | auto argc = argv.argc(); | 128 | auto argc = argv.argc(); |
| @@ -131,10 +133,15 @@ TEST_CASE("Short options", "[options]") | @@ -131,10 +133,15 @@ TEST_CASE("Short options", "[options]") | ||
| 131 | CHECK(result["a"].as<std::string>() == "value"); | 133 | CHECK(result["a"].as<std::string>() == "value"); |
| 132 | 134 | ||
| 133 | auto& arguments = result.arguments(); | 135 | auto& arguments = result.arguments(); |
| 134 | - REQUIRE(arguments.size() == 1); | 136 | + REQUIRE(arguments.size() == 3); |
| 135 | CHECK(arguments[0].key() == "a"); | 137 | CHECK(arguments[0].key() == "a"); |
| 136 | CHECK(arguments[0].value() == "value"); | 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 | REQUIRE_THROWS_AS(options.add_options()("", "nothing option"), | 145 | REQUIRE_THROWS_AS(options.add_options()("", "nothing option"), |
| 139 | cxxopts::exceptions::invalid_option_format&); | 146 | cxxopts::exceptions::invalid_option_format&); |
| 140 | } | 147 | } |
| @@ -703,8 +710,8 @@ TEST_CASE("Allow bad short syntax", "[options]") { | @@ -703,8 +710,8 @@ TEST_CASE("Allow bad short syntax", "[options]") { | ||
| 703 | ("s,short", "a short option"); | 710 | ("s,short", "a short option"); |
| 704 | 711 | ||
| 705 | Argv av({ | 712 | Argv av({ |
| 706 | - "unknown_options", | ||
| 707 | - "-some_bad_short", | 713 | + "--ab?", |
| 714 | + "-?b?#@" | ||
| 708 | }); | 715 | }); |
| 709 | 716 | ||
| 710 | auto** argv = av.argv(); | 717 | auto** argv = av.argv(); |
| @@ -718,7 +725,7 @@ TEST_CASE("Allow bad short syntax", "[options]") { | @@ -718,7 +725,7 @@ TEST_CASE("Allow bad short syntax", "[options]") { | ||
| 718 | options.allow_unrecognised_options(); | 725 | options.allow_unrecognised_options(); |
| 719 | CHECK_NOTHROW(options.parse(argc, argv)); | 726 | CHECK_NOTHROW(options.parse(argc, argv)); |
| 720 | REQUIRE(argc == 2); | 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 |