diff --git a/examples/compatible_docopt.sh/arguments_example.sh b/examples/compatible_docopt.sh/arguments_example.sh new file mode 100755 index 0000000..ae69280 --- /dev/null +++ b/examples/compatible_docopt.sh/arguments_example.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +DOC="Argument parser +Usage: arguments_example.sh [-vqrh] [FILE] ... + arguments_example.sh (--left | --right) CORRECTION FILE + +Process FILE and optionally apply correction to either left-hand side or +right-hand side. + +Arguments: + FILE optional input file + CORRECTION correction angle, needs FILE, --left or --right to be present + +Options: + -h --help + -v verbose mode + -q quiet mode + -r make report + --left use left-hand side + --right use right-hand side" + +main_arguments() +{ + # main function for this script + set | grep "^$DOCOPT_PREFIX" + + return 0 +} + +# docopt.sh place holder + +DOCOPT_PREFIX=ARGS_ +case $DOCOPT_PARSER in + docopts) + if [[ -z $(type -p docopts) ]] ; then + echo "docopts not found in PATH, use: source example_env.sh" + exit 1 + fi + # docopts append _ to prefix + eval "$(docopts -G ${DOCOPT_PREFIX%_} --docopt_sh -h "$DOC" : "$@")" + ;; + docopt.sh) + eval "$(docopt "$@")" + ;; + "") + echo "DOCOPT_PARSER is undefined" + exit 1 + ;; + *) + echo "DOCOPT_PARSER unsuported value: $DOCOPT_PARSER" + exit 1 + ;; +esac + +main_arguments "$@" diff --git a/examples/compatible_docopt.sh/example_env.sh b/examples/compatible_docopt.sh/example_env.sh new file mode 100644 index 0000000..a573380 --- /dev/null +++ b/examples/compatible_docopt.sh/example_env.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# +# Helper to setup environment for this examples +# + +pathadd() { + local after=0 + if [[ "$1" == "after" ]] ; then + after=1 + shift + fi + + local p + + for p in $* + do + if [ -d "$p" ] && ! echo $PATH | grep -E -q "(^|:)$p($|:)" ; then + if [[ $after -eq 1 ]] + then + PATH="$PATH:${p%/}" + else + PATH="${p%/}:$PATH" + fi + fi + done +} + +# comput current dir +# reandlink -f use GNU readlink, available on macos via: brew install coreutils +EXAMPLES_DIR=$(dirname $(readlink -f $BASH_SOURCE)) +DOCOPTS_BIN=$(type -p docopts) + +if [[ -z $DOCOPTS_BIN ]] ; then + echo "adding to PATH: $EXAMPLES_DIR/../.." + pathadd $EXAMPLES_DIR/../.. + DOCOPTS_BIN=$(type -p docopts) + if [[ -z $DOCOPTS_BIN ]] ; then + echo "ERROR: docopts not found in PATH, get a binary or compile it." + fi +else + echo "using docopts in PATH: $DOCOPTS_BIN" +fi