Home: Perl Programming Help: Regular Expressions:
Print usage whenever there is an argument with no option



guru_perl
Novice

Jan 26, 2011, 6:38 PM


Views: 9885
Print usage whenever there is an argument with no option

 
I am running my script finddata.exe -b file.txt -l data.txt The tool can work 1) with only option b 2.)with only with option l 3.) or both

I have the below code forces to use both the options.My problem is when some doesn't enter the options (-b,-l).I need help on whenever there is an argument with no option. For example when the command is run like below

finddata.exe -b file.txt data.txt (note the missing -l before data.txt).

Code
use strict; 
use warnings;
use Getopt::Std; my %opts;
getopts('b:l:', \%opts);
if (! exists $opts{b} || ! exists $opts{l})//I want to print whenever there is an argument with no option
{ die "Both -b and -l options are required\n"; } print "-b: $opts{b}, -l $opts{l}\n";



BillKSmith
Veteran

Jan 26, 2011, 7:33 PM


Views: 9877
Re: [guru_perl] Print usage whenever there is an argument with no option

Refer perldoc -f defined.


Code
use strict; 
use warnings;
use Getopt::Std;
my %opts;
getopts('b:l:', \%opts);
if ( (!exists $opts{b} and !exists $opts{l})
or (exists $opts{b} and !defined $opts{b})
or (exists $opts{l} and !defined $opts{l})
){
die "Usage:\n"
." finddata.pl -b *file* -l *file*\n"
." finddata.pl -b *file*\n"
." finddata.pl -l *file*\n";
}
print "options ok\n";

Good Luck,
Bill


guru_perl
Novice

Jan 26, 2011, 7:56 PM


Views: 9875
Re: [BillKSmith] Print usage whenever there is an argument with no option

Is the below right?both are opts{b}.Also I dont think we are making sure when there is an argument without option print the usage?

Code
or (exists $opts{b} and !defined $opts{b})



(This post was edited by guru_perl on Jan 26, 2011, 7:59 PM)


BillKSmith
Veteran

Jan 26, 2011, 8:55 PM


Views: 9871
Re: [guru_perl] Print usage whenever there is an argument with no option

You are right. Just add a test for anything left over in ARGV. If the command is coded correctly, getopts will remove everything.


Code
 if  ( (!exists $opts{b} and !exists $opts{l}) 
or (exists $opts{b} and !defined $opts{b})
or (exists $opts{l} and !defined $opts{l})
or @ARGV
){

Good Luck,
Bill


guru_perl
Novice

Jan 26, 2011, 9:07 PM


Views: 9869
Re: [BillKSmith] Print usage whenever there is an argument with no option

@ARGV is not working because all the options are getting stored on ARGV(-bfile.txt-cdata.txt), Also how do I make the options case-insensitive?

For example if i use getopt("blc",\%options);I want to make b,l,c case-insensitive?


(This post was edited by guru_perl on Jan 26, 2011, 9:20 PM)


BillKSmith
Veteran

Jan 27, 2011, 4:27 AM


Views: 9858
Re: [guru_perl] Print usage whenever there is an argument with no option

I still believe that my second solution solves your original problem. Please post your new code and a sample command line which it does not parse correctly.



There are several options for your case problem.

  • Consider using another module such as Getopt::Long


  • Fix the case of the options in AGRV before calling Getopts.


  • Put upper and lower case options in %opts




  • The second is not a good idea for new code, but probably the easiest way to fix existing code.
    Good Luck,
    Bill