Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


docopt for shell - make beautiful CLI with ease.

Status: working.

docopts : the command line wrapper for bash.

Most concepts are documented in the docopt (without S) manual - see

Many examples use associative arrays in bash 4.x, but there is legacy support for bash 3.2 on macOS (OS X) or legacy GNU/Linux OS.

This is a transitional release: v0.6.3-rc2

This release will be maintained for compatibility, only fixes will be provided. The 0.6.3 version is fully compatible with the previous version of docopts.


  docopts [options] -h <msg> : [<argv>...]
  docopts [options] [--no-declare] -A <name>   -h <msg> : [<argv>...]
  docopts [options] -G <prefix>  -h <msg> : [<argv>...]
  docopts [options] --no-mangle  -h <msg> : [<argv>...]


docopts parses the command line argument vector <argv> according to the docopt string <msg> and echoes the results to standard output as a snippet of Bash source code. Passing this snippet as an argument to eval(1) is sufficient for handling the CLI needs of most scripts.

If <argv> matches one of the usage patterns defined in <msg>, docopts generates code for storing the parsed arguments as Bash variables. As most command line argument names are not valid Bash identifiers, some name mangling will take place:

  • <Angle_Brackets> ==> Angle_Brackets
  • --Long-Option ==> Long_Option
  • -S ==> S
  • -4 ==> INVALID (without -G)

If one of the argument names cannot be mangled into a valid Bash identifier, or two argument names map to the same variable name, docopts will exit with an error, and you should really rethink your CLI, or use -A or -G. The -- and - commands will not be stored.

Note: You can use --no-mangle if you still want full input, this wont produce output suitable for bash eval(1) but can be parsed by your own code.

Alternatively, docopts can be invoked with the -A <name> option, which stores the parsed arguments as fields of a Bash 4 associative array called <name> instead. However, as Bash does not natively support nested arrays, they are faked for repeatable arguments with the following access syntax:

    ${args[ARG,#]} # the number of arguments to ARG
    ${args[ARG,0]} # the first argument to ARG
    ${args[ARG,1]} # the second argument to ARG, etc.

The arguments are stored as follows:

  • Non-repeatable, valueless arguments: true if found, false if not
  • Repeatable valueless arguments: the count of their instances in <argv>
  • Non-repeatable arguments with values: the value as a string if found, the empty string if not
  • Repeatable arguments with values: a Bash array of the parsed values

Unless the --no-help option is given, docopts handles the --help and --version options and their possible aliases specially, generating code for printing the relevant message to standard output and terminating successfully if either option is encountered when parsing <argv>.

Note however that this also requires listing the relevant option in <msg> and, in --version's case, invoking docopts with the --version option.

If <argv> does not match any usage pattern in <msg>, docopts will generate code for exiting the program with status 64 EX_USAGE in sysexits(3) and printing a diagnostic error message.

Note that due to the above, docopts can't be used to parse shell function arguments: exit(1) quits the entire interpreter, not just the current function.


This is the verbatim output of the --help:

  -h <msg>, --help=<msg>        The help message in docopt format.
                                Without argument outputs this help.
                                If - is given, read the help message from
                                standard input.
                                If no argument is given, print docopts's own
                                help message and quit.
  -V <msg>, --version=<msg>     A version message.
                                If - is given, read the version message from
                                standard input.  If the help message is also
                                read from standard input, it is read first.
                                If no argument is given, print docopts's own
                                version message and quit.
  -s <str>, --separator=<str>   The string to use to separate the help message
                                from the version message when both are given
                                via standard input. [default: ----]
  -O, --options-first           Disallow interspersing options and positional
                                arguments: all arguments starting from the
                                first one that does not begin with a dash will
                                be treated as positional arguments.
  -H, --no-help                 Don't handle --help and --version specially.
  -A <name>                     Export the arguments as a Bash 4.x associative
                                array called <name>.
  -G <prefix>                   Don't use associative array but output
                                Bash 3.2 compatible GLOBAL variables assignment:
                                Can be used with numeric incompatible options
                                as well.  See also: --no-mangle
  --no-mangle                   Output parsed option not suitable for bash eval.
                                Full option names are kept. Rvalue is still
                                shellquoted. Extra parsing is required.
  --no-declare                  Don't output 'declare -A <name>', used only
                                with -A argument.
  --debug                       Output extra parsing information for debugging.
                                Output cannot be used in bash eval.


Bash 4.x and higher is the main target.

In order to use docopts with bash 3.2 (for macOS and old GNU/Linux versions) by avoiding bash 4.x associative arrays, you can:

  • don't use the -A option
  • use GLOBAL generated mangled variables
  • use -G <prefix> option to generate GLOBAL with prefix_
  • use source --auto -G (see example)

The helper allows the use of set -u, which gives an error on undefined variables in your scripts.

Unofficial strict mode for bash should also work with, please report any issue with examples. helper

The helper has its own documentation here docs/


Find more examples in examples/ folder.

This example reads the help and version messages from standard input (docopts found in $PATH):

source examples/legacy_bash/

#!/usr/bin/env bash
# Example from

eval "$(docopts -V - -h - : "$@" <<EOF
Usage: rock [options] <argv>...

      --verbose  Generate verbose messages.
      --help     Show help options.
      --version  Print program version.
rock 0.1.0
Copyright (C) 200X Thomas Light
License RIT (Robot Institute of Technology)
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

if $verbose ; then
    echo "Hello, world!"

The following example, parses the help and version messages from script comments and pass them as command line arguments:

source examples/legacy_bash/

#!/usr/bin/env bash
# Example from

#? rock 0.1.0
#? Copyright (C) 200X Thomas Light
#? License RIT (Robot Institute of Technology)
#? This is free software: you are free to change and redistribute it.
#? There is NO WARRANTY, to the extent permitted by law.

##? Usage: rock [options] <argv>...
##? Options:
##?       --help     Show help options.
##?       --version  Print program version.

help=$(grep "^##?" "$0" | cut -c 5-)
version=$(grep "^#?"  "$0" | cut -c 4-)
eval "$(docopts -h "$help" -V "$version" : "$@")"

for arg in "${argv[@]}"; do
    echo "$arg"

The next example shows how using the Bash 4.x associative array with -A:

Usage: example [--long-option-with-argument=value] <argument-with-multiple-values>...
eval "$(docopts -A args -h "$help" : "$@")"

if ${args[subcommand]} ; then
    echo "subcommand was given"

if [ -n "${args[--long-option-with-argument]}" ] ; then
    echo "${args[--long-option-with-argument]}"
    echo "--long-option-with-argument was not given"

while [[ $i -lt ${args[<argument-with-multiple-values>,#]} ]] ; do
    echo "$i: ${args[<argument-with-multiple-values>,$i]}"


docopts was first developed by Lari Rasku and was written in Python based on the docopt Python parser.

The current version is written in go and is 100% compatible with previous Python-based docopts. Please report any non working code with issue and examples.

Roadmap: A new shell API is proposed

Starting at release: 0.7.0 a new lib API based on JSON will be introduced:

See and contribute on the docopts Wiki.


You only have to drop the binary, and optionally also the lib helper, in a directory on your PATH. The binary is standalone and statically linked, so it runs everywhere.

See build section.

With root privileges you could do:

cp docopts /usr/local/bin

Pre-built binaries

Pre-built Go binaries for some OS (32 and 64 bits) are attached to releases.

We provide a download helper:

git clone
cd docopts

You should get a renamed docopts in the current folder. Put it in your PATH:

sudo cp docopts /usr/local/bin

The cloned repository is no more used at this stage, but still contains a lot of bash examples.

Learn more about pre-built binaries.


We encourage you to build your own binary, which is easy once you have Go installed. Or find a local golang developer that you trust and ask her, in exchange for a beer or two, if she could build it for you. ;)

Requires a Go workspace.

local build: (also done with our Makefile default target: make)

go get
go get
cd src/
go build docopts.go

cross compile for 32 bit:

env GOOS=linux GOARCH=386 go build docopts.go

or via Makefile:

cd src/
make all
make test

Tested builds are built on:

go version go1.14.1 linux/amd64


Warning: may be not up-to-date feature list.

The helper is an extra bash library that you can source in your shell script. This library provides some bash helpers and is not required in order to use docopts. See documentation.

docopts doesn't need a python interpreter anymore, so it works on any legacy system too.

As of 2019-05-18

  • docopts is able to reproduce 100% of the python version.
  • unit tests for Go are provided, so hack as you wish.
  • 100% of tests pass (GNU/Linux 64bits).
  • bats-core unittests and fonctional testing are provided too.


Read the doc for developer.

You can’t perform that action at this time.