Blame Scripts/tcar_setModuleArguments.sh

Alain Reguera Delgado e54180
#!/bin/bash
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   tcar_setModuleArguments.sh -- This function uses getopt to process
Alain Reguera Delgado e54180
#   arguments passed to centos-art.sh script.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   This function works with the following three variables:
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#       ARGSS
Alain Reguera Delgado e54180
#           Stores getopt short arguments definition.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#       ARGSL
Alain Reguera Delgado e54180
#           Stores getopt long arguments definition.  
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#       TCAR_MODULE_ARGUMENT
Alain Reguera Delgado e54180
#           Stores arguments passed to functions or command-line
Alain Reguera Delgado e54180
#           interface depending the context it is defined.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   These three variables are not defined in this function but the
Alain Reguera Delgado e54180
#   function environment you want to provide option parsing for,
Alain Reguera Delgado e54180
#   through getopt command. Using local definition for these three
Alain Reguera Delgado e54180
#   variables let you to nest option parsing inside different
Alain Reguera Delgado e54180
#   function-environment levels.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   Written by:
Alain Reguera Delgado e54180
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado e54180
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado e54180
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado e54180
# your option) any later version.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado e54180
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado e54180
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado e54180
# General Public License for more details.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado e54180
# along with this program; if not, write to the Free Software
Alain Reguera Delgado e54180
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
function tcar_setModuleArguments {
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define gettext-specific variables locally, to provide
Alain Reguera Delgado e54180
    # per-function localization.
Alain Reguera Delgado e54180
    local TEXTDOMAIN="${FUNCNAME}.sh"
Alain Reguera Delgado e54180
    local TEXTDOMAINDIR="${TCAR_SCRIPT_BASEDIR}/Scripts/Locales"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    local ARGUMENT=''
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Fill up arguments global variable with current positional
Alain Reguera Delgado e54180
    # parameter  information. To avoid interpretation problems, use
Alain Reguera Delgado e54180
    # single quotes to enclose each argument (TCAR_MODULE_ARGUMENT) from
Alain Reguera Delgado e54180
    # command-line individually.
Alain Reguera Delgado e54180
    for ARGUMENT in "${@}"; do
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
        # Remove any single quote from arguments passed to
Alain Reguera Delgado e54180
        # centos-art.sh script. We will use single quotes for grouping
Alain Reguera Delgado e54180
        # option values so white space can be passed through them.
Alain Reguera Delgado e54180
        ARGUMENT=$(printf %s "${ARGUMENT}" | tr -d "'")
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
        # Concatenate arguments and enclose them to let getopt to
Alain Reguera Delgado e54180
        # process them when they have spaces inside.
Alain Reguera Delgado e54180
        TCAR_MODULE_ARGUMENT="${TCAR_MODULE_ARGUMENT} '${ARGUMENT}'"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    done
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Verify non-option arguments passed to command-line. If there
Alain Reguera Delgado e54180
    # isn't any or dot is provided, redefine the TCAR_MODULE_ARGUMENT
Alain Reguera Delgado e54180
    # variable to use the current location the centos-art.sh script
Alain Reguera Delgado e54180
    # was called from.
Alain Reguera Delgado e54180
    if [[ -z "${TCAR_MODULE_ARGUMENT}" ]];then
Alain Reguera Delgado e54180
        TCAR_MODULE_ARGUMENT=${PWD}
Alain Reguera Delgado e54180
    fi
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Verify presence of either short or long options in the
Alain Reguera Delgado e54180
    # environment. If they are present apply option validation through
Alain Reguera Delgado e54180
    # getopt.
Alain Reguera Delgado e54180
    if [[ ! -z ${ARGSS} ]] || [[ ! -z ${ARGSL} ]];then
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
        # Redefine positional parameters using TCAR_MODULE_ARGUMENT variable.
Alain Reguera Delgado e54180
        eval set -- "${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
        # Process positional parameters using getopt's option validation.
Alain Reguera Delgado e54180
        TCAR_MODULE_ARGUMENT=$(getopt -o "${ARGSS}" -l "${ARGSL}" \
Alain Reguera Delgado e54180
            -n "${TCAR_SCRIPT_COMMAND} (${TCAR_MODULE_NAME})" -- "${@}")
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
        # Verify getopt's exit status and finish the script execution
Alain Reguera Delgado e54180
        # with an error message, if it failed.
Alain Reguera Delgado e54180
        if [[ $? -ne 0 ]];then
Alain Reguera Delgado e54180
            tcar_printMessage "`gettext "The argument verification failed."`" --as-error-line
Alain Reguera Delgado e54180
        fi
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    fi
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
}