3

I am using docopt in Python 3 for the first time. When I run the code, the output only displays the Usage option data, and does not execute the function in the module.

Here is an example. I have this code in a file.

"""Usage:  scratch.py [-h] [--boston | --sandiego] [--prostitution |
        --drugs] [--lim VALUE]
    Options:
        -h --help        :Show help information
        --boston         : work on boston addresses
        --sandiego       : work on san diego addresses
        --prostitution  : prostitution incidents
        --drugs         : drug sale incidents
        --lim          : number of addresses to work with
"""


from docopt import docopt

def scratch(args):
    print(args['--boston'])
    print('hello')

if __name__ == '__main__':

    arg = docopt(__doc__, help=False)
    print(arg)
    scratch(arg)

When I run this file with various options, all that I get is just a replica of the docstring. I should see a copy of the dictionary that docopt creates from the docstring arguments. Then I should also see the print results from the function call. But I don't see anything but the docstring.

So if I make a command line call like:

python scratch.py --boston --drugs --lim 100

All that is returned is the following.

Usage:  scratch.py <city> [--boston | --sandiego] <crime> [--prostitution |
        --drugs] --count=N
    Options:
        city            : city to geocode
        --boston         : work on boston addresses
        --sandiego       : work on san diego addresses
        crime           : crime to select on
        --prostitution  : prostitution incidents
        --drugs         : drug sale incidents
        -count          : number of addresses to work with

I want to know what is wrong so that I can actually get the defined function to run.

krishnab
  • 9,270
  • 12
  • 66
  • 123
  • You never actually *call* the defined function – jonrsharpe Aug 09 '15 at 08:42
  • Sorry, I was cutting and pasting. Even when I call the function, it still does not display the data in the function. Even without the function call, it should still display the dictionary that `docopt` creates from the different arguments. – krishnab Aug 09 '15 at 09:42
  • Could you show some example calls? – jonrsharpe Aug 09 '15 at 09:56
  • So I added an example of a call from the command line. No matter what options I give the command line call, the output is always the same. – krishnab Aug 09 '15 at 10:19
  • 1
    [adding newline before `Options` helps](http://try.docopt.org/?doc=Usage%3A++scratch.py+%5B-h%5D+%5B--boston+%7C+--sandiego%5D+%5B--prostitution+%7C%0D%0A++++++++--drugs%5D+%5B--lim+VALUE%5D%0D%0A%0D%0A++++Options%3A%0D%0A++++++++-h+--help++++++++%3AShow+help+information%0D%0A++++++++--boston+++++++++%3A+work+on+boston+addresses%0D%0A++++++++--sandiego+++++++%3A+work+on+san+diego+addresses%0D%0A++++++++--prostitution++%3A+prostitution+incidents%0D%0A++++++++--drugs+++++++++%3A+drug+sale+incidents%0D%0A++++++++--lim++++++++++%3A+number+of+addresses+to+work+with&argv=--boston+--drugs+--lim+100) – jfs Aug 09 '15 at 14:28
  • Haha, that totally solved it. Man, I read all sorts of documentation and stuff and never saw that. Thanks so much. – krishnab Aug 09 '15 at 16:36

1 Answers1

10

This credit for this answer belongs to J.F. Sebastian, above; I just wanted to make sure that I closed out this question.

There needs to be an empty line between the Usage: and the Options: sections of the docstring, otherwise docopt will give you results like my problem above. This requirement is mentioned in the documentation, but it might be hard to catch (emphasis mine):

Usage pattern format

Usage pattern is a substring of doc that starts with usage: (case insensitive) and ends with a visibly empty line.

Community
  • 1
  • 1
krishnab
  • 9,270
  • 12
  • 66
  • 123