Package: docopts
WARNING: Theupcoming 0.7.0 release of docoptswill feature a completely
different user interface.  Prepare for script breakage.docoptsshell interface for docopt, the CLI description languageAuthor:Lari RaskuDate:2013-02-07Copyright:MITVersion:0.6.1+fixManual section:1SYNOPSISdocopts[options]-hmsg: [argv…]DESCRIPTIONdocoptsparses the command line argument vectorargvaccording to thedocoptstringmsgand echoes the results to standard
output as a snippet of Bash source code.  Passing this snippet as an argument toeval(1)is sufficient for handling the CLI needs of
most scripts.Ifargvmatches one of the usage patterns defined inmsg,docoptsgenerates 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_BracketsUPPER-CASE:UPPER_CASE--Long-Option:Long_Option-S:SIf one of the argument names cannot be mangled into a valid Bash identifier,
or two argument names map to the same variable name,docoptwill exit with
an error, and you should really rethink your CLI.  The--and-commands will not be stored.Alternatively,docoptscan 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:trueif found,falseif notRepeatable valueless arguments: the count of their instances inargvNon-repeatable arguments with values: the value as a string if found,
the empty string if notRepeatable arguments with values: a Bash array of the parsed valuesUnless the--no-helpoption is given,docoptshandles the--helpand--versionoptions and their possible aliases specially,
generating code for printing the relevant message to standard output and
terminating successfully if either option is encountered when parsingargv.
Note however that this also requires listing the relevant option inmsgand, in--version’s case, invokingdocoptswith the--versionoption.Ifargvdoes not match any usage pattern inmsg,docoptswill generate
code for exiting the program with status 64 (EX_USAGEinsysexits(3)) and printing a diagnostic error
message.OPTIONS-h<msg>,--help=<msg>The help message in docopt format.
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, read it first.
If no argument is given, print docopts’s own
version message and quit.-O,--options-firstDisallow 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-helpDon’t handle –help and –version specially.-A<name>Export the arguments as a Bash 4.x associative
array called <name>.-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: —-]EXAMPLESRead the help and version messages from standard input: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.
EOF
)"

if $verbose ; then
    echo "Hello, world!"
fiParse the help and version messages from script comments and pass them as
command line arguments:#? 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>...
##?
##?       --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"
doneUsing the associative array:eval "$(docopts -A args -h "$help" : "$@")"

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

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

i=0
while [[ $i -lt ${args[<argument-with-multiple-values>,#]} ]] ; do
    echo "${args[<argument-with-multiple-values>,$i]}"
    i=$[$i+1]
doneVERSIONINGThedocoptsversion number always matches that of thedocopt Python reference implementationversion against which it was built.  Asdocoptfollowssemantic versioning,docoptsshould work with anydocoptrelease it shares the major version number with; however, as bothdocoptsanddocoptare in major version number 0 at the moment of
writing this,docoptscan only be relied to work with an installation ofdocoptwith the exact same version number.