|
|
037ebf |
#!/bin/bash
|
|
|
037ebf |
#
|
|
|
037ebf |
# cli_doParseArgumentsCommon.sh -- This function parses positional
|
|
|
037ebf |
# parameters to divide arguments in common argumetns and specific
|
|
|
037ebf |
# arguments. Common arguments might be used by all specific
|
|
|
037ebf |
# functionalities. There is no need to have all common argument
|
|
|
037ebf |
# definitions duplicated in each specific functionality individually.
|
|
|
037ebf |
# Once the value of common arguments have been retrived in FLAG_
|
|
|
037ebf |
# variables they are removed from ARGUMENTS positional parameters, in
|
|
|
037ebf |
# order to leave the specific arguments that specific functionalities
|
|
|
037ebf |
# most interpret.
|
|
|
037ebf |
#
|
|
|
037ebf |
# Copyright (C) 2009-2011 Alain Reguera Delgado
|
|
|
037ebf |
#
|
|
|
037ebf |
# This program is free software; you can redistribute it and/or
|
|
|
037ebf |
# modify it under the terms of the GNU General Public License as
|
|
|
037ebf |
# published by the Free Software Foundation; either version 2 of the
|
|
|
037ebf |
# License, or (at your option) any later version.
|
|
|
037ebf |
#
|
|
|
037ebf |
# This program is distributed in the hope that it will be useful, but
|
|
|
037ebf |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
037ebf |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
037ebf |
# General Public License for more details.
|
|
|
037ebf |
#
|
|
|
037ebf |
# You should have received a copy of the GNU General Public License
|
|
|
037ebf |
# along with this program; if not, write to the Free Software
|
|
|
037ebf |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
037ebf |
# USA.
|
|
|
037ebf |
#
|
|
|
037ebf |
# ----------------------------------------------------------------------
|
|
|
037ebf |
# $Id$
|
|
|
037ebf |
# ----------------------------------------------------------------------
|
|
|
037ebf |
|
|
|
037ebf |
function cli_doParseArgumentsCommon {
|
|
|
037ebf |
|
|
|
037ebf |
local -a SHORT
|
|
|
037ebf |
local -a LONG
|
|
|
037ebf |
local -a REQUIRED
|
|
|
037ebf |
local ARGUMENT=''
|
|
|
037ebf |
local ARGUMENTS_DEFAULT=''
|
|
|
037ebf |
local COMMONS=''
|
|
|
037ebf |
local COMMON=''
|
|
|
037ebf |
local ARGSS=''
|
|
|
037ebf |
local ARGSL=''
|
|
|
037ebf |
local COUNT=0
|
|
|
037ebf |
|
|
|
037ebf |
# Define local array to store short definition of common arguments.
|
|
|
037ebf |
SHORT[0]='f'
|
|
|
037ebf |
SHORT[1]='q'
|
|
|
037ebf |
SHORT[2]='y'
|
|
|
037ebf |
|
|
|
037ebf |
# Define local array to store long definition of common arguments.
|
|
|
037ebf |
LONG[0]='filter'
|
|
|
037ebf |
LONG[1]='quiet'
|
|
|
037ebf |
LONG[2]='answer-yes'
|
|
|
037ebf |
|
|
|
037ebf |
# Define local array to store definition of whether the common
|
|
|
037ebf |
# argument is required [one colon], optional [two colons] or not
|
|
|
037ebf |
# required at all [empty]).
|
|
|
037ebf |
REQUIRED[0]=':'
|
|
|
037ebf |
REQUIRED[1]=''
|
|
|
037ebf |
REQUIRED[2]=''
|
|
|
037ebf |
|
|
|
037ebf |
# Save default arguments passed to centos-art.sh command-line.
|
|
|
037ebf |
# Since ARGUMENTS variable is used as convenction when arguments
|
|
|
037ebf |
# are redefined (see cli_doParseArgumentsReDef), it is required to
|
|
|
037ebf |
# use an intermediate-pattern variable in order to create the
|
|
|
037ebf |
# common and non-comon arguments information from it.
|
|
|
037ebf |
ARGUMENTS_DEFAULT=$ARGUMENTS
|
|
|
037ebf |
|
|
|
037ebf |
# Build list of common arguments.
|
|
|
037ebf |
for ARGUMENT in $ARGUMENTS_DEFAULT;do
|
|
|
037ebf |
while [[ $COUNT -lt ${#LONG[*]} ]];do
|
|
|
037ebf |
if [[ $ARGUMENT =~ "^'(--${LONG[$COUNT]}|-${SHORT[$COUNT]} )" ]];then
|
|
|
037ebf |
if [[ $COMMONS == '' ]];then
|
|
|
037ebf |
COMMONS="${ARGUMENT}"
|
|
|
037ebf |
else
|
|
|
037ebf |
COMMONS="${COMMONS} ${ARGUMENT}"
|
|
|
037ebf |
fi
|
|
|
037ebf |
fi
|
|
|
037ebf |
COUNT=$(($COUNT + 1))
|
|
|
037ebf |
done
|
|
|
037ebf |
# Reset counter.
|
|
|
037ebf |
COUNT=0
|
|
|
037ebf |
done
|
|
|
037ebf |
|
|
|
037ebf |
# Reset positional paramenters to start using common arguments and
|
|
|
037ebf |
# this way be able of performing common arguments verification
|
|
|
037ebf |
# independently from non-common arguments verification (which is
|
|
|
037ebf |
# done inside specific functionalities).
|
|
|
037ebf |
eval set -- "$COMMONS"
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine positional parameters stored inside ARGUMENTS variable
|
|
|
037ebf |
# to use common arguments only.
|
|
|
037ebf |
cli_doParseArgumentsReDef "$@"
|
|
|
037ebf |
|
|
|
037ebf |
# Define short and long arguments variables using getopt format.
|
|
|
037ebf |
# This information is passed to getopt in order to now which the
|
|
|
037ebf |
# common arguments are.
|
|
|
037ebf |
while [[ $COUNT -lt ${#LONG[*]} ]];do
|
|
|
037ebf |
|
|
|
037ebf |
# Define short arguments.
|
|
|
037ebf |
if [[ $ARGSS == '' ]];then
|
|
|
037ebf |
ARGSS=${SHORT[$COUNT]}${REQUIRED[$COUNT]}
|
|
|
037ebf |
else
|
|
|
037ebf |
ARGSS="${ARGSS},${SHORT[$COUNT]}${REQUIRED[$COUNT]}"
|
|
|
037ebf |
fi
|
|
|
037ebf |
|
|
|
037ebf |
# Define long arguments.
|
|
|
037ebf |
if [[ $ARGSL == '' ]];then
|
|
|
037ebf |
ARGSL=${LONG[$COUNT]}${REQUIRED[$COUNT]}
|
|
|
037ebf |
else
|
|
|
037ebf |
ARGSL="${ARGSL},${LONG[$COUNT]}${REQUIRED[$COUNT]}"
|
|
|
037ebf |
fi
|
|
|
037ebf |
|
|
|
037ebf |
# Increment counter.
|
|
|
037ebf |
COUNT=$(($COUNT + 1))
|
|
|
037ebf |
|
|
|
037ebf |
done
|
|
|
037ebf |
|
|
|
037ebf |
# Parse arguments using getopt(1) command parser.
|
|
|
037ebf |
cli_doParseArguments
|
|
|
037ebf |
|
|
|
037ebf |
# Reset positional parameters using output from (getopt) argument
|
|
|
037ebf |
# parser.
|
|
|
037ebf |
eval set -- "$ARGUMENTS"
|
|
|
037ebf |
|
|
|
037ebf |
# Define action to take for each option passed.
|
|
|
037ebf |
while true; do
|
|
|
037ebf |
case "$1" in
|
|
|
037ebf |
|
|
|
037ebf |
--filter )
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine flag.
|
|
|
037ebf |
FLAG_FILTER="$2"
|
|
|
037ebf |
|
|
|
037ebf |
# Break while loop.
|
|
|
037ebf |
shift 2
|
|
|
037ebf |
;;
|
|
|
037ebf |
|
|
|
037ebf |
--quiet )
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine flag.
|
|
|
037ebf |
FLAG_QUIET="true"
|
|
|
037ebf |
|
|
|
037ebf |
# Break while loop.
|
|
|
037ebf |
shift 1
|
|
|
037ebf |
;;
|
|
|
037ebf |
|
|
|
037ebf |
--answer-yes )
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine flag.
|
|
|
037ebf |
FLAG_YES="yes"
|
|
|
037ebf |
|
|
|
037ebf |
# Break while loop.
|
|
|
037ebf |
shift 1
|
|
|
037ebf |
;;
|
|
|
037ebf |
|
|
|
037ebf |
* )
|
|
|
037ebf |
break
|
|
|
037ebf |
;;
|
|
|
037ebf |
esac
|
|
|
037ebf |
done
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine ARGUMENTS to use no-common arguments. Common arguments
|
|
|
037ebf |
# has been already parsed, so free specific functions from parsing
|
|
|
037ebf |
# them (there is no need to parse them twice).
|
|
|
037ebf |
eval set -- "$(echo $ARGUMENTS_DEFAULT | sed -r "s!${COMMONS}!!g")"
|
|
|
037ebf |
|
|
|
037ebf |
# Redefine positional parameters stored inside ARGUMENTS variable.
|
|
|
037ebf |
cli_doParseArgumentsReDef "$@"
|
|
|
037ebf |
|
|
|
037ebf |
}
|