There seemed to be an inconsistency in the various example rules given for -x; whatever the case, the first step I would take is to define the usage rules clearly insead of by example, e.g.:

[-x argx1 [argx2 [argx3]]] [-y argy1 [argy2]]

or whatever it should be.

Although a module may or may not be suitable for this, my own approach when the requirement gets too deviant from norms is to:

1) define the rules in a control hash

2) hand-build the required getopts module, deriving everything from the rules you defined.

In this case, I get something a bit complicated that looks like this, but who knows, someone might know a module that does all this...(warning untested!)

my %controlCentre = (); $controlCentre{ ALLOWED }{ x }{ MIN } = 1; $controlCentre{ ALLOWED }{ x }{ MAX } = 3; ... etc. GetOpts( ctrl => \%controlCentre ); # # at this point actual options and arguments have been # loaded into the control hash in the form # $controlCentre{ ACTUAL }{ option-letter } = argument-array # sub GetOpts { my %par=(@_); while ( $ARGV[0] =~ /^\-(.)$/ ) { my $opt = $1; $par{ ctrl } -> { ALLOWED }{ $opt } or Usage( ctrl => $par{ ctrl }); shift @ARGV; my $minAllowed = $par{ ctrl } -> { ALLOWED }{ $opt }{ MIN }; my $maxAllowed = $par{ ctrl } -> { ALLOWED }{ $opt }{ MAX }; $par{ ctrl } -> { ACTUAL }{ $opt } = []; $aref = $par{ ctrl } -> { ACTUAL }{ $opt }; for ( my $i = 1; $i <= $maxAllowed; $i++ ) { if ( $ARGV[0] =~ /^\-(.)$/ ) { ( $i >= $minAllowed ) or Usage( ctrl => $par{ ctrl }); last; } else { push @$aref, shift( @ARGV ); } } } # leftovers must have disobeyed the rules... $ARGV[0] and Usage( ctrl => $par{ ctrl } ); } sub Usage { # even the Usage message is driven by the defined rules my %par = (@_); print STDERR "Usage: $0 "; my $allowRef = $par{ ctrl } -> { ALLOWED }; foreach ( sort keys %$allowref ) { print STDERR "-$_ "; my $minAllowed = $par{ ctrl } -> { ALLOWED }{ $_ }{ MIN }; my $maxAllowed = $par{ ctrl } -> { ALLOWED }{ $_ }{ MAX }; for ( my $i = 1; $i <= $minAllowed; $i++ ) { print STDERR "arg${_}${i} "; } $maxAllowed and print STDERR "[ "; for ( my $i = $minAllowed + 1; $i <= $maxAllowed; $i++ ) { print STDERR "arg${_}${i} "; } $maxAllowed and print STDERR "] "; } print STDERR "\n"; exit 1; }

-S


In reply to Re: -x accepts up to 3 additional arguments by anonymized user 468275
in thread -x accepts up to 3 additional arguments by tcf03

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.