From f5ded50576860cc432c254876e7c5a8e33dd1fcf Mon Sep 17 00:00:00 2001 From: SuperCoolPencil Date: Sun, 16 Aug 2026 09:47:42 +0530 Subject: [PATCH 1/2] test: add test case for optional argument with choices and nargs(1) failing on empty input --- test/test_choices.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) 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); +} From 21bc3af9b8e01bd45232c93ec76c8e5f2e39065d Mon Sep 17 00:00:00 2001 From: SuperCoolPencil Date: Sun, 16 Aug 2026 10:01:22 +0530 Subject: [PATCH 2/2] fix: use raw max range instead of processed choices when validating argument count --- include/argparse/argparse.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) {