[email_account] Use getopt to parse options

- Parse options using getopts
- Delete --help option
This commit is contained in:
Thomas Preud'homme 2010-01-26 19:14:51 +01:00 committed by Thomas Preud'homme
parent 068d816a82
commit 5fe150c2a8
1 changed files with 31 additions and 19 deletions

View File

@ -29,7 +29,7 @@ usage ()
echo "Usage :" echo "Usage :"
echo echo
echo "$progname emailaccount [emailalias]" echo "$progname emailaccount [emailalias]"
echo "$progname -h | --help" echo "$progname -h"
echo echo
echo "Fist form adds an email account named emailaccount if it doesn't already exist" 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." echo "and creates an alias named emailalias for this email account if specified."
@ -82,30 +82,42 @@ ask_user_default_no ()
test_args () test_args ()
{ {
local - user domain 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 then
echo "Error! Too many arguments." >&2 echo "Error! Too many arguments." >&2
exit 1 exit 1
fi 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 then
usage $(basename $0) eval test_email "\$$((OPTIND+1))"
exit 0 aliasuser="$user"
else aliasdomain="$domain"
# Test email arguments if [ "$emaildomain" != "$aliasdomain" ]
test_email "$1"
emailuser="$user"
emaildomain="$domain"
if [ $# -eq 2 ]
then then
test_email "$2" echo "Error! Domain of the alias must be identical to the domain of the email account." >&2
aliasuser="$user" exit 1
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
fi fi
fi fi
} }