Skip to content

Commit 8b572a9

Browse files
committed
fix(_comp_array_filter): adjust error messages for consistency
1 parent 0cafcb8 commit 8b572a9

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

bash_completion

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,12 @@ _upvars()
263263
# Filter the array elements with the specified condition.
264264
# @param $1 Array name (that is not "value" or other internal variable names)
265265
# @param $2 When none of the options -EFG are specified, this is used as the
266-
# command that tests the array element. If this is an existing function
267-
# name, the function is called with the value of the array element.
268-
# Otherwise, this shall be the shell command that tests the array-element
269-
# value stored in the shell variable "value".
266+
# command that tests the array element. The command is supposed to exit with
267+
# status 0 when the element should be preserved, and 1 when the element
268+
# should be removed. The other exit status will cancel the array filtering.
269+
# If this is an existing function name, the function is called with the value
270+
# of the array element. Otherwise, this shall be the shell command that
271+
# tests the array-element value stored in the shell variable "value".
270272
#
271273
# Options:
272274
# -E $2 is interpreted as a POSIX extended regular expression.
@@ -297,25 +299,26 @@ _comp_array_filter()
297299
[psm]) _comp_local_anchoring=$_comp_local_opt ;;
298300
[rC]) _comp_local_flags=$_comp_local_opt$_comp_local_flags ;;
299301
*)
300-
echo "bash_completion: $FUNCNAME: usage error" >&2
302+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" 'usage error' >&2
303+
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmrC] ARRAY_NAME CONDITION" >&2
301304
return 2
302305
;;
303306
esac
304307
done
305308

306309
shift $((OPTIND - 1))
307310
if (($# != 2)); then
308-
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "unexpected number of arguments." >&2
311+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "unexpected number of arguments: $#" >&2
309312
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmrC] ARRAY_NAME CONDITION" >&2
310313
return 2
311314
elif [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
312315
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "invalid array name '$1'." >&2
313316
return 2
314317
elif [[ $1 == @(_comp_local_*|OPTIND|OPTARG|OPTERR) ]]; then
315-
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' is reserved for internal uses." >&2
318+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' is reserved for internal uses" >&2
316319
return 2
317320
elif [[ ! $_comp_local_pattype && $1 == value ]]; then
318-
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' cannot be used for the predicate." >&2
321+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' cannot be used for the predicate" >&2
319322
return 2
320323
fi
321324
# When the array is empty:
@@ -365,15 +368,16 @@ _comp_array_filter()
365368
_comp_local_unset=1
366369
;;
367370
*)
368-
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "the filter condition broken." >&2
371+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" \
372+
"the filter condition broken '${_comp_local_pattype:+-$_comp_local_pattype }$2'" >&2
369373
return 2
370374
;;
371375
esac
372376
done
373377

374378
# Compaction of the sparse array
375379
[[ $_comp_local_flags == *C* ]] ||
376-
eval -- "((\${#$1[@]})) && $1=(\"\${$1[@]}\")"
380+
eval "((\${#$1[@]})) && $1=(\"\${$1[@]}\")"
377381

378382
[[ ! $_comp_local_unset ]]
379383
}

0 commit comments

Comments
 (0)