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

前端 未结 1 617
小蘑菇
小蘑菇 2021-02-05 07:54

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 s

相关标签:
1条回答
  • 2021-02-05 08:29

    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.

    0 讨论(0)
提交回复
热议问题