Using python version 3.9.5 I have a problem with the following code snippet:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(required=False)
sub_parser = subparsers.add_parser("sub")
parser.add_argument("global-argument")
args = parser.parse_args()
Running the python example without arguments, results into the following error message:
$ python3 main.py
usage: main.py [-h] {sub} ... global-argument
main.py: error: the following arguments are required: global-argument
The error message shows that only the global-argument is required and the subcommand is optional.
But running the python example with the global argument, results into the following error message:
$ python3 main.py global
usage: main.py [-h] {sub} ... global-argument
main.py: error: invalid choice: 'global' (choose from 'sub')
So it's impossible to provide the global-argument without the subcommand (which should be optional)?