Skip to content

Commit

Permalink
Added logic and unit tests for the required flag in mutex_args
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ranav committed Nov 4, 2023
1 parent 7bbde0d commit de42394
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [Deciding if the value was given by the user](#deciding-if-the-value-was-given-by-the-user)
* [Joining values of repeated optional arguments](#joining-values-of-repeated-optional-arguments)
* [Repeating an argument to increase a value](#repeating-an-argument-to-increase-a-value)
* [Mutually Exclusive Group](#mutually-exclusive-group)
* [Negative Numbers](#negative-numbers)
* [Combining Positional and Optional Arguments](#combining-positional-and-optional-arguments)
* [Printing Help](#printing-help)
Expand Down Expand Up @@ -280,6 +281,23 @@ program.parse_args(argc, argv); // Example: ./main -VVVV
std::cout << "verbose level: " << verbosity << std::endl; // verbose level: 4
```
#### Mutually Exclusive Group
Create a mutually exclusive group using `program.add_mutually_exclusive_group(required = false)`. `argparse`` will make sure that only one of the arguments in the mutually exclusive group was present on the command line:
```cpp
auto &group = program.add_mutually_exclusive_group();
group.add_argument("--first");
group.add_argument("--second");
```

with the following usage will yield an error:

```console
foo@bar:/home/dev/$ ./main --first 1 --second 2
Argument '--second VAR' not allowed with '--first VAR'
```

### Negative Numbers

Optional arguments start with ```-```. Can ```argparse``` handle negative numbers? The answer is yes!
Expand Down
19 changes: 19 additions & 0 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,25 @@ class ArgumentParser {
mutex_argument_it->get_usage_full() + "'");
}
}

if (!mutex_argument_used && group.m_required) {
// at least one argument from the group is
// required
std::string argument_names{};
std::size_t i = 0;
std::size_t size = group.m_elements.size();
for (Argument *arg : group.m_elements) {
if (i + 1 == size) {
// last
argument_names += "'" + arg->get_usage_full() + "' ";
} else {
argument_names += "'" + arg->get_usage_full() + "' or ";
}
i += 1;
}
throw std::runtime_error("One of the arguments " + argument_names +
"is required");
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/test_mutually_exclusive_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,37 @@ TEST_CASE("Create mutually exclusive group with 2 arguments" *
std::runtime_error);
}

TEST_CASE(
"Create mutually exclusive group with 2 arguments with required flag" *
test_suite("mutex_args")) {
argparse::ArgumentParser program("test");

auto &group = program.add_mutually_exclusive_group(true);
group.add_argument("--first");
group.add_argument("--second");

REQUIRE_THROWS_WITH_AS(
program.parse_args({"test"}),
"One of the arguments '--first VAR' or '--second VAR' is required",
std::runtime_error);
}

TEST_CASE(
"Create mutually exclusive group with 3 arguments with required flag" *
test_suite("mutex_args")) {
argparse::ArgumentParser program("test");

auto &group = program.add_mutually_exclusive_group(true);
group.add_argument("--first");
group.add_argument("--second");
group.add_argument("--third");

REQUIRE_THROWS_WITH_AS(program.parse_args({"test"}),
"One of the arguments '--first VAR' or '--second VAR' "
"or '--third VAR' is required",
std::runtime_error);
}

TEST_CASE(
"Create mutually exclusive group with 2 arguments, then copy the parser" *
test_suite("mutex_args")) {
Expand Down

0 comments on commit de42394

Please sign in to comment.