#!/usr/bin/env bash
# nerf-az-boards-wi-list -- Query work items using WIQL (Work Item Query Language). Returns matching work items as JSON. The organization and project are auto-detected from the git remote; pass --project to override.
# Generated from az-boards manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=none

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-az-boards-wi-list [--project|-p <project>] <wiql>

Options:
  --project, -p <project>
      Azure DevOps project name or ID (auto-detected from the git remote if omitted)

Arguments:
  <wiql> (required)
      WIQL query string (e.g. "SELECT [System.Id], [System.Title], [System.State] FROM WorkItems WHERE [System.AssignedTo] = @Me AND [System.State] <> 'Closed' ORDER BY [System.ChangedDate] DESC")

Maps to: az boards query --wiql <wiql> <project> --output json

Query work items using WIQL (Work Item Query Language). Returns matching work items as JSON. The organization and project are auto-detected from the git remote; pass --project to override.
EOF
  exit 1
}

PROJECT=""
_PROJECT_SET=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --project|-p) if [[ -n "${_PROJECT_SET}" ]]; then echo "error: --project can only be specified once" >&2; exit 1; fi; PROJECT="$2"; _PROJECT_SET=true; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_WIQL_SET=""
if [[ $# -gt 0 ]]; then
  WIQL="$1"
  _WIQL_SET=true
  shift
else
  WIQL=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-az-boards-wi-list: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_WIQL_SET}" ]] && [[ "${WIQL}" == -* ]]; then
  echo "error: nerf-az-boards-wi-list: <wiql> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${WIQL}" ]]; then
  echo "error: nerf-az-boards-wi-list: missing required argument <wiql>" >&2
  echo "  hint: provide a value for <wiql>" >&2
  usage
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(az boards query --wiql "${WIQL}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json)
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec az boards query --wiql "${WIQL}" ${_PROJECT_SET:+"--project"} ${_PROJECT_SET:+"$PROJECT"} --output json
