如何在__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()
但是得到的结果是:None
... 这是为什么呢?
2 个回答
4
因为 __doc__
是一个函数的 属性,而不是一个局部变量。所以你需要像这样来引用它: main.__doc__
:
def main():
"""Display Information about a Google Calendar
..."""
print(main.__doc__)
if __name__ == "__main__":
main()
2
如果你想打印的帮助信息是“全局”的,那么把它放在你程序的主要文档里可能会更合理:
#!/usr/bin/env python
"""
Display Information about a Google Calendar
...
"""
if __name__ == '__main__':
print __doc__
__doc__
是一个全局变量,它包含了你脚本的文档字符串。