From 5fe150c2a8ddf5f0efd4ba328c3119147a9e0bb0 Mon Sep 17 00:00:00 2001 From: Thomas Preud'homme Date: Tue, 26 Jan 2010 19:14:51 +0100 Subject: [PATCH] [email_account] Use getopt to parse options - Parse options using getopts - Delete --help option --- email_account/email_account | 50 +++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/email_account/email_account b/email_account/email_account index 5297f00..8d1ce0a 100755 --- a/email_account/email_account +++ b/email_account/email_account @@ -29,7 +29,7 @@ usage () echo "Usage :" echo echo "$progname emailaccount [emailalias]" - echo "$progname -h | --help" + echo "$progname -h" echo echo "Fist form adds an email account named emailaccount if it doesn't already exist" echo "and creates an alias named emailalias for this email account if specified." @@ -82,30 +82,42 @@ ask_user_default_no () test_args () { local - user domain - if [ \( $# -gt 1 -a "$1" = "-h" \) -o $# -gt 2 ] + while getopts "h" opt + do + case $opt in + "h") + if [ $# -gt 1 ] + then + echo "Error! Too many arguments." >&2 + exit 1 + fi + usage $(basename "$0") + exit 0 ;; + esac + done + if [ $# -eq 1 -a -z "$1" ] + then + usage $(basename "$0") + exit 0 + fi + if [ $# -gt $((OPTIND+1)) ] then echo "Error! Too many arguments." >&2 exit 1 fi - if [ -z "$1" -o "$1" = "-h" -o "$1" = "-help" ] + # Test email arguments + eval test_email "\$$OPTIND" + emailuser="$user" + emaildomain="$domain" + if [ $# -eq $((OPTIND+1)) ] then - usage $(basename $0) - exit 0 - else - # Test email arguments - test_email "$1" - emailuser="$user" - emaildomain="$domain" - if [ $# -eq 2 ] + eval test_email "\$$((OPTIND+1))" + aliasuser="$user" + aliasdomain="$domain" + if [ "$emaildomain" != "$aliasdomain" ] then - test_email "$2" - aliasuser="$user" - aliasdomain="$domain" - if [ "$emaildomain" != "$aliasdomain" ] - then - echo "Error! Domain of the alias must be identical to the domain of the email account." >&2 - exit 1 - fi + echo "Error! Domain of the alias must be identical to the domain of the email account." >&2 + exit 1 fi fi }