我在用于配置Docopt的docstring中有一些详细的选项规范。有些项目非常冗长。有没有办法包装文字,使其更容易辨认,或更容易使其适合线宽?
假设docstring中相关的文本位如下:
Usage:
program [options]
Options:
-h, --help Show this help message.
-c, --configuration=CONF Configuration (file) [default: None]
-f, --files=FILESLIST Comma-delimited list of input data files [default: 169888_ttH_el.root]
-v, --variables=VARIABLESLIST Comma-delimited list of variables to plot [default: trk_pt]
-t, --tree=TREE Tree in input data files [default: mini]
-u, --username=USERNAME Username
-t, --topanalysis=DIRECTORY Directory of TopRootCore or TopAnalysis [default: /home/user/Dropbox/TopAnalysis]
-s, --superlongoption=TEST This is a very long option that requires a bit of text to explain it. [default: 101001011011101010010100110101010]
--version Show the version and exit.
是否可以将文本换成如下所示的样式?
Usage:
program [options]
Options:
-h, --help Show this help message.
-c, --configuration=CONF Configuration (file) [default: None]
-f, --files=FILESLIST Comma-delimited list of input data files
[default: 169888_ttH_el.root]
-v, --variables=VARIABLESLIST Comma-delimited list of variables to plot
[default: trk_pt]
-t, --tree=TREE Tree in input data files [default: mini]
-u, --username=USERNAME Username
-t, --topanalysis=DIRECTORY Directory of TopRootCore or TopAnalysis
[default: /home/user/Dropbox/TopAnalysis]
-s, --superlongoption=TEST This is a very long option that requires a
bit of text to explain it.
[default: 101001011011101010010100110101010]
--version Show the version and exit.
答案 0 :(得分:1)
"秘密"是:
-
或--
开始(忽略空格)。有一些东西可以帮助您使用更长的选项描述或选项定义。
Option:
没有任何实际意义(无论是经常在docopt
的教程中使用)。选项被识别为以-
或--
开头的任何行(忽略初始空格)。这允许将选项分成组。以下是您示例中重新组织的 doc 字符串的示例。
做了什么:
最终代码如下:
"""
Usage:
program [options]
General options:
These things are rather general, so placed in this group of option.
-h, --help Show this help message.
--version Show the version and exit.
-c, --configuration=CONF
Configuration (file) [default: None]
Directory and path related stuff:
Whatever relates to file or directory, comes here.
-f, --files=FILESLIST
Comma-delimited list of input data files
[default: 169888_ttH_el.root]
-t, --tree=TREE Tree in input data files [default: mini]
-t, --topanalysis=DIRECTORY
Directory of TopRootCore or TopAnalysis
[default: /home/user/Dropbox/TopAnalysis]
Other things:
Remaining options live here.
-v, --variables=VARIABLESLIST
Comma-delimited list of variables to plot
[default: trk_pt]
-u, --username=USERNAME
Username
-s, --superlongoption=TEST
This is a very long option that requires a bit of text
to explain it.
[default: 101001011011101010010100110101010]
"""
if __name__ == "__main__":
from docopt import docopt
args = docopt(__doc__)
print args