在使用 Python 的 optparse 时,如何从帮助中去除一个选项?

4

请考虑以下内容:

parser.add_option("-f", "--file", "--secret", action = "append", type = "string", dest = "filename", default = [], help = "specify the files")

当用户调用帮助时,我希望隐藏--secret选项。我能通过以下方式做到吗?

parser.add_option("-f", "--file", action = "append", type = "string", dest = "filename", default = [], help = "specify the files")
parser.add_option("--secret", action = "append", type = "string", dest = "filename", default = [], help = "specify the files")

我这样做会有什么隐藏问题吗?如果有,有人能建议另一种实现方式吗?

为什么你想要那样做? - shx2
你问能不能做到这件事 - 你试过了吗,看看会发生什么? - Bryan Oakley
1个回答

5
尝试使用help=SUPPRESS_HELP技巧(见文档):
from optparse import OptionParser, SUPPRESS_HELP

parser.add_option("-f", "--file", action = "append", type = "string", dest = "filename", default = [], help = "specify the files")
parser.add_option("--secret", action = "append", type = "string", dest = "filename", default = [], help=SUPPRESS_HELP)

谢谢!我本来只想在我的问题中输入这个内容,但我错误地将 --secret 选项的帮助信息也输入了。我已经尝试过它并确实看到 secret 被隐藏了,但我想知道是否可以将同一个变量分配给两个选项的 dest。 - user2242512

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接