Skip to content

Instantly share code, notes, and snippets.

@cwalv
Created December 9, 2023 04:38
Show Gist options
  • Save cwalv/467cee6bd086f80d32cc50947c0a2063 to your computer and use it in GitHub Desktop.
Save cwalv/467cee6bd086f80d32cc50947c0a2063 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -euo pipefail
declare -a PAT_ARGS
declare -a CAPTURE
declare PAT_COUNT=0
while (("$#" > 0)) && [ "$1" != "--" ]; do
PAT_ARGS+=("--arg" "pat${PAT_COUNT}" "$1")
CAPTURE+=("capture(\$pat${PAT_COUNT})")
PAT_COUNT=$((PAT_COUNT + 1))
shift
done
if (("$#" > 0)) && [ "$1" = "--" ]; then
shift
fi
CAPTURE_JSON="$(jq -r --raw-input \
'split(" ")
| join(",")
| "[" + . + "]"' <<< "${CAPTURE[@]}")"
jq --stream --raw-input \
"${PAT_ARGS[@]}" \
"${CAPTURE_JSON} | add // empty" \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment