diff --git a/install.sh b/install.sh index 9297baa..22c96e2 100755 --- a/install.sh +++ b/install.sh @@ -209,7 +209,7 @@ install_update() DFS_UPDATED_RET=85 ${DOTFILES}/update.sh if [[ $? == 85 ]]; then fmt_note "dfs updated. re-running install.sh ..." - "${DOTFILES}/install.sh" "$ORIGIN_ARGS" && exit + "${DOTFILES}/install.sh" "$@" && exit fi } @@ -249,19 +249,16 @@ uninstall() fmt_note "done uninstalling!" } -ORIGIN_ARGS="$@" -parse_arg "$@" FUNC=install INSTALL_DEP=0 -for i in ${PARSE_ARG_RET[@]}; do +for i in ${GOT_OPTS[@]}; do case $i in -i ) FUNC=install ;; -r ) FUNC=uninstall ;; -d|--dev ) export DFS_DEV=1 ;; - -l|--lite ) export DFS_LITE=1 ;; -a|--auto ) INSTALL_DEP=1 ;; -s|--secure ) export DFS_DEV=0 ;; - * ) fmt_fatal "unknown option \"$i\". available: -i, -r, -q, -d, -l, -a, -s" ;; + * ) fmt_fatal "unknown option \"$i\"" ;; esac done $FUNC diff --git a/tools/alpine.sh b/tools/alpine.sh index ba32b54..1bf3688 100755 --- a/tools/alpine.sh +++ b/tools/alpine.sh @@ -34,8 +34,8 @@ router() apk-add ) apk_add ;; set-timezone | set-tz ) set_timezone ;; set-mirror ) set_mirror $2 ;; - * ) echo unknown command "$1". available: apk-add, set-timezone;; + * ) echo unknown command \"$1\". available: apk-add, set-timezone;; esac } -router $@ +router "${GOT_OPTS[@]}" diff --git a/tools/common.sh b/tools/common.sh index 7d65a68..1e019d9 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -4,6 +4,33 @@ THIS_DIR_COMMON_SH=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) export DOTFILES=$( cd "$THIS_DIR_COMMON_SH/.." && pwd ) if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; set +a; fi +# parse args and set env, when it is sourced +if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then + ORIGIN_ARGS="$@" + ARG="" + GOT_OPTS=() + while [[ $# > 0 || -n "$ARG" ]]; do + if [[ -z "$ARG" ]]; then ARG=$1; shift; fi + case $ARG in + -q*|--quite ) export DFS_QUIET=1 ;; + -l*|--lite ) export DFS_LITE=1 ;; + --color ) export DFS_COLOR=1 ;; + --*=* ) GOT_OPTS+=("${ARG%%=*}" "${ARG#*=}") ;; + --* ) GOT_OPTS+=("$ARG") ;; + -* ) GOT_OPTS+=("${ARG:0:2}") ;; + * ) GOT_OPTS+=("$ARG") ;; + esac + if [[ "$ARG" == "--"* || ! "$ARG" == "-"* || ${#ARG} -le 2 ]]; then + ARG="" + else + ARG=-${ARG:2} + fi + done + set -- "$ORIGIN_ARGS" + unset ARG + unset ORIGIN_ARGS +fi + # Color settings # Source: https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh if [[ -t 1 || "$DFS_COLOR" == "1" ]]; then @@ -102,28 +129,6 @@ if [[ "$EUID" != "0" && -x $(command -v sudo) ]]; then SUDO='sudo' fi -parse_arg() -{ - local ARG="" - PARSE_ARG_RET=() - while [[ $# > 0 || -n "$ARG" ]]; do - if [[ -z "$ARG" ]]; then ARG=$1; shift; fi - case $ARG in - -q*|--quite ) export DFS_QUIET=1 ;; - -l*|--lite ) export DFS_LITE=1 ;; - --color ) export DFS_COLOR=1; is_tty() { true }; setup_color ;; - --* ) PARSE_ARG_RET+=("$ARG") ;; - -* ) PARSE_ARG_RET+=("${ARG:0:2}") ;; - * ) PARSE_ARG_RET+=("$ARG") ;; - esac - if [[ "$ARG" == "--"* || ! "$ARG" == "-"* || ${#ARG} -le 2 ]]; then - ARG="" - else - ARG=-${ARG:2} - fi - done -} - ask_for_yN() { while [[ -z "$DFS_QUIET" || "$DFS_QUIET" == "0" ]]; do diff --git a/tools/macos.sh b/tools/macos.sh index fca9e2a..bfd2ba0 100755 --- a/tools/macos.sh +++ b/tools/macos.sh @@ -13,8 +13,8 @@ router() { case $1 in brew-install ) brew_install ;; - * ) echo unknown command "$1". available: brew-install;; + * ) echo unknown command \"$1\". available: brew-install;; esac } -router $@ +router "${GOT_OPTS[@]}" diff --git a/tools/msys2.sh b/tools/msys2.sh index a1c7c6b..7f230b6 100755 --- a/tools/msys2.sh +++ b/tools/msys2.sh @@ -27,8 +27,8 @@ router() case $1 in pacman-S ) pacman_S ;; set-mirror ) set_mirror $2 ;; - * ) echo unknown command "$1". available: pacman-S, set-mirror ;; + * ) echo unknown command \"$1\". available: pacman-S, set-mirror ;; esac } -router $@ +router "${GOT_OPTS[@]}" diff --git a/tools/ubuntu.sh b/tools/ubuntu.sh index 483c2f8..224ebfb 100755 --- a/tools/ubuntu.sh +++ b/tools/ubuntu.sh @@ -35,8 +35,8 @@ router() set-mirror ) set_mirror $2 ;; set-timezone\ | set-tz ) set_timezone $2 ;; - * ) echo unknown command "$1". available: apt-install, set-mirror, set-timezone;; + * ) echo unknown command \"$1\". available: apt-install, set-mirror, set-timezone;; esac } -router $@ +router "${GOT_OPTS[@]}"