在 -h 中显示选项的选择。在带有 argparse 的 python 中使用可选的命令行参数时

Show choices of option in -h. When using and optional command line argument in python with argparse

所以我想用一个可选参数启动我的程序,该参数具有预定义的选项,选项显示在 -h 屏幕中。以便用户知道可以选择哪个选项。

parser = argparse.ArgumentParser(description="This is a program which creates a prediction model for the boston housing dataset.")

parser.add_argument("Model", help="Choose which model you want to use for prediction.",
type=str, choices=["linear_regression", "polynomial_regression"])

parser.add_argument("--fd", metavar="FEEDBACK", help="Set how much feedback you want.",
type=str, choices=["full", "immediate", "weak"])

args = parser.parse_args()

问题是当我使用 -h 启动程序时,我没有看到 --fd 的可用选项。我怎样才能改变它,以便用户可以看到 --fd 的可用选项,就像图片中显示的模型选项一样?

h4z3 评论中所述。你需要离开 metavar
如评论中所述 hpaulj 您也可以在帮助行中执行 %(choices)。但对我来说,我需要将 %(default) 排除在外。似乎你只能在辅助线中使用其中之一。