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()
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.
Bug report
Bug description:
Combining
argparsetypeandchoicesarguments results in cryptic usage messages.This will result in the following usage message:
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
choicesto be specified as strings in presence of atypeargument #132743convert_choicesparameter toadd_argument#133826argparsedocs on combiningtypeandchoices#133827argparsedocs on combiningtypeandchoices(GH-133827) #139057argparsedocs on combiningtypeandchoices(GH-133827) #139058