diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index 06d30fd4..c9413e1f 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -1012,7 +1012,7 @@ class Argument { (m_choices.has_value()) ? passed_options : m_num_args_range.get_max(); const auto num_args_min = m_num_args_range.get_min(); std::size_t dist = 0; - if (num_args_max == 0) { + if (m_num_args_range.get_max() == 0) { if (!dry_run) { m_values.emplace_back(m_implicit_value); for(auto &action: m_actions) { diff --git a/test/test_choices.cpp b/test/test_choices.cpp index 7e8917fa..1aa4d645 100644 --- a/test/test_choices.cpp +++ b/test/test_choices.cpp @@ -203,3 +203,13 @@ TEST_CASE("Parse multiple arguments that are not in range of allowed choices" * "Invalid argument \"d\" - allowed options: {a, b, c}", std::runtime_error); } + +TEST_CASE("Optional option with choices and nargs(1) appearing last accepts " + "empty argument" * + test_suite("choices")) { + argparse::ArgumentParser program("test"); + program.add_argument("--color").choices("red", "green").nargs(1); + + REQUIRE_THROWS_AS(program.parse_args({"test", "--color"}), + std::runtime_error); +}