Blame Automation/Modules/Cli/Scripts/cli_getOptions.sh

Alain Reguera Delgado ddf7d2
#!/bin/bash
Alain Reguera Delgado ddf7d2
######################################################################
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
#   cli_getOptions.sh -- This function parses common options passed to
Alain Reguera Delgado ddf7d2
#   to `centos-art.sh' script. Because option parsing is different for
Alain Reguera Delgado ddf7d2
#   each function environment inside centos-art.sh script, we cannot
Alain Reguera Delgado ddf7d2
#   use getopt here. That would enter in conflict with options from
Alain Reguera Delgado ddf7d2
#   deeper function environments making impossible to parse both
Alain Reguera Delgado ddf7d2
#   common and specific options through getopt in a modular way (e.g.,
Alain Reguera Delgado ddf7d2
#   without duplicating common option definition inside each different
Alain Reguera Delgado ddf7d2
#   module the centos-art.sh script is made of).
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
#   To solve this issue centos-art.sh script restricts the order in
Alain Reguera Delgado ddf7d2
#   which common options are passed through the command line. This
Alain Reguera Delgado ddf7d2
#   way, common options must be passed first and specific options
Alain Reguera Delgado ddf7d2
#   later. Common options are defined in this file and specific
Alain Reguera Delgado ddf7d2
#   options are defined in module-specific files.
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
#   This introduces the nature of common options: common options do
Alain Reguera Delgado ddf7d2
#   not modify the centos-art.sh script behaviour in any way. Common
Alain Reguera Delgado ddf7d2
#   options exist to show information about centos-art.sh script and
Alain Reguera Delgado ddf7d2
#   its modules only (e.g., --help and --version) and once done, exit
Alain Reguera Delgado ddf7d2
#   successfully without processing any other option or argument
Alain Reguera Delgado ddf7d2
#   passed in the command-line.
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
#   Written by: 
Alain Reguera Delgado ddf7d2
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado ddf7d2
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado ddf7d2
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado ddf7d2
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado ddf7d2
# your option) any later version.
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado ddf7d2
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado ddf7d2
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado ddf7d2
# General Public License for more details.
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado ddf7d2
# along with this program; if not, write to the Free Software
Alain Reguera Delgado ddf7d2
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado ddf7d2
#
Alain Reguera Delgado ddf7d2
######################################################################
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
function cli_getOptions {
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Define valid short options.
Alain Reguera Delgado ddf7d2
    local ARGSS="h,v"
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Define valid long options.
Alain Reguera Delgado ddf7d2
    local ARGSL="help,version"
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Initialize arguments local to this function environment.
Alain Reguera Delgado ddf7d2
    local TCAR_ARGUMENTS=''
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Redefine arguments using getopt(1) command parser.
Alain Reguera Delgado ddf7d2
    cli_setArguments "${@}"
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Reset positional parameters on this function, using output
Alain Reguera Delgado ddf7d2
    # produced from (getopt) arguments parser.
Alain Reguera Delgado ddf7d2
    eval set -- "${TCAR_ARGUMENTS}"
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    # Look for options passed through command-line.
Alain Reguera Delgado ddf7d2
    while true; do
Alain Reguera Delgado ddf7d2
        case ${1} in
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
            -h | --help )
Alain Reguera Delgado ddf7d2
                cli_printHelp
Alain Reguera Delgado ddf7d2
                ;;
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
            -v | --version )
Alain Reguera Delgado ddf7d2
                cli_printVersion
Alain Reguera Delgado ddf7d2
                ;;
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
            * )
Alain Reguera Delgado ddf7d2
                # At this point none of the valid options were
Alain Reguera Delgado ddf7d2
                # provided so, finish the script execution. This is
Alain Reguera Delgado ddf7d2
                # not absolutely needed because getopt will complain
Alain Reguera Delgado ddf7d2
                # about non-valid options and probably never execution
Alain Reguera Delgado ddf7d2
                # reaches this point. Consider this a backup
Alain Reguera Delgado ddf7d2
                # verification.
Alain Reguera Delgado ddf7d2
                exit 0
Alain Reguera Delgado ddf7d2
                ;;
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
        esac
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
    done
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
}