#!/usr/bin/env bash
set -euo pipefail
source json.bash
# Print a JSON array containing the program's command-line arguments.

JSON_TYPE=${JSON_TYPE:-string}
[[ $JSON_TYPE =~ ^(string|number|bool|null|true|false|auto|json)$ ]] || {
    echo "$0: invalid JSON_TYPE: '$JSON_TYPE'" >&2
    exit 1
}

inputs=("$@")

case $JSON_TYPE in
(string|auto) # these can't fail, so we can output them straight to stdout
  echo -n '['
  out= join=, in=inputs "json.encode_${JSON_TYPE}"
  echo ']'
;;
(*) # these can fail for invalid inputs, so we need to output only on success
  out=values join=, in=inputs "json.encode_${JSON_TYPE}"
  echo "[${values[0]}]"
;;
esac
