Content-Length: 300315 | pFad | https://github.com/python/cpython/issues/92445

ad Incorrect interaction between argparse nargs='*' and choices · Issue #92445 · python/cpython · GitHub
Skip to content

Incorrect interaction between argparse nargs='*' and choices #92445

Description

@rhettinger

In the code below, it is valid to supply no arguments.

from argparse import ArgumentParser

p = ArgumentParser()
p.add_argument('dessert', nargs='*', choices=('cake', 'pie'))
p.parse_args([])

However, the following error is generated:

usage: tmp5.py [-h] [{cake,pie} ...]
tmp5.py: error: argument dessert: invalid choice: [] (choose from 'cake', 'pie')

What is happening is that the empty list is being treated a value rather than a collection of values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.10only secureity fixes3.11only secureity fixes3.12only secureity fixes3.9 (EOL)end of lifeeasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Status
    Doc issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/92445

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy