From 0676d7a9c0757e3651f1fb3437c34565ddce4323 Mon Sep 17 00:00:00 2001 From: Nathaniel Landau Date: Tue, 9 Nov 2021 10:13:06 -0500 Subject: [PATCH] option to _printarray_ when not VERBOSE --- test/debug.bats | 11 ++++++++++- utilities/debug.bash | 32 +++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/test/debug.bats b/test/debug.bats index c223a9f..97d7cd3 100755 --- a/test/debug.bats +++ b/test/debug.bats @@ -110,5 +110,14 @@ teardown() { assert_success assert_line --index 1 "[ debug] foo = bar" 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" } diff --git a/utilities/debug.bash b/utilities/debug.bash index 26adaec..b58a7e9 100644 --- a/utilities/debug.bash +++ b/utilities/debug.bash @@ -42,6 +42,8 @@ _printArray_() { # ARGS: # $1 (Required) - String variable name of the array # $2 (Optional) - Line number where _printArray_ is called + # OPTS: + # -v - Prints array when VERBOSE is false # OUTS: # stdout: Formatted key value of array # USAGE: @@ -52,15 +54,35 @@ _printArray_() { [[ $# == 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 _lineNumber="${2:-}" 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 "${_k} = ${_arr[${_k}]}" - done + debug "Contents of \${${_arrayName}[@]}" "${_lineNumber}" + + 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 }