krishnab
krishnab

Reputation: 10060

docopt on python 3 only prints help screen and does not execute the function

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.

Upvotes: 3

Views: 1924

Answers (1)

krishnab
krishnab

Reputation: 10060

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.

Upvotes: 10

Related Questions