When I use the subparsers, the subparers are optional parameters, and I have to choose one of them. Now, I want to implement the ability to pass in a default option when an option parameter that is not defined in the subparse is passed in, for example, theadd_codition
.
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('add_condition', nargs='*')
subparsers = parser.add_subparsers(help='sub-command help', dest="character")
subparsers.required = False
# create the parser for the "a" command
parser_a = subparsers.add_parser('a', help='a help')
parser_a.add_argument('--bar', choices='ABC', help='bar help')
# create the parser for the "b" command
parser_b = subparsers.add_parser('b', help='b help')
parser_b.add_argument('--baz', choices='XYZ', help='baz help')
However, when I pass in options that a subparser and b parser don't have, e.g. key=value
. The key=value
should theoretically be passed in add_codition
.
The program result always prompts: error: argument character: invalid choice: 'key' (choose from 'a', 'b')
The python version is 3.6.9.