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

不问归期 提交于 2019-12-03 01:32:10

So per the suggestion in the other question I cloned the docopt repo and installed the current tip with zero effect. Now that I had the source code though I decided to do some debugging and see if I could find the problem.

On line 200 in the parse method on the Option class is the regex used to grab default values:

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

After printing a bunch of the surrounding variables I found that the description vars value was an empty string. Here is the line that sets the description:

options, _, description = option_description.strip().partition(' ')

The part that caught my eye was this: .partition(' '), that's two spaces. So after updating my code successfully I head back to the docs and search for "spaces": https://github.com/docopt/docopt#option-descriptions-format sixth bullet:

"Use two spaces to separate options with their informal description"

TL;DR RTFM (or at least the code).

Bonus tip: docopt understands multi-line descriptions, so you can just wrap anything that crosses the 80 character line:

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]

Not quite as readable, but parses correctly.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!