Skip to content

Instantly share code, notes, and snippets.

@CristianCantoro
Last active June 27, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CristianCantoro/01bd1261132ce225f60001822cc0c47b to your computer and use it in GitHub Desktop.
Save CristianCantoro/01bd1261132ce225f60001822cc0c47b to your computer and use it in GitHub Desktop.
Using docopts for bash and docopt for python
#!/usr/bin/env python3
"""
Usage:
example.sh [options]
example.sh ( -h | --help )
example.sh ( --version )
Options:
-a, --all An optional flaag.
-b Another optional flag.
-f=FOO, --foo=FOO An option with an argument.
-h, --help Show this help message and exits.
-V, --version Print version and copyright information.
"""
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='example.py 0.1.0')
print(arguments)
exit(0)
$ ./example.py -a --foo bar
{'--all': True,
'--foo': 'bar',
'--help': False,
'--version': False,
'-b': False}
#!/usr/bin/env bash
# shellcheck disable=SC2128
SOURCED=false && [ "$0" = "$BASH_SOURCE" ] || SOURCED=true
read -rd '' docstring <<EOD
Usage:
example.sh [options]
example.sh ( -h | --help )
example.sh ( --version )
Options:
-a, --all An optional flaag.
-b Another optional flag.
-f=FOO, --foo=FOO An option with an argument.
-h, --help Show this help message and exits.
-V, --version Print version and copyright information.
EOD
eval "$(docopts -V 'example.sh 0.0.1' -h "$docstring" : "$@" )"
if ! $SOURCED; then
set -euo pipefail
IFS=$'\n\t'
fi
echo "all: $all"
echo "b: $b"
echo "foo: $foo"
exit 0
$ ./example.sh -a --foo bar 64 ↵
all: true
b: false
foo: bar
#!/usr/bin/env bash
# shellcheck disable=SC2128
SOURCED=false && [ "$0" = "$BASH_SOURCE" ] || SOURCED=true
read -rd '' docstring <<EOD
Usage:
example_array.sh [options]
example_array.sh ( -h | --help )
example_array.sh ( --version )
Options:
-a, --all An optional flaag.
-b Another optional flag.
-f=FOO, --foo=FOO An option with an argument.
-h, --help Show this help message and exits.
-V, --version Print version and copyright information.
EOD
declare -A arguments
eval "$(docopts -A 'arguments' \
-V 'example_array.sh 0.0.1' \
-h "$docstring" : "$@" )"
if ! $SOURCED; then
set -euo pipefail
IFS=$'\n\t'
fi
for k in "${!arguments[@]}"; do
echo "arguments['$k']: ${arguments[$k]}"
done
exit 0
$ ./example_array.sh
arguments['--version']: false
arguments['--help']: false
arguments['--foo']:
arguments['--all']: false
arguments['-b']: false
#!/usr/bin/env bash
# shellcheck disable=SC2128
SOURCED=false && [ "$0" = "$BASH_SOURCE" ] || SOURCED=true
read -rd '' docstring <<EOD
Usage:
example_function.sh [options]
example_function.sh ( -h | --help )
example_function.sh ( --version )
Options:
-a, --all An optional flaag.
-b Another optional flag.
-f=FOO, --foo=FOO An option with an argument.
-h, --help Show this help message and exits.
-V, --version Print version and copyright information.
EOD
eval "$(docopts -V 'example_array.sh 0.0.1' \
-h "$docstring" : "$@" )"
read -rd '' docopts_function <<EOF
function arguments() {
local __all=$all
local _b=$b
local __foo=$foo
local __help=$help
local __version=$version
local parname=\$(echo "\$1" | sed 's/-/_/g')
echo "\${!parname}"
}
EOF
eval "$docopts_function"
if ! $SOURCED; then
set -euo pipefail
IFS=$'\n\t'
fi
echo "arguments '--all': $(arguments '--all')"
echo "arguments '-b': $(arguments '-b')"
echo "arguments '--foo': $(arguments '--foo')"
echo "arguments '--help': $(arguments '--help')"
echo "arguments '--version': $(arguments '--version')"
exit 0
$ ./example_function.sh
arguments '--all': false
arguments '-b': false
arguments '--foo':
arguments '--help': false
arguments '--version': false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment