It would be nice to design the command-line options so as to avoid any need to use shell meta-characters in the option arguments. That is, if you never require characters like = < > & | ! $ to be part of the option strings, things will be easier and less error-prone for users. (Forgetting to quote some of these things on the command line can have nasty consequences.)
Maybe a set of options like --eq field_1,value_1 --gt field_2,value2 --lt field_3,value_3 and so on? Using this sort of "Polish notation" (operator followed by operands) wouldn't be too hard for users to grok.
And having two operands be comma separated within the single arg string is not a problem, either. Something like this might be suitable:
#!/usr/bin/perl
use strict;
use Getopt::Long;
my $Usage = <<ENDUSE;
Usage: $0 [--operator operand1,operand2] ...
operators: --gt --lt --eq
operand1: field_id
operand2: value
ENDUSE
my %opts;
( GetOptions( \%opts, 'gt=s@', 'lt=s@', 'eq=s@' ) and keys %opts )
or die $Usage;
my @conditions;
for my $opt ( sort keys %opts ) {
for my $arg ( @{$opts{$opt}} ) {
if ( $arg =~ /^([^,]+),([^,]+)$/ ) {
push @conditions, "$1 $opt $2";
} else {
die "Bad option values at '--$opt $arg'\n\n$Usage";
}
}
}
print join "\n", "The stated conditions for this run are:", @condition
+s, "";
That will print the usage synopsis if run with no args or if run with unusable args (like "-?" or "--help" or whatever). It will also check, for each option, whether the supplied arg string contains two things separated by a comma. More checks would be easy to add -- whatever you think is appropriate. |