Break main executable logic into functions

This commit is contained in:
2014-08-21 03:00:40 +01:00
parent f8a0ad4ea3
commit 6bddccd199
5 changed files with 89 additions and 73 deletions

View File

@@ -87,81 +87,14 @@ source "../lib/dotfile-commands/default_action.sh"
source "../lib/dotfile-commands/include.sh"
#
# Argument Parsing
# Main
#
ARG_DOTFILE="" # --dotfile / -f
ARG_TARGET="" # --target / -t
ARG_DRY_RUN="" # --dry-run / -d
ARG_HELP="" # --help / -h
ARG_VERSION="" # --version / -v
source "../lib/main/parse-arguments.sh"
source "../lib/main/parse-command.sh"
source "../lib/main/dispatcher.sh"
source "../lib/main/main.sh"
if has-argument dotfile f "$@"; then
ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi
if has-argument target t "$@"; then
ARG_TARGET="$(parse-argument target t "$@")"
fi
if has-argument dry-run d "$@"; then
ARG_DRY_RUN="1"
fi
if has-argument help h "$@"; then
ARG_HELP="1"
fi
if has-argument version v "$@"; then
ARG_VERSION="1"
fi
#
# Command Parsing
#
# Command is first argument that does not start with a dash or plus.
for arg in "$@"; do
if [ -n "$skip_next" ]; then
skip_next=
elif [[ "$arg" =~ ^(--dotfile|-f|--taraget|-t)$ ]]; then
skip_next=1
elif [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then
command="$arg"
break
fi
done
# Show help and exit if help arguments or command are given.
if [ -n "$ARG_HELP" ] || [ "$command" == "help" ]; then
dotify-command-help
exit
fi
# Show version info and exit if version arguments or command are given.
if [ -n "$ARG_VERSION" ] || [ "$command" == "version" ]; then
dotify-command-help | head -1
exit
fi
# Deal with the commands.
case "$command" in
"info" )
dotify-command-info
;;
"compile" )
dotify-command-compile
;;
"" | "install" )
dotify-command-install
;;
"uninstall" )
dotify-command-uninstall
;;
"clean" )
dotify-command-clean
;;
esac
dotify-main $@
exit $?

View File

@@ -0,0 +1,34 @@
dotify-main-dispatcher() {
# Show help and exit if help arguments or command are given.
if [ -n "$ARG_HELP" ] || [ "$COMMAND" == "help" ]; then
dotify-command-help
exit
fi
# Show version info and exit if version arguments or command are given.
if [ -n "$ARG_VERSION" ] || [ "$COMMAND" == "version" ]; then
dotify-command-help | head -1
exit
fi
# Deal with the commands.
case "$COMMAND" in
"info" )
dotify-command-info
;;
"compile" )
dotify-command-compile
;;
"" | "install" )
dotify-command-install
;;
"uninstall" )
dotify-command-uninstall
;;
"clean" )
dotify-command-clean
;;
esac
return $?
}

7
src/lib/main/main.sh Normal file
View File

@@ -0,0 +1,7 @@
dotify-main() {
dotify-main-parse-arguments $@
dotify-main-parse-command $@
dotify-main-dispatcher $@
return $?
}

View File

@@ -0,0 +1,27 @@
dotify-main-parse-arguments() {
ARG_DOTFILE="" # --dotfile / -f
ARG_TARGET="" # --target / -t
ARG_DRY_RUN="" # --dry-run / -d
ARG_HELP="" # --help / -h
ARG_VERSION="" # --version / -v
if has-argument dotfile f "$@"; then
ARG_DOTFILE="$(parse-argument dotfile f "$@")"
fi
if has-argument target t "$@"; then
ARG_TARGET="$(parse-argument target t "$@")"
fi
if has-argument dry-run d "$@"; then
ARG_DRY_RUN="1"
fi
if has-argument help h "$@"; then
ARG_HELP="1"
fi
if has-argument version v "$@"; then
ARG_VERSION="1"
fi
}

View File

@@ -0,0 +1,15 @@
dotify-main-parse-command() {
local skip_next
# Command is first argument that does not start with a dash or plus.
for arg in "$@"; do
if [ -n "$skip_next" ]; then
skip_next=
elif [[ "$arg" =~ ^(--dotfile|-f|--taraget|-t)$ ]]; then
skip_next=1
elif [[ "$arg" != "-"* ]] && [[ "$arg" != "+"* ]]; then
COMMAND="$arg"
break
fi
done
}