mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 22:54:39 +00:00
Merge pull request #310 from p-ranav/bugfix/307_choices
Bugfix/307 choices
This commit is contained in:
commit
69dabd88a8
@ -845,8 +845,14 @@ public:
|
||||
if (m_choices.has_value()) {
|
||||
// Check each value in (start, end) and make sure
|
||||
// it is in the list of allowed choices/options
|
||||
std::size_t i = 0;
|
||||
auto max_number_of_args = m_num_args_range.get_max();
|
||||
for (auto it = start; it != end; ++it) {
|
||||
if (i == max_number_of_args) {
|
||||
break;
|
||||
}
|
||||
find_value_in_choices_or_throw(it);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,72 @@ TEST_CASE("Parse argument that is in the fixed number of allowed choices" *
|
||||
program.parse_args({"test", "red"});
|
||||
}
|
||||
|
||||
TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
|
||||
"other positional argument" *
|
||||
test_suite("choices")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--input")
|
||||
.default_value(std::string{"baz"})
|
||||
.choices("foo", "bar", "baz");
|
||||
program.add_argument("--value").scan<'i', int>().default_value(0);
|
||||
|
||||
REQUIRE_NOTHROW(
|
||||
program.parse_args({"test", "--input", "foo", "--value", "1"}));
|
||||
REQUIRE(program.get("--input") == "foo");
|
||||
REQUIRE(program.get<int>("--value") == 1);
|
||||
}
|
||||
|
||||
TEST_CASE(
|
||||
"Parse nargs argument that is in the fixed number of allowed choices, with "
|
||||
"other positional argument" *
|
||||
test_suite("choices")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--input")
|
||||
.default_value(std::string{"baz"})
|
||||
.choices("foo", "bar", "baz")
|
||||
.nargs(2);
|
||||
program.add_argument("--value").scan<'i', int>().default_value(0);
|
||||
|
||||
REQUIRE_NOTHROW(
|
||||
program.parse_args({"test", "--input", "foo", "bar", "--value", "1"}));
|
||||
REQUIRE((program.get<std::vector<std::string>>("--input") ==
|
||||
std::vector<std::string>{"foo", "bar"}));
|
||||
REQUIRE(program.get<int>("--value") == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
|
||||
"other positional argument (reversed)" *
|
||||
test_suite("choices")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--input")
|
||||
.default_value(std::string{"baz"})
|
||||
.choices("foo", "bar", "baz");
|
||||
program.add_argument("--value").scan<'i', int>().default_value(0);
|
||||
|
||||
REQUIRE_NOTHROW(
|
||||
program.parse_args({"test", "--value", "1", "--input", "foo"}));
|
||||
REQUIRE(program.get("--input") == "foo");
|
||||
REQUIRE(program.get<int>("--value") == 1);
|
||||
}
|
||||
|
||||
TEST_CASE(
|
||||
"Parse nargs argument that is in the fixed number of allowed choices, with "
|
||||
"other positional argument (reversed)" *
|
||||
test_suite("choices")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("--input")
|
||||
.default_value(std::string{"baz"})
|
||||
.choices("foo", "bar", "baz")
|
||||
.nargs(2);
|
||||
program.add_argument("--value").scan<'i', int>().default_value(0);
|
||||
|
||||
REQUIRE_NOTHROW(
|
||||
program.parse_args({"test", "--value", "1", "--input", "foo", "bar"}));
|
||||
REQUIRE((program.get<std::vector<std::string>>("--input") ==
|
||||
std::vector<std::string>{"foo", "bar"}));
|
||||
REQUIRE(program.get<int>("--value") == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("Parse argument that is in the fixed number of allowed choices, with "
|
||||
"invalid default" *
|
||||
test_suite("choices")) {
|
||||
|
Loading…
Reference in New Issue
Block a user