Hi!

Consider this as a regex problem. First we need to put the options into an order suitable for regex'ing then we'll apply a regex to the string.

Let's assume, for example, that our primary options are represented by the vowels 'a', 'e' ... , 'y' and that we may have one and only one. Let's assume that the primary option 'a' may have a string and must have one of options 'b', 'c', or 'd' etc.

#!/usr/bin/env perl use Getopt::Long; GetOptions(\my %h ,qw(a:s b c d) ,qw(e=s f g h) ,qw(i:o j k l m n) ,qw(o=o p q r s t) ,qw(u:f w x) ,qw(y=f z) ); ### dump: %h ### 'Order them ...' my $command_line; for my $option ('a'..'z') { $command_line.="$option " if (exists $h{$option}); } ### dump: $command_line ### 'Parse $command_line with a regex ...' if ($command_line !~ m{^(\ba\b( [b..d])|e|i|o|u|y) $}) { # conflicting + options die "Conflicting options in '$command_line'!"; } ### "We're good to go with '$command_line'!" exit;

Building on the above we get

package Getopt::Long::Confused; use Regexp::Assemble; sub check { ### Params::Validate::validate_pos(@_,{ type=>HASHREF },{ type=>HA +SHREF }); my ($option_HREF,$rule_HREF)=@_; my $string=''; my $re=Regexp::Assemble->new(); for my $rule (keys %$rule_HREF) { # Add the regex for this list $re->add($rule_HREF->{$rule}); # Make a copy of our option hash my %option_copy=%$option_HREF; for my $option (split ' ',$rule) { if ($option eq '*') { # Any and all leftovers for my $option (keys %option_copy) { $string.="$option "; } } elseif (exists $option_copy{$option}) { # Add the option's + name and remove it from the copy $string.="$option "; delete $option_copy{$option}; }; }; # Separate the rules with a new line $string.="\n"; } ### dump: $string,$re->as_string() if ($string =~ m{$re}m) { return "good!"; } else { return "bad"; }; }; # check: 0**0

which we can invoke with

#!/use/bin/env perl use Getopt::Long; use Getopt::Long::Confused; GetOptions(\my %h ,qw(a:s b c d) ,qw(e=s f g h) ,qw(i:o j k l m n) ,qw(o=o p q r s t) ,qw(u:f w x) ,qw(y=f z) ); my %option=( # after placing options in this order # => check with this regex 'a b c d *' => '^a( [bcd]) $' # a requires one of b, c +or d ,'e f g h *' => '^e( [fgh])? $' # e requires at most one +of f g or h ,'i j k l m n *' => '^$' # don't allow i and o opt +ions ,'o p q r s t *' => '^$' # in this example ,'u v w x *' => '^u( v)|( w x) $' # u requires v OR w and x ,'y z *' => '^y z $' # y requires z ); if (my $warning=Getopt::Long::Confused::check(\%h,\%option)) { die $warning; } ### 'done!' exit;

In reply to Re: How do I process many (conflicting) command line parameters? by clueless newbie
in thread How do I process many (conflicting) command line parameters? by ozboomer

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.