#!/usr/bin/env bash
# nerf-gh-issue-list -- List open issues in the current repository
# Generated from gh 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-gh-issue-list [--state|-s <state>] [--assignee|-a <assignee>] [--label|-l <label>] [--limit|-L <limit>]

Options:
  --state, -s <state>
      Filter by state (default: open)
      Allowed values: open, closed, all
  --assignee, -a <assignee>
      Filter by assignee (GitHub username or @me)
  --label, -l <label>
      Filter by label
  --limit, -L <limit>
      Maximum number of issues to list (default 30)
      Must match: ^[0-9]+$

Maps to: gh issue list <state> <assignee> <label> <limit>

List open issues in the current repository.
EOF
  exit 1
}

STATE=""
ASSIGNEE=""
LABEL=""
LIMIT=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --state|-s) if [[ -n "${STATE}" ]]; then echo "error: --state can only be specified once" >&2; exit 1; fi; STATE="$2"; shift 2 ;;
    --assignee|-a) if [[ -n "${ASSIGNEE}" ]]; then echo "error: --assignee can only be specified once" >&2; exit 1; fi; ASSIGNEE="$2"; shift 2 ;;
    --label|-l) if [[ -n "${LABEL}" ]]; then echo "error: --label can only be specified once" >&2; exit 1; fi; LABEL="$2"; shift 2 ;;
    --limit|-L) if [[ -n "${LIMIT}" ]]; then echo "error: --limit can only be specified once" >&2; exit 1; fi; LIMIT="$2"; shift 2 ;;
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) echo "error: unknown argument: $1" >&2; usage ;;
  esac
done

if [[ -n "${STATE}" ]] && [[ "${STATE}" != "open" && "${STATE}" != "closed" && "${STATE}" != "all" ]]; then
  echo "error: nerf-gh-issue-list: option --state is not an allowed value" >&2
  echo "  value:   \"${STATE}\"" >&2
  echo "  allowed: open, closed, all" >&2
  echo "  hint: use one of the allowed values" >&2
  exit 1
fi

_NERF_PATTERN='^[0-9]+$'
if [[ -n "${LIMIT}" ]] && ! [[ "${LIMIT}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-gh-issue-list: option --limit does not match required pattern" >&2
  echo "  value:   \"${LIMIT}\"" >&2
  echo "  pattern: ^[0-9]+$" >&2
  echo "  hint: value must match ^[0-9]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: gh issue list ${STATE:+"--state"} ${STATE:+"$STATE"} ${ASSIGNEE:+"--assignee"} ${ASSIGNEE:+"$ASSIGNEE"} ${LABEL:+"--label"} ${LABEL:+"$LABEL"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}"
  exit 0
fi

exec gh issue list ${STATE:+"--state"} ${STATE:+"$STATE"} ${ASSIGNEE:+"--assignee"} ${ASSIGNEE:+"$ASSIGNEE"} ${LABEL:+"--label"} ${LABEL:+"$LABEL"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}
