Home > Blockchain >  Using two argument values for an option with getopt
Using two argument values for an option with getopt

Time:11-17

Have been using getopt for some time. I am looking into the possibility of having two argument values for an option. Is this good and possible to do this with getopt. An example would help.

Have done some tests and figured out that doing

myfunc -S 13 21

gives

opts:  -S '13' -- '21'

where

opts=$( getopt -o "$shortopts" -l "$longopts" -n "${0##*/}" -- "$@" )

Thusly getopt is incapable of accepting myfunc -S 13 21.

CodePudding user response:

Look here Retrieving multiple arguments for a single option using getopts in Bash Basically says that you can add an option multiple times at the command line like this: myscript.sh -x value1 -x value2 And when you loop through the options you can append it every time getopt encounters that option. Here is an example:

#!/bin/bash
$XVALUE
while getopts "x:" arg; do
  case $arg in
    x)
      XVALUE="$XVALUE$OPTARG"
      ;;
  esac
done

echo "Values: $XVALUE"
# ./myscript -x a -x b
# Values: ab

CodePudding user response:

I am handling things this way

 while (( $# > 0 )); do
   case $1 in
    ("-S"|"--seam")
      [[ "$2" =  ([[:digit:]]) ]] && { sp="$2" ; shift ; }
      [[ "$2" =  ([[:digit:]]) ]] && { sq="$2" ; shift ; }
      ;;
   esac
 done
  • Related