Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: How do I process many (conflicting) command line parameters?

by clueless newbie (Deacon)
on Jul 04, 2017 at 14:22 UTC ( #1194151=note: print w/replies, xml ) Need Help??

in reply to How do I process many (conflicting) command line parameters?


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;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1194151]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2019-11-20 06:36 GMT
Find Nodes?
    Voting Booth?
    Strict and warnings: which comes first?

    Results (96 votes). Check out past polls.