方式二:推荐
# 处理参数信息
judgment_parameters() {
local HELP=""
while [[ "$#" -gt '0' ]]; do
case "$1" in
'-i' | '--image')
if [[ -z "${2:-}" ]]; then
echo "?error: Please specify the correct image name."
exit 1
fi
IMAGE_NAME="${2}"
shift
;;
'-r' | '--registry')
if [[ -z "${2:-}" ]]; then
echo "error: Please specify the correct registry name."
exit 1
fi
IMAGE_REGISTRY="${2}"
shift
;;
'-h' | '--help')
HELP='1'
break
;;
*)
echo "$0: unknown option -- $1"
exit 1
;;
esac
shift
done
if [ -n "${HELP}" ]; then
show_help
fi
}
# 打印帮助信息
show_help() {
echo "usage: $0 [ options ]"
echo ' -i, --image [IMAGE_NAME] example: nginx:latest, my/nginx:latest'
echo ' -r, --registry [REGISTRY_NAME] options: g,gcr | h,ghcr | k,k8s | q,quay | m,mcr'
echo ' -h, --help print help'
exit 0
}
judgment_parameters "$@"