mirror of
https://github.com/jimeh/dotify.git
synced 2026-02-19 01:56:39 +00:00
Clean up has-argument and parse-argument functions a bit
This commit is contained in:
29
src/lib/helpers/arguments.sh
Normal file → Executable file
29
src/lib/helpers/arguments.sh
Normal file → Executable file
@@ -1,12 +1,14 @@
|
||||
# Checks for specified argument.
|
||||
#
|
||||
# Requires bash extended globbing: shopt -s extglob
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# $ has-argument help h "-t none"
|
||||
# $ has-argument help h -t none
|
||||
# > returns 1
|
||||
# $ has-argument help h "-t none --help"
|
||||
# $ has-argument help h -t none --help
|
||||
# > returns 0
|
||||
# $ has-argument help h "-t none -h"
|
||||
# $ has-argument help h -t none -h
|
||||
# > returns 0
|
||||
#
|
||||
# Returns 0 if argument was found, returns 1 otherwise.
|
||||
@@ -17,16 +19,17 @@ has-argument() {
|
||||
short="-$2"
|
||||
shift 2
|
||||
|
||||
if [[ " $@ " == *" $long "* ]] || [[ " $@ " == *" $long="* ]]; then
|
||||
return 0
|
||||
elif [[ " $@ " == *" $short "* ]] || [[ " $@ " == *" $short="* ]]; then
|
||||
if [[ " $* " =~ ^.*\ ($long|$short)(=.+)?\ .*$ ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
# Parses and echos value of specified argument.
|
||||
#
|
||||
# Requires bash extended globbing: shopt -s extglob
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# $ parse-argument file f -t none --file /tmp/foobar.txt
|
||||
@@ -50,17 +53,13 @@ parse-argument() {
|
||||
if [ -n "$next_arg" ]; then
|
||||
echo "$arg"
|
||||
return 0
|
||||
elif [[ " $arg " == *" $long "* ]] || [[ " $arg " == *" $short "* ]]; then
|
||||
next_arg="yes"
|
||||
elif [[ " $arg " == *" $long="* ]]; then
|
||||
arg="${arg/#$long=/}"
|
||||
echo "$arg"
|
||||
return 0
|
||||
elif [[ " $arg " == *" $short="* ]]; then
|
||||
arg="${arg/#$short=/}"
|
||||
echo "$arg"
|
||||
elif [[ " $arg " =~ ^\ ($long|$short)\ $ ]]; then
|
||||
next_arg=1
|
||||
elif [[ " $arg " =~ ^\ ($long|$short)=(.+)\ $ ]]; then
|
||||
echo "${BASH_REMATCH[2]}"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user