chcę używać mój skrypt tak:argparse: Jak uczynić opcjonalne wykluczające się wzajemnie argumenty?
python test.py run
python test.py stop
i mój kod jest tak:
parser = argparse.ArgumentParser()
command_group = parser.add_mutually_exclusive_group(required=True)
command_group.add_argument('run', help='run it', action='store_true')
command_group.add_argument('stop', help='stop it', action='store_true')
kiedy go wykonać, wyjątek jest podniesione:
ValueError: mutually exclusive arguments must be optional
tak Próbuję dodać required=False
po dodaniu każdego argumentu. Następnie otrzymuję inny wyjątek:
TypeError: 'required' is an invalid argument for positionals
Jestem zdezorientowany.
Dlaczego warto używać grupy mutex zamiast ['choices'] (https://docs.python.org/3/library/argparse.html#choices)? – kennytm