Need help with docopt?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

7.5K Stars 555 Forks MIT License 447 Commits 252 Opened issues


Pythonic command line arguments parser, that will make you smile

Services available


Need anything else?

Contributors list

creates beautiful command-line interfaces

.. image:: :target:

.. image:: :target:

Video introduction to docopt:

PyCon UK 2012: Create *beautiful*
command-line interfaces with Python 
New in version 0.6.1:

  • Fix issue #85 <https:>_ which caused improper handling of [options] shortcut if it was present several times.

New in version 0.6.0:

  • New argument options_first, disallows interspersing options and arguments. If you supply options_first=True to docopt, it will interpret all arguments as positional arguments after first positional argument.

  • If option with argument could be repeated, its default value will be interpreted as space-separated list. E.g. with [default: ./here ./there] will be interpreted as ['./here', './there'].

Breaking changes:

  • Meaning of [options] shortcut slightly changed. Previously it meant "any known option". Now it means "any option not in usage-pattern". This avoids the situation when an option is allowed to be repeated unintentionally.

  • argv is None by default, not sys.argv[1:]. This allows docopt to always use the latest sys.argv, not sys.argv during import time.

Isn't it awesome how

generate help messages based on your code?!

Hell no! You know what's awesome? It's when the option parser is generated based on the beautiful help message that you write yourself! This way you don't need to write this stupid repeatable parser-code, and instead can write only the help message--the way you want it.

docopt helps you create most beautiful command-line interfaces easily:

.. code:: python

"""Naval Fate.

Usage: ship new ... ship move [--speed=] ship shoot mine (set|remove) [--moored | --drifting] (-h | --help) --version

Options: -h --help Show this screen. --version Show version. --speed= Speed in knots [default: 10]. --moored Moored (anchored) mine. --drifting Drifting mine.

""" from docopt import docopt

if name == 'main': arguments = docopt(doc, version='Naval Fate 2.0') print(arguments)

Beat that! The option parser is generated based on the docstring above that is passed to

