python argparse choices with a default choice

I'm trying to use argparse in a Python 3 application where there's an explicit list of choices, but a default if none are specified.

The code I have is:

parser.add_argument('--list', default='all', choices=['servers', 'storage', 'all'], help='list servers, storage, or both (default: %(default)s)')
args = parser.parse_args()
print(vars(args))

However, when I run this I get the following with an option:

$ python3 ./myapp.py --list all
{'list': 'all'}

Or without an option:

$ python3 ./myapp.py --list
usage: myapp.py [-h] [--list {servers,storage,all}]
myapp.py: error: argument --list: expected one argument

Am I missing something here? Or can I not have a default with choices specified?

86236 次浏览

Pass the nargs and const arguments to add_argument:

parser.add_argument('--list',
default='all',
const='all',
nargs='?',
choices=['servers', 'storage', 'all'],
help='list servers, storage, or both (default: %(default)s)')

If you want to know if --list was passed without an argument, remove the const argument, and check if args.list is None.


Documention:

nargs with '?'

One argument will be consumed from the command line if possible, and produced as a single item. If no command-line argument is present, the value from default will be produced. Note that for optional arguments, there is an additional case - the option string is present but not followed by a command-line argument. In this case the value from const will be produced.

const

When add_argument() is called with option strings (like -f or --foo) and nargs='?'. This creates an optional argument that can be followed by zero or one command-line arguments. When parsing the command line, if the option string is encountered with no command-line argument following it, the value of const will be assumed instead. See the nargs description for examples.

Thanks @ShadowRanger. Subcommands is exactly what I need, combined with nargs and const. The following works:

parser = argparse.ArgumentParser()
subparser = parser.add_subparsers()
parser_list = subparser.add_parser('list')
parser_list.add_argument('list_type', default='all', const='all', nargs='?', choices=['all', 'servers', 'storage'])


parser_create = subparser.add_parser('create')
parser_create.add_argument('create_type', default='server', const='server', nargs='?', choices=['server', 'storage'])


args = parser.parse_args()
pprint(vars(args))


$ python3 ./myapp.py -h
usage: dotool.py [-h] {list,create} ...


Digital Ocean tool


positional arguments:
{list,create}


optional arguments:
-h, --help     show this help message and exit

list option alone:

$ python3 ./myapp.py list
{'list_type': 'all'}

List option with a parameter:

$ python3 ./myapp.py list servers
{'list_type': 'servers'}