Skip to content

Commit 03b61a8

Browse files
committed
Tweak parameter validation to be a little more forgiving
1 parent ca2dea4 commit 03b61a8

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

source.sh

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function script_trap_err() {
2020
set +o pipefail
2121

2222
# Validate any provided exit code
23-
if [[ $# -eq 1 && $1 =~ ^[0-9]+$ ]]; then
23+
if [[ ${1-} =~ ^[0-9]+$ ]]; then
2424
exit_code="$1"
2525
fi
2626

@@ -77,7 +77,7 @@ function script_exit() {
7777
exit 0
7878
fi
7979

80-
if [[ $# -eq 2 && $2 =~ ^[0-9]+$ ]]; then
80+
if [[ ${2-} =~ ^[0-9]+$ ]]; then
8181
printf '%b\n' "$1"
8282
# If we've been provided a non-zero exit code run the error trap
8383
if [[ $2 -ne 0 ]]; then
@@ -87,7 +87,7 @@ function script_exit() {
8787
fi
8888
fi
8989

90-
script_exit 'Invalid arguments passed to script_exit()!' 2
90+
script_exit 'Missing required argument to script_exit()!' 2
9191
}
9292

9393

@@ -205,8 +205,8 @@ function cron_init() {
205205
# escape code or one of the prepopulated colour variables.
206206
# $3 (optional): Set to any value to not append a new line to the message
207207
function pretty_print() {
208-
if [[ $# -eq 0 || $# -gt 3 ]]; then
209-
script_exit 'Invalid arguments passed to pretty_print()!' 2
208+
if [[ $# -lt 1 ]]; then
209+
script_exit 'Missing required argument to pretty_print()!' 2
210210
fi
211211

212212
if [[ -z ${no_colour-} ]]; then
@@ -241,8 +241,8 @@ function verbose_print() {
241241
# OUTS: $build_path: The constructed path
242242
# NOTE: Heavily inspired by: https://unix.stackexchange.com/a/40973
243243
function build_path() {
244-
if [[ -z ${1-} || $# -gt 2 ]]; then
245-
script_exit 'Invalid arguments passed to build_path()!' 2
244+
if [[ $# -lt 1 ]]; then
245+
script_exit 'Missing required argument to build_path()!' 2
246246
fi
247247

248248
local new_path path_entry temp_path
@@ -272,8 +272,8 @@ function build_path() {
272272
# ARGS: $1 (required): Name of the binary to test for existence
273273
# $2 (optional): Set to any value to treat failure as a fatal error
274274
function check_binary() {
275-
if [[ $# -ne 1 && $# -ne 2 ]]; then
276-
script_exit 'Invalid arguments passed to check_binary()!' 2
275+
if [[ $# -lt 1 ]]; then
276+
script_exit 'Missing required argument to check_binary()!' 2
277277
fi
278278

279279
if ! command -v "$1" > /dev/null 2>&1; then
@@ -293,10 +293,6 @@ function check_binary() {
293293
# DESC: Validate we have superuser access as root (via sudo if requested)
294294
# ARGS: $1 (optional): Set to any value to not attempt root access via sudo
295295
function check_superuser() {
296-
if [[ $# -gt 1 ]]; then
297-
script_exit 'Invalid arguments passed to check_superuser()!' 2
298-
fi
299-
300296
local superuser test_euid
301297
if [[ $EUID -eq 0 ]]; then
302298
superuser=true
@@ -329,16 +325,16 @@ function check_superuser() {
329325
# ARGS: $1 (optional): Set to zero to not attempt execution via sudo
330326
# $@ (required): Passed through for execution as root user
331327
function run_as_root() {
328+
if [[ $# -eq 0 ]]; then
329+
script_exit 'Missing required argument to run_as_root()!' 2
330+
fi
331+
332332
local try_sudo
333333
if [[ ${1-} =~ ^0$ ]]; then
334334
try_sudo=true
335335
shift
336336
fi
337337

338-
if [[ $# -eq 0 ]]; then
339-
script_exit 'Invalid arguments passed to run_as_root()!' 2
340-
fi
341-
342338
if [[ $EUID -eq 0 ]]; then
343339
"$@"
344340
elif [[ -z ${try_sudo-} ]]; then

template.sh

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function script_trap_err() {
2424
set +o pipefail
2525

2626
# Validate any provided exit code
27-
if [[ $# -eq 1 && $1 =~ ^[0-9]+$ ]]; then
27+
if [[ ${1-} =~ ^[0-9]+$ ]]; then
2828
exit_code="$1"
2929
fi
3030

@@ -81,7 +81,7 @@ function script_exit() {
8181
exit 0
8282
fi
8383

84-
if [[ $# -eq 2 && $2 =~ ^[0-9]+$ ]]; then
84+
if [[ ${2-} =~ ^[0-9]+$ ]]; then
8585
printf '%b\n' "$1"
8686
# If we've been provided a non-zero exit code run the error trap
8787
if [[ $2 -ne 0 ]]; then
@@ -91,7 +91,7 @@ function script_exit() {
9191
fi
9292
fi
9393

94-
script_exit 'Invalid arguments passed to script_exit()!' 2
94+
script_exit 'Missing required argument to script_exit()!' 2
9595
}
9696

9797

@@ -209,8 +209,8 @@ function cron_init() {
209209
# escape code or one of the prepopulated colour variables.
210210
# $3 (optional): Set to any value to not append a new line to the message
211211
function pretty_print() {
212-
if [[ $# -eq 0 || $# -gt 3 ]]; then
213-
script_exit 'Invalid arguments passed to pretty_print()!' 2
212+
if [[ $# -lt 1 ]]; then
213+
script_exit 'Missing required argument to pretty_print()!' 2
214214
fi
215215

216216
if [[ -z ${no_colour-} ]]; then
@@ -245,8 +245,8 @@ function verbose_print() {
245245
# OUTS: $build_path: The constructed path
246246
# NOTE: Heavily inspired by: https://unix.stackexchange.com/a/40973
247247
function build_path() {
248-
if [[ -z ${1-} || $# -gt 2 ]]; then
249-
script_exit 'Invalid arguments passed to build_path()!' 2
248+
if [[ $# -lt 1 ]]; then
249+
script_exit 'Missing required argument to build_path()!' 2
250250
fi
251251

252252
local new_path path_entry temp_path
@@ -276,8 +276,8 @@ function build_path() {
276276
# ARGS: $1 (required): Name of the binary to test for existence
277277
# $2 (optional): Set to any value to treat failure as a fatal error
278278
function check_binary() {
279-
if [[ $# -ne 1 && $# -ne 2 ]]; then
280-
script_exit 'Invalid arguments passed to check_binary()!' 2
279+
if [[ $# -lt 1 ]]; then
280+
script_exit 'Missing required argument to check_binary()!' 2
281281
fi
282282

283283
if ! command -v "$1" > /dev/null 2>&1; then
@@ -297,10 +297,6 @@ function check_binary() {
297297
# DESC: Validate we have superuser access as root (via sudo if requested)
298298
# ARGS: $1 (optional): Set to any value to not attempt root access via sudo
299299
function check_superuser() {
300-
if [[ $# -gt 1 ]]; then
301-
script_exit 'Invalid arguments passed to check_superuser()!' 2
302-
fi
303-
304300
local superuser test_euid
305301
if [[ $EUID -eq 0 ]]; then
306302
superuser=true
@@ -333,16 +329,16 @@ function check_superuser() {
333329
# ARGS: $1 (optional): Set to zero to not attempt execution via sudo
334330
# $@ (required): Passed through for execution as root user
335331
function run_as_root() {
332+
if [[ $# -eq 0 ]]; then
333+
script_exit 'Missing required argument to run_as_root()!' 2
334+
fi
335+
336336
local try_sudo
337337
if [[ ${1-} =~ ^0$ ]]; then
338338
try_sudo=true
339339
shift
340340
fi
341341

342-
if [[ $# -eq 0 ]]; then
343-
script_exit 'Invalid arguments passed to run_as_root()!' 2
344-
fi
345-
346342
if [[ $EUID -eq 0 ]]; then
347343
"$@"
348344
elif [[ -z ${try_sudo-} ]]; then

0 commit comments

Comments
 (0)