diff --git a/tools/common.sh b/tools/common.sh index 1e019d9..4e9163e 100755 --- a/tools/common.sh +++ b/tools/common.sh @@ -6,7 +6,7 @@ if [[ -f ~/.config/dotfiles/env ]]; then set -a; source ~/.config/dotfiles/env; # parse args and set env, when it is sourced if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then - ORIGIN_ARGS="$@" + ORIGIN_ARGS=("$@") ARG="" GOT_OPTS=() while [[ $# > 0 || -n "$ARG" ]]; do @@ -26,7 +26,7 @@ if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then ARG=-${ARG:2} fi done - set -- "$ORIGIN_ARGS" + set -- "${ORIGIN_ARGS[@]}" unset ARG unset ORIGIN_ARGS fi diff --git a/tools/logger.sh b/tools/logger.sh index 1fc0342..df2b527 100755 --- a/tools/logger.sh +++ b/tools/logger.sh @@ -79,22 +79,27 @@ print_help() fmt_info "usage: $0 " } -if [[ $# != 2 ]]; then - print_help >&2 - exit 1 -fi +router() +{ + if [[ $# != 2 ]]; then + print_help >&2 + exit 1 + fi -case "$1" in - -h|--help) - fmt_info "usage: $0 " - ;; - beacon) - post_beacon "$2" - ;; - log) - post_log "$2" - ;; - *) - fmt_fatal "invalid argument" - ;; -esac + case "$1" in + -h|--help) + fmt_info "usage: $0 " + ;; + beacon) + post_beacon "$2" + ;; + log) + post_log "$2" + ;; + *) + fmt_fatal "invalid argument" + ;; + esac +} + +router "${GOT_OPTS[@]}" \ No newline at end of file diff --git a/tools/test-getopts.sh b/tools/test-getopts.sh index 430910d..9e14e47 100755 --- a/tools/test-getopts.sh +++ b/tools/test-getopts.sh @@ -1,14 +1,21 @@ #!/bin/bash set -ex -OPTS="-a -bcl --color --arg1=1 --arg2 2 yes" -TARGET_OPTS="-a -b -c --arg1 1 --arg2 2 yes" -set -- $OPTS +OPTS='-a -bcl --color --arg1=1 --arg2 2 " 1 2" yes' +TARGET_OPTS='-a -b -c --arg1 1 --arg2 2 1 2 yes' +eval set -- $OPTS THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) source "$THIS_DIR/common.sh" test "${GOT_OPTS[*]}" = "$TARGET_OPTS" -test "$*" = "$OPTS" +test $# -eq 8 +test "$*" = "${OPTS//\"/}" test "$DFS_LITE" = "1" is_tty -test -z "$DFS_QUIET" \ No newline at end of file +test -z "$DFS_QUIET" + +set +x +echo "test passed, args:" +for i in "${GOT_OPTS[@]}"; do + echo "$i" +done \ No newline at end of file