#!/usr/bin/env bash
# nerf-gh-pr-list -- List open pull requests 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-pr-list [--web|-w] [--state|-s <state>] [--author <author>] [--base|-B <base>] [--limit|-L <limit>]

Switches:
  --web, -w
      Open the list in the browser

Options:
  --state, -s <state>
      Filter by state (default: open)
      Allowed values: open, closed, merged, all
  --author <author>
      Filter by author (GitHub username or @me)
  --base, -B <base>
      Filter by base branch
  --limit, -L <limit>
      Maximum number of PRs to list (default 30)
      Must match: ^[0-9]+$

Maps to: gh pr list <web> <state> <author> <base> <limit>

List open pull requests in the current repository.
EOF
  exit 1
}

WEB=""
STATE=""
AUTHOR=""
BASE=""
LIMIT=""

while [[ $# -gt 0 ]]; do
  case "$1" in
    --web|-w) if [[ -n "${WEB}" ]]; then echo "error: --web can only be specified once" >&2; exit 1; fi; WEB="true"; shift 1 ;;
    --state|-s) if [[ -n "${STATE}" ]]; then echo "error: --state can only be specified once" >&2; exit 1; fi; STATE="$2"; shift 2 ;;
    --author) if [[ -n "${AUTHOR}" ]]; then echo "error: --author can only be specified once" >&2; exit 1; fi; AUTHOR="$2"; shift 2 ;;
    --base|-B) if [[ -n "${BASE}" ]]; then echo "error: --base can only be specified once" >&2; exit 1; fi; BASE="$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}" != "merged" && "${STATE}" != "all" ]]; then
  echo "error: nerf-gh-pr-list: option --state is not an allowed value" >&2
  echo "  value:   \"${STATE}\"" >&2
  echo "  allowed: open, closed, merged, 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-pr-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 pr list ${WEB:+"--web"} ${STATE:+"--state"} ${STATE:+"$STATE"} ${AUTHOR:+"--author"} ${AUTHOR:+"$AUTHOR"} ${BASE:+"--base"} ${BASE:+"$BASE"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}"
  exit 0
fi

exec gh pr list ${WEB:+"--web"} ${STATE:+"--state"} ${STATE:+"$STATE"} ${AUTHOR:+"--author"} ${AUTHOR:+"$AUTHOR"} ${BASE:+"--base"} ${BASE:+"$BASE"} ${LIMIT:+"--limit"} ${LIMIT:+"$LIMIT"}
