I have the following code:
import argparse
parser = argparse.ArgumentParser(description='do stuff')
TOP = parser.add_mutually_exclusive_group(required=True)
#
TOP.add_argument('--interactive', action="store_true")
noninteractive = TOP.add_argument_group()
##
noninteractive.add_argument('--required1', required=True, type=float)
noninteractive.add_argument('--required2', required=True, type=float)
##
noninteractive_excl = noninteractive.add_mutually_exclusive_group(required=True)
noninteractive_excl.add_argument('--mutuallyexcl1', default=None, type=float)
noninteractive_excl.add_argument('--mutuallyexcl2', default=None, type=float)
args = parser.parse_args()
My program should work like this:
You can either call it with the option --interactive
(which then will ask the user to enter the data step by step) or a group of arguments containing all necessary options for running the program. The group and --interactive
shall be mutually exclusive.
The group then has two options which are required and two options that are mutually exclusive again.
My expectation was, that, when called with --interactive
, the program would not ask for any other arguments since they are in a group which is part of the first mutually exclusive group.
Instead, when I call the program with
python dostuff.py --interactive
I get the following message:
dostuff.py: error: the following arguments are required: --required1, --required2
What did I do wrong and how can I fix this? Many thanks in advance.