forked from docopt/docopts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sylvain Viart
committed
Aug 11, 2019
1 parent
b49e4f5
commit cfc1468
Showing
2 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
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
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 "$@" |
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
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 |