'Can argparse do mutual exclusion of groups instead of just individual options?
I would like to build an ArgumentParser object with two groups of arguments that are mutually exclusive to one another, like so
usage: obtain_pizza [-h] --topping TOPPING ( --pizzeria PIZZERIA --budget BUDGET | --check-fridge-only )
I attempted to do this by just adding a group inside a mutual exclusion group, like so:
parser = argparse.ArgumentParser()
parser.add_argument('--topping', nargs=1, required=True)
mutually_exclusive_group = parser.add_mutually_exclusive_group(required = True)
purchase_group = mutually_exclusive_group.add_argument_group('Purchase options')
purchase_group.add_argument('--pizzeria', nargs=1)
purchase_group.add_argument('--budget', nargs=1)
mutually_exclusive_group.add_argument('--check_fridge_only', action='store_true')
However this results in the program apparently behaving as though purchase_group were at top level:
usage: obtain_pizza.py [-h] --topping TOPPING
[--pizzeria PIZZERIA] [--budget BUDGET]
--check_fridge_only
(line broken up for readability)
Note: I see a similar question at Set up mutually exclusive sets in argparse but it's nine years old and doesn't have an answer other than using third-party packages. My question is about doing this using argparse specifically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
