How can I print a Python file's docstring when executing it?

后端 未结 4 964
南笙
南笙 2020-12-29 01:49

I have a Python script with a docstring. When the parsing of the command-line arguments does not succeed, I want to print the docstring for the user\'s information.

4条回答
  •  Happy的楠姐
    2020-12-29 02:45

    This will print the __doc__ string when --help is the only argument.

    if __name__=='__main__':
     if len(sys.argv)==2 and sys.argv[1]=='--help':
        print(__doc__)
    

    Works for both:

    • ./yourscriptname.py --help
    • python3 yourscriptname.py --help

提交回复
热议问题