#!/bin/bash
#
# how to specify the same option multiple times using Bash - case C
# https://sleeplessbeastie.eu/

# bash case-c.sh -- "first argument" "second one" "third one"

# usage info
usage(){
  echo "Usage:"
  echo "  $0 -p argument -- additional-arguments"
  echo ""
  echo "Parameters:"
  echo "  -p argument : set single parameter"
  echo ""
  exit
}

# parse options and arguments
while getopts 'p:' opt; do
  case "${opt}" in
    "p")
      single_argument="${OPTARG}"
      ;;
    *)
      usage
      ;;
  esac
done

# shift options/arguments list
shift $((OPTIND - 1))

arguments=""
# parse additional arguments
while [ "$#" -gt 0 ]; do
  arguments="${arguments}$1"
  shift
done

# no options were provided or there was a mistake
if [ "${OPTIND}" -eq "1" ] || [ "${OPTIND}" -le "$#" ]; then
  usage
fi

echo "Provided additional arguments:"
for argument in ${arguments}; do
  echo "  \"${argument}\""
done
