如何在__doc__中定义CLI程序的帮助?

发布于 2024-08-11 14:40:10 字数 378 浏览 2 评论 0原文

我想做这样的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2024-08-18 14:40:10

因为 __doc__ 是函数的一个属性,而不是局部变量。您需要将其引用为 main.__doc__,如下所示:

def main():
    """Display Information about a Google Calendar

    ..."""
    print(main.__doc__)

if __name__ == "__main__":
    main()

Because __doc__ is an attribute of the function, not a local variable. You need to refer to it as main.__doc__ like this:

def main():
    """Display Information about a Google Calendar

    ..."""
    print(main.__doc__)

if __name__ == "__main__":
    main()
叫嚣ゝ 2024-08-18 14:40:10

如果您要打印的帮助是“全局”的,您可能会发现将其作为程序的主要文档更合乎逻辑:

#!/usr/bin/env python
"""
Display Information about a Google Calendar
...
"""

if __name__ == '__main__':
    print __doc__

__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:

#!/usr/bin/env python
"""
Display Information about a Google Calendar
...
"""

if __name__ == '__main__':
    print __doc__

__doc__ is a global variable that contains the documentation string of your script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文