Add debug mode and help message

This commit is contained in:
Jeremy Gardais 2021-11-18 11:37:59 +01:00
parent 65b4028556
commit 0145fad1d6
Signed by: jegardai
GPG Key ID: E759BAA22501AF32
1 changed files with 109 additions and 0 deletions

View File

@ -4,6 +4,12 @@
## https://gist.github.com/Kehet/5ba8a530e52ea3a0ae251d756faef680
# Vars {{{
readonly PROGNAME=$(basename "${0}")
readonly PROGDIR=$(readlink -m $(dirname "${0}"))
readonly ARGS="${*}"
readonly NBARGS="${#}"
[ -z "${DEBUG}" ] && DEBUG=1
## Current tasks temp file
readonly TASKW_CURRENT_LIST="/tmp/rofi-task.sh-current.tasks"
@ -11,13 +17,60 @@ readonly TASKW_CURRENT_LIST="/tmp/rofi-task.sh-current.tasks"
black="#000000"
blue="#0094cc"
# }}}
usage() { # {{{
cat <<- EOF
usage: $PROGNAME [-d|-h]
Start any taskwarrior task or allow to stop a running one.
EXAMPLES:
- Open rofi launcher with the list of pending tasks :
${PROGNAME}
OPTIONS:
-d,--debug
Enable debug messages.
-h,--help
Print this help message.
EOF
}
# }}}
debug_message() { # {{{
local_message="${1}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '\e[1;35m%-6b\e[m\n' "DEBUG ${PROGNAME}: ${local_message}"
return 0
}
# }}}
error_message() { # {{{
local_error_message="${1}"
local_error_code="${2}"
## Print message if DEBUG is enable (=0)
[ "${DEBUG}" -eq "0" ] && printf '%b\n' "ERROR ${PROGNAME}: ${RED}${local_error_message}${RESET}"
exit "${local_error_code:=66}"
}
# }}}
is_task_running() { # {{{
if task active >/dev/null 2>&1; then
return_is_task_running="0"
debug_message "is_task_running \
A task is ${RED}running${COLOR_DEBUG}."
else
return_is_task_running="1"
debug_message "is_task_running \
No task currently running."
fi
return "${return_is_task_running:=/dev/null}"
@ -80,6 +133,62 @@ main() { # {{{
}
# }}}
# Manage arguments # {{{
# This code can't be in a function due to argument management
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