Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Argparse: Cryptic usage message when combining choices with type #132558

Open
@hansthen

Description

@hansthen

Bug report

Bug description:

Combining argparse type and choices arguments results in cryptic usage messages.

import argparse

parser = argparse.ArgumentParser()

day_names = ["mo", "tu", "we", "th", "fr", "sa", "su"]
days = [1, 2, 3, 4, 5, 6, 7]

def name_day(value):
    return day_names.index(value) + 1

parser.add_argument(
    '--day',
    type=name_day,
    choices=days,
)
parser.parse_args()

This will result in the following usage message:

usage: example.py [-h] [--day {1,2,3,4,5,6,7}]

This is strange, because the user is expected to enter the day name as a string.

See https://discuss.python.org/t/argparse-unable-to-combine-type-and-choices/88687 for discussion.

CPython versions tested on:

3.10, 3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions