#!/bin/bash
# How to specify the same option multiple times using Bash - case A
# https://sleeplessbeastie.eu/
# milosz at sleeplessbeastie.eu

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

# usage info
usage(){
  echo "Usage:"
  echo "  $0 -p argument"
  echo ""
  echo "Options:"
  echo "  -p argument : set a single option with argument, repeat it multiple times"
  echo ""
  exit
}

# parse options and arguments
while getopts "p:" option; do
  case $option in
    "p")
      arguments+=("$OPTARG")
      ;;
    *)
      usage
      ;;
  esac
done

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

# result
echo "Provided arguments:"
for argument in "${arguments[@]}"; do
  echo "  \"${argument}\""
done
