88

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?

3
  • 6
    Don't pass --list at all and your default will be used. Oct 29, 2016 at 22:40
  • I see... That'd work except for the fact that I need to tell it to list, create, delete, etc.
    – MVanOrder
    Oct 29, 2016 at 22:43
  • 4
    Sounds like you want subcommands, not a switch at all. Even with nargs='?' you can't tell the difference between --list and not passing anything when there is a default set. Oct 29, 2016 at 23:04

2 Answers 2

120

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.

1
16

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'}
1
  • 2
    Can we add help description for positional arguments? such as for {list,create}
    – alper
    Feb 8, 2022 at 14:39

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.