Manage options outside of a function

This commit is contained in:
Jeremy Gardais 2020-06-12 15:13:53 +02:00
parent f51f207890
commit 19dcf6a96e
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 51 additions and 18 deletions

View File

@ -12,7 +12,7 @@ readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}")) readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}" readonly ARGS="${*}"
readonly NBARGS="${#}" readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && readonly DEBUG=0 [ -z "${DEBUG}" ] && DEBUG=1
## Export DEBUG for sub-script ## Export DEBUG for sub-script
export DEBUG export DEBUG
@ -27,21 +27,6 @@ readonly MACO_LOCAL_DIR="/opt/maco"
readonly MACO_INSTALL_DIR="/mnt/store.ipr/InstallProgs/ipr/maco" readonly MACO_INSTALL_DIR="/mnt/store.ipr/InstallProgs/ipr/maco"
# }}} # }}}
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() { # {{{ usage() { # {{{
cat <<- EOF cat <<- EOF
@ -123,8 +108,6 @@ Local Maco version (${CURRENT_MACO_VERSION}) is different from latest version ($
# }}} # }}}
main() { # {{{ main() { # {{{
manage_args "${ARGS}"
## Get all Maco's versions (date) ## Get all Maco's versions (date)
readonly CURRENT_MACO_VERSION=$(< "${MACO_LOCAL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' \ readonly CURRENT_MACO_VERSION=$(< "${MACO_LOCAL_DIR}/maco-version.txt" awk -v FS=. '{ print $1 "-" $2 "-" $3 "T" $4 ":" $5 ":" $6 }' \
|| exit 1) || exit 1)
@ -149,6 +132,56 @@ main() { # {{{
} }
# }}} # }}}
# Manage arguments {{{
# This code can't be in a function due to arguments
if [ ! "${NBARGS}" -eq "0" ]; then
manage_arg="0"
# Parse all options (start with a "-") one by one
while printf -- '%s' "${1}" | grep -q -E -- "^-*"; do
case "${1}" in
-h|--help ) ## help
usage
## Exit after help informations
exit 0
;;
-d|--debug ) ## debug
DEBUG=0
## Re-export new DEBUG value
export DEBUG
;;
-- ) ## End of options list
## End the while loop
break
;;
* )
printf '%b\n' "${RED}Invalid option: ${1}${RESET}"
printf '%b\n' "---"
usage
exit 1
;;
esac
debug_message "Arguments management \
${1} option managed."
## Next arg
shift
manage_arg=$((manage_arg+1))
done
debug_message "Arguments management \
${manage_arg} argument(s) successfully managed."
else
debug_message "Arguments management \
No arguments/options to manage."
fi
# }}}
main main
exit 255 exit 255