[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,23 +82,36 @@ 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 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" ] usage $(basename "$0")
exit 0 ;;
esac
done
if [ $# -eq 1 -a -z "$1" ]
then then
usage $(basename $0) usage $(basename "$0")
exit 0 exit 0
else fi
if [ $# -gt $((OPTIND+1)) ]
then
echo "Error! Too many arguments." >&2
exit 1
fi
# Test email arguments # Test email arguments
test_email "$1" eval test_email "\$$OPTIND"
emailuser="$user" emailuser="$user"
emaildomain="$domain" emaildomain="$domain"
if [ $# -eq 2 ] if [ $# -eq $((OPTIND+1)) ]
then then
test_email "$2" eval test_email "\$$((OPTIND+1))"
aliasuser="$user" aliasuser="$user"
aliasdomain="$domain" aliasdomain="$domain"
if [ "$emaildomain" != "$aliasdomain" ] if [ "$emaildomain" != "$aliasdomain" ]
@ -107,7 +120,6 @@ test_args ()
exit 1 exit 1
fi fi
fi fi
fi
} }
# Are we root? # Are we root?