is this a bug in getopts

D

dcruncher4

my perl script takes two arguments -s and -t and both require a value.
this is the code

#! /bin/perl -w

use strict;
use Getopt::Std;
use constant OPTS => "s:t:" ;

use vars qw (%opt) ;

getopts(OPTS,\%opt) or &usage();


if (defined($opt{s})) {
print "you supplied $opt{s} as value for -s\n" ;
}
if (defined($opt{t})) {
print "you supplied $opt{t} as value for -t\n" ;
}

sub usage() {
print "your usage is incorrect\n" ;
exit(-1);
}

if i call the script as follows

script_name -s first_argument -t second_argument
you supplied first_argument as value for -s
you supplied second_argument as value for -t

which is fine.
Now if I call it as
script_name -s -t second_argument
the output is
you supplied -t as value for -s

Shouldn't getopts go to subroutine usage or spew out an error message
that
-s is a mandatory parameter.

What am I missing?
 
B

Brian McCauley

my perl script takes two arguments -s and -t and both require a value.
this is the code

#! /bin/perl -w

use strict;
use Getopt::Std;
use constant OPTS => "s:t:" ;

use vars qw (%opt) ;

getopts(OPTS,\%opt) or &usage();

if (defined($opt{s})) {
print "you supplied $opt{s} as value for -s\n" ;}

if (defined($opt{t})) {
print "you supplied $opt{t} as value for -t\n" ;

}

sub usage() {
print "your usage is incorrect\n" ;
exit(-1);

}

if i call the script as follows

script_name -s first_argument -t second_argument
you supplied first_argument as value for -s
you supplied second_argument as value for -t

which is fine.
Now if I call it as
script_name -s -t second_argument
the output is
you supplied -t as value for -s

Shouldn't getopts go to subroutine usage or spew out an error message
that
-s is a mandatory parameter.

I'm guessing Perl's Getopt::Std is supposed to behave according to the
"standard".

The only standard I can think of is http://www.unix.org/single_unix_specification/
and as far as I can see it doesn't mandate one way or the other on
this matter.
 
B

Brian McCauley

I'm guessing Perl's Getopt::Std is supposed to behave according to the
"standard".

The only standard I can think of ishttp://www.unix.org/single_unix_specification/
and as far as I can see it doesn't mandate one way or the other on
this matter.
From a very small and unscientific sample of commands on a GNU/Linux
box I have here it appears that the Getopt::Std behaviour is not
totally unprecedented.

And thinking about it, since there would be no way to quote the '-' it
_must_ be assumed that in @ARGV=('-s','-t') the '-t' is the optarg for
'-s'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top