getopt(3) posting

Daniel R. Levy levy at ttrdc.UUCP
Thu Oct 24 10:24:46 AEST 1985


In article <324 at graffiti.UUCP>, peter at graffiti.UUCP (Peter da Silva) writes:
>	connect: a UNIX modem program that I wrote. It allows a series of
>phone numbers on the command line & keeps trying them until it gets one that
>works. Handy for calling bbs-es:
>	usage: connect -s<baud> -l<line> number...
>		Note: direct is considered a number for compatibility with cu.
>
>	connect -s 1200 4445555 4446666 -s300 5556666 6667777 -l tty1 direct
>
>How would you deal with that using getopt, which seems to require that all
>options be before all arguments?
>
>Peter da Silva

Maybe with a bit of change in the command line syntax, it would be amenable
to getopt.  Remember, that there is nothing keeping the same flag from being
used more than once:

   connect -s 1200 -n 4445555,4446666 -s300 -n 5556666,6667777 -l tty1 direct

If you MUST keep the original syntax (mixing flags with nonflag arguments)
you can still use getopt with a little bit of shimming.  Just increment
optind (presuming it is still smaller than argc) after getopt has returned
EOF, check that the first character of the corresponding argument is a '-'
(i.e., another flag, else handle the argument specially) then jump back
into the loop calling getopt.  It's still cleaner looking inside the program
than a brute force parse.

Of course someone is going to ask what if the argument was supposed to begin
with '-' and it is not a flag.  Oh well, life ain't easy....
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!ihnp4!ttrdc!levy



More information about the Comp.sources.bugs mailing list