繁体   English   中英

为什么我的docopt选项没有默认值?

[英]Why doesn't my docopt option have its default value?

我在一个示例中使用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选项永远不会有默认值!

我多次重读这个问题 ,但似乎无关紧要。

因此,根据另一个问题的建议,我克隆了docopt repo并安装了当前提示,但效果为零。 现在我有了源代码,虽然我决定做一些调试,看看能不能找到问题。

在Option类的parse方法中的第200行是用于获取默认值的正则表达式:

matched = re.findall('\\[default: (.*)\\]', description, flags=re.I)

打印出一堆周围的变量后,我发现description变量值是一个空字符串。 以下是设置描述的行:

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]

不太可读,但正确解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM