Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Description

@rhettinger

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

from argparse import ArgumentParserp = 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 security fixes3.11only security fixes3.12only security 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp