in reply to How to pass and retrieve command-line arguments specifying search conditions?

Ok, for command line args, by far the best way to do it (IMHO) is to use Getopt, but it will require you to change your syntax a little. Here is some example (sortof tested) code:

use Getopt::Std; # set up informative strings my $usage = "$0 version $VERSION by -= $AUTHOR =- use -h for help"; my $help = "use $0 {-a | -o} <search1> <search2> use $0 -a to "AND" your criteria use $0 -o to "OR" your criteria "; #when you call getopts, you pass a string and a hash ref. # the string defines what switches you accept, and the hash # gets filled in accordingly. For instance, if someone put # in a -h here, $options{h} would be set to 1 (true). my %options; getopts('huao', \%options); # in this case, I'm assuming either and "and" or "or" is # required. $options{u}=1 unless %options && ($options{a} || $options{o}); #it's more readable to break this into two tests #we need to make sure someone didn't put in -a and -o $options{u}=1 if ($options{a} && $options{o}); # I haven't done it this way in a while, but IIRC # anything not handled by getopts gets left in ARGV # Therefore, we need to make sure that there are two # options left (two criteria) $options{u}=1 unless $#ARGV==2; # now here we go. for any of our test cases we would # have set $options{u}, so if the command line was bad # it will get caught and exited here. if ($options{h} || $options{u}){ print "$usage\n"; print "$help\n" if ($options{h}); exit 0; }

From here I hope it's pretty obvious. Either $options{o} or $options{a} is set, and the criteria just need to be assigned out of ARGV.

Like I said, the code is untested. It comes from my boilerplate new script template, but I've never tested this particular writeup.

Take some time to read the documentation for getopt::std and getopt::long, they are worth keeping in your perl arsenal
  • Comment on Re: How to pass and retrieve command-line arguments specifying search conditions?
  • Download Code