|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Define the regex for matching the arguments |
| 4 | +regexArgShort='^-([a-zA-Z])$' |
| 5 | +regexArgShortChained='^-([a-zA-Z]{2,})$' |
| 6 | +regexArgLong='^--([a-zA-Z\-]{2,})$' |
| 7 | + |
| 8 | +argChunks=() |
| 9 | + |
| 10 | +ARG_DEBUG=false |
| 11 | + |
| 12 | +# Expand chained short form arguments, eg -aih => -a -i -h |
| 13 | +for argChunk in "$@"; do |
| 14 | + |
| 15 | + # See if this argument is a chained short form argument |
| 16 | + [[ $argChunk =~ $regexArgShortChained ]] |
| 17 | + if [ "${BASH_REMATCH[1]}" != "" ]; then |
| 18 | + |
| 19 | + # Get the chunk or arguments |
| 20 | + chainedChunk="${BASH_REMATCH[1]}"; |
| 21 | + |
| 22 | + [ $ARG_DEBUG == true ] && echo "Expanding chained argument chunk: $chainedChunk" |
| 23 | + |
| 24 | + i=0 |
| 25 | + # Expand out the chunk into individual arguments |
| 26 | + while (( i++ < ${#chainedChunk} )); do |
| 27 | + |
| 28 | + # Get just the argument on its own |
| 29 | + argumentIsolated="${chainedChunk:$i-1:1}" |
| 30 | + |
| 31 | + # Add the isolated argument to the argument chunk array |
| 32 | + argChunks+=("-$argumentIsolated") |
| 33 | + done |
| 34 | + continue; |
| 35 | + fi |
| 36 | + |
| 37 | + # Add the argument to the argument array |
| 38 | + argChunks+=("$argChunk") |
| 39 | +done |
| 40 | + |
| 41 | +[ $ARG_DEBUG == true ] && echo "Expanded argument list: ${argChunks[@]}" |
| 42 | + |
| 43 | +# Initialise some variables |
| 44 | +declare -A args |
| 45 | +lastWasArgument=0 |
| 46 | +lastArgument="" |
| 47 | + |
| 48 | +# Loop over all the argument chunks and determine if the argument type and value |
| 49 | +for argChunk in "${argChunks[@]}"; do |
| 50 | + |
| 51 | + # Check if this chunk is a short form argument |
| 52 | + [[ $argChunk =~ $regexArgShort ]] |
| 53 | + if [ "${BASH_REMATCH[1]}" != "" ]; then |
| 54 | + argument="${BASH_REMATCH[1]}" |
| 55 | + lastWasArgument=1 |
| 56 | + lastArgument="$argument" |
| 57 | + |
| 58 | + # Add the argument to the arguments array |
| 59 | + args["${BASH_REMATCH[1]}"]='' |
| 60 | + |
| 61 | + [ $ARG_DEBUG == true ] && echo "Argument (short): ${BASH_REMATCH[1]}" |
| 62 | + |
| 63 | + continue; |
| 64 | + fi |
| 65 | + |
| 66 | + # Check if this chunk is a long form argument |
| 67 | + [[ $argChunk =~ $regexArgLong ]] |
| 68 | + if [ "${BASH_REMATCH[1]}" != "" ]; then |
| 69 | + argument="${BASH_REMATCH[1]}" |
| 70 | + lastWasArgument=1 |
| 71 | + lastArgument="$argument" |
| 72 | + |
| 73 | + # Add the argument to the arguments array |
| 74 | + args["${BASH_REMATCH[1]}"]='' |
| 75 | + |
| 76 | + [ $ARG_DEBUG == true ] && echo "Argument (long): ${BASH_REMATCH[1]}" |
| 77 | + |
| 78 | + continue; |
| 79 | + fi |
| 80 | + |
| 81 | + # If the last chunk was an argument and this wasn't assume its an argument value |
| 82 | + if [ $lastWasArgument == 1 ]; then |
| 83 | + |
| 84 | + # Add the arguments value to the arguments array |
| 85 | + args["$lastArgument"]="$argChunk" |
| 86 | + |
| 87 | + [ $ARG_DEBUG == true ] && echo "Argument Value: $argChunk" |
| 88 | + |
| 89 | + lastWasArgument=0 |
| 90 | + fi |
| 91 | +done |
| 92 | + |
| 93 | +[ $ARG_DEBUG == true ] && echo "Argument array:" |
| 94 | +[ $ARG_DEBUG == true ] && for k in "${!args[@]}" |
| 95 | +do |
| 96 | + echo "ARG: $k = ${args[$k]}" |
| 97 | +done |
| 98 | + |
| 99 | +argExists() { |
| 100 | + if [ -z ${args["$1"]+abc} ]; then |
| 101 | + return 1 |
| 102 | + else |
| 103 | + return 0 |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +argValue() { |
| 108 | + if argExists "$1"; then |
| 109 | + echo "${args["$1"]}" |
| 110 | + fi |
| 111 | +} |
0 commit comments