Lucid.pm

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