|
|
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=''
|
|
|
3e903f |
local PATTERN=''
|
|
|
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'
|
|
|
210cfa |
SHORT[3]='c'
|
|
|
037ebf |
|
|
|
037ebf |
# Define local array to store long definition of common arguments.
|
|
|
037ebf |
LONG[0]='filter'
|
|
|
037ebf |
LONG[1]='quiet'
|
|
|
5942a5 |
LONG[2]='answer'
|
|
|
210cfa |
LONG[3]='dont-commit-changes'
|
|
|
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]=''
|
|
|
5942a5 |
REQUIRED[2]=':'
|
|
|
210cfa |
REQUIRED[3]=''
|
|
|
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
|
|
|
3e903f |
|
|
|
037ebf |
while [[ $COUNT -lt ${#LONG[*]} ]];do
|
|
|
3e903f |
|
|
|
3e903f |
# Be specific about the pattern used to match both long
|
|
|
3e903f |
# and short arguments. Notice that when arguments values
|
|
|
3e903f |
# are required we add an equal sign (`=') and a space (`
|
|
|
3e903f |
# ') character to the end of pattern in order to match
|
|
|
3e903f |
# both long and short arguments respectively.
|
|
|
3e903f |
if [[ ${REQUIRED[$COUNT]} =~ '^:$' ]];then
|
|
|
3e903f |
PATTERN="^'(--${LONG[$COUNT]}=|-${SHORT[$COUNT]} )"
|
|
|
3e903f |
else
|
|
|
3e903f |
PATTERN="^'(--${LONG[$COUNT]}|-${SHORT[$COUNT]})"
|
|
|
3e903f |
fi
|
|
|
3e903f |
|
|
|
3e903f |
# Check argument against common argument pattern.
|
|
|
3e903f |
if [[ $ARGUMENT =~ "${PATTERN}" ]];then
|
|
|
037ebf |
if [[ $COMMONS == '' ]];then
|
|
|
037ebf |
COMMONS="${ARGUMENT}"
|
|
|
037ebf |
else
|
|
|
037ebf |
COMMONS="${COMMONS} ${ARGUMENT}"
|
|
|
037ebf |
fi
|
|
|
037ebf |
fi
|
|
|
3e903f |
|
|
|
3e903f |
# Increment counter.
|
|
|
037ebf |
COUNT=$(($COUNT + 1))
|
|
|
3e903f |
|
|
|
037ebf |
done
|
|
|
3e903f |
|
|
|
037ebf |
# Reset counter.
|
|
|
037ebf |
COUNT=0
|
|
|
3e903f |
|
|
|
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 |
FLAG_FILTER="$2"
|
|
|
037ebf |
shift 2
|
|
|
037ebf |
;;
|
|
|
037ebf |
|
|
|
037ebf |
--quiet )
|
|
|
037ebf |
FLAG_QUIET="true"
|
|
|
037ebf |
shift 1
|
|
|
037ebf |
;;
|
|
|
037ebf |
|
|
|
5942a5 |
--answer )
|
|
|
5942a5 |
FLAG_ANSWER="$2"
|
|
|
5942a5 |
shift 2
|
|
|
210cfa |
;;
|
|
|
037ebf |
|
|
|
210cfa |
--dont-commit-changes )
|
|
|
210cfa |
FLAG_DONT_COMMIT_CHANGES="true"
|
|
|
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).
|
|
|
210cfa |
if [[ ${COMMONS} != '' ]];then
|
|
|
3e903f |
|
|
|
3e903f |
# Escape special regular expression characters that might be
|
|
|
3e903f |
# passed through common arguments like `--filter' in order to
|
|
|
3e903f |
# interpreted them literally. Otherwise they might be
|
|
|
3e903f |
# interpreted when they be stript out from default arguments.
|
|
|
210cfa |
COMMONS=$(echo ${COMMONS} | sed -r 's!(\(|\[|\{|\.|\+|\*)!\\\1!g')
|
|
|
3e903f |
|
|
|
210cfa |
# Stript out common arguments from default arguments one by
|
|
|
210cfa |
# one to avoid order restrictions when removing them from
|
|
|
210cfa |
# string of default arguments.
|
|
|
210cfa |
for COMMON in ${COMMONS};do
|
|
|
210cfa |
ARGUMENTS_DEFAULT=$(echo ${ARGUMENTS_DEFAULT} | sed -r "s!${COMMON}!!g")
|
|
|
210cfa |
done
|
|
|
3e903f |
|
|
|
62abc4 |
fi
|
|
|
037ebf |
|
|
|
210cfa |
# Use just default arguments. No common argument was passed.
|
|
|
210cfa |
eval set -- "${ARGUMENTS_DEFAULT}"
|
|
|
210cfa |
|
|
|
037ebf |
# Redefine positional parameters stored inside ARGUMENTS variable.
|
|
|
037ebf |
cli_doParseArgumentsReDef "$@"
|
|
|
037ebf |
|
|
|
037ebf |
}
|