#!/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;
####
package Getopt::Long::Confused;
use Regexp::Assemble;
sub check {
### Params::Validate::validate_pos(@_,{ type=>HASHREF },{ type=>HASHREF });
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
####
#!/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 options
,'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;