Why doesn't my docopt option have its default

2019-03-14 12:46发布

I'm using docopt in an example for a module I'm working on, and all the option default values are working except one. I've modified all the code containing and surrounding the option trying to identify the problem, but it won't take a default value!

My options block looks like this:

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]

The girders option never has a default value!

I re-read this question several times, but it seems unrelated.

标签: python docopt
1条回答
淡お忘
2楼-- · 2019-03-14 12:50

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.

查看更多
登录 后发表回答