mirror of
https://github.com/DictXiong/dotfiles.git
synced 2025-04-24 19:07:08 +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
|
||||
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
|
||||
|
|
|
@ -79,22 +79,27 @@ print_help()
|
|||
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
||||
}
|
||||
|
||||
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|log> <beacon_type|log_content>"
|
||||
;;
|
||||
beacon)
|
||||
post_beacon "$2"
|
||||
;;
|
||||
log)
|
||||
post_log "$2"
|
||||
;;
|
||||
*)
|
||||
fmt_fatal "invalid argument"
|
||||
;;
|
||||
esac
|
||||
case "$1" in
|
||||
-h|--help)
|
||||
fmt_info "usage: $0 <beacon|log> <beacon_type|log_content>"
|
||||
;;
|
||||
beacon)
|
||||
post_beacon "$2"
|
||||
;;
|
||||
log)
|
||||
post_log "$2"
|
||||
;;
|
||||
*)
|
||||
fmt_fatal "invalid argument"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
router "${GOT_OPTS[@]}"
|
|
@ -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"
|
||||
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