Help with getopt

Hi,

I want to use the getopt function to parse some arguments for a script.

while getopts "i:f:r:" OPTION
do
 
    case $OPTION in

    i)  iter=$OPTARG;;
    f)  frame=$OPTARG;;
    r)  roi=$OPTARG;;       
    ?)  echo Usage: ......
        exit 2;;

    esac
done

However, I want the -r flag to parse more than 1 argument. That is, the usage of this sript to be like:

script.sh -i [number] -f [frames] -r [roi1 roi2 roi3....]

The code that I already have parses only the first argument after -r (i.e. roi1). What do I have to change in order to parse all the arguments after the flag -r?

Thanks a lot in advance!
giorgos

you can put quotes around the multiple arguments, the the shell would pass them through as one (and getopt should do the same)
or have mutltiple -r statements and alter the code to add to the roi variable rather than overwriting it

Thanks for your response!

Yes, this makes sense... I tried before the double quotes and it worked... but I was wondering if there was a way to do this without the double quotes...

Thanks, though!

well you could monkey with the IFS variable in bash, but that would throw up even more headaches. Basically, you need to stop the shell from reading the spaces as spaces (i.e. field seperators) so you could also quote each individual space by backslashing it.
I think the easiest way is to enforce multiple -r <option> on the command line, and alter the code accordingly.

How could i enforce multiple option?

Ok... it seems to be too difficult for me.... I should probably give up on this...

I'll try to implement differently my code! Thanks a lot for your response!

you could ditch the -r requirement and have it's options as the last parameters on the command line.

while getopts "i:f:r:" OPTION
do
 
    case $OPTION in

    i)  iter=$OPTARG;;
    f)  frame=$OPTARG;;  
    ?)  echo Usage: ......
        exit 2;;

    esac

done

# remove any arguments found so far
shift $(($OPTIND - 1))
# here roi will be the remaining arguments on the command line
roi="'$*'"

the roi variable will now have the rest of the command line, so if it was called thus:

application -i 2 -f 3 roi1 roi2 roi3

then the output would be:

$iter==2
$frame==3
$roi=='roi1 roi2 roi3'

and if you didn't want the roi variable to be quoted, as I have it here, then remove the single quotes around '$*' on the line in red above.
Hope this helps, and never give up, it is never tooooo difficult.