[comp.lang.perl] getopt

emv@math.lsa.umich.edu (Edward Vielmetti) (12/13/89)

how can I use getopt to detect the presence or absence of
a switch?  I thought I had it down pat:

#!/usr/local/bin/perl

do 'getopt.pl';

&Getopt('p');

if ($opt_p) {
	print STDERR "P option selected\n" ;
} else {
	print STDERR "No P option\n";
}

picasso /tmp/emv %  ./post.pl -q		# OK
No P option				
picasso /tmp/emv %  ./post.pl -p		# huh?
No P option
picasso /tmp/emv %  ./post.pl -p 1		# OK again
P option selected

How do I recognize '-p' w/o any argument to it ?

thanks,

--Ed

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (12/13/89)

In article <EMV.89Dec12114043@picasso.math.lsa.umich.edu> emv@math.lsa.umich.edu (Edward Vielmetti) writes:
: 
: how can I use getopt to detect the presence or absence of
: a switch?  I thought I had it down pat:
: 
: #!/usr/local/bin/perl
: 
: do 'getopt.pl';
: 
: &Getopt('p');
: 
: if ($opt_p) {
: 	print STDERR "P option selected\n" ;
: } else {
: 	print STDERR "No P option\n";
: }
: 
: picasso /tmp/emv %  ./post.pl -q		# OK
: No P option				
: picasso /tmp/emv %  ./post.pl -p		# huh?
: No P option
: picasso /tmp/emv %  ./post.pl -p 1		# OK again
: P option selected
: 
: How do I recognize '-p' w/o any argument to it ?

The argument to Getopt() specifies the switches that take an argument.
You can't have a switch that both does and doesn't take an argument.
How would you parse this:

	./post.pl -p -q

Larry

scs@itivax.iti.org (Steve Simmons) (12/13/89)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

>In article <EMV.89Dec12114043@picasso.math.lsa.umich.edu> emv@math.lsa.umich.edu (Edward Vielmetti) writes:
>: 
>: picasso /tmp/emv %  ./post.pl -q		# OK
>: No P option				
>: picasso /tmp/emv %  ./post.pl -p		# huh?
>: No P option
>: picasso /tmp/emv %  ./post.pl -p 1		# OK again
>: P option selected
>: 
>: How do I recognize '-p' w/o any argument to it ?

>The argument to Getopt() specifies the switches that take an argument.
>You can't have a switch that both does and doesn't take an argument.
>How would you parse this:

>	./post.pl -p -q

There do exist a number of switches which take optional trailing
arguements with a default assumed when the trailer isn't there.  The
getopt() function distributed by AT&T handles this correctly, with
optarg (I think that's the name) being NULL.  Way back when AT&T was
suggesting a standard for switch parsing, this was allowed.

A real-world example:

   cc -O

means optimse the program to level 2, while

   cc -O3

means use level 3.
-- 
Steve Simmons	       scs@iti.org         Industrial Technology Institute
	'"You're not a big name on Usenet until someone puts
	  you in their .sig file."		-- Anonymous'

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (12/13/89)

In article <4644@itivax.iti.org> scs@itivax.iti.org (Steve Simmons) writes:
: There do exist a number of switches which take optional trailing
: arguements with a default assumed when the trailer isn't there.  The
: getopt() function distributed by AT&T handles this correctly, with
: optarg (I think that's the name) being NULL.  Way back when AT&T was
: suggesting a standard for switch parsing, this was allowed.
: 
: A real-world example:
: 
:    cc -O
: 
: means optimse the program to level 2, while
: 
:    cc -O3
: 
: means use level 3.

Way back when AT&T was suggesting a standard for switch parsing,

    cc -O3

would mean the same as two separate switches:

    cc -O -3

They were requiring space between a switch and any option so that they could
do switch bundling.  Someplace here I've got the brochure they were handing
out at Usenix at the time.

Just to put this into perspective, I never use getopt, either in C or in perl.
I suppose this could be construed as a character flaw.

Larry

scs@itivax.iti.org (Steve Simmons) (12/13/89)

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) writes:

>In article <4644@itivax.iti.org> scs@itivax.iti.org (Steve Simmons) writes:
>: There do exist a number of switches which take optional trailing
>: arguements . . .

>Way back when AT&T was suggesting a standard for switch parsing,

>    cc -O3

>would mean the same as two separate switches:

>    cc -O -3

>They were requiring space between a switch and any option so that they could
>do switch bundling.  Someplace here I've got the brochure they were handing
>out at Usenix at the time.

"-O3" == "-O -3" if and only if (1) O is a defined switch which does
not take a trailing arg, and (2) 3 is a defined switch.  Otherwise the
first means "-O with value 3" while the second means "-O with value -3".

Switch grouping was allowed as you describe, but any switch with a
trailer consumed the next arg.  'Next arg' in this case had the very
curious definition of being (my paraphrase) 'if there are unprocessed
characters following the switch they will be used as the trailer,
otherwise the next argument in the list is the trailer'.  So if we
assume -O takes no trailer but -G does
  cc -O3G4
means "-O switch", "-3 switch (illegal, since we haven't defined such)",
"-G switch with optarg 4".

Of course, without my brochure handy I can't quote chapter and verse, but
that's was my interpretation of it based on reading the brochure and
their getopt() source some years ago.

Whatever happened to that "standard", anyway?  I believe the brochure claimed
it would be part of SVID utilities or something like that.
-- 
Steve Simmons	       scs@iti.org         Industrial Technology Institute
	'"You're not a big name on Usenet until someone puts
	  you in their .sig file."		-- Anonymous'