Operating System - HP-UX
1834288 Members
2433 Online
110066 Solutions
New Discussion

getopts with optional parameters

 
SOLVED
Go to solution
Jaris Detroye
Frequent Advisor

getopts with optional parameters

I would like to use the 'getopts' command to parse my command line, but *ALL* options are optional.
So far I have been unable to figure out how to tell the 'getopts' command the two options which are valid e.g. 'r' & 'n', but that they are optional and it is valid to call the script with no options at all.

Any suggestions?
8 REPLIES 8
Jdamian
Respected Contributor

Re: getopts with optional parameters

'but *ALL* options are optional' --> what does it mean ?.... Of course, all options are optional.
Graham Cameron_1
Honored Contributor

Re: getopts with optional parameters

I think you need something like
--
--
while getopts r:n name
do
case ${name} in
r)
...CODE TO SET "r" FLAGS HERE;;
n)
...CODE TO SET "n" FLAGS HERE;;
?)
...CODE TO HANDLE INVALID OPTIONS HERE ;;
esac
done
--
in your "...CODE" sections you can refer to ${OPTARG}, so that
if your program is invoked with "-r fred", then OPTARG would
contain "fred".
Hope you get the idea
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Bhuvaneswari Selvaraj
Valued Contributor

Re: getopts with optional parameters

int
main (int argc, char *argv[])
{
int c=0,rflag=0, nflag=0;
while (( c = getopt(argc,argv,":r:n:")) != -1 ) {
switch (c) {
case 'r':
printf("Option r chosen");
rflag++;
break;
case 'n':
printf("Option n chosen");
nflag++;
break;
case '?':
printf ("Error");
break;
}
}
}

if (rflag>1)
printf ("Error");
if (nflag >1)
printf("Error");
Hope this helps...
Jaris Detroye
Frequent Advisor

Re: getopts with optional parameters

The problem that I see is this, if I get a '?' in the $OPTARG when either an invalid option is used, or when no options at all are used, then it is up to me to detirmine which case it is. I was hoping that I could aviod having to do that.
Graham Cameron_1
Honored Contributor
Solution

Re: getopts with optional parameters

YOu can examine $OPTIND, which is initialised to 1, and incremented as arguments are processed. It it is still 1, at the end of the while loop, then no arguments were processed.
Also, processing will drop through to the "?)" clause whenever an unexpected argument is found, ie anything except 'r', 'n'.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
john korterman
Honored Contributor

Re: getopts with optional parameters

Hi,
I tried to experiment a little; perhaps you can use it...

regards,
John K.
it would be nice if you always got a second chance
Jaris Detroye
Frequent Advisor

Re: getopts with optional parameters

Graham you hit it right on the head. That did not occur to me. Good job, thanks.

John, that is one approach I never thought of. Encasing the getopts command within a case statement hmmm..... might have to try that one out sometime.

Cudos to all!!!