mirror of
https://github.com/jimeh/dotify.git
synced 2026-02-19 10:06:39 +00:00
Improve argument/command handling
This commit is contained in:
@@ -35,30 +35,57 @@ source "../lib/dotify-help.sh"
|
|||||||
source "../lib/dotify-info.sh"
|
source "../lib/dotify-info.sh"
|
||||||
source "../lib/dotify-install.sh"
|
source "../lib/dotify-install.sh"
|
||||||
|
|
||||||
|
#
|
||||||
|
# Argument Parsing
|
||||||
|
#
|
||||||
|
|
||||||
|
DOTFILE="" # --dotfile / -f
|
||||||
|
TARGET="" # --target / -t
|
||||||
|
HELP="" # --help / -h
|
||||||
|
VERSION="" # --version / -v
|
||||||
|
|
||||||
|
if has-argument dotfile f "$@"; then
|
||||||
|
DOTFILE="$(parse-argument dotfile f "$@")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if has-argument target t "$@"; then
|
||||||
|
TARGET="$(parse-argument target t "$@")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if has-argument help h "$@"; then
|
||||||
|
HELP="1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if has-argument version v "$@"; then
|
||||||
|
VERSION="1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Command Parsing
|
# Command Parsing
|
||||||
#
|
#
|
||||||
|
|
||||||
# If arguments include "--help" or "-h" display help and exit.
|
# Command is first argument that does not start with a dash or plus.
|
||||||
if has-argument help h "$@"; then
|
|
||||||
dotify-help "$@"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Command is first argument that does not start with a dash.
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
if [[ "$arg" != "-"* ]]; then
|
if [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then
|
||||||
command="$arg"
|
command="$arg"
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
if [ -n "$HELP" ] || [ "$command" == "help" ]; then
|
||||||
|
dotify-help
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$VERSION" ] || [ "$command" == "version" ]; then
|
||||||
|
dotify-help | head -1
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
case "$command" in
|
case "$command" in
|
||||||
"help" )
|
|
||||||
dotify-help "$@"
|
|
||||||
;;
|
|
||||||
"info" )
|
"info" )
|
||||||
dotify-info "$@"
|
dotify-info
|
||||||
;;
|
;;
|
||||||
"" | "install" )
|
"" | "install" )
|
||||||
dotify-install
|
dotify-install
|
||||||
|
|||||||
@@ -4,9 +4,8 @@
|
|||||||
|
|
||||||
locate-dotfile() {
|
locate-dotfile() {
|
||||||
local dotfile
|
local dotfile
|
||||||
if has-argument dotfile f "$@"; then
|
if [ -n "$DOTFILE" ]; then
|
||||||
dotfile="$(parse-argument dotfile f "$@")"
|
dotfile="$DOTFILE"
|
||||||
dotfile="$(expand-path "$dotfile")"
|
|
||||||
if [ ! -f "$dotfile" ]; then
|
if [ ! -f "$dotfile" ]; then
|
||||||
echo "ERROR: \"$dotfile\" does not exist." >&2
|
echo "ERROR: \"$dotfile\" does not exist." >&2
|
||||||
return 1
|
return 1
|
||||||
@@ -23,19 +22,18 @@ locate-dotfile() {
|
|||||||
|
|
||||||
locate-target() {
|
locate-target() {
|
||||||
local target
|
local target
|
||||||
if has-argument target t "$@"; then
|
if [ -n "$DOTFILE" ]; then
|
||||||
target="$(parse-argument target t "$@")"
|
target="$TARGET"
|
||||||
target="$(expand-path "$target")"
|
|
||||||
if [ ! -d "$target" ]; then
|
if [ ! -d "$target" ]; then
|
||||||
echo "ERROR: Target \"$target\" is not a directory."
|
echo "ERROR: Target \"$target\" is not a directory." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
|
elif [ -n "$HOME" ] && [ -d "$HOME" ]; then
|
||||||
target="$HOME"
|
target="$HOME"
|
||||||
elif [ -d "$(expand-path "~")" ]; then
|
elif [ -d ~ ]; then
|
||||||
target="$HOME"
|
target=~
|
||||||
else
|
else
|
||||||
echo "ERROR: Your \$HOME folder could not be found."
|
echo "ERROR: Your \$HOME folder could not be found." >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user