#!/usr/bin/env bash
# nerf-gh-issue-comment -- Add a comment to an issue.
# Generated from gh manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=remote

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-gh-issue-comment <issue> <body>

Arguments:
  <issue> (required)
      Issue number or URL
  <body> (required)
      Comment text

Maps to: gh issue comment <issue> --body <body>

Add a comment to an issue.
EOF
  exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

_ISSUE_SET=""
if [[ $# -gt 0 ]]; then
  ISSUE="$1"
  _ISSUE_SET=true
  shift
else
  ISSUE=""
fi
_BODY_SET=""
if [[ $# -gt 0 ]]; then
  BODY="$1"
  _BODY_SET=true
  shift
else
  BODY=""
fi
if [[ $# -gt 0 ]]; then
  echo "error: nerf-gh-issue-comment: unexpected extra arguments: $*" >&2
  echo "  hint: switches and options must come before positional arguments" >&2
  exit 1
fi

if [[ -n "${_ISSUE_SET}" ]] && [[ "${ISSUE}" == -* ]]; then
  echo "error: nerf-gh-issue-comment: <issue> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${ISSUE}" ]]; then
  echo "error: nerf-gh-issue-comment: missing required argument <issue>" >&2
  echo "  hint: provide a value for <issue>" >&2
  usage
fi

if [[ -n "${_BODY_SET}" ]] && [[ "${BODY}" == -* ]]; then
  echo "error: nerf-gh-issue-comment: <body> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${BODY}" ]]; then
  echo "error: nerf-gh-issue-comment: missing required argument <body>" >&2
  echo "  hint: provide a value for <body>" >&2
  usage
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  _NERF_DRY_CMD=(gh issue comment "${ISSUE}" --body "${BODY}")
  printf 'dry-run:'
  for _a in "${_NERF_DRY_CMD[@]}"; do printf " %q" "$_a"; done
  echo
  exit 0
fi

exec gh issue comment "${ISSUE}" --body "${BODY}"
