-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from joao-paulo-parity/opt-args
Receive CLI arguments as options for check_dependent_project.sh
- Loading branch information
Showing
2 changed files
with
83 additions
and
13 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
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,68 @@ | ||
die() { | ||
if [ "${1:-}" ]; then | ||
>&2 echo "$1" | ||
fi | ||
exit 1 | ||
} | ||
|
||
get_arg() { | ||
local arg_type="$1" | ||
shift | ||
|
||
local is_required | ||
case "$arg_type" in | ||
required|required-many) | ||
is_required=true | ||
;; | ||
optional|optional-many) ;; | ||
*) | ||
die "Invalid is_required argument \"$2\" in get_arg" | ||
;; | ||
esac | ||
|
||
local has_many_values | ||
if [ "${arg_type: -6}" == "-many" ]; then | ||
has_many_values=true | ||
fi | ||
|
||
local option_arg="$1" | ||
shift | ||
|
||
unset out | ||
out=() | ||
|
||
local get_next_arg | ||
for arg in "$@"; do | ||
if [ "${get_next_arg:-}" ]; then | ||
out+=("$arg") | ||
unset get_next_arg | ||
if [ ! "${has_many_values:-}" ]; then | ||
break | ||
fi | ||
# --foo=bar (get the value after '=') | ||
elif [ "${arg:0:$(( ${#option_arg} + 1 ))}" == "$option_arg=" ]; then | ||
out+=("${arg:$(( ${#option_arg} + 1 ))}") | ||
if [ ! "${has_many_values:-}" ]; then | ||
break | ||
fi | ||
# --foo bar (get the next argument) | ||
elif [ "$arg" == "$option_arg" ]; then | ||
get_next_arg=true | ||
fi | ||
done | ||
|
||
# arg list ended with --something but no argument was provided next | ||
if [ "${get_next_arg:-}" ]; then | ||
die "Expected argument after \"${args[-1]}"\" | ||
fi | ||
|
||
if [ "${out[0]:-}" ]; then | ||
if [ ! "${has_many_values:-}" ]; then | ||
out="${out[0]}" | ||
fi | ||
elif [ "${is_required:-}" ]; then | ||
die "Argument $option_arg is required, but was not found" | ||
else | ||
unset out | ||
fi | ||
} |