为什么默认值没有出现在docopt的命令行参数字典中?

blz*_*blz 5 python docopt

我一直在尝试使用docopt来创建一个简单的CLI,但由于某种原因,我的默认参数没有出现.下面是我的测试代码.我正在使用docopt.pygithub存储库的最新版本.

"""
Usage:  scrappy <path> ... [options]

-a --auto           Automatically scrape and rename without user interaction.
-l --lang           Specify language code [default: en].
--scan-individual   Evaluate series information individually for each file.
-c --cfg            User alternate config file [default: ../scrappy.conf]
-t --test           Test run.  Do not modify files.
-v --verbose        Print verbose output
"""

from docopt import docopt
arguments = docopt(__doc__, version='0.1.0 alpha')
print arguments  # DEBUG
Run Code Online (Sandbox Code Playgroud)

这是我跑步时的输出 $ scrappy/scrappy.py first_path_parameter second/path/parameter

{'--auto': None,
 '--cfg': None,
 '--lang': None,
 '--scan-individual': None,
 '--test': None,
 '--verbose': None,
 '<path>': ['first_path_parameter', 'second/path/parameter']}
Run Code Online (Sandbox Code Playgroud)

谁知道发生了什么事?

编辑:

我更新了我的代码,但我仍然得到类似的输出.更重要的是,当我试图通过时--scan-individual,我得到一个错误,根据它需要一个参数.再次,如果它很重要,我正在运行docopt,只需将docopt.py复制到我项目的工作目录中.这里发生了什么?

#!/usr/bin/env python

"""Scrappy:  Rename media files based on scraped information.

Usage:  scrappy <path> ... [options]

-a --auto               Automatically scrape and rename without user interaction.
-l LANG --lang LANG     Specify language code [default: en].
--scan-individual       Evaluate series information individually for each file.
-c CONF --cfg CONF      User alternate config file [default: ../scrappy.conf]
-t --test               Test run.  Do not modify files.
-v --verbose            Print verbose output
"""

from docopt import docopt
arguments = docopt(__doc__, version='0.1.0 alpha')
print arguments  # DEBUG
Run Code Online (Sandbox Code Playgroud)

输出:

$ scrappy/scrappy.py first_path_parameter second/path/parameter --scan-individual
--scan-individual requires argument
Usage:  scrappy <path> ... [options]
Run Code Online (Sandbox Code Playgroud)

DSM*_*DSM 5

通过查看示例,似乎如果要传递默认值,则可能必须指定目标变量,例如

naval_fate.py:  --speed=<kn>  Speed in knots [default: 10].
options_example.py-  --exclude=PATTERNS   exclude files or directories which match these comma
options_example.py:                       separated patterns [default: .svn,CVS,.bzr,.hg,.git]
options_example.py-  -f NAME --file=NAME  when parsing directories, only check filenames matching
options_example.py:                       these comma separated patterns [default: *.py]
Run Code Online (Sandbox Code Playgroud)

所以在你的情况下,

-l LANG --lang LANG  Specify language code [default: en].
-c CONFIG            User alternate config file [default: ../scrappy.conf]
Run Code Online (Sandbox Code Playgroud)

产生

localhost-2:coding $ python doc.py --auto a b c
{'--auto': True,
 '--lang': 'en',
 '--scan-individual': False,
 '--test': False,
 '--verbose': False,
 '-c': '../scrappy.conf',
 '<path>': ['a', 'b', 'c']}
Run Code Online (Sandbox Code Playgroud)

编辑:OP发布的更新代码对我来说很合适,我在半小时前下载的github版本:

localhost-2:coding $ ./scrappy.py first_path_parameter second/path/parameter --scan-individual
{'--auto': False,
 '--cfg': '../scrappy.conf',
 '--lang': 'en',
 '--scan-individual': True,
 '--test': False,
 '--verbose': False,
 '<path>': ['first_path_parameter', 'second/path/parameter']}
Run Code Online (Sandbox Code Playgroud)

所以我很茫然.

  • 哦...sheesh...那应该是显而易见的!谢谢! (2认同)