如何在__doc__中定义CLI程序的帮助?
我想做这样的事情:
def main():
"""
Display Information about a Google Calendar
-u --user login Google Login
-p --pass password Google Password
-d --debug Set DEBUG = True
-h --help Display this help
"""
print(__doc__)
if __name__ == "__main__":
main()
但答案是:无
...为什么?
I would like to do something like this :
def main():
"""
Display Information about a Google Calendar
-u --user login Google Login
-p --pass password Google Password
-d --debug Set DEBUG = True
-h --help Display this help
"""
print(__doc__)
if __name__ == "__main__":
main()
But the answer is : None
... Why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为
__doc__
是函数的一个属性,而不是局部变量。您需要将其引用为main.__doc__
,如下所示:Because
__doc__
is an attribute of the function, not a local variable. You need to refer to it asmain.__doc__
like this:如果您要打印的帮助是“全局”的,您可能会发现将其作为程序的主要文档更合乎逻辑:
__doc__
是一个全局变量,其中包含脚本的文档字符串。If the help that you want to print is "global", you might find it more logical to put it as the main documentation for your program:
__doc__
is a global variable that contains the documentation string of your script.