| 1 | 12 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | package Getopt::Lucid; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Clear, readable syntax for command line processing | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | our $VERSION = '1.09'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | our @EXPORT_OK = qw(Switch Counter Param List Keypair); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] ); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use Exporter (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use List::Util (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 12 | ▶ | use Getopt::Lucid::Exception; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | use Storable 2.16 qw(dclone); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | # Definitions | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | my $VALID_STARTCHAR = "a-zA-Z0-9"; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 12 | my $VALID_CHAR = "a-zA-Z0-9_-"; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 12 | my $VALID_LONG = qr/--[$VALID_STARTCHAR][$VALID_CHAR]*/; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 12 | my $VALID_SHORT = qr/-[$VALID_STARTCHAR]/; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | my $VALID_BARE = qr/[$VALID_STARTCHAR][$VALID_CHAR]*/; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | my $VALID_NAME = qr/$VALID_LONG|$VALID_SHORT|$VALID_BARE/; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | my $SHORT_BUNDLE = qr/-[$VALID_STARTCHAR]{2,}/; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | my $NEGATIVE = qr/(?:--)?no-/; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 12 | my @valid_keys = qw( name type default nocase valid needs canon doc ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 12 | my @valid_types = qw( switch counter parameter list keypair); | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | sub Switch { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 49 | ▶ | return bless { name => shift, type => 'switch' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Switch: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | "Getopt::Lucid::Spec"; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | sub Counter { | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 24 | ▶ | return bless { name => shift, type => 'counter' }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Counter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | "Getopt::Lucid::Spec"; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||
| 41 | sub Param { | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 36 | ▶ | my $self = { name => shift, type => 'parameter' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Param: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 36 | ▶ | $self->{valid} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 36 | return bless $self, "Getopt::Lucid::Spec"; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | sub List { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 10 | ▶ | my $self = { name => shift, type => 'list' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
List: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | ▶ | $self->{valid} = shift if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | return bless $self, "Getopt::Lucid::Spec"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | sub Keypair { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | ▶ | my $self = { name => shift, type => 'keypair' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Keypair: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 13 | ▶ | $self->{valid} = [ @_ ] if scalar @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | return bless $self, "Getopt::Lucid::Spec"; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | package | ||||||||||||||||||||||||||||||||||||||||||
| 58 | Getopt::Lucid::Spec; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 12 | $Getopt::Lucid::Spec::VERSION = $Getopt::Lucid::VERSION; | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | # alternate way to specify validation | ||||||||||||||||||||||||||||||||||||||||||
| 62 | sub valid { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
valid: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | Getopt::Lucid::throw_spec("valid() is not supported for '$self->{type}' options") | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | ▶ | unless grep { $self->{type} eq $_ } qw/parameter list keypair/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | $self->{valid} = $self->{type} eq 'keypair' ? [ @_ ] : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | sub default { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 37 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 37 | my $type = $self->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 37 | ▶ | if ($self->{type} eq 'keypair') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 6 | ▶ | if (ref($_[0]) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | $self->{default} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | elsif ( @_ % 2 == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | $self->{default} = { @_ }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | $self->{default} = []; # will cause an exception later | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | elsif ( $self->{type} eq 'list' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | $self->{default} = [ @_ ]; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 26 | $self->{default} = shift; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 37 | return $self | |||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | sub anycase { my $self = shift; $self->{nocase}=1; return $self }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
anycase: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 5 | ▶ | sub needs { my $self = shift; $self->{needs}=[@_]; return $self }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
needs: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | sub doc { my $self = shift; $self->{doc}=shift; return $self }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
doc: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 541 | ▶ | sub _clone { my $self = shift; bless { %$self }, ref $self } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | package Getopt::Lucid; | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # new() | ||||||||||||||||||||||||||||||||||||||||||
| 105 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 12 | my @params = qw/strict target/; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 190 | ▶ | my ($class, $spec, $target) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 190 | ▶ | my $args = ref($_[-1]) eq 'HASH' ? pop(@_) : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 190 | ▶ | $args->{target} = ref($target) eq 'ARRAY' ? $target : \@ARGV; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 190 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 190 | $self->{$_} = $args->{$_} for @params; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 190 | $self->{raw_spec} = $spec; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 190 | ▶ | bless ($self, ref($class) ? ref($class) : $class); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | throw_usage("Getopt::Lucid->new() requires an option specification array reference") | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 190 | ▶ | unless ref($self->{raw_spec}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 189 | _parse_spec($self); | |||||||||||||||||||||||||||||||||||||||||
| 120 | 180 | _set_defaults($self); | |||||||||||||||||||||||||||||||||||||||||
| 121 | 178 | $self->{options} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 122 | 178 | $self->{parsed} = []; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 178 | $self->{seen}{$_} = 0 for keys %{$self->{spec}}; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 178 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 128 | # append_defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 129 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | sub append_defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
append_defaults: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | my %append = | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 8 | ▶ | ref $_[0] eq 'HASH' ? %{+shift} : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | (@_ % 2 == 0) ? @_ : | ||||||||||||||||||||||||||||||||||||||||||
| 136 | throw_usage("Argument to append_defaults() must be a hash or hash reference"); | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | for my $name ( keys %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 138 | 45 | my $spec = $self->{spec}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 45 | my $strip = $self->{strip}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 45 | ▶ | next unless exists $append{$strip}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 29 | for ( $spec->{type} ) { | |||||||||||||||||||||||||||||||||||||||||
| 142 | 29 | ▶ | /switch|parameter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 15 | $self->{default}{$strip} = $append{$strip}; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 15 | last; | |||||||||||||||||||||||||||||||||||||||||
| 145 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 14 | ▶ | /counter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 4 | $self->{default}{$strip} += $append{$strip}; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 10 | ▶ | /list/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | throw_usage("Option '$strip' in append_defaults() must be scalar or array reference") | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 5 | ▶ | if ref($append{$strip}) && ref($append{$strip}) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $append{$strip} and ref $append{$strip} ne "ARRAY"
MC/DC100%2/2
ref $append{$strip} and ref $append{$strip} ne "ARRAY"
ref $append{$strip}
ref $append{$strip} ne "ARRAY"
Truth table100%3/3
A and B
A= ref $append{$strip}B= ref $append{$strip} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | $append{$strip} = ref($append{$strip}) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 154 | ? dclone( $append{$strip} ) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 4 | ▶ | : [ $append{$strip} ] ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | push @{$self->{default}{$strip}}, @{$append{$strip}}; | |||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 158 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 5 | ▶ | /keypair/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | throw_usage("Option '$strip' in append_defaults() must be scalar or hash reference") | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 5 | ▶ | if ref($append{$strip}) && ref($append{$strip}) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $append{$strip} and ref $append{$strip} ne "HASH"
MC/DC50%1/2
ref $append{$strip} and ref $append{$strip} ne "HASH"
ref $append{$strip}
ref $append{$strip} ne "HASH"
Truth table66%2/3
A and B
A= ref $append{$strip}B= ref $append{$strip} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | $self->{default}{$strip} = { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | %{$self->{default}{$strip}}, | |||||||||||||||||||||||||||||||||||||||||
| 164 | 4 | %{$append{$strip}}, | |||||||||||||||||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 167 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | throw_spec("Default '$spec->{canon}' = '$self->{default}{$strip}' fails to validate") | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 27 | ▶ | unless _validate_value($self, $self->{default}{$strip}, $spec->{valid} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 4 | _recalculate_options($self); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | return $self->options; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 177 | # defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 178 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | sub defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 181 | 7 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 7 | return %{dclone($self->{default})}; | |||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 187 | # getopt() | ||||||||||||||||||||||||||||||||||||||||||
| 188 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | sub getopt { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 141 | ▶ | my ($self,$spec,$target) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
getopt: called CC=16 SCAR=27.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | 141 | ▶ | if ( $self eq 'Getopt::Lucid' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | throw_usage("Getopt::Lucid->getopt() requires an option specification array reference") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | unless ref($spec) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | $self = new(@_) | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | 140 | my (@passthrough); | |||||||||||||||||||||||||||||||||||||||||
| 198 | 140 | ▶ | while (@{$self->{target}}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 287 | my $raw = shift @{$self->{target}}; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 287 | ▶ | last if $raw =~ /^--$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 272 | my ($orig, $val) = _split_equals($self, $raw); | |||||||||||||||||||||||||||||||||||||||||
| 202 | 272 | ▶ | next if _unbundle($self, $orig, $val); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 248 | ▶ | my $neg = $orig =~ s/^$NEGATIVE(.*)$/$1/ ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | 248 | my $arg = _find_arg($self, $orig); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 248 | ▶ | if ( $arg ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | $neg ? | ||||||||||||||||||||||||||||||||||||||||||
| 207 | $self->{seen}{$arg} = 0 : | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 232 | ▶ | $self->{seen}{$arg}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 232 | for ($self->{spec}{$arg}{type}) { | |||||||||||||||||||||||||||||||||||||||||
| 210 | 232 | ▶ | /switch/ ? _switch ($self, $arg, $val, $neg) : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | /counter/ ? _counter ($self, $arg, $val, $neg) : | ||||||||||||||||||||||||||||||||||||||||||
| 212 | /parameter/ ? _parameter($self, $arg, $val, $neg) : | ||||||||||||||||||||||||||||||||||||||||||
| 213 | /list/ ? _list ($self, $arg, $val, $neg) : | ||||||||||||||||||||||||||||||||||||||||||
| 214 | /keypair/ ? _keypair ($self, $arg, $val, $neg) : | ||||||||||||||||||||||||||||||||||||||||||
| 215 | throw_usage("can't handle type '$_'"); | ||||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 16 | ▶ | throw_argv("Invalid argument: $orig") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | if $orig =~ /^-./; # invalid if looks like it could be an arg; | ||||||||||||||||||||||||||||||||||||||||||
| 220 | 5 | push @passthrough, $orig; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||
| 223 | 103 | _recalculate_options($self); | |||||||||||||||||||||||||||||||||||||||||
| 224 | 103 | @{$self->{target}} = (@passthrough, @{$self->{target}}); | |||||||||||||||||||||||||||||||||||||||||
| 225 | 103 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 12 | ▶ | BEGIN { *getopts = \&getopt }; # handy alias | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 231 | # validate | ||||||||||||||||||||||||||||||||||||||||||
| 232 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | sub validate { | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 83 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=10 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 83 | ▶ | throw_usage("Getopt::Lucid->validate() takes a hashref argument") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$arg and ref $arg ne "HASH"
MC/DC0%0/2
$arg and ref $arg ne "HASH"
$arg
ref $arg ne "HASH"
Truth table33%1/3
A and B
A= $argB= ref $arg ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | if $arg && ref($arg) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 83 | ▶ | if ( $arg && exists $arg->{requires} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$arg and exists $arg->{"requires"}
MC/DC50%1/2
$arg and exists $arg->{"requires"}
$arg
exists $arg->{"requires"}
Truth table66%2/3
A and B
A= $argB= exists $arg->{"requires"}
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 7 | my $requires = $arg->{requires}; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 7 | ▶ | throw_usage("'validate' argument 'requires' must be an array reference") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$requires and ref $requires ne "ARRAY"
MC/DC0%0/2
$requires and ref $requires ne "ARRAY"
$requires
ref $requires ne "ARRAY"
Truth table33%1/3
A and B
A= $requiresB= ref $requires ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | if $requires && ref($requires) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 7 | for my $p ( @$requires ) { | |||||||||||||||||||||||||||||||||||||||||
| 244 | throw_spec("Requiring an unspecified option ('$p') in validate()") | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 7 | ▶ | unless exists $self->{spec}{$p}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | throw_argv( "Required option '$self->{spec}{$p}{canon}' not found") | ||||||||||||||||||||||||||||||||||||||||||
| 247 | 7 | ▶ | if ( !$self->{seen}{$p} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 81 | _check_prereqs($self); | |||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 76 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 257 | # merge_defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 258 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | sub merge_defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
merge_defaults: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | my %merge = | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 8 | ▶ | ref $_[0] eq 'HASH' ? %{+shift} : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | (@_ % 2 == 0) ? @_ : | ||||||||||||||||||||||||||||||||||||||||||
| 265 | throw_usage("Argument to merge_defaults() must be a hash or hash reference"); | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 7 | for my $name ( keys %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 267 | 44 | my $spec = $self->{spec}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 44 | my $strip = $self->{strip}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 269 | 44 | ▶ | next unless exists $merge{$strip}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 29 | for ( $self->{spec}{$name}{type} ) { | |||||||||||||||||||||||||||||||||||||||||
| 271 | 29 | ▶ | /switch|counter|parameter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 19 | $self->{default}{$strip} = $merge{$strip}; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 19 | last; | |||||||||||||||||||||||||||||||||||||||||
| 274 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 10 | ▶ | /list/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | throw_usage("Option '$strip' in merge_defaults() must be scalar or array reference") | ||||||||||||||||||||||||||||||||||||||||||
| 277 | 5 | ▶ | if ref($merge{$strip}) && ref($merge{$strip}) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $merge{$strip} and ref $merge{$strip} ne "ARRAY"
MC/DC100%2/2
ref $merge{$strip} and ref $merge{$strip} ne "ARRAY"
ref $merge{$strip}
ref $merge{$strip} ne "ARRAY"
Truth table100%3/3
A and B
A= ref $merge{$strip}B= ref $merge{$strip} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | $merge{$strip} = ref($merge{$strip}) eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||||||||
| 279 | ? dclone( $merge{$strip} ) | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 4 | ▶ | : [ $merge{$strip} ] ; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 281 | 4 | $self->{default}{$strip} = $merge{$strip}; | |||||||||||||||||||||||||||||||||||||||||
| 282 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 283 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 284 | 5 | ▶ | /keypair/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 285 | throw_usage("Option '$strip' in merge_defaults() must be scalar or hash reference") | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 5 | ▶ | if ref($merge{$strip}) && ref($merge{$strip}) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $merge{$strip} and ref $merge{$strip} ne "HASH"
MC/DC50%1/2
ref $merge{$strip} and ref $merge{$strip} ne "HASH"
ref $merge{$strip}
ref $merge{$strip} ne "HASH"
Truth table66%2/3
A and B
A= ref $merge{$strip}B= ref $merge{$strip} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | 4 | $self->{default}{$strip} = dclone($merge{$strip}); | |||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | last; | |||||||||||||||||||||||||||||||||||||||||
| 289 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | throw_spec("Default '$spec->{canon}' = '$self->{default}{$strip}' fails to validate") | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 27 | ▶ | unless _validate_value($self, $self->{default}{$strip}, $spec->{valid} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | 4 | _recalculate_options($self); | |||||||||||||||||||||||||||||||||||||||||
| 295 | 4 | return $self->options; | |||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # names() | ||||||||||||||||||||||||||||||||||||||||||
| 300 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | sub names { | ||||||||||||||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | return values %{$self->{strip}}; | |||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 309 | # options() | ||||||||||||||||||||||||||||||||||||||||||
| 310 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | sub options { | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 122 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | 122 | return %{dclone($self->{options})}; | |||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 318 | # replace_defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 319 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | sub replace_defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace_defaults: called CC=18 SCAR=28.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | my %replace = | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 8 | ▶ | ref $_[0] eq 'HASH' ? %{+shift} : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | (@_ % 2 == 0) ? @_ : | ||||||||||||||||||||||||||||||||||||||||||
| 326 | throw_usage("Argument to replace_defaults() must be a hash or hash reference"); | ||||||||||||||||||||||||||||||||||||||||||
| 327 | 7 | for my $name ( keys %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 328 | 45 | my $spec = $self->{spec}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 329 | 45 | my $strip = $self->{strip}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 330 | 45 | for ( $self->{spec}{$name}{type} ) { | |||||||||||||||||||||||||||||||||||||||||
| 331 | 45 | ▶ | /switch|counter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 14 | ▶ | $self->{default}{$strip} = $replace{$strip} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$replace{$strip} || 0
MC/DC100%1/1
$replace{$strip} || 0
$replace{$strip}
Truth table100%2/2
A
A= $replace{$strip}
| |||||||||||||||||||||||||||||||||||||||||||
| 333 | 14 | last; | |||||||||||||||||||||||||||||||||||||||||
| 334 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 335 | 31 | ▶ | /parameter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 18 | $self->{default}{$strip} = $replace{$strip}; | |||||||||||||||||||||||||||||||||||||||||
| 337 | 18 | last; | |||||||||||||||||||||||||||||||||||||||||
| 338 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 339 | 13 | ▶ | /list/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 340 | throw_usage("Option '$strip' in replace_defaults() must be scalar or array reference") | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 6 | ▶ | if ref($replace{$strip}) && ref($replace{$strip}) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $replace{$strip} and ref $replace{$strip} ne "ARRAY"
MC/DC100%2/2
ref $replace{$strip} and ref $replace{$strip} ne "ARRAY"
ref $replace{$strip}
ref $replace{$strip} ne "ARRAY"
Truth table100%3/3
A and B
A= ref $replace{$strip}B= ref $replace{$strip} ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 5 | ▶ | if ( exists $replace{$strip} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | $replace{$strip} = ref($replace{$strip}) eq 'ARRAY' ? | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 4 | ▶ | $replace{$strip} : [ $replace{$strip} ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 1 | $replace{$strip} = []; | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | 5 | $self->{default}{$strip} = dclone($replace{$strip}); | |||||||||||||||||||||||||||||||||||||||||
| 349 | 5 | last; | |||||||||||||||||||||||||||||||||||||||||
| 350 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 7 | ▶ | /keypair/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 352 | throw_usage("Option '$strip' in replace_defaults() must be scalar or hash reference") | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 7 | ▶ | if ref($replace{$strip}) && ref($replace{$strip}) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $replace{$strip} and ref $replace{$strip} ne "HASH"
MC/DC100%2/2
ref $replace{$strip} and ref $replace{$strip} ne "HASH"
ref $replace{$strip}
ref $replace{$strip} ne "HASH"
Truth table100%3/3
A and B
A= ref $replace{$strip}B= ref $replace{$strip} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | 6 | ▶ | $replace{$strip} = {} unless exists $replace{$strip}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 6 | $self->{default}{$strip} = dclone($replace{$strip}); | |||||||||||||||||||||||||||||||||||||||||
| 356 | 6 | last; | |||||||||||||||||||||||||||||||||||||||||
| 357 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||
| 359 | throw_spec("Default '$spec->{canon}' = '$self->{default}{$strip}' fails to validate") | ||||||||||||||||||||||||||||||||||||||||||
| 360 | 43 | ▶ | unless _validate_value($self, $self->{default}{$strip}, $spec->{valid} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 4 | _recalculate_options($self); | |||||||||||||||||||||||||||||||||||||||||
| 363 | 4 | return $self->options; | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||
| 366 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 367 | # reset_defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 368 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | sub reset_defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_defaults: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | 6 | _set_defaults($self); | |||||||||||||||||||||||||||||||||||||||||
| 373 | 6 | _recalculate_options($self); | |||||||||||||||||||||||||||||||||||||||||
| 374 | 6 | return $self->options; | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 378 | # usage() | ||||||||||||||||||||||||||||||||||||||||||
| 379 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||
| 381 | sub usage { | ||||||||||||||||||||||||||||||||||||||||||
| 382 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | my @short_opts; | |||||||||||||||||||||||||||||||||||||||||
| 384 | my @doc; | ||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | for my $opt ( sort { $a->{strip} cmp $b->{strip} } values %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 386 | 7 | my $names = [ @{ $opt->{names} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 387 | push @doc, [ | ||||||||||||||||||||||||||||||||||||||||||
| 388 | _build_usage_left_column( $names, \@short_opts ), | ||||||||||||||||||||||||||||||||||||||||||
| 389 | 7 | _build_usage_right_column( $opt->{doc}, $opt->{default}, $opt->{type} ), | |||||||||||||||||||||||||||||||||||||||||
| 390 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | 1 | my $max_width = 3 + List::Util::max( map { length } @doc ); | |||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | 1 | my $prog = File::Basename::basename($0); | |||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | 1 | local $" = ''; | |||||||||||||||||||||||||||||||||||||||||
| 398 | my $usage = "Usage: $prog [-@short_opts] [long options] [arguments]\n" | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 1 | . join( "", map { sprintf( "\t%-${max_width}s %s\n", @$_ ) } @doc ); | |||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | sub _build_usage_left_column { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 7 | ▶ | my ($names, $all_short_opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_usage_left_column: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | my @sorted_names = | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 7 | sort { length $a <=> length $b } map { my $s = $_; $s =~ s/^-*//; $s } @$names; | |||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | 7 | my @short_opts = grep { length == 1 } @sorted_names; | |||||||||||||||||||||||||||||||||||||||||
| 408 | 7 | my @long_opts = grep { length > 1 } @sorted_names; | |||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 7 | push @$all_short_opts, @short_opts; | |||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | my $group = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 8 | ▶ | my $list = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 8 | ▶ | '-' . ( @$list == 1 ? $list->[0] : '[' . join( '|', @$list ) . ']' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 416 | my $prepare = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 417 | 14 | ▶ | my $list = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 418 | 14 | ▶ | return ( length $list->[0] > 1 ? '-' : '' ) . $group->($list) if @$list; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 6 | return; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | 7 | return join ', ' => map { $prepare->($_) } \@short_opts, \@long_opts; | |||||||||||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | sub _build_usage_right_column { | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 7 | ▶ | my ( $doc, $default, $type ) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_usage_right_column: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 427 | 7 | ▶ | my $str = defined $doc ? $doc : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 428 | 7 | ▶ | return $str unless defined $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 429 | 4 | ▶ | $str .= " " if length $str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | 4 | $str .= "(default: "; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 4 | ▶ | if ($type eq 'list') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | $str .= join( ", ", @$default ); | |||||||||||||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||||||||||||
| 434 | elsif ( $type eq 'keypair' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | $str .= join( ", ", map { "$_=$default->{$_}" } sort keys %$default ); | |||||||||||||||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||||||||||||||||
| 437 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 2 | $str .= $default | |||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||
| 440 | 4 | $str .= ')'; | |||||||||||||||||||||||||||||||||||||||||
| 441 | 4 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 445 | # _check_prereqs() | ||||||||||||||||||||||||||||||||||||||||||
| 446 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||
| 448 | sub _check_prereqs { | ||||||||||||||||||||||||||||||||||||||||||
| 449 | 81 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_prereqs: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 81 | for my $key ( keys %{$self->{seen}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 451 | 203 | ▶ | next unless $self->{seen}{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 452 | 105 | ▶ | next unless exists $self->{spec}{$key}{needs}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | 10 | for (@{$self->{spec}{$key}{needs}}) { | |||||||||||||||||||||||||||||||||||||||||
| 454 | throw_argv("Option '$self->{spec}{$key}{canon}' ". | ||||||||||||||||||||||||||||||||||||||||||
| 455 | "requires option '$self->{spec}{$_}{canon}'") | ||||||||||||||||||||||||||||||||||||||||||
| 456 | 12 | ▶ | unless $self->{seen}{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | } | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 462 | # _counter() | ||||||||||||||||||||||||||||||||||||||||||
| 463 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||
| 465 | sub _counter { | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 82 | ▶ | my ($self, $arg, $val, $neg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_counter: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 82 | ▶ | throw_argv("Counter option can't take a value: $self->{spec}{$arg}{canon}=$val") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 468 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 469 | 80 | push @{$self->{parsed}}, [ $arg, 1, $neg ]; | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 473 | # _find_arg() | ||||||||||||||||||||||||||||||||||||||||||
| 474 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | sub _find_arg { | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 344 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_arg: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 344 | ▶ | $arg =~ s/^-*// unless $self->{strict}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | 344 | ▶ | return $self->{alias_hr}{$arg} if exists $self->{alias_hr}{$arg}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | 66 | for ( keys %{$self->{alias_nocase}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 483 | 14 | ▶ | return $self->{alias_nocase}{$_} if $arg =~ /^$_$/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 62 | return; | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 490 | # _keypair() | ||||||||||||||||||||||||||||||||||||||||||
| 491 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||
| 493 | sub _keypair { | ||||||||||||||||||||||||||||||||||||||||||
| 494 | 24 | ▶ | my ($self, $arg, $val, $neg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_keypair: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 24 | my ($key, $data); | |||||||||||||||||||||||||||||||||||||||||
| 496 | 24 | ▶ | if ($neg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 4 | $key = $val; | |||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||
| 499 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 500 | 20 | ▶ | my $value = defined $val ? $val : shift @{$self->{target}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 20 | ▶ | if (! defined $val && ! defined $value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $val or defined $value
MC/DC100%2/2
defined $val or defined $value
defined $val
defined $value
Truth table100%3/3
A or B
A= defined $valB= defined $value
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 1 | throw_argv("Option '$self->{spec}{$arg}{canon}' requires a value"); | |||||||||||||||||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||||||||||
| 505 | 19 | ▶ | throw_argv("Badly formed keypair for '$self->{spec}{$arg}{canon}'") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 506 | unless $value =~ /[^=]+=.+/; | ||||||||||||||||||||||||||||||||||||||||||
| 507 | 16 | ($key, $data) = ( $value =~ /^([^=]*)=(.*)$/ ) ; | |||||||||||||||||||||||||||||||||||||||||
| 508 | throw_argv("Invalid keypair '$self->{spec}{$arg}{canon}': $key => $data") | ||||||||||||||||||||||||||||||||||||||||||
| 509 | unless _validate_value($self, { $key => $data }, | ||||||||||||||||||||||||||||||||||||||||||
| 510 | 16 | ▶ | $self->{spec}{$arg}{valid}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||
| 512 | 16 | push @{$self->{parsed}}, [ $arg, [ $key, $data ], $neg ]; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # _list() | ||||||||||||||||||||||||||||||||||||||||||
| 517 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | sub _list { | ||||||||||||||||||||||||||||||||||||||||||
| 520 | 20 | ▶ | my ($self, $arg, $val, $neg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_list: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 521 | 20 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 522 | 20 | ▶ | if ($neg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 4 | $value = $val; | |||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 16 | ▶ | $value = defined $val ? $val : shift @{$self->{target}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 527 | 16 | ▶ | if (! defined $val) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 528 | 13 | ▶ | if (! defined $value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 529 | 1 | throw_argv("Option '$self->{spec}{$arg}{canon}' requires a value"); | |||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 12 | $value =~ s/^$NEGATIVE(.*)$/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | 15 | ▶ | throw_argv("Ambiguous value for $self->{spec}{$arg}{canon} could be option: $value") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $val and _find_arg($self, $value)
MC/DC100%2/2
not defined $val and _find_arg($self, $value)
not defined $val
_find_arg($self, $value)
Truth table100%3/3
not(A) and B
A= not defined $valB= _find_arg($self, $value)
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | if ! defined $val and _find_arg($self, $value); | ||||||||||||||||||||||||||||||||||||||||||
| 536 | throw_argv("Invalid list option $self->{spec}{$arg}{canon} = $value") | ||||||||||||||||||||||||||||||||||||||||||
| 537 | 14 | ▶ | unless _validate_value($self, $value, $self->{spec}{$arg}{valid}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||
| 539 | 17 | push @{$self->{parsed}}, [ $arg, $value, $neg ]; | |||||||||||||||||||||||||||||||||||||||||
| 540 | } | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 543 | # _parameter() | ||||||||||||||||||||||||||||||||||||||||||
| 544 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | sub _parameter { | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 52 | ▶ | my ($self, $arg, $val, $neg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parameter: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 52 | my $value; | |||||||||||||||||||||||||||||||||||||||||
| 549 | 52 | ▶ | if ($neg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 550 | 6 | ▶ | throw_argv("Negated parameter option can't take a value: $self->{spec}{$arg}{canon}=$val") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 552 | } | ||||||||||||||||||||||||||||||||||||||||||
| 553 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 554 | 46 | ▶ | $value = defined $val ? $val : shift @{$self->{target}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 555 | 46 | ▶ | if (! defined $val) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 556 | 39 | ▶ | if (! defined $value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 2 | throw_argv("Option '$self->{spec}{$arg}{canon}' requires a value"); | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | 37 | $value =~ s/^$NEGATIVE(.*)$/$1/; | |||||||||||||||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||||||||||||||||
| 561 | 44 | ▶ | throw_argv("Ambiguous value for $self->{spec}{$arg}{canon} could be option: $value") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $val and _find_arg($self, $value)
MC/DC100%2/2
not defined $val and _find_arg($self, $value)
not defined $val
_find_arg($self, $value)
Truth table100%3/3
not(A) and B
A= not defined $valB= _find_arg($self, $value)
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | if ! defined $val and _find_arg($self, $value); | ||||||||||||||||||||||||||||||||||||||||||
| 563 | throw_argv("Invalid parameter $self->{spec}{$arg}{canon} = $value") | ||||||||||||||||||||||||||||||||||||||||||
| 564 | 41 | ▶ | unless _validate_value($self, $value, $self->{spec}{$arg}{valid}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||||||||||||||||
| 566 | 42 | push @{$self->{parsed}}, [ $arg, $value, $neg ]; | |||||||||||||||||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||
| 569 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 570 | # _parse_spec() | ||||||||||||||||||||||||||||||||||||||||||
| 571 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | sub _parse_spec { | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 189 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_spec: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 575 | 189 | my $spec = $self->{raw_spec}; | |||||||||||||||||||||||||||||||||||||||||
| 576 | 189 | for my $v ( @$spec ) { | |||||||||||||||||||||||||||||||||||||||||
| 577 | 541 | my $type = ref $v; | |||||||||||||||||||||||||||||||||||||||||
| 578 | 541 | ▶ | throw_spec( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | "'$type' is not a valid option type" | ||||||||||||||||||||||||||||||||||||||||||
| 580 | ) unless $type eq 'Getopt::Lucid::Spec'; | ||||||||||||||||||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||||||||||||||||
| 582 | 189 | for my $opt ( map { $_->_clone } @$spec ) { | |||||||||||||||||||||||||||||||||||||||||
| 583 | 541 | my $name = $opt->{name}; | |||||||||||||||||||||||||||||||||||||||||
| 584 | 541 | my @names = split( /\|/, $name ); | |||||||||||||||||||||||||||||||||||||||||
| 585 | 541 | $opt->{canon} = $names[0]; | |||||||||||||||||||||||||||||||||||||||||
| 586 | 541 | _validate_spec($self,\@names,$opt); | |||||||||||||||||||||||||||||||||||||||||
| 587 | 533 | $opt->{names} = \@names; | |||||||||||||||||||||||||||||||||||||||||
| 588 | 533 | ($opt->{strip} = $names[0]) =~ s/^-+//; | |||||||||||||||||||||||||||||||||||||||||
| 589 | 533 | ▶ | @names = map { s/^-*//; $_ } @names unless $self->{strict}; ## no critic | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 590 | 533 | for (@names) { | |||||||||||||||||||||||||||||||||||||||||
| 591 | 848 | $self->{alias_hr}{$_} = $names[0]; | |||||||||||||||||||||||||||||||||||||||||
| 592 | 848 | ▶ | $self->{alias_nocase}{$_} = $names[0] if $opt->{nocase}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | 533 | $self->{spec}{$names[0]} = $opt; | |||||||||||||||||||||||||||||||||||||||||
| 595 | 533 | $self->{strip}{$names[0]} = $opt->{strip}; | |||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | 181 | _validate_prereqs($self); | |||||||||||||||||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||||||||||||||||
| 600 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 601 | # _recalculate_options() | ||||||||||||||||||||||||||||||||||||||||||
| 602 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | sub _recalculate_options { | ||||||||||||||||||||||||||||||||||||||||||
| 605 | 121 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recalculate_options: called CC=18 SCAR=28.9
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 121 | my %result; | |||||||||||||||||||||||||||||||||||||||||
| 607 | 121 | for my $k ( keys %{$self->{default}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 608 | 463 | my $d = $self->{default}{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 609 | 463 | ▶ | $result{$k} = ref($d) eq 'ARRAY' ? [ @$d ] : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 610 | ref($d) eq 'HASH' ? { %$d } : $d; | ||||||||||||||||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 121 | for my $opt ( @{$self->{parsed}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 613 | 274 | my ($name, $value, $neg) = @$opt; | |||||||||||||||||||||||||||||||||||||||||
| 614 | 274 | for ($self->{spec}{$name}{type}) { | |||||||||||||||||||||||||||||||||||||||||
| 615 | 274 | my $strip = $self->{strip}{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 616 | 274 | ▶ | /switch/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 617 | 59 | ▶ | $result{$strip} = $neg ? 0 : $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 618 | 59 | last; | |||||||||||||||||||||||||||||||||||||||||
| 619 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 620 | 215 | ▶ | /counter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 102 | ▶ | $result{$strip} = $neg ? 0 : $result{$strip} + $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 622 | 102 | last; | |||||||||||||||||||||||||||||||||||||||||
| 623 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 624 | 113 | ▶ | /parameter/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 625 | 42 | ▶ | $result{$strip} = $neg ? "" : $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 626 | 42 | last; | |||||||||||||||||||||||||||||||||||||||||
| 627 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 628 | 71 | ▶ | /list/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 629 | 40 | ▶ | if ($neg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 630 | $result{$strip} = $value ? | ||||||||||||||||||||||||||||||||||||||||||
| 631 | 4 | ▶ | [ grep { $_ ne $value } @{$result{$strip}} ] : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 632 | []; | ||||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 36 | else { push @{$result{$strip}}, $value } | |||||||||||||||||||||||||||||||||||||||||
| 635 | 40 | last; | |||||||||||||||||||||||||||||||||||||||||
| 636 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 637 | 31 | ▶ | /keypair/ && do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | 31 | ▶ | if ($neg) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | 4 | ▶ | if ($value->[0]) { delete $result{$strip}{$value->[0]} } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 640 | 2 | else { $result{$strip} = {} } | |||||||||||||||||||||||||||||||||||||||||
| 641 | } | ||||||||||||||||||||||||||||||||||||||||||
| 642 | 27 | else { $result{$strip}{$value->[0]} = $value->[1]}; | |||||||||||||||||||||||||||||||||||||||||
| 643 | 31 | last; | |||||||||||||||||||||||||||||||||||||||||
| 644 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||
| 647 | 121 | return $self->{options} = \%result; | |||||||||||||||||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 651 | # _regex_or_code | ||||||||||||||||||||||||||||||||||||||||||
| 652 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||
| 654 | sub _regex_or_code { | ||||||||||||||||||||||||||||||||||||||||||
| 655 | 174 | ▶ | my ($value,$valid) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_regex_or_code: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 656 | 174 | ▶ | return 1 unless defined $valid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | 171 | ▶ | if ( ref($valid) eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 658 | 10 | local $_ = $value; | |||||||||||||||||||||||||||||||||||||||||
| 659 | 10 | return $valid->($value); | |||||||||||||||||||||||||||||||||||||||||
| 660 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 661 | 161 | return $value =~ /^$valid$/; | |||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 666 | # _set_defaults() | ||||||||||||||||||||||||||||||||||||||||||
| 667 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||
| 669 | sub _set_defaults { | ||||||||||||||||||||||||||||||||||||||||||
| 670 | 186 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_defaults: called CC=20 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 671 | 186 | my %default; | |||||||||||||||||||||||||||||||||||||||||
| 672 | 186 | for my $k ( keys %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 673 | 588 | my $spec = $self->{spec}{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 674 | 588 | ▶ | my $d = exists ($spec->{default}) ? $spec->{default} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 675 | 588 | my $type = $self->{spec}{$k}{type}; | |||||||||||||||||||||||||||||||||||||||||
| 676 | 588 | my $strip = $self->{strip}{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 588 | ▶ | throw_spec("Default for list '$spec->{canon}' must be array reference") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$type eq "list" and defined $d and ref $d ne "ARRAY"
$type eq "list" and defined $d
MC/DC0%0/3
$type eq "list" and defined $d and ref $d ne "ARRAY"
$type eq "list"
defined $d
ref $d ne "ARRAY"
Truth table75%3/4
A and B and C
A= $type eq "list"B= defined $dC= ref $d ne "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 678 | if ( $type eq "list" && defined $d && ref($d) ne "ARRAY" ); | ||||||||||||||||||||||||||||||||||||||||||
| 679 | 588 | ▶ | throw_spec("Default for keypair '$spec->{canon}' must be hash reference") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$type eq "keypair" and defined $d and ref $d ne "HASH"
$type eq "keypair" and defined $d
MC/DC100%3/3
$type eq "keypair" and defined $d and ref $d ne "HASH"
$type eq "keypair"
defined $d
ref $d ne "HASH"
Truth table100%4/4
A and B and C
A= $type eq "keypair"B= defined $dC= ref $d ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 680 | if ( $type eq "keypair" && defined $d && ref($d) ne "HASH" ); | ||||||||||||||||||||||||||||||||||||||||||
| 681 | 587 | ▶ | if (defined $d) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | throw_spec("Default '$spec->{canon}' = '$d' fails to validate") | ||||||||||||||||||||||||||||||||||||||||||
| 683 | 221 | ▶ | unless _validate_value($self, $d, $spec->{valid}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||||||||||||||||
| 685 | 586 | $default{$strip} = do { | |||||||||||||||||||||||||||||||||||||||||
| 686 | 586 | local $_ = $type; | |||||||||||||||||||||||||||||||||||||||||
| 687 | 586 | ▶ | /switch/ ? (defined $d ? $d: 0) : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 9 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 688 | /counter/ ? (defined $d ? $d: 0) : | ||||||||||||||||||||||||||||||||||||||||||
| 689 | /parameter/ ? $d : | ||||||||||||||||||||||||||||||||||||||||||
| 690 | /list/ ? (defined $d ? dclone($d): []) : | ||||||||||||||||||||||||||||||||||||||||||
| 691 | /keypair/ ? (defined $d ? dclone($d): {}) : | ||||||||||||||||||||||||||||||||||||||||||
| 692 | undef; | ||||||||||||||||||||||||||||||||||||||||||
| 693 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | 184 | $self->{default} = \%default; | |||||||||||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||
| 698 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 699 | # _split_equals() | ||||||||||||||||||||||||||||||||||||||||||
| 700 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||
| 702 | sub _split_equals { | ||||||||||||||||||||||||||||||||||||||||||
| 703 | 272 | ▶ | my ($self,$raw) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_equals: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 704 | 272 | my ($arg,$val); | |||||||||||||||||||||||||||||||||||||||||
| 705 | 272 | ▶ | if ( $raw =~ /^($NEGATIVE?$VALID_NAME|$SHORT_BUNDLE)=(.*)/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 706 | 27 | $arg = $1; | |||||||||||||||||||||||||||||||||||||||||
| 707 | 27 | $val = $2; | |||||||||||||||||||||||||||||||||||||||||
| 708 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 709 | 245 | $arg = $raw; | |||||||||||||||||||||||||||||||||||||||||
| 710 | } | ||||||||||||||||||||||||||||||||||||||||||
| 711 | 272 | return ($arg, $val); | |||||||||||||||||||||||||||||||||||||||||
| 712 | } | ||||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 715 | # _switch() | ||||||||||||||||||||||||||||||||||||||||||
| 716 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||||||||||
| 718 | sub _switch { | ||||||||||||||||||||||||||||||||||||||||||
| 719 | 54 | ▶ | my ($self, $arg, $val, $neg) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_switch: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 720 | 54 | ▶ | throw_argv("Switch can't take a value: $self->{spec}{$arg}{canon}=$val") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 721 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 722 | 52 | ▶ | if (! $neg ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 723 | throw_argv("Switch used twice: $self->{spec}{$arg}{canon}") | ||||||||||||||||||||||||||||||||||||||||||
| 724 | 48 | ▶ | if $self->{seen}{$arg} > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 725 | } | ||||||||||||||||||||||||||||||||||||||||||
| 726 | 51 | push @{$self->{parsed}}, [ $arg, 1, $neg ]; | |||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||
| 728 | |||||||||||||||||||||||||||||||||||||||||||
| 729 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 730 | # _unbundle() | ||||||||||||||||||||||||||||||||||||||||||
| 731 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||||||||||||
| 733 | sub _unbundle { | ||||||||||||||||||||||||||||||||||||||||||
| 734 | 272 | ▶ | my ($self,$arg, $val) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_unbundle: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 735 | 272 | ▶ | if ( $arg =~ /^$SHORT_BUNDLE$/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | 24 | my @flags = split(//,substr($arg,1)); | |||||||||||||||||||||||||||||||||||||||||
| 737 | 24 | ▶ | unshift @{$self->{target}}, ("-" . pop(@flags) . "=" . $val) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 738 | if defined $val; | ||||||||||||||||||||||||||||||||||||||||||
| 739 | 24 | for ( reverse @flags ) { | |||||||||||||||||||||||||||||||||||||||||
| 740 | 61 | unshift @{$self->{target}}, "-$_"; | |||||||||||||||||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||||||||||||||||
| 742 | 24 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 743 | } | ||||||||||||||||||||||||||||||||||||||||||
| 744 | 248 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||
| 747 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 748 | # _validate_prereqs() | ||||||||||||||||||||||||||||||||||||||||||
| 749 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||||||||||
| 751 | sub _validate_prereqs { | ||||||||||||||||||||||||||||||||||||||||||
| 752 | 181 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_prereqs: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 753 | 181 | for my $key ( keys %{$self->{spec}} ) { | |||||||||||||||||||||||||||||||||||||||||
| 754 | 530 | ▶ | next unless exists $self->{spec}{$key}{needs}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 755 | 18 | my $needs = $self->{spec}{$key}{needs}; | |||||||||||||||||||||||||||||||||||||||||
| 756 | 18 | ▶ | my @prereq = ref($needs) eq 'ARRAY' ? @$needs : ( $needs ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 757 | 18 | for (@prereq) { | |||||||||||||||||||||||||||||||||||||||||
| 758 | 24 | ▶ | throw_spec("Prerequisite '$_' for '$self->{spec}{$key}{canon}' is not recognized") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | unless _find_arg($self,$_); | ||||||||||||||||||||||||||||||||||||||||||
| 760 | 23 | $_ = _find_arg($self,$_); | |||||||||||||||||||||||||||||||||||||||||
| 761 | } | ||||||||||||||||||||||||||||||||||||||||||
| 762 | 17 | $self->{spec}{$key}{needs} = \@prereq; | |||||||||||||||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||
| 767 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 768 | # _validate_spec() | ||||||||||||||||||||||||||||||||||||||||||
| 769 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | sub _validate_spec { | ||||||||||||||||||||||||||||||||||||||||||
| 772 | 541 | ▶ | my ($self,$names,$details) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_spec: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 773 | 541 | for my $name ( @$names ) { | |||||||||||||||||||||||||||||||||||||||||
| 774 | 857 | my $alt_name = $name; | |||||||||||||||||||||||||||||||||||||||||
| 775 | 857 | ▶ | $alt_name =~ s/^-*// unless $self->{strict}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 776 | 857 | ▶ | throw_spec( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 777 | "'$name' is not a valid option name/alias" | ||||||||||||||||||||||||||||||||||||||||||
| 778 | ) unless $name =~ /^$VALID_NAME$/; | ||||||||||||||||||||||||||||||||||||||||||
| 779 | throw_spec( | ||||||||||||||||||||||||||||||||||||||||||
| 780 | "'$name' is not unique" | ||||||||||||||||||||||||||||||||||||||||||
| 781 | 855 | ▶ | ) if exists $self->{alias_hr}{$alt_name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 782 | 852 | my $strip; | |||||||||||||||||||||||||||||||||||||||||
| 783 | 852 | ($strip = $name) =~ s/^-+//; | |||||||||||||||||||||||||||||||||||||||||
| 784 | throw_spec( | ||||||||||||||||||||||||||||||||||||||||||
| 785 | "'$strip' conflicts with other options" | ||||||||||||||||||||||||||||||||||||||||||
| 786 | 852 | ▶ | ) if grep { $strip eq $_ } values %{$self->{strip}}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 787 | } | ||||||||||||||||||||||||||||||||||||||||||
| 788 | 536 | for my $key ( keys %$details ) { | |||||||||||||||||||||||||||||||||||||||||
| 789 | throw_spec( | ||||||||||||||||||||||||||||||||||||||||||
| 790 | "'$key' is not a valid option specification key" | ||||||||||||||||||||||||||||||||||||||||||
| 791 | 1910 | ▶ | ) unless grep { $key eq $_ } @valid_keys; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 792 | } | ||||||||||||||||||||||||||||||||||||||||||
| 793 | 535 | my $type = $details->{type}; | |||||||||||||||||||||||||||||||||||||||||
| 794 | throw_spec( | ||||||||||||||||||||||||||||||||||||||||||
| 795 | "'$type' is not a valid option type" | ||||||||||||||||||||||||||||||||||||||||||
| 796 | 535 | ▶ | ) unless grep { $type eq $_ } @valid_types; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 797 | } | ||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||
| 799 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 800 | # _validate_value() | ||||||||||||||||||||||||||||||||||||||||||
| 801 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||
| 803 | sub _validate_value { | ||||||||||||||||||||||||||||||||||||||||||
| 804 | 389 | ▶ | my ($self, $value, $valid) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_value: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 805 | 389 | ▶ | return 1 unless defined $valid; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 806 | 102 | ▶ | if ( ref($value) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 807 | 30 | my $valid_key = $valid->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 808 | 30 | my $valid_val = $valid->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 809 | 30 | ▶ | while (my ($k,$v) = each %$value) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 810 | 44 | ▶ | _regex_or_code($k, $valid_key) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 811 | 42 | ▶ | _regex_or_code($v, $valid_val) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||||||||||||||||
| 813 | 26 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 814 | } elsif ( ref($value) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 815 | 20 | for (@$value) { | |||||||||||||||||||||||||||||||||||||||||
| 816 | 36 | ▶ | _regex_or_code($_, $valid) or return 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 817 | } | ||||||||||||||||||||||||||||||||||||||||||
| 818 | 20 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 819 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 820 | 52 | return _regex_or_code($value, $valid); | |||||||||||||||||||||||||||||||||||||||||
| 821 | } | ||||||||||||||||||||||||||||||||||||||||||
| 822 | } | ||||||||||||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||||||||||
| 824 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 825 | # AUTOLOAD() | ||||||||||||||||||||||||||||||||||||||||||
| 826 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||||||||||
| 828 | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||||
| 829 | 205 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 830 | 205 | my $name = $Getopt::Lucid::AUTOLOAD; | |||||||||||||||||||||||||||||||||||||||||
| 831 | 205 | $name =~ s/.*:://; # strip fully-qualified portion | |||||||||||||||||||||||||||||||||||||||||
| 832 | 205 | ▶ | return if $name eq "DESTROY"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 833 | 15 | my ($action, $maybe_opt) = $name =~ /^(get|set)_(.+)/ ; | |||||||||||||||||||||||||||||||||||||||||
| 834 | 15 | ▶ | if ($action) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 835 | # look for a match | ||||||||||||||||||||||||||||||||||||||||||
| 836 | 15 | my $opt; | |||||||||||||||||||||||||||||||||||||||||
| 837 | SEARCH: | ||||||||||||||||||||||||||||||||||||||||||
| 838 | 15 | for my $known_opt ( values %{ $self->{strip} } ) { | |||||||||||||||||||||||||||||||||||||||||
| 839 | 45 | ▶ | if ( $maybe_opt eq $known_opt ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 840 | 12 | $opt = $known_opt; | |||||||||||||||||||||||||||||||||||||||||
| 841 | 12 | last SEARCH; | |||||||||||||||||||||||||||||||||||||||||
| 842 | } | ||||||||||||||||||||||||||||||||||||||||||
| 843 | # try without dashes | ||||||||||||||||||||||||||||||||||||||||||
| 844 | 33 | (my $fuzzy_opt = $known_opt) =~ s/-/_/g; | |||||||||||||||||||||||||||||||||||||||||
| 845 | 33 | ▶ | if ( $maybe_opt eq $fuzzy_opt ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 846 | 3 | $opt = $known_opt; | |||||||||||||||||||||||||||||||||||||||||
| 847 | 3 | last SEARCH; | |||||||||||||||||||||||||||||||||||||||||
| 848 | } | ||||||||||||||||||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||
| 851 | # throw if no valid option was found | ||||||||||||||||||||||||||||||||||||||||||
| 852 | 15 | ▶ | throw_usage("Can't $action unknown option '$maybe_opt'") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 853 | if ! $opt; | ||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||
| 855 | # handle the accessor if an option was found | ||||||||||||||||||||||||||||||||||||||||||
| 856 | 15 | ▶ | if ($action eq "set") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 857 | $self->{options}{$opt} = | ||||||||||||||||||||||||||||||||||||||||||
| 858 | ref($self->{options}{$opt}) eq 'ARRAY' ? [@_] : | ||||||||||||||||||||||||||||||||||||||||||
| 859 | 5 | ▶ | ref($self->{options}{$opt}) eq 'HASH' ? {@_} : shift; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||
| 861 | } | ||||||||||||||||||||||||||||||||||||||||||
| 862 | 15 | my $ans = $self->{options}{$opt}; | |||||||||||||||||||||||||||||||||||||||||
| 863 | 15 | ▶ | return ref($ans) eq 'ARRAY' ? @$ans : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 864 | ref($ans) eq 'HASH' ? %$ans : $ans; | ||||||||||||||||||||||||||||||||||||||||||
| 865 | } | ||||||||||||||||||||||||||||||||||||||||||
| 866 | 0 | my $super = "SUPER::$name"; | |||||||||||||||||||||||||||||||||||||||||
| 867 | 0 | $self->$super(@_); | |||||||||||||||||||||||||||||||||||||||||
| 868 | } | ||||||||||||||||||||||||||||||||||||||||||
| 869 | |||||||||||||||||||||||||||||||||||||||||||
| 870 | 12 | 1; # modules must be true | |||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||
| 872 | __END__ | ||||||||||||||||||||||||||||||||||||||||||