From 8b7a32ccfb916c928b6f71900e1ec09aa0e23b67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gardais=20J=C3=A9r=C3=A9my?= Date: Thu, 24 Sep 2020 15:18:09 +0200 Subject: [PATCH] Allow some basics options (-h and -d) --- cluster/apt.check.update.sh | 82 +++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 18 deletions(-) diff --git a/cluster/apt.check.update.sh b/cluster/apt.check.update.sh index c605032..b594f7d 100755 --- a/cluster/apt.check.update.sh +++ b/cluster/apt.check.update.sh @@ -13,7 +13,7 @@ readonly PROGNAME=$(basename "${0}") readonly PROGDIR=$(readlink -m $(dirname "${0}")) readonly ARGS="${*}" readonly NBARGS="${#}" -[ -z "${DEBUG}" ] && readonly DEBUG=1 +[ -z "${DEBUG}" ] && DEBUG=1 ## Export DEBUG for sub-script export DEBUG @@ -26,21 +26,6 @@ readonly RESET='\033[0m' readonly COLOR_DEBUG="${PURPLE}" # }}} -manage_args() { # {{{ - - case "${NBARGS}" in - 0 ) ## Nothing to do - ;; - * ) - printf '%b\n' "${RED}Don't expect any arguments.${RESET}" - printf '%b\n' "---" - usage - exit 1 - ;; - esac - -} -# }}} usage() { # {{{ cat <<- EOF @@ -53,6 +38,13 @@ try to prepare the host by : EXAMPLES : - Verify upgrade and prepare the current host ${PROGNAME} + +OPTIONS : + -d,--debug + Enable debug messages. + + -h,--help + Print this help message. EOF } @@ -141,8 +133,6 @@ Pending jobs for the compute cluster: ${RED}${local_pending_jobs:=/dev/null}${CO # }}} main() { # {{{ - manage_args "${ARGS}" - sge_disable_host_queue_script="${PROGDIR}/sge.disable.host.queue.sh" ## If NO APT package upgrade is available {{{ @@ -176,6 +166,62 @@ main() { # {{{ } # }}} +# Manage arguments # {{{ +# This code can't be in a function due to arguments + +if [ ! "${NBARGS}" -eq "0" ]; then + + manage_arg="0" + + ## If the first argument is not an option + if ! printf -- '%s' "${1}" | grep -q -E -- "^-+"; + then + ## Print help message and exit + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + + exit 1 + fi + + # Parse all options (start with a "-") one by one + while printf -- '%s' "${1}" | grep -q -E -- "^-+"; do + + case "${1}" in + -d|--debug ) ## debug + DEBUG=0 + ;; + -h|--help ) ## help + usage + ## Exit after help informations + exit 0 + ;; + * ) ## unknow option + printf '%b\n' "${RED}Invalid option: ${1}${RESET}" + printf '%b\n' "---" + usage + exit 1 + ;; + esac + + debug_message "Arguments management − \ +${RED}${1}${COLOR_DEBUG} option managed." + + ## Move to the next argument + shift + manage_arg=$((manage_arg+1)) + + done + + debug_message "Arguments management − \ +${RED}${manage_arg}${COLOR_DEBUG} argument(s) successfully managed." +else + debug_message "Arguments management − \ +No arguments/options to manage." +fi + +# }}} + main exit 255