#!/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=""
_STATE_SET=""
AUTHOR=""
_AUTHOR_SET=""
BASE=""
_BASE_SET=""
LIMIT=""
_LIMIT_SET=""

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_SET}" ]]; then echo "error: --state can only be specified once" >&2; exit 1; fi; STATE="$2"; _STATE_SET=true; shift 2 ;;
    --author) if [[ -n "${_AUTHOR_SET}" ]]; then echo "error: --author can only be specified once" >&2; exit 1; fi; AUTHOR="$2"; _AUTHOR_SET=true; shift 2 ;;
    --base|-B) if [[ -n "${_BASE_SET}" ]]; then echo "error: --base can only be specified once" >&2; exit 1; fi; BASE="$2"; _BASE_SET=true; shift 2 ;;
    --limit|-L) if [[ -n "${_LIMIT_SET}" ]]; then echo "error: --limit can only be specified once" >&2; exit 1; fi; LIMIT="$2"; _LIMIT_SET=true; 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_SET}" ]] && [[ "${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_SET}" ]] && ! [[ "${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
  _NERF_DRY_CMD=(gh pr list ${WEB:+"--web"} ${_STATE_SET:+"--state"} ${_STATE_SET:+"$STATE"} ${_AUTHOR_SET:+"--author"} ${_AUTHOR_SET:+"$AUTHOR"} ${_BASE_SET:+"--base"} ${_BASE_SET:+"$BASE"} ${_LIMIT_SET:+"--limit"} ${_LIMIT_SET:+"$LIMIT"})
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

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