Natim
Natim

Reputation: 18122

How to define the help of the CLI program in the __doc__?

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 ?

Upvotes: 0

Views: 980

Answers (2)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94575

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.

Upvotes: 2

intgr
intgr

Reputation: 20486

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()

Upvotes: 4

Related Questions