If you have struggled more than once with creating command lines arguments for your script you have probably never stumble upon the –docopt.

Docopt is a command-line interface description language with implementations in several programming languages:


The following ports are on their way:


Here’s the link to some more examples.
And here’s the URL to the website.

<br /> <br /> <br /> Let's see some usecase in one of my projects:<br /> <br />
#!/usr/bin/env python
"""Atlassian CLI 0.1

Usage:
    run.py applink --src=<app> --dest=<app>
    run.py exists <app> < projectkey>
    run.py feedback <issue> [-]
    run.py createproject <app> <projectkey> [options]
    run.py -h | --help
    run.py --version

App:
    bamboo
    confluence
    crucible
    fisheye
    jira
    stash

Options:
    -u --projectadmin=<user>    Username from Atlassian Crowd.
    -p --projectname=<project>  App project name.
    -k --projectkey=<key>       App project key.
    -i --issue=<issue>          Jira issue to post information in comment.

"""
from docopt import docopt if __name__ == '__main__': arguments = docopt(__doc__, version=__doc__.split('\n')[0]) print(arguments)

Will results with:

./run.py --version


Atlassian CLI 0.1


./run.py exists jira TEST


{'-': False,
 '--dest': None,
 '--help': False,
 '--issue': None,
 '--projectadmin': None,
 '--projectkey': None,
 '--projectname': None,
 '--src': None,
 '--version': False,
 '-h': False,
 '<app>': 'jira',
 '<issue>': None,
 '<key>': 'TEST',
 'applink': False,
 'exists': True,
 'feedback': False}




./run.py createproject jira MYKEY -u "matt.harasymczuk" -i TEST-1337 --projectname="My Project"
{'-': False,
 '--dest': None,
 '--help': False,
 '--issue': 'TEST-1337',
 '--projectadmin': 'matt.harasymczuk',
 '--projectkey': None,
 '--projectname': 'My Project',
 '--src': None,
 '--version': False,
 '-h': False,
 '<app>': 'jira',
 '<issue>': None,
 '<projectkey>': 'MYKEY',
 'applink': False,
 'createproject': True,
 'exists': False,
 'feedback': False}


Now it is quite simple to parse this output and run your logic.