# shellcheck disable=SC2034
# Parse command line options.
optParse() {
  SEP="$(printf '\037')"
  POS=''
  while [ "${#}" -gt '0' ]; do
    case "${1?}" in
      # Short options that accept a value need a "*" in their pattern because they can be found in the "-A<value>" form.
      '-O'* | '--output')
        optArgStr "${@-}"
        outputFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-H'* | '--header')
        optArgStr "${@-}"
        headerFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-F'* | '--footer')
        optArgStr "${@-}"
        footerFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-S'* | '--sources')
        optArgStr "${@-}"
        sourcesFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-A'* | '--allowlist')
        optArgStr "${@-}"
        allowlistFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-D'* | '--denylist')
        optArgStr "${@-}"
        denylistFile="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-R'* | '--redirection')
        optArgStr "${@-}"
        redirection="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-W'* | '--wrap')
        optArgStr "${@-}"
        wrap="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-T'* | '--template')
        optArgStr "${@-}"
        template="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-C'* | '--comment')
        optArgStr "${@-}"
        comment="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-l' | '--lenient' | '--no-lenient')
        optArgBool "${@-}"
        lenient="${optArg:?}"
        ;;
      '-r' | '--regex' | '--no-regex')
        optArgBool "${@-}"
        regex="${optArg:?}"
        ;;
      '-f' | '--filter-subdomains' | '--no-filter-subdomains')
        optArgBool "${@-}"
        filterSubdomains="${optArg:?}"
        ;;
      '-c' | '--continue' | '--no-continue')
        optArgBool "${@-}"
        continue="${optArg:?}"
        ;;
      '-p'* | '--parallel')
        optArgStr "${@-}"
        parallel="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-q' | '--quiet' | '--no-quiet')
        optArgBool "${@-}"
        quiet="${optArg:?}"
        ;;
      '-x'* | '--color')
        optArgStr "${@-}"
        color="${optArg?}"
        shift "${optShift:?}"
        ;;
      '-v' | '--version')
        showVersion
        ;;
      '-h' | '--help')
        showHelp
        ;;
      # If "--" is found, the remaining positional parameters are saved and the parsing ends.
      --)
        shift
        _IFS="${IFS?}"
        IFS="${SEP:?}"
        POS="${POS-}${POS+${SEP:?}}${*-}"
        IFS="${_IFS?}"
        break
        ;;
      # If a long option in the form "--opt=value" is found, it is split into "--opt" and "value".
      --*=*)
        optSplitEquals "${@-}"
        shift
        set -- "${optName:?}" "${optArg?}" "${@-}"
        continue
        ;;
      # If an option did not match any pattern, an error is thrown.
      -? | --*)
        optDie "Illegal option ${1:?}"
        ;;
      # If multiple short options in the form "-AB" are found, they are split into "-A" and "-B".
      -?*)
        optSplitShort "${@-}"
        shift
        set -- "${optAName:?}" "${optBName:?}" "${@-}"
        continue
        ;;
      # If a positional parameter is found, it is saved.
      *)
        POS="${POS-}${POS+${SEP:?}}${1?}"
        ;;
    esac
    shift
  done
}
optSplitShort() {
  optAName="${1%"${1#??}"}"
  optBName="-${1#??}"
}
optSplitEquals() {
  optName="${1%="${1#--*=}"}"
  optArg="${1#--*=}"
}
optArgStr() {
  if [ -n "${1#??}" ] && [ "${1#--}" = "${1:?}" ]; then
    optArg="${1#??}"
    optShift='0'
  elif [ -n "${2+x}" ]; then
    optArg="${2-}"
    optShift='1'
  else
    optDie "No argument for ${1:?} option"
  fi
}
optArgBool() {
  if [ "${1#--no-}" = "${1:?}" ]; then
    optArg='true'
  else
    optArg='false'
  fi
}
optDie() {
  printf '%s\n' "${@-}" "Try 'hblock --help' for more information" >&2
  exit 2
}
