mirror of
https://github.com/jimeh/dotify.git
synced 2026-02-19 10:06: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.
|
# Checks for specified argument.
|
||||||
#
|
#
|
||||||
|
# Requires bash extended globbing: shopt -s extglob
|
||||||
|
#
|
||||||
# Example:
|
# Example:
|
||||||
#
|
#
|
||||||
# $ has-argument help h "-t none"
|
# $ has-argument help h -t none
|
||||||
# > returns 1
|
# > returns 1
|
||||||
# $ has-argument help h "-t none --help"
|
# $ has-argument help h -t none --help
|
||||||
# > returns 0
|
# > returns 0
|
||||||
# $ has-argument help h "-t none -h"
|
# $ has-argument help h -t none -h
|
||||||
# > returns 0
|
# > returns 0
|
||||||
#
|
#
|
||||||
# Returns 0 if argument was found, returns 1 otherwise.
|
# Returns 0 if argument was found, returns 1 otherwise.
|
||||||
@@ -17,16 +19,17 @@ has-argument() {
|
|||||||
short="-$2"
|
short="-$2"
|
||||||
shift 2
|
shift 2
|
||||||
|
|
||||||
if [[ " $@ " == *" $long "* ]] || [[ " $@ " == *" $long="* ]]; then
|
if [[ " $* " =~ ^.*\ ($long|$short)(=.+)?\ .*$ ]]; then
|
||||||
return 0
|
|
||||||
elif [[ " $@ " == *" $short "* ]] || [[ " $@ " == *" $short="* ]]; then
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
# Parses and echos value of specified argument.
|
# Parses and echos value of specified argument.
|
||||||
#
|
#
|
||||||
|
# Requires bash extended globbing: shopt -s extglob
|
||||||
|
#
|
||||||
# Example:
|
# Example:
|
||||||
#
|
#
|
||||||
# $ parse-argument file f -t none --file /tmp/foobar.txt
|
# $ parse-argument file f -t none --file /tmp/foobar.txt
|
||||||
@@ -50,17 +53,13 @@ parse-argument() {
|
|||||||
if [ -n "$next_arg" ]; then
|
if [ -n "$next_arg" ]; then
|
||||||
echo "$arg"
|
echo "$arg"
|
||||||
return 0
|
return 0
|
||||||
elif [[ " $arg " == *" $long "* ]] || [[ " $arg " == *" $short "* ]]; then
|
elif [[ " $arg " =~ ^\ ($long|$short)\ $ ]]; then
|
||||||
next_arg="yes"
|
next_arg=1
|
||||||
elif [[ " $arg " == *" $long="* ]]; then
|
elif [[ " $arg " =~ ^\ ($long|$short)=(.+)\ $ ]]; then
|
||||||
arg="${arg/#$long=/}"
|
echo "${BASH_REMATCH[2]}"
|
||||||
echo "$arg"
|
|
||||||
return 0
|
|
||||||
elif [[ " $arg " == *" $short="* ]]; then
|
|
||||||
arg="${arg/#$short=/}"
|
|
||||||
echo "$arg"
|
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user