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.


In reply to Re: Command Line Argument Processing by graff
in thread Command Line Argument Processing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.