Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 896
kitsu.eb Avatar asked May 31 '13 18:05

kitsu.eb


Video Answer


1 Answers

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.

like image 159
kitsu.eb Avatar answered Oct 05 '22 09:10

kitsu.eb