Skip to content

Commit

Permalink
Add --quiet.
Browse files Browse the repository at this point in the history
  • Loading branch information
danfuzz committed Oct 27, 2023
1 parent 068ef8e commit a3ba9c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions scripts/lib/bashy-core/arg-processor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ function _argproc_parse-enum {
local value="$1"
local values

set-array-from-vals values "${value}" \
set-array-from-vals --quiet values "${value}" \
|| return "$?"

if (( ${#values[@]} == 0 )); then
Expand Down Expand Up @@ -1072,7 +1072,7 @@ function _argproc_statements-from-args {
;;
'[]=')
# Multi-value option. Parse the value into elements.
if set-array-from-vals values "${value}"; then
if set-array-from-vals --quiet values "${value}"; then
_argproc_statements+=(
"${handler} $(vals "${values[@]}")")
else
Expand Down
24 changes: 18 additions & 6 deletions scripts/lib/bashy-core/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,28 @@ function set-array-from-lines {

# Reverse of `vals`: Assigns parsed elements of the given multi-value string
# (as produced by `vals` or similar) into the indicated variable, as an array.
# Values must be separated by at least one whitespace character.
# Values must be separated by at least one whitespace character. The option
# `--quiet` suppresses error output.
function set-array-from-vals {
# Note: Because we use `eval`, local variables are given name prefixes to
# avoid conflicts with the caller.
local _bashy_quiet=0
if [[ $1 == '--quiet' ]]; then
_bashy_quiet=1
shift
fi

if (( $# != 2 )); then
error-msg --file-line=1 'Missing argument(s) to `set-array-from-vals`.'
return 1
fi

# Note: Because we use `eval`, local variables are given name prefixes to
# avoid conflicts with the caller.
local _bashy_name="$1"
local _bashy_value="$2"
local _bashy_origValue="$2"

local _bashy_notsp=$'[^ \n\r\t]'
local _bashy_space=$'[ \n\r\t]'
local _bashy_value="${_bashy_origValue}"
local _bashy_notsp=$'[^ \n\r\t]' # Regex constant.
local _bashy_space=$'[ \n\r\t]' # Ditto.

# Trim _ending_ whitespace, and prefix `value` with a space, the latter to
# maintain the constraint that values are whitespace-separated.
Expand Down Expand Up @@ -134,6 +142,10 @@ function set-array-from-vals {
done

if ! [[ ${_bashy_value} =~ ^${_bashy_space}*$ ]]; then
if (( !_bashy_quiet )); then
error-msg 'Invalid `vals`-style multi-value string:'
error-msg " $(vals "${_bashy_origValue}")"
fi
return 1
fi

Expand Down

0 comments on commit a3ba9c0

Please sign in to comment.