我们说我有一个文件
myfile.py
"""
long documentation of myfile.py
...
"""
# tons of stuf
# this include some functions and their documentation
我想做
bash $ python myfile.py -h
这样它显示所有文档但不执行tons of stuff
。 (我不在乎选项是--help
而不是-h
。)
我很确定我已经在某个地方见过它,但我找不到它。它与this question和this second question有关吗?
答案 0 :(得分:4)
argparse
使用argparse
,您可以通过将__doc__
字符串传递给Argparse的描述参数
#!/usr/bin/env python
"""
long documentation of myfile.py
...
"""
# tons of stuf
# this include some functions and their documentation
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)
运行python文件
$ python myfile.py --help