Blame Scripts/Bash/Cli/Functions/cli_doParseArguments.sh

b7b189
#!/bin/bash
b7b189
#
f4da7e
# cli_doParseArguments.sh -- This function redefines arguments
f4da7e
# (ARGUMENTS) global variable using getopt(1) output.
b7b189
#
073c6f
# Copyright (C) 2009-2011 Alain Reguera Delgado
b7b189
# 
b7b189
# This program is free software; you can redistribute it and/or
b7b189
# modify it under the terms of the GNU General Public License as
b7b189
# published by the Free Software Foundation; either version 2 of the
b7b189
# License, or (at your option) any later version.
b7b189
# 
b7b189
# This program is distributed in the hope that it will be useful, but
b7b189
# WITHOUT ANY WARRANTY; without even the implied warranty of
b7b189
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b7b189
# General Public License for more details.
b7b189
#
b7b189
# You should have received a copy of the GNU General Public License
b7b189
# along with this program; if not, write to the Free Software
b7b189
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
b7b189
# USA.
b7b189
# 
b7b189
# ----------------------------------------------------------------------
9aebad
# $Id$
b7b189
# ----------------------------------------------------------------------
b7b189
b7b189
function cli_doParseArguments {
b7b189
f23cb5
    local ARG1=''
f23cb5
    local ARG2=''
f23cb5
    local COUNT=0
f23cb5
f23cb5
    # Verify no option has been passed twice in the command-line.
f23cb5
    for ARG1 in $ARGUMENTS;do
f23cb5
        ARG1=$(echo $ARG1 | sed -r "s!^'(--[[:alpha:]-]+)=?.+'!\1!")
f23cb5
        for ARG2 in $ARGUMENTS;do
f23cb5
            ARG2=$(echo $ARG2 | sed -r "s!^'(--[[:alpha:]-]+)=?.+'!\1!")
f23cb5
            if [[ $ARG1 == $ARG2 ]];then
f23cb5
                COUNT=$(($COUNT + 1))
f23cb5
            fi
f23cb5
            #echo "$ARG1 : $ARG2 : $COUNT"
f23cb5
            if [[ $COUNT -gt 1 ]];then
f23cb5
                cli_printMessage "`eval_gettext "The option \\\`\\\$ARG1' can't be duplicated."`" 'AsErrorLine'
f23cb5
                cli_printMessage "$(caller)" 'AsToKnowMoreLine'
f23cb5
            fi
f23cb5
        done
f23cb5
        COUNT=0
f23cb5
    done
f23cb5
b7b189
    # Reset positional parameters using optional arguments.
b7b189
    eval set -- "$ARGUMENTS"
b7b189
b7b189
    # Parse optional arguments using getopt.
7b46a4
    ARGUMENTS=$(getopt -o "$ARGSS" -l "$ARGSL" -n $CLI_PROGRAM -- "$@")
b7b189
b7b189
    # Be sure getout parsed arguments successfully.
b7b189
    if [[ $? != 0 ]]; then 
b7b189
        cli_printMessage "$(caller)" 'AsToKnowMoreLine'
b7b189
    fi
b7b189
b7b189
}