Blame Automation/Scripts/tcar_setArguments.sh

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