How to use docopt to parse command-line options

chriswelborn 1 Tallied Votes 3K Views Share

I just wanted to show the basic usage of docopt, a module that makes parsing command-line arguments so much easier. I'm not affiliated with the creators of docopt, but I'm certainly thankful for their work and happy to post this little snippet. I will point you to docopt.org for more information because there is so much more you can do with this module, that I'm not showing you here. Watch the video, it was an eye-opener for me. Also, if you'd like to install docopt you can go to their github repo to download and install it.

Try running this script in different ways to see the results, any invalid arguments will kick the user out to a Usage message (which also happens to be the way docopt recognizes valid or invalid arguments, awesome). The -h and --help flags are automagically recognized and used, so no code for them needs to be written:

./docopt_example.py -h
./docopt_example.py myrequiredargument
./docopt_example.py myrequiredargument -f
./docopt_example.py myrepeatingarg1 myrepeatingarg2 myrepeatingarg3
#!/usr/bin/env python
# -*- coding: utf-8 -*-

""" docopt_example.py
    
    Usage:
        docopt_example.py -h
        docopt_example.py <required> [-f | -g | -o ]
        docopt_example.py <repeating>...

    Options:
        -h,--help       : show this help message
        required        : example of a required argument
        repeating       : example of repeating arguments
        -f,--flag       : example flag #1
        -g,--greatflag  : example of flag #2
        -o,--otherflag  : example of flag #3
"""
# the above is our usage string that docopt will read and use to determine
# whether or not the user has passed valid arguments.

# import the docopt function from the docopt module
from docopt import docopt

def main(docopt_args):
    """ main-entry point for program, expects dict with arguments from docopt() """
    
    # Notice, no checking for -h, or --help is written here.
    # docopt will automagically check for it and use your usage string.
    
    # User passed the required argument
    if docopt_args["<required>"]:
        print "You have used the required argument: " + docopt_args["<required>"]
        
        # Get flags used
        if docopt_args["--flag"]:
            print "   with --flag\n"
        elif docopt_args["--greatflag"]:
            print "   with --greatflag\n"
        elif docopt_args["--otherflag"]:
            print "   with --otherflag\n"
        else:
            print "   with no flags.\n"

    # User passed 1 or more repeating arguments
    elif docopt_args["<repeating>"]:
        print "You have used the repeating args:"
        print '   ' + '\n   '.join(docopt_args["<repeating>"]) + '\n'

# START OF SCRIPT
if __name__ == "__main__":
    # Docopt will check all arguments, and exit with the Usage string if they
    # don't pass.
    # If you simply want to pass your own modules documentation then use __doc__,
    # otherwise, you would pass another docopt-friendly usage string here.
    # You could also pass your own arguments instead of sys.argv with: docopt(__doc__, argv=[your, args])
    args = docopt(__doc__)

    # We have valid args, so run the program.
    main(args)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.