Last active
June 27, 2018 12:47
-
-
Save CristianCantoro/01bd1261132ce225f60001822cc0c47b to your computer and use it in GitHub Desktop.
Using docopts for bash and docopt for python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./example.py -a --foo bar | |
{'--all': True, | |
'--foo': 'bar', | |
'--help': False, | |
'--version': False, | |
'-b': False} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./example.sh -a --foo bar 64 ↵ | |
all: true | |
b: false | |
foo: bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./example_array.sh | |
arguments['--version']: false | |
arguments['--help']: false | |
arguments['--foo']: | |
arguments['--all']: false | |
arguments['-b']: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./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