Fix issue #85
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 latestsys.argv,
not sys.argv during import time.

Isn’t it awesome how optparse and argparse 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.

Beat that! The option parser is generated based on the docstring above
that is passed to docopt function. docopt 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.

doc could be a module docstring (__doc__) 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:

argv is an optional argument vector; by default docopt uses
the argument vector passed to your program (sys.argv[1:]).
Alternatively you can supply a list of strings like ['--verbose','-o','hai.txt'].

help, by default True, specifies whether the parser should
automatically print the help message (supplied as doc) and
terminate, in case -h or --help option is encountered
(options should exist in usage pattern, more on that below). If you
want to handle -h or --help options manually (as other
options), set help=False.

version, by default None, is an optional argument that
specifies the version of your program. If supplied, then, (assuming
--version option is mentioned in usage pattern) when parser
encounters the --version option, it will print the supplied
version and terminate. version could be any printable object,
but most likely a string, e.g. "2.1.0rc1".

Note, when docopt is set to automatically handle -h,
--help and --version 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 False. If set to True 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:

–options. Options are words started with dash (-), e.g.
--output, -o. You can “stack” several of one-letter
options, e.g. -oiv which will be the same as -o-i-v. The
options can have arguments, e.g. --input=FILE or -iFILE or
even -iFILE. 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 --options or <arguments> or ARGUMENTS,
plus two special commands: dash “-” and double dash “--”
(see below).

Use the following constructs to specify patterns:

[ ] (brackets) optional elements. e.g.: my_program.py[-hvqoFILE]

( ) (parens) required elements. All elements that are not
put in [ ] are also required, e.g.: my_program.py--path=<path><file>... is the same as my_program.py(--path=<path><file>...). (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:
my_program.py(--clockwise|--counter-clockwise)TIME. Group
them using [ ] if none of the mutually-exclusive elements are
required: my_program.py[--left|--right].

... (ellipsis) one or more elements. To specify that
arbitrary number of repeating elements could be accepted, use
ellipsis (...), e.g. my_program.pyFILE... means one or
more FILE-s are accepted. If you want to accept zero or more
elements, use brackets, e.g.: my_program.py[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
stdin is used instead of a file. To support this add “[-]”
to your usage patterns. “-” acts as a normal command.

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 doc that starts with - or -- (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 <angular-brackets> 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.:

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 options_first
parameter (described in API section above). To get you started quickly
we implemented a subset of git command-line interface as an example:
examples/git

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 validation_example.py
which uses schema to validate data and report an error to the
user.

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.
config_file_example.py provides
and example of how to use docopt with JSON or INI config-file.

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.