parses the usage pattern (
"Usage: ..."
) and option descriptions (lines starting with dash "
") and ensures that the program invocation matches the usage pattern; it parses options, arguments and commands based on that. The basic idea is that a good help message has all necessary information in it to make a parser.


PEP 257 
_ recommends putting help message in the module docstrings.



_ or easy_install::
pip install docopt==0.6.2

Alternatively, you can just drop
file into your project--it is self-contained.

docopt is tested with Python 2.7, 3.4, 3.5, and 3.6.


You can run unit tests using the command:

python test


.. code:: python

from docopt import docopt

.. code:: python

docopt(doc, argv=None, help=True, version=None, options_first=False)

takes 1 required and 4 optional arguments:
  • doc
    could be a module docstring (
    ) or some other string that contains a help message that will be parsed to create the option parser. The simple rules of how to write such a help message are given in next sections. Here is a quick example of such a string:

.. code:: python

"""Usage: [-hso FILE] [--quiet | --verbose] [INPUT ...]

-h --help show this -s --sorted sorted output -o FILE specify output file [default: ./test.txt] --quiet print less text --verbose print more text


  • argv
    is an optional argument vector; by default
    uses the argument vector passed to your program (
    ). Alternatively you can supply a list of strings like
    '-o', 'hai.txt']
  • help
    , by default
    , specifies whether the parser should automatically print the help message (supplied as
    ) and terminate, in case
    option is encountered (options should exist in usage pattern, more on that below). If you want to handle
    options manually (as other options), set
  • version
    , by default
    , is an optional argument that specifies the version of your program. If supplied, then, (assuming
    option is mentioned in usage pattern) when parser encounters the
    option, it will print the supplied version and terminate.
    could be any printable object, but most likely a string, e.g.

    Note, when

    is set to automatically handle
    options, you still need to mention them in usage pattern for this to work. Also, for your users to know about them.
  • options_first
    , by default
    . If set to
    will disallow mixing options and positional argument. I.e. after first positional argument, all arguments will be interpreted as positional even if the look like options. This can be used for strict compatibility with POSIX, or if you want to dispatch your arguments to other programs.

The return value is a simple dictionary with options, arguments and commands as keys, spelled exactly like in your help message. Long versions of options are given priority. For example, if you invoke the top example as:: ship Guardian move 100 150 --speed=15

the return dictionary will be:

.. code:: python

{'--drifting': False,    'mine': False,
 '--help': False,        'move': True,
 '--moored': False,      'new': False,
 '--speed': '15',        'remove': False,
 '--version': False,     'set': False,
 '': ['Guardian'], 'ship': True,
 '': '100',           'shoot': False,
 '': '150'}

Help message format

Help message consists of 2 parts:

  • Usage pattern, e.g.::

    Usage: [-hso FILE] [--quiet | --verbose] [INPUT ...]

  • Option descriptions, e.g.::

    -h --help show this -s --sorted sorted output -o FILE specify output file [default: ./test.txt] --quiet print less text --verbose print more text

Their format is described below; other text is ignored.

Usage pattern format

Usage pattern is a substring of

that starts with
(case insensitive) and ends with a visibly empty line. Minimum example:

.. code:: python



The first word after

is interpreted as your program's name. You can specify your program's name several times to signify several exclusive patterns:

.. code:: python

"""Usage: FILE


Each pattern can consist of the following elements:

  • , ARGUMENTS. Arguments are specified as either upper-case words, e.g. CONTENT-PATH
    or words surrounded by angular brackets: 
  • --options. Options are words started with dash (
    ), e.g.
    . You can "stack" several of one-letter options, e.g.
    which will be the same as
    -o -i -v
    . The options can have arguments, e.g.
    -i FILE
    or even
    . However it is important that you specify option descriptions if you want your option to have an argument, a default value, or specify synonymous short/long versions of the option (see next section on option descriptions).
  • commands are words that do not follow the described above conventions of
    , plus two special commands: dash "
    " and double dash "
    " (see below).

Use the following constructs to specify patterns:

  • [ ] (brackets) optional elements. e.g.:
    [-hvqo FILE]
  • ( ) (parens) required elements. All elements that are not put in [ ] are also required, e.g.:
    --path= ...
    is the same as
    (--path= ...)
    . (Note, "required options" might be not a good idea for your users).
  • | (pipe) mutually exclusive elements. Group them using ( ) if one of the mutually exclusive elements is required: (--clockwise | --counter-clockwise) TIME
    . Group them using [ ] if none of the mutually-exclusive elements are required: [--left | --right]
  • ... (ellipsis) one or more elements. To specify that arbitrary number of repeating elements could be accepted, use ellipsis (
    ), e.g. FILE ...
    means one or more
    -s are accepted. If you want to accept zero or more elements, use brackets, e.g.: [FILE ...]
    . Ellipsis works as a unary operator on the expression to the left.
  • [options] (case sensitive) shortcut for any options. You can use it if you want to specify that the usage pattern could be provided with any options defined below in the option-descriptions and do not want to enumerate them all in usage-pattern.
  • "
    ". Double dash "
    " is used by convention to separate positional arguments that can be mistaken for options. In order to support this convention add "
    " to your usage patterns.
  • "
    ". Single dash "
    " is used by convention to signify that
    is used instead of a file. To support this add "
    " to your usage patterns. "
    " acts as a normal command.

If your pattern allows to match argument-less option (a flag) several times::

Usage: [-v | -vv | -vvv]

then number of occurrences of the option will be counted. I.e.

will be
if program was invoked as
. Same works for commands.

If your usage patterns allows to match same-named option with argument or positional argument several times, the matched arguments will be collected into a list::

Usage:   --path=...

I.e. invoked with file1 file2 --path=./here
the returned dict will contain
args[''] ==
['file1', 'file2']
args['--path'] == ['./here', './there']

Option descriptions format

Option descriptions consist of a list of options that you put below your usage patterns.

It is necessary to list option descriptions in order to specify:

  • synonymous short and long options,
  • if an option has an argument,
  • if option's argument has a default value.

The rules are as follows:

  • Every line in

    that starts with
    (not counting spaces) is treated as an option description, e.g.::

    Options: --verbose # GOOD -o FILE # GOOD Other: --bad # BAD, line does not start with dash "-"

  • To specify that option has an argument, put a word describing that argument after space (or equals "

    " sign) as shown below. Follow either or UPPER-CASE convention for options' arguments. You can use comma if you want to separate options. In the example below, both lines are valid, however you are recommended to stick to a single style.::

    -o FILE --output=FILE # without comma, with "=" sign -i , --input # with comma, without "=" sign

  • Use two spaces to separate options with their informal description::

    --verbose More text. # BAD, will be treated as if verbose option had # an argument "More", so use 2 spaces instead -q Quit. # GOOD -o FILE Output file. # GOOD --stdout Use stdout. # GOOD, 2 spaces

  • If you want to set a default value for an option with an argument, put it into the option-description, in form


    --coefficient=K The K coefficient [default: 2.95] --output=FILE Output file [default: test.txt] --directory=DIR Some directory [default: ./]

  • If the option is not repeatable, the value inside

    [default: ...]
    will be interpreted as string. If it is repeatable, it will be splited into a list on whitespace::

    Usage: [--repeatable= --repeatable=] [--another-repeatable=]... [--not-repeatable=]

    will be ['./here', './there']

    --repeatable= [default: ./here ./there]

    will be ['./here']

    --another-repeatable= [default: ./here]

    will be './here ./there', because it is not repeatable

    --not-repeatable= [default: ./here ./there]


We have an extensive list of

_ which cover every aspect of functionality of docopt. Try them out, read the source if in doubt.

Subparsers, multi-level help and huge applications (like git)

If you want to split your usage-pattern into several, implement multi-level help (with separate help-screen for each subcommand), want to interface with existing scripts that don't use docopt, or you're building the next "git", you will need the new

parameter (described in API section above). To get you started quickly we implemented a subset of git command-line interface as an example:

Data validation

docopt does one thing and does it well: it implements your command-line interface. However it does not validate the input data. On the other hand there are libraries like

python schema
_ which make validating data a breeze. Take a look at
_ which uses schema to validate data and report an error to the user.

Using docopt with config-files

Often configuration files are used to provide default values which could be overriden by command-line arguments. Since docopt returns a simple dictionary it is very easy to integrate with config-files written in JSON, YAML or INI formats. 
_ provides and example of how to use docopt with JSON or INI config-file.


We would love to hear what you think about docopt on our


Make pull requests, report bugs, suggest ideas and discuss docopt. You can also drop a line directly to [email protected].

to other languages

We think docopt is so good, we want to share it beyond the Python community! All official docopt ports to other languages can be found under the

docopt organization page 
_ on GitHub.

If your favourite language isn't among then, you can always create a port for it! You are encouraged to use the Python version as a reference implementation. A Language-agnostic test suite is bundled with

Python implementation 

Porting discussion is on

issues page


docopt follows

semantic versioning 
_. The first release with stable API will be 1.0.0 (soon). Until then, you are encouraged to specify explicitly the version in your dependency tools, e.g.::
pip install docopt==0.6.2
  • 0.6.2 Bugfix release.
  • 0.6.1 Bugfix release.
  • 0.6.0
    parameter. Breaking changes: Corrected
    defaults to
  • 0.5.0 Repeated options/commands are counted or accumulated into a list.
  • 0.4.2 Bugfix release.
  • 0.4.0 Option descriptions become optional, support for "
    " and "
    " commands.
  • 0.3.0 Support for (sub)commands like
    git remote add
    . Introduce
    shortcut for any options. Breaking changes:
    returns dictionary.
  • 0.2.0 Usage pattern matching. Positional arguments parsing based on usage patterns. Breaking changes:
    returns namespace (for arguments), not list. Usage pattern is formalized.
  • 0.1.0 Initial release. Options-parsing only (based on options description).

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.