Home > Software engineering >  printing invalid options using arrays
printing invalid options using arrays

Time:12-16

I am storing details on invalid option to a function using arrays, w to store the positional index and warg to store the option name.

 declare -a w=()
 declare -a warg=()

 local k=0 vb=0 ctp="" sty=""
 while (( $# > 0 )); do
  k=$((k 1))
  arg="$1"
  case $arg in
   ("--vlt")
     ctp="$vlt" ; r=$k ; shift 1 ;;
   ("--blu")
     blu=$(tput setaf 12)
     ctp="$blu" ; r=$k ; shift 1 ;;
   ("--grn")
     grn=$(tput setaf 2)
     ctp="$grn" ; r=$k ; shift 1 ;;
   ("-m"*)
     sty="${1#-m}" ; r=$k ; shift 1 ;;
   ("--")
     shift 1 ; break ;;
   ("-"*)
     w =($k) ; warg =("$arg")
     shift 1 ;;
   (*)
     break ;;
  esac
 done

After that, I try to loop through the invalid options but as one can see, the positional elements in iw do not map to ${warg[$iw]}, in a way that I can print the invalid option name. What can I do?

 r=4
 local iw
 for iw in "${w[@]}"; do
  if (( iw < r )); then
    printf '%s\n' "Invalid option | ${warg[$iw]}"
  fi
 done

CodePudding user response:

Numerically-indexed arrays don't have to have sequentical indices.

Using an array of color name to number reduces the duplication in the case branches.

declare -A colors=(
  [blu]=12
  [grn]=2
  [ylw]=3
  [orn]=166
  [pur]=93
  [red]=1
  [wht]=7
)
  
declare -a warg=()

for ((i = 1; i <= $#; i  )); do
    arg=${!i}   # value at position $i
    case $arg in
        --blu|--grn|--ylw|--orn|--pur|--red|--wht)
            ctp=$(tput setaf "${colors[${arg#--}]}") ;;
        --vlt) ctp="$vlt" ;;
        -m*) sty="${arg#-m}" ;;
        --)  break ;;
        -*)  warg[i]=$arg ;;
        *)   break ;;
    esac
done

numArgs=$i
shift $numArgs

# iterate over the _indices_ of the wrong args array
for i in "${!warg[@]}"; do
  echo "wrong arg ${warg[i]} at postition $i"
done

That is untested, so there may be bugs

CodePudding user response:

Addressing just the mapping issue between w[] and warg[] ...

The problem seems to be that $k is being incremented for every $arg, but the index for warg[] is only incremented for an 'invalid' $arg.

If the sole purpose of the w[] array is to track the position of 'invalid' args then you can eliminate the w[] array and populate warg[] as a sparse array with the following change to the current code:

# replace:

warg =("$arg")

# with:

warg[$k]="$arg"

With this change your for loop becomes:

for iw in "${!warg[@]}"
do
    ....
    printf '%s\n' "Invalid option | ${warg[$iw]}"
done
  • Related