mirror of
https://github.com/DictXiong/dotfiles.git
synced 2025-04-28 23:02:22 +08:00
common.sh: argparser supports spaces
This commit is contained in:
parent
b62207db4d
commit
6bdba0a1b3
|
@ -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
|
# parse args and set env, when it is sourced
|
||||||
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||||
ORIGIN_ARGS="$@"
|
ORIGIN_ARGS=("$@")
|
||||||
ARG=""
|
ARG=""
|
||||||
GOT_OPTS=()
|
GOT_OPTS=()
|
||||||
while [[ $# > 0 || -n "$ARG" ]]; do
|
while [[ $# > 0 || -n "$ARG" ]]; do
|
||||||
|
@ -26,7 +26,7 @@ if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
|
||||||
ARG=-${ARG:2}
|
ARG=-${ARG:2}
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
set -- "$ORIGIN_ARGS"
|
set -- "${ORIGIN_ARGS[@]}"
|
||||||
unset ARG
|
unset ARG
|
||||||
unset ORIGIN_ARGS
|
unset ORIGIN_ARGS
|
||||||
fi
|
fi
|
||||||
|
|
|
@ -79,12 +79,14 @@ print_help()
|
||||||
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ $# != 2 ]]; then
|
router()
|
||||||
|
{
|
||||||
|
if [[ $# != 2 ]]; then
|
||||||
print_help >&2
|
print_help >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h|--help)
|
-h|--help)
|
||||||
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
||||||
;;
|
;;
|
||||||
|
@ -97,4 +99,7 @@ case "$1" in
|
||||||
*)
|
*)
|
||||||
fmt_fatal "invalid argument"
|
fmt_fatal "invalid argument"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
router "${GOT_OPTS[@]}"
|
|
@ -1,14 +1,21 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -ex
|
set -ex
|
||||||
OPTS="-a -bcl --color --arg1=1 --arg2 2 yes"
|
OPTS='-a -bcl --color --arg1=1 --arg2 2 " 1 2" yes'
|
||||||
TARGET_OPTS="-a -b -c --arg1 1 --arg2 2 yes"
|
TARGET_OPTS='-a -b -c --arg1 1 --arg2 2 1 2 yes'
|
||||||
set -- $OPTS
|
eval set -- $OPTS
|
||||||
|
|
||||||
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
|
||||||
source "$THIS_DIR/common.sh"
|
source "$THIS_DIR/common.sh"
|
||||||
|
|
||||||
test "${GOT_OPTS[*]}" = "$TARGET_OPTS"
|
test "${GOT_OPTS[*]}" = "$TARGET_OPTS"
|
||||||
test "$*" = "$OPTS"
|
test $# -eq 8
|
||||||
|
test "$*" = "${OPTS//\"/}"
|
||||||
test "$DFS_LITE" = "1"
|
test "$DFS_LITE" = "1"
|
||||||
is_tty
|
is_tty
|
||||||
test -z "$DFS_QUIET"
|
test -z "$DFS_QUIET"
|
||||||
|
|
||||||
|
set +x
|
||||||
|
echo "test passed, args:"
|
||||||
|
for i in "${GOT_OPTS[@]}"; do
|
||||||
|
echo "$i"
|
||||||
|
done
|
Loading…
Reference in New Issue
Block a user