From 1f82e12eae02d34922a42014aa0772edaf98d27c Mon Sep 17 00:00:00 2001 From: Luigi Semenzato Date: Tue, 23 Mar 2010 12:43:08 -0700 Subject: [PATCH] Auxiliary function for args checks. Review URL: http://codereview.chromium.org/1141010 --- common.sh | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/common.sh b/common.sh index 6c97adc2d2..f869b58e64 100644 --- a/common.sh +++ b/common.sh @@ -255,6 +255,29 @@ is_whitelisted() { test $count -ne 0 } +# Check that all arguments are flags; that is, there are no remaining arguments +# after parsing from shflags. Allow (with a warning) a single empty-string +# argument. +# +# TODO: fix buildbot so that it doesn't pass the empty-string parameter, +# then change this function. +# +# Usage: check_flags_only_and_allow_null_arg "$@" && set -- +function check_flags_only_and_allow_null_arg { + do_shift=1 + if [[ $# == 1 && -z "$@" ]]; then + echo "$0: warning: ignoring null argument" >&2 + shift + do_shift=0 + fi + if [[ $# -gt 0 ]]; then + echo "error: invalid arguments: \"$@\"" >&2 + flags_help + exit 1 + fi + return $do_shift +} + V_RED="\e[31m" V_YELLOW="\e[33m"