python argparse choices with a default choice
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
.
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 -husage: dotool.py [-h] {list,create} ...Digital Ocean toolpositional 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'}