Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am creating a command line tool for processing a text file. I have some problem in reading the condition fields from command line. Here is the example of the command line input.
-c:'field_1'='value_1','field_2'>'value_2'
I am using GetOpt::Long module to reading from the command line and reading the fields and their corresponding values. My problem is i have to read the conditional operators (=,>,<) also. Is there any best to do that? Thankx in Advance.

Replies are listed 'Best First'.
Re: Command Line Argument Processing
by quester (Vicar) on Nov 27, 2006 at 06:55 UTC
    You need to change the format of your command line. Because the ">" is not inside quotes it will be interpreted by the shell, redirecting the output of the command to a file named value_2. This happens before your script gets control. You might try changing the command line to
    -c:'field_1=value_1','field_2>value_2'
    Update: after looking at Getopts::Long again, it needs to be more like this, with repeated -c options:
    -c 'field_1=value_1' -c 'field_2>value_2'
    This can be parsed with
    use Getopt::Long; our @opt_c; GetOptions("c=s@"); print join ", ", @opt_c;
    but you will still need to break out the field names and values from each element of the array @opt_c with a regex, something like
    /^(.*?)([<=>])(.*)/
      Thanks quester.
Re: Command Line Argument Processing
by graff (Chancellor) on Nov 28, 2006 at 02:41 UTC
    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.