# -*- mode: snippet -*-
# name: parse-arg
# key: parse-arg
# --

# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash

function usage() {
  cat <<EOS
${1:Description}
Usage: \$(basename \$0) [options]
        -h     Show help.
EOS
}

POSITIONAL_ARGS=()
while [[ \$# -gt 0 ]]; do
  case \$1 in
    -h|--help)
      usage
      exit 0
      ;;
    -*|--*)
      echo "Unknown option \$1"
      usage
      exit 1
      ;;
    *)
      POSITIONAL_ARGS+=("\$1") # save positional arg
      ;;
  esac
  shift
done
set -- "\${POSITIONAL_ARGS[@]:-}" # restore positional parameters