common.sh: arg parse init

This commit is contained in:
Dict Xiong 2022-11-15 22:51:04 +08:00
parent c354f980bb
commit df9cd2042a
2 changed files with 33 additions and 19 deletions

View File

@ -258,21 +258,14 @@ uninstall(){
BIN=install
ARG=""
while [[ $# > 0 || -n "$ARG" ]]; do
if [[ -z "$ARG" ]]; then ARG=$1 ORIGIN_ARG=$1; shift; fi
for i in ${ARG_PARSED[@]}; do
case $ARG in
-i* ) BIN=install ;;
-r* ) BIN=uninstall ;;
-q*|--quite ) export DFS_QUIET=1 ;;
-d*|--dev ) export DFS_DEV=1 ;;
-l*|--lite ) export DFS_LITE=1 ;;
-a*|--auto ) install_dependencies ;;
* ) fmt_warning "unknown command \"$ORIGIN_ARG\". available: -i, -r, -q, -d, -l, -a"; exit 1 ;;
-i ) BIN=install ;;
-r ) BIN=uninstall ;;
-d|--dev ) export DFS_DEV=1 ;;
-l|--lite ) export DFS_LITE=1 ;;
-a|--auto ) install_dependencies ;;
* ) fmt_warning "unknown command \"$ORIGIN_ARG\". available: -i, -r, -q, -d, -l, -a"; exit 1 ;;
esac
if [[ "$ARG" == "--"* || ${#ARG} == 2 ]]; then
ARG=""
else
ARG=-${ARG:2}
fi
done
$BIN

View File

@ -2,11 +2,7 @@
THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
SUDO=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo'
fi
if [[ -f ~/.config/dotfiles/env ]]; then source ~/.config/dotfiles/env; fi
# Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
@ -101,6 +97,31 @@ setup_color() {
}
# END of color settings
SUDO=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo'
fi
# arg parse
ARG=""
ARG_PARSED=()
while [[ $# > 0 || -n "$ARG" ]]; do
if [[ -z "$ARG" ]]; then ARG=$1 ORIGIN_ARG=$1; shift; fi
case $ARG in
-q*|--quite ) export DFS_QUIET=1 ;;
--* ) ARG_PARSED+=("$ARG") ;;
-* ) ARG_PARSED+=("${ARG:0:2}") ;;
* ) fmt_fatal "error parsing argument \"$ORIGIN_ARG\"" ;;
esac
if [[ "$ARG" == "--"* || ${#ARG} == 2 ]]; then
ARG=""
else
ARG=-${ARG:2}
fi
done
unset ARG
ask_for_yN()
{
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do