Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/307 choices #310

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class Argument {
}

template <class F, class... Args>
auto action(F &&callable, Args &&...bound_args)
auto action(F &&callable, Args &&... bound_args)
-> std::enable_if_t<std::is_invocable_v<F, Args..., std::string const>,
Argument &> {
using action_type = std::conditional_t<
Expand Down Expand Up @@ -783,7 +783,7 @@ class Argument {
}

template <typename T, typename... U>
Argument &choices(T &&first, U &&...rest) {
Argument &choices(T &&first, U &&... rest) {
add_choice(std::forward<T>(first));
choices(std::forward<U>(rest)...);
return *this;
Expand Down Expand Up @@ -845,8 +845,14 @@ class Argument {
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;
}
}

Expand Down Expand Up @@ -1546,7 +1552,7 @@ class ArgumentParser {
// Parameter packed add_parents method
// Accepts a variadic number of ArgumentParser objects
template <typename... Targs>
ArgumentParser &add_parents(const Targs &...f_args) {
ArgumentParser &add_parents(const Targs &... f_args) {
for (const ArgumentParser &parent_parser : {std::ref(f_args)...}) {
for (const auto &argument : parent_parser.m_positional_arguments) {
auto it = m_positional_arguments.insert(
Expand Down
68 changes: 67 additions & 1 deletion test/test_choices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down Expand Up @@ -88,4 +154,4 @@ TEST_CASE("Parse multiple arguments that are not in fixed number of allowed "
program.parse_args({"test", "6", "7"}),
"Invalid argument \"6\" - allowed options: {1, 2, 3, 4, 5}",
std::runtime_error);
}
}
Loading