mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-10 06:03:47 -05:00
option to _printarray_ when not VERBOSE
This commit is contained in:
@@ -110,5 +110,14 @@ teardown() {
|
|||||||
assert_success
|
assert_success
|
||||||
assert_line --index 1 "[ debug] foo = bar"
|
assert_line --index 1 "[ debug] foo = bar"
|
||||||
assert_line --index 2 "[ debug] baz = foobar"
|
assert_line --index 2 "[ debug] baz = foobar"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "_printArray_: print without verbose" {
|
||||||
|
testArray=(1 2 3)
|
||||||
|
VERBOSE=false
|
||||||
|
run _printArray_ -v "testArray"
|
||||||
|
assert_success
|
||||||
|
assert_output --partial "[ info] 0 = 1"
|
||||||
|
assert_output --partial "[ info] 1 = 2"
|
||||||
|
assert_output --partial "[ info] 2 = 3"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ _printArray_() {
|
|||||||
# ARGS:
|
# ARGS:
|
||||||
# $1 (Required) - String variable name of the array
|
# $1 (Required) - String variable name of the array
|
||||||
# $2 (Optional) - Line number where _printArray_ is called
|
# $2 (Optional) - Line number where _printArray_ is called
|
||||||
|
# OPTS:
|
||||||
|
# -v - Prints array when VERBOSE is false
|
||||||
# OUTS:
|
# OUTS:
|
||||||
# stdout: Formatted key value of array
|
# stdout: Formatted key value of array
|
||||||
# USAGE:
|
# USAGE:
|
||||||
@@ -52,15 +54,35 @@ _printArray_() {
|
|||||||
|
|
||||||
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
||||||
|
|
||||||
|
local _printNoVerbose=false
|
||||||
|
local opt
|
||||||
|
local OPTIND=1
|
||||||
|
while getopts ":vV" opt; do
|
||||||
|
case ${opt} in
|
||||||
|
v | V) _printNoVerbose=true ;;
|
||||||
|
*) fatal "Unrecognized option '${1}' passed to ${FUNCNAME[0]}. Exiting." ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND - 1))
|
||||||
|
|
||||||
local _arrayName="${1}"
|
local _arrayName="${1}"
|
||||||
local _lineNumber="${2:-}"
|
local _lineNumber="${2:-}"
|
||||||
declare -n _arr="${1}"
|
declare -n _arr="${1}"
|
||||||
|
|
||||||
[[ ${VERBOSE:-} != true ]] && return 0
|
if [[ ${_printNoVerbose} == "false" ]]; then
|
||||||
|
|
||||||
debug "Contents of \${${_arrayName}[@]}" "${_lineNumber}"
|
[[ ${VERBOSE:-} != true ]] && return 0
|
||||||
|
|
||||||
for _k in "${!_arr[@]}"; do
|
debug "Contents of \${${_arrayName}[@]}" "${_lineNumber}"
|
||||||
debug "${_k} = ${_arr[${_k}]}"
|
|
||||||
done
|
for _k in "${!_arr[@]}"; do
|
||||||
|
debug "${_k} = ${_arr[${_k}]}"
|
||||||
|
done
|
||||||
|
else
|
||||||
|
info "Contents of \${${_arrayName}[@]}" "${_lineNumber}"
|
||||||
|
|
||||||
|
for _k in "${!_arr[@]}"; do
|
||||||
|
info "${_k} = ${_arr[${_k}]}"
|
||||||
|
done
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user