#!/bin/bash

# usage message
usage() {
  MSG="${1:-}"

  cat <<EOF
$0 [options]

example:

-x      Show commands being executed.

-h      Show this help message.

EOF
  if [ -n "${MSG}" ]; then
    echo "${MSG}"
    exit 1
  fi
  exit 0
}

###############################################################################
# maybe print a command before executing it

SET_X="${SET_X:-false}"

set_x() {
  if [ "${SET_X:-false}" = "true" ]; then
    IFS=$(printf ' \n\t')
    echo "${PS4}$*" 1>&2
  fi
  "$@"
}

###############################################################################

SET_X=false

while getopts ':hx' OPT; do
  case ${OPT} in
    h)
      usage
      ;;
    x)
      SET_X=true
      ;;
    *)
      echo "ERROR: unknown option '${OPTARG}'"
      exit 1
      ;;
  esac
done
shift "$((OPTIND - 1))"

# examples

set_x echo hello

ID="$(set_x gh pr list ...)"
