Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if options have necessary arguments provided

Status
Not open for further replies.

Maldini

Technical User
Nov 10, 2002
55
GB
Working on a script that takes in arguments along with options and using the getopts package right now...
What I seem stuck on is how to implement it so that I can set some options that must be provided with arguments to work like in the usual unix shell getopts...

So for example, I have
getopts ('hd:', \%opts);


Right now, despite D having the ":" after it, it still works if I call
scriptname -d
without any arguments provided to it....

Anyone can give me a hand so that I can enforce this upon the option?
 
I'm not sure if I'm interpreting your problem correctly, so excuse me if this is of no assistance....

I have a number of scripts which have to be run with 2 or more switches e.g.

myscript.pl switch1 switch2

The script will stop with an error message unless 2 switches are given:

my $firstswitch = $ARGV[0];
my $secondswitch = $ARGV[1];
unless ( defined($secondswitch) ) {
die &quot;the syntax should be 'myscript.pl <firstfile> <secondfile>'\n&quot;;}

I hope the above is of some assistance.

 
thx guys, tonykent's way worked the treat :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top