views:

24

answers:

2

I am seeing if there is a way for getopts to handle switches with strings instead of characters.

For example, I would like to supply something like this:

script.ksh -file1 file1.txt -file2.txt

Instead of:

script.ksh -f file1.txt -g file2.txt

Is this possible with unix getopts? Thanks!

A: 

No, it's not possible with getopts. You must do your own parsing, e.g. with a case switch:

while (($# > 0))
do
    case "$1" in
    -file1)
        shift
        file1=$1;;
    -file2)
        shift
        file2=$1;;
    esac
    shift
done
Philipp
I had a feeling this was the case. Thanks a lot!
Shane
A: 

The external getopt (note no "s") can handle long options, but it has its own disadvantages.

From BashFAQ/035:

Never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed.

Dennis Williamson