44# # DESCRIPTION : General Bash script template
55# # CREATED : #~TIME~#
66# # TEMVER : v2.1.1
7+ # # TEMURL : https://github.com/Silverbullet069/bash-script-template
78# # AUTHOR : ralish (https://github.com/ralish/)
89# # CONTRIBUTOR : Silverbullet069 (https://github.com/Silverbullet069/)
910# # LICENSE : MIT License
@@ -21,7 +22,7 @@ function parse_params() {
2122 # shellcheck disable=SC2016,SC2312
2223 local script_file=" ${BASH_SOURCE[0]} "
2324 local in_case_block=false
24- local -A options=() # associative array
25+ local -A options=() # associative array
2526 declare -g help_options=() # indexed array
2627
2728 while IFS= read -r line; do
@@ -38,12 +39,12 @@ function parse_params() {
3839 if [[ $line =~ ^[[:space:]]* -([a-z])[[:space:]]\| [[:space:]]--([a-z-]+)\) $ ]]; then
3940 option_name=" ${BASH_REMATCH[2]// -/ _} "
4041 option_help=" -${BASH_REMATCH[1]} , --${BASH_REMATCH[2]} "
41- options[" ${option_name} " ]= # empty
42+ options[" ${option_name} " ]= # empty
4243
4344 elif [[ $line =~ ^[[:space:]]* --([a-z-]+)\) $ ]]; then
4445 option_name=" ${BASH_REMATCH[1]// -/ _} "
4546 option_help=" --${BASH_REMATCH[1]} "
46- options[" ${option_name} " ]= # empty
47+ options[" ${option_name} " ]= # empty
4748
4849 elif [[ $line =~ ^[[:space:]]* # ##[[:space:]]*(.*)$ ]]; then
4950 help_options+= (" $( printf " %-28s %s\n" " ${option_help} " " ${BASH_REMATCH[1]} " ) " )
@@ -54,7 +55,7 @@ function parse_params() {
5455 fi
5556 fi
5657 fi
57- done < " $script_file "
58+ done < " $script_file "
5859
5960 # Check if options array is empty
6061 # shellcheck disable=SC2015
@@ -71,50 +72,50 @@ function parse_params() {
7172 local param=" ${1} "
7273 shift
7374 case " ${param} " in
74- # Add your options here
75- # ...
76-
77- # Built-in options
78- # NOTE: ### comment will be displayed as short description for options in --help output
79- -l | --log-level)
80- # ## Specify log level. Add DEBUG=1 to turn on Bash debug mode.
81- # ## Valid values: DBG, INF, WRN, ERR. Default: INF
82-
83- _option_log_level=" ${1} "
84- shift
85- if [[ -z " ${LOG_LEVELS[${_option_log_level}]} " ]]; then
86- script_exit " Invalid log level: ${_option_log_level} . Choose 1 of the following: ${LOG_LEVELS[*]} " 2
87- fi
88- ;;
89- -n | --no-colour)
90- # ## Disables colour output
91-
92- _option_no_colour=1
93- ;;
94- -q | --quiet)
95- # ## Run silently unless an error is encountered
96-
97- _option_quiet=1
98- ;;
99- -t | --timestamp)
100- # ## Enables timestamp output
101-
102- _option_timestamp=1
103- ;;
104- -h | --help)
105- # ## Displays this help and exit
106-
107- script_usage
75+ # Add your options here
76+ # ...
77+
78+ # Built-in options
79+ # NOTE: ### comment will be displayed as short description for options in --help output
80+ -l | --log-level)
81+ # ## Specify log level. Add DEBUG=1 to turn on Bash debug mode.
82+ # ## Valid values: DBG, INF, WRN, ERR. Default: INF
83+
84+ _option_log_level=" ${1} "
85+ shift
86+ if [[ -z " ${LOG_LEVELS[${_option_log_level}]} " ]]; then
87+ script_exit " Invalid log level: ${_option_log_level} . Choose 1 of the following: ${LOG_LEVELS[*]} " 2
88+ fi
89+ ;;
90+ -n | --no-colour)
91+ # ## Disables colour output
92+
93+ _option_no_colour=1
94+ ;;
95+ -q | --quiet)
96+ # ## Run silently unless an error is encountered
97+
98+ _option_quiet=1
99+ ;;
100+ -t | --timestamp)
101+ # ## Enables timestamp output
102+
103+ _option_timestamp=1
104+ ;;
105+ -h | --help)
106+ # ## Displays this help and exit
107+
108+ script_usage
109+ exit 0
110+ ;;
111+ * )
112+ # internal function calling
113+ if declare -F " ${param} " & > /dev/null && [[ -n " ${DEBUG-} " ]]; then
114+ " ${param} " " $@ "
108115 exit 0
109- ;;
110- * )
111- # internal function calling
112- if declare -F " ${param} " & > /dev/null && [[ -n " ${DEBUG-} " ]]; then
113- " ${param} " " $@ "
114- exit 0
115- fi
116- script_exit " Invalid parameter was provided: ${param} " 1
117- ;;
116+ fi
117+ script_exit " Invalid parameter was provided: ${param} " 1
118+ ;;
118119 esac
119120 done
120121
@@ -123,7 +124,7 @@ function parse_params() {
123124 [[ ${# options[@]} -eq 0 ]] && script_exit " No options found in parse_params() function." 1 || true
124125
125126 # Make the options read-only
126- for option in " ${options[@]} " ; do
127+ for option in " ${! options[@]} " ; do
127128 readonly " _option_${option} "
128129 done
129130}
@@ -133,7 +134,7 @@ function parse_params() {
133134# OUTS: None
134135# RETS: None
135136function script_usage() {
136- cat << EOF
137+ cat << EOF
137138
138139Usage: #~NAME~# [OPTIONS] ...
139140
176177
177178# Only enable these shell behaviours if we're not being sourced
178179# Approach via: https://stackoverflow.com/a/28776166/8787985
179- if ! (return 0 2> /dev/null); then
180+ if ! (return 0 2> /dev/null); then
180181 # A better class of script...
181182 set -o errexit # Exit on most errors (see the manual)
182183 set -o nounset # Disallow expansion of unset variables
@@ -198,7 +199,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/source.sh"
198199
199200# Invoke main with args if not sourced
200201# Approach via: https://stackoverflow.com/a/28776166/8787985
201- if ! (return 0 2> /dev/null); then
202+ if ! (return 0 2> /dev/null); then
202203 main " $@ "
203204fi
204205
0 commit comments