无论标志的顺序是什么,我都希望能够处理给定标志的多个参数。大家觉得这样可以接受吗?有什么改进吗?
所以:
$ ./script -c opt1 opt2 opt3 -b foo
opt1 opt2 opt3
foo
代码:
echo_args () {
echo "$@"
}
while (( $# > 0 )); do
case "$1" in
-b)
echo $2
;;
-c|--create)
c_args=()
# start looping from this flag
for arg in ${@:2}; do
[ "${arg:0:1}" == "-" ] && break
c_args+=("$arg")
done
echo_args "${c_args[@]}"
;;
*)
echo "huh?"
;;
esac
shift 1
done