shflags icon indicating copy to clipboard operation
shflags copied to clipboard

Feature request: support for multi-value arguments

Open stephenchu opened this issue 9 years ago • 3 comments

Will it be possible to support something like such in Bash getopts?

#! /bin/bash

while getopts ":t:" opt; do
  case $opt in
    t)
      TAGS+=($OPTARG)
      ;;
  esac
done
shift $((OPTIND-1))

for $tag in "${TAGS[@]}"; do
  echo "Tag: $tag"
done

Output:

$ foo.sh -t one -t two
Tag: one                                                                                            
Tag: two                                                                                            

stephenchu avatar May 10 '16 21:05 stephenchu

Do you mean just to incorporate tags or multi-value parameters into the template? Like the following

while getopts ":vht:" optname
  do
    case "$optname" in
      "v")
        echo "Version $VERSION"
        exit 0;
        ;;
      "h")
        echo $USAGE
        exit 0;
        ;;
      "t")
        TAGS+=($OPTARG)

RenatGilmanov avatar May 12 '16 21:05 RenatGilmanov

I meant having multi-value parameters. "tags" is just an example of mine as most things that are taggable these days have multiple tags.

The existing boolean/integer/float/string data types may not be sufficient to represent TAGS in the example. It might make sense to have DEFINE_array but I can imagine it being pretty difficult to implement.

stephenchu avatar Jun 02 '16 16:06 stephenchu

Ok, I see. You are right saying multi-value parameters are pretty useful. Will try to reuse/invent something. Thank you for providing valuable comments and sharing your ideas.

RenatGilmanov avatar Jun 06 '16 14:06 RenatGilmanov