Created
February 3, 2023 01:38
-
-
Save mattmc3/a907eb2d04226db455657f9d0370bf13 to your computer and use it in GitHub Desktop.
Zsh collect args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Collect args | |
# -- traditional, piped|, <redirected | |
function collect_args { | |
local data args=() | |
if [[ ! -t 0 ]]; then | |
while IFS= read -r data || [[ -n "$data" ]]; do | |
args+=("$data") | |
done | |
fi | |
printf '%s\n' "$@[@]" "$args[@]" | |
} | |
function foo { | |
set -- "${(f@)$(collect_args "$@")}" | |
local arg | |
for arg in "$@"; do | |
echo "arg: '$arg'" | |
done | |
} | |
arr=(1 2 "" 4) | |
printf "%s\n" "$arr[@]" | foo x "" y z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment