如何在使用Python';s光传送模块?
根据命令行脚本的docstring应该是它的用法消息 脚本(a)的文档字符串 独立程序)应可用 作为其“用法”信息,在 使用不正确的命令调用脚本 或缺少论点(或可能与 一个“-h”选项,表示“帮助”)。这样的 docstring应该记录脚本的 函数和命令行语法, 环境变量和文件。 使用信息可以相当详细 (多个屏幕已满)并应 足以让新用户使用 正确指挥,以及 完整快速参考所有 项目的选项和参数 老练的用户 所以我的文档字符串看起来像这样: <tool name> <copyright info> Usage: <prog name> [options] [args] some text explaining the usage... Options: -h, --help show this help message and exit ... 因此,使用“-h”标志调用脚本将打印: Usage: script.py [options] Options: -h, --help show this help message and exit 导致 Usage: script.py [options] [args] some text explaining the usage... Options: -h, --help show this help message and exit 用法:script.py[options][args] 一些解释用法的文字。。。 选项: -h、 --帮助显示此帮助消息并退出 但是我如何使用这里的文档字符串呢?将docstring作为用法消息传递有两个问题如何在使用Python';s光传送模块?,python,optparse,Python,Optparse,根据命令行脚本的docstring应该是它的用法消息 脚本(a)的文档字符串 独立程序)应可用 作为其“用法”信息,在 使用不正确的命令调用脚本 或缺少论点(或可能与 一个“-h”选项,表示“帮助”)。这样的 docstring应该记录脚本的 函数和命令行语法, 环境变量和文件。 使用信息可以相当详细 (多个屏幕已满)并应 足以让新用户使用 正确指挥,以及 完整快速参考所有 项目的选项和参数 老练的用户 所以我的文档字符串看起来像这样: <tool name> <copyrig
“用法:”部分由IndentedHelpFormatter引入,它可以替换为OptionParser中的formatter参数。\uuu init\uuu()。选项1:复制和粘贴。不干燥,但可行 选择2:解析您自己的docstring以去掉描述段落。它总是第二段,所以您可以在“\n\n”上拆分
usage, description= __doc__.split('\n\n')[:2]
由于
optpasse
生成用法,您可能不想向其提供用法语句。你对我的用法的理解可能是错误的。如果您坚持要向optpasse
提供用法字符串,我将把它作为练习留给读者,让他们研究如何删除“用法:”
从上面生成的用法
字符串的前面开始。我认为我们必须合理对待这个政治公众人物的建议——我认为让模块保留\uuuu doc\uuuu
作为总结长期用法的简短描述是很好的。但如果你是完美主义者:
'''<tool name>
The full description and usage can be generated by optparse module.
Description: ...
'''
...
# Generate usage and options using optparse.
usage, options = ...
# Modify the docstring on the fly.
docstring = __doc__.split('\n\n')
docstring[1:2] = [__license__, usage, options]
__doc__ = '\n\n'.join(docstring)
“”
OPTPASE模块可以生成完整的说明和用法。
描述:。。。
'''
...
#使用optpass生成用法和选项。
用法,选项=。。。
#动态修改文档字符串。
docstring=\uuuuu doc\uuuu.split('\n\n')
docstring[1:2]=[[许可证,用法,选项]
__doc\u='\n\n'.连接(docstring)
我写了一个模块docopt
来做你想做的事情——用docstring写用法信息,保持干燥。
它还允许避免编写冗长的OptionParser
代码,因为docopt
正在生成解析器
基于使用信息
请查看:
海军的命运。
用法:
海军命运号新。。。
海军命运号[]移动[--速度=]
海军舰艇射击
海军水雷(设置|移除)[--系泊|--漂移]
海军_fate.py-h |——救命
navalu_fate.py--版本
选项:
-h--帮助显示此屏幕。
--版本显示版本。
--速度=以节为单位的速度[默认值:10]。
--停泊(锚定)的矿井。
--漂流矿。
"""
从docopt导入docopt
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
arguments=docopt(\uuuu doc\uuuu,version='2.0')
打印(参数)
我喜欢第二种解决方案。不太干净,但聪明实用。由于段落之间的空行是RST标准,这节省了对文档运行完整的Docutils解析,并获得了(根据定义)预期的结果。这可能是描述的一个选项。但我仍然不能重用“用法”部分,optpass强制消息以“用法:”开头。
usage, description= __doc__.split('\n\n')[:2]
'''<tool name>
The full description and usage can be generated by optparse module.
Description: ...
'''
...
# Generate usage and options using optparse.
usage, options = ...
# Modify the docstring on the fly.
docstring = __doc__.split('\n\n')
docstring[1:2] = [__license__, usage, options]
__doc__ = '\n\n'.join(docstring)
"""Naval Fate.
Usage:
naval_fate.py ship new <name>...
naval_fate.py ship [<name>] move <x> <y> [--speed=<kn>]
naval_fate.py ship shoot <x> <y>
naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
naval_fate.py -h | --help
naval_fate.py --version
Options:
-h --help Show this screen.
--version Show version.
--speed=<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
print(arguments)