CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

Post #1 of 6 (4413 views)
Print usage whenever there is an argument with no option Can't Post

 
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

Post #2 of 6 (4405 views)
Re: [guru_perl] Print usage whenever there is an argument with no option [In reply to] Can't Post

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

Post #3 of 6 (4403 views)
Re: [BillKSmith] Print usage whenever there is an argument with no option [In reply to] Can't Post

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

Post #4 of 6 (4399 views)
Re: [guru_perl] Print usage whenever there is an argument with no option [In reply to] Can't Post

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

Post #5 of 6 (4397 views)
Re: [BillKSmith] Print usage whenever there is an argument with no option [In reply to] Can't Post

@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

Post #6 of 6 (4386 views)
Re: [guru_perl] Print usage whenever there is an argument with no option [In reply to] Can't Post

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

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives