如何打印Python文件';在执行它的时候是docstring吗?
我有一个带有docstring的Python脚本。当命令行参数解析失败时,我想打印docstring以获取用户信息 有没有办法做到这一点 最小示例如何打印Python文件';在执行它的时候是docstring吗?,python,docstring,Python,Docstring,我有一个带有docstring的Python脚本。当命令行参数解析失败时,我想打印docstring以获取用户信息 有没有办法做到这一点 最小示例 #/usr/bin/env python """ 用法:script.py 这描述了脚本。 """ 导入系统 如果len(系统argv)
#/usr/bin/env python
"""
用法:script.py
这描述了脚本。
"""
导入系统
如果len(系统argv)<2:
打印(“”)
文档字符串存储在模块的\uuuuuuuuuuu
全局文件中
print(__doc__)
顺便说一下,这适用于任何模块:
import sys;打印(系统文件)
。函数和类的docstring也在它们的\uuu doc\uuu
属性中。这里有一个替代方法,它不硬编码脚本的文件名,而是使用sys.argv[0]打印它。使用%(scriptName)s而不是%s可以提高代码的可读性
#!/usr/bin/env python
"""
Usage: %(scriptName)s
This describes the script.
"""
import sys
if len(sys.argv) < 2:
print __doc__ % {'scriptName' : sys.argv[0].split("/")[-1]}
sys.exit(0)
#/usr/bin/env python
"""
用法:%(脚本名)s
这描述了脚本。
"""
导入系统
如果len(系统argv)<2:
打印{'scriptName':sys.argv[0]。拆分(“/”[-1]}
系统出口(0)
参数解析应始终使用
您可以将\uuuu doc\uuuu
字符串传递给Argparse的说明
参数来显示该字符串:
#!/usr/bin/env python
"""
This describes the script.
"""
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
# Add your arguments here
parser.add_argument("-f", "--file", dest="myFilenameVariable",
required=True,
help="write report to FILE", metavar="FILE")
args = parser.parse_args()
print(args.myFilenameVariable)
#!/usr/bin/env python
"""
This summarizes the script.
Additional descriptive paragraph(s).
""" # Edited this docstring
if __name__ == '__main__':
from argparse import ArgumentParser, RawTextHelpFormatter # Edited this line
parser = ArgumentParser(description=__doc__
formatter_class=RawTextHelpFormatter) # Added this line
# Add your arguments here
parser.add_argument("-f", "--file", dest="myFilenameVariable",
required=True,
help="write report to FILE", metavar="FILE")
args = parser.parse_args()
print(args.myFilenameVariable)
如果调用此mysuperscript.py并执行它,您会得到:
$ ./mysuperscript.py --help
usage: mysuperscript.py [-h] -f FILE
This describes the script.
optional arguments:
-h, --help show this help message and exit
-f FILE, --file FILE write report to FILE
$ ./mysuperscript.py --help
usage: mysuperscript.py [-h] -f FILE
This summarizes the script.
Additional descriptive paragraph(s).
optional arguments:
-h, --help show this help message and exit
-f FILE, --file FILE write report to FILE
当
--help
是唯一的参数时,这将打印\uuuu文档
字符串
if __name__=='__main__':
if len(sys.argv)==2 and sys.argv[1]=='--help':
print(__doc__)
这两方面都适用:
/yourscriptname.py--help
python3 yourscriptname.py--help
参数解析应始终使用argparse完成 通过将文档字符串传递给描述,可以显示该字符串 Argparse的参数:
#!/usr/bin/env python
"""
This describes the script.
"""
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
# Add your arguments here
parser.add_argument("-f", "--file", dest="myFilenameVariable",
required=True,
help="write report to FILE", metavar="FILE")
args = parser.parse_args()
print(args.myFilenameVariable)
#!/usr/bin/env python
"""
This summarizes the script.
Additional descriptive paragraph(s).
""" # Edited this docstring
if __name__ == '__main__':
from argparse import ArgumentParser, RawTextHelpFormatter # Edited this line
parser = ArgumentParser(description=__doc__
formatter_class=RawTextHelpFormatter) # Added this line
# Add your arguments here
parser.add_argument("-f", "--file", dest="myFilenameVariable",
required=True,
help="write report to FILE", metavar="FILE")
args = parser.parse_args()
print(args.myFilenameVariable)
如果调用此mysuperscript.py并执行它,则会得到:
$ ./mysuperscript.py --help
usage: mysuperscript.py [-h] -f FILE
This describes the script.
optional arguments:
-h, --help show this help message and exit
-f FILE, --file FILE write report to FILE
$ ./mysuperscript.py --help
usage: mysuperscript.py [-h] -f FILE
This summarizes the script.
Additional descriptive paragraph(s).
optional arguments:
-h, --help show this help message and exit
-f FILE, --file FILE write report to FILE
如果不添加
formatter\u类
,输出将不会在docstring中有换行符。有用于cmdline参数解析的库:argparse(>=2.7)和optparse。我知道,但这与问题无关,谢谢。我通常有一个usage()函数,它使用sys.argv[0],该函数在打印docstring之前被调用。@wint3rschlaefer,您能解释一下usage:%(scriptName)s如何获取脚本名吗?这个机制在python中被称为什么?@wint3rschlaefer也许值得使用python3版本进行更新,比如“用法:{scriptName}”“。format(scriptName=sys.argv[0])
使用_name如何?这当然有效,但还有另一种方式可以显示更自然的模块帮助界面:帮助(模块名称)
导入该模块后。@danbgray我想您得到的是argparse的用途