p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.72k stars 250 forks source link

Closes #221 #301

Closed p-ranav closed 1 year ago

p-ranav commented 1 year ago

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:

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

with the following usage will yield an error:

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

The add_mutually_exclusive_group() function also accepts a required argument, to indicate that at least one of the mutually exclusive arguments is required:

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

with the following usage will yield an error:

foo@bar:/home/dev/$ ./main
One of the arguments '--first VAR' or '--second VAR' is required