在示例中,我正在为正在处理的模块使用docopt,除一个选项外,所有其他选项默认值均有效。我已经修改了所有包含该选项并包含该选项的代码,以试图识别问题,但不会采用默认值!

我的选项块如下所示:

Options:
  --help                       Show this message and exit
  --version                    Show version info and exit
  -w WIDTH --width=WIDTH       The out to out width of the deck (feet) [default: 73]
  -g GIRDERS --girders=GIRDERS The number of girders [default: 8]
  -h HEIGHT --height=HEIGHT    The height of the girders (inches) [default: 56]
  -t THICK --thick=THICK       The deck thickness (inches) [default: 8]
  -a ADIM --adim=ADIM          The "A" dimension, max deck thick @ CL girder [default: 12]
  -l LSLP --leftslope=LSLP     The left-hand deck slope (ft/ft) [default: -0.02]
  -r RSLP --rightslope=RSLP    The right-hand deck slope (ft/ft) [default: -0.02]
  -c --center                  Indicates pivot point is at center of bridge
  -o OFFSET --offset=OFFSET    The offset of pivot point from center [default: 0]
girders选项永远不会具有默认值!

我重读了this question几次,但这似乎无关。

最佳答案

因此,根据另一个问题的建议,我克隆了docopt存储库,并以零影响安装了当前提示。现在我有了源代码,尽管我决定进行一些调试,看看是否可以找到问题。

在Option类的parse方法中的第200行是用于获取默认值的正则表达式:
matched = re.findall('\[default: (.*)\]', description, flags=re.I)
在打印了一堆周围的变量后,我发现description vars值是一个空字符串。这是设置说明的行:
options, _, description = option_description.strip().partition(' ')
引起我注意的部分是:.partition(' '),这是两个空格。因此,在成功更新代码后,我返回文档并搜索“空格”:https://github.com/docopt/docopt#option-descriptions-format第六项:

“使用两个空格将选项及其非正式描述分开”

TL; DR RTFM (或至少是代码)。

奖励提示:docopt可以理解多行描述,因此您可以包装所有超过80个字符的行:

Options:
  --help                        Show this message and exit
  --version                     Show version info and exit
  -w WIDTH --width=WIDTH        The out to out width of the deck (feet)
                                [default: 73]
  -g GIRDERS --girders=GIRDERS  The number of girders [default: 8]
  -h HEIGHT --height=HEIGHT     The height of the girders (inches)
                                [default: 56]
  -t THICK --thick=THICK        The deck thickness (inches) [default: 8]
  -a ADIM --adim=ADIM           The "A" dimension, max. deck thickness at
                                centerline of girder (inches) [default: 12]
  -l LSLP --leftslope=LSLP      The left-hand deck slope (ft/ft)
                                [default: -0.02]
  -r RSLP --rightslope=RSLP     The right-hand deck slope (ft/ft)
                                [default: -0.02]
  -c --center                   Indicates pivot point is at center of bridge
  -o OFFSET --offset=OFFSET     The offset of pivot point from center
                                [default: 0]

可读性不强,但可以正确解析。

10-06 07:15
查看更多