Argparse: Cryptic usage message when combining `choices` with `type` · Issue #132558 · python/cpython · GitHub
Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions