Commit 9d16b98eddbe5333832335ba6306fa3ce9a299c5
1 parent
77b1b43a
Support only short option
Fixes #47. Adds support for options that only have a short option.
Showing
3 changed files
with
56 additions
and
5 deletions
include/cxxopts.hpp
| ... | ... | @@ -851,7 +851,7 @@ namespace cxxopts |
| 851 | 851 | ("--([[:alnum:]][-_[:alnum:]]+)(=(.*))?|-([[:alnum:]]+)"); |
| 852 | 852 | |
| 853 | 853 | std::basic_regex<char> option_specifier |
| 854 | - ("(([[:alnum:]]),)?([[:alnum:]][-_[:alnum:]]+)"); | |
| 854 | + ("(([[:alnum:]]),)?([[:alnum:]][-_[:alnum:]]*)?"); | |
| 855 | 855 | |
| 856 | 856 | String |
| 857 | 857 | format_option |
| ... | ... | @@ -985,8 +985,39 @@ OptionAdder::operator() |
| 985 | 985 | const auto& s = result[2]; |
| 986 | 986 | const auto& l = result[3]; |
| 987 | 987 | |
| 988 | - m_options.add_option(m_group, s.str(), l.str(), desc, value, | |
| 989 | - std::move(arg_help)); | |
| 988 | + if (!s.length() && !l.length()) | |
| 989 | + { | |
| 990 | + throw invalid_option_format_error(opts); | |
| 991 | + } else if (l.length() == 1 && s.length()) | |
| 992 | + { | |
| 993 | + throw invalid_option_format_error(opts); | |
| 994 | + } | |
| 995 | + | |
| 996 | + auto option_names = [] | |
| 997 | + ( | |
| 998 | + const std::sub_match<const char*>& s, | |
| 999 | + const std::sub_match<const char*>& l | |
| 1000 | + ) | |
| 1001 | + { | |
| 1002 | + if (l.length() == 1) | |
| 1003 | + { | |
| 1004 | + return std::make_tuple(l.str(), s.str()); | |
| 1005 | + } | |
| 1006 | + else | |
| 1007 | + { | |
| 1008 | + return std::make_tuple(s.str(), l.str()); | |
| 1009 | + } | |
| 1010 | + }(s, l); | |
| 1011 | + | |
| 1012 | + m_options.add_option | |
| 1013 | + ( | |
| 1014 | + m_group, | |
| 1015 | + std::get<0>(option_names), | |
| 1016 | + std::get<1>(option_names), | |
| 1017 | + desc, | |
| 1018 | + value, | |
| 1019 | + std::move(arg_help) | |
| 1020 | + ); | |
| 990 | 1021 | |
| 991 | 1022 | return *this; |
| 992 | 1023 | } | ... | ... |
test/CMakeLists.txt
test/options.cpp
| 1 | -#define CATCH_CONFIG_MAIN | |
| 2 | 1 | #include "catch.hpp" |
| 3 | 2 | |
| 4 | 3 | #include <initializer_list> |
| ... | ... | @@ -80,6 +79,27 @@ TEST_CASE("Basic options", "[options]") |
| 80 | 79 | CHECK(options.count("6") == 1); |
| 81 | 80 | } |
| 82 | 81 | |
| 82 | +TEST_CASE("Short options", "[options]") | |
| 83 | +{ | |
| 84 | + cxxopts::Options options("test_short", " - test short options"); | |
| 85 | + | |
| 86 | + options.add_options() | |
| 87 | + ("a", "a short option", cxxopts::value<std::string>()); | |
| 88 | + | |
| 89 | + Argv argv({"test_short", "-a", "value"}); | |
| 90 | + | |
| 91 | + auto actual_argv = argv.argv(); | |
| 92 | + auto argc = argv.argc(); | |
| 93 | + | |
| 94 | + options.parse(argc, actual_argv); | |
| 95 | + | |
| 96 | + CHECK(options.count("a") == 1); | |
| 97 | + CHECK(options["a"].as<std::string>() == "value"); | |
| 98 | + | |
| 99 | + REQUIRE_THROWS_AS(options.add_options()("", "nothing option"), | |
| 100 | + cxxopts::invalid_option_format_error); | |
| 101 | +} | |
| 102 | + | |
| 83 | 103 | TEST_CASE("No positional", "[positional]") |
| 84 | 104 | { |
| 85 | 105 | cxxopts::Options options("test_no_positional", | ... | ... |