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

View File

@ -2,11 +2,7 @@
THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd ) export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd )
if [[ -f ~/.config/dotfiles/env ]]; then source ~/.config/dotfiles/env; fi
SUDO=''
if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then
SUDO='sudo'
fi
# Color settings # Color settings
# Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh # Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh
@ -101,6 +97,31 @@ setup_color() {
} }
# END of color settings # 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() ask_for_yN()
{ {
while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do