Skip to content

Commit

Permalink
initial import docopt#36
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Viart committed Aug 11, 2019
1 parent b49e4f5 commit cfc1468
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
55 changes: 55 additions & 0 deletions examples/compatible_docopt.sh/arguments_example.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
42 changes: 42 additions & 0 deletions examples/compatible_docopt.sh/example_env.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cfc1468

Please sign in to comment.