Commit d92988c6a218e2fef987e30d99445e7c87c4b87b

Authored by Jarryd Beck
1 parent db674a46

Allow for numbers in options

Closes #32. This adds numbers as allowed values in all parts
of short and long option specifiers.
src/cxxopts.hpp
@@ -829,10 +829,10 @@ namespace cxxopts @@ -829,10 +829,10 @@ namespace cxxopts
829 constexpr int OPTION_DESC_GAP = 2; 829 constexpr int OPTION_DESC_GAP = 2;
830 830
831 std::basic_regex<char> option_matcher 831 std::basic_regex<char> option_matcher
832 - ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([a-zA-Z]+)"); 832 + ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)");
833 833
834 std::basic_regex<char> option_specifier 834 std::basic_regex<char> option_specifier
835 - ("(([a-zA-Z]),)?([a-zA-Z0-9][-_a-zA-Z0-9]+)"); 835 + ("(([[:alnum:]]),)?([[:alnum:]][-_[:alnum:]]+)");
836 836
837 String 837 String
838 format_option 838 format_option
test/options.cpp
@@ -51,7 +51,9 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -51,7 +51,9 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
51 ("long", "a long option") 51 ("long", "a long option")
52 ("s,short", "a short option") 52 ("s,short", "a short option")
53 ("value", "an option with a value", cxxopts::value<std::string>()) 53 ("value", "an option with a value", cxxopts::value<std::string>())
54 - ("a,av", "a short option with a value", cxxopts::value<std::string>()); 54 + ("a,av", "a short option with a value", cxxopts::value<std::string>())
  55 + ("6,six", "a short number option")
  56 + ;
55 57
56 Argv argv({ 58 Argv argv({
57 "tester", 59 "tester",
@@ -60,7 +62,8 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -60,7 +62,8 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
60 "--value", 62 "--value",
61 "value", 63 "value",
62 "-a", 64 "-a",
63 - "b" 65 + "b",
  66 + "-6"
64 }); 67 });
65 68
66 char** actual_argv = argv.argv(); 69 char** actual_argv = argv.argv();
@@ -74,6 +77,7 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;) @@ -74,6 +77,7 @@ TEST_CASE(&quot;Basic options&quot;, &quot;[options]&quot;)
74 CHECK(options.count("a") == 1); 77 CHECK(options.count("a") == 1);
75 CHECK(options["value"].as<std::string>() == "value"); 78 CHECK(options["value"].as<std::string>() == "value");
76 CHECK(options["a"].as<std::string>() == "b"); 79 CHECK(options["a"].as<std::string>() == "b");
  80 + CHECK(options.count("6") == 1);
77 } 81 }
78 82
79 TEST_CASE("No positional", "[positional]") 83 TEST_CASE("No positional", "[positional]")