Lucid.pm

filter: Statementstmt 99.6% 498 / 500 Branchbran 93.8% 306 / 326 Conditioncond 84.0% 42 / 50 MC/DCmcdc 67.7% 21 / 31 Subroutinesub 100% 53 / 53 Podpod 100% 16 / 16 totaltotal 95.9% 936 / 976 SCAR 53.5
CC
209
Cov
97%
_set_defaults
20.1
replace_defaults
18.0
_recalculate_options
18.0
CRAP 210.8
↑ Summary Lucid/Exception.pm »
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
BranchTrueFalseTotal
if @_ 7 29 36
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
BranchTrueFalseTotal
if @_ 1 9 10
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
BranchTrueFalseTotal
if scalar @_ 4 9 13
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
BranchTrueFalseTotal
unless grep {$self->{"type"} eq $_;} "parameter", "list", "keypair" 0 2 2
66 2 ▶ $self->{valid} = $self->{type} eq 'keypair' ? [ @_ ] : shift;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'type'} eq 'keypair' ? : 0 2 2
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
BranchTrueFalseTotal
if ($self->{'type'} eq 'keypair') { } 6 31 37
elsif ($self->{'type'} eq 'list') { } 5 26 31
74 6 ▶ if (ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 4 6
elsif (@_ % 2 == 0) { } 3 1 4
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
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 8 182 190
112 190 ▶ $args->{target} = ref($target) eq 'ARRAY' ? $target : \@ARGV;
Branch: 1 branch
BranchTrueFalseTotal
ref $target eq 'ARRAY' ? : 48 142 190
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
BranchTrueFalseTotal
ref $class ? : 0 190 190
117 throw_usage("Getopt::Lucid->new() requires an option specification array reference")
118 190 ▶ unless ref($self->{raw_spec}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"raw_spec"} eq "ARRAY" 1 189 190
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
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 4 8
@_ % 2 == 0 ? : 3 1 4
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
BranchTrueFalseTotal
unless exists $append{$strip} 16 29 45
141 29 for ( $spec->{type} ) {
142 29 ▶ /switch|parameter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/switch|parameter/ and do { $self->{"default"}{$strip} = $append{$strip}; last } 15 14 29
143 15 $self->{default}{$strip} = $append{$strip};
144 15 last;
145 };
146 14 ▶ /counter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/counter/ and do { $self->{"default"}{$strip} += $append{$strip}; last } 4 10 14
147 4 $self->{default}{$strip} += $append{$strip};
148 4 last;
149 };
150 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
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
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
153 $append{$strip} = ref($append{$strip}) eq 'ARRAY'
154 ? dclone( $append{$strip} )
155 4 ▶ : [ $append{$strip} ] ;
Branch: 1 branch
BranchTrueFalseTotal
ref $append{$strip} eq 'ARRAY' ? : 2 2 4
156 4 push @{$self->{default}{$strip}}, @{$append{$strip}};
157 4 last;
158 };
159 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
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
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
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
BranchTrueFalseTotal
unless _validate_value($self, $self->{"default"}{$strip}, $spec->{"valid"}) 1 26 27
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
BranchTrueFalseTotal
if ($self eq "Getopt::Lucid") 2 139 141
193 2 ▶ throw_usage("Getopt::Lucid->getopt() requires an option specification array reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $spec eq "ARRAY" 1 1 2
194 unless ref($spec) eq 'ARRAY';
195 1 $self = new(@_)
196 }
197 140 my (@passthrough);
198 140 ▶ while (@{$self->{target}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"target"};}) 287 88 375
199 287 my $raw = shift @{$self->{target}};
200 287 ▶ last if $raw =~ /^--$/;
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /^--$/ 15 272 287
201 272 my ($orig, $val) = _split_equals($self, $raw);
202 272 ▶ next if _unbundle($self, $orig, $val);
Branch: 1 branch
BranchTrueFalseTotal
if _unbundle($self, $orig, $val) 24 248 272
203 248 ▶ my $neg = $orig =~ s/^$NEGATIVE(.*)$/$1/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$orig =~ s/^$NEGATIVE(.*)$/$1/ ? : 23 225 248
204 248 my $arg = _find_arg($self, $orig);
205 248 ▶ if ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) { } 232 16 248
206 $neg ?
207 $self->{seen}{$arg} = 0 :
208 232 ▶ $self->{seen}{$arg}++;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 23 209 232
209 232 for ($self->{spec}{$arg}{type}) {
210 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
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
BranchTrueFalseTotal
if $orig =~ /^-./ 11 5 16
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
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
237 if $arg && ref($arg) ne 'HASH';
238  
239 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
240 7 my $requires = $arg->{requires};
241 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
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
BranchTrueFalseTotal
unless exists $self->{"spec"}{$p} 0 7 7
246 throw_argv( "Required option '$self->{spec}{$p}{canon}' not found")
247 7 ▶ if ( !$self->{seen}{$p} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"seen"}{$p} 2 5 7
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
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 4 8
@_ % 2 == 0 ? : 3 1 4
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
BranchTrueFalseTotal
unless exists $merge{$strip} 15 29 44
270 29 for ( $self->{spec}{$name}{type} ) {
271 29 ▶ /switch|counter|parameter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/switch|counter|parameter/ and do { $self->{"default"}{$strip} = $merge{$strip}; last } 19 10 29
272 19 $self->{default}{$strip} = $merge{$strip};
273 19 last;
274 };
275 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
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
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
278 $merge{$strip} = ref($merge{$strip}) eq 'ARRAY'
279 ? dclone( $merge{$strip} )
280 4 ▶ : [ $merge{$strip} ] ;
Branch: 1 branch
BranchTrueFalseTotal
ref $merge{$strip} eq 'ARRAY' ? : 2 2 4
281 4 $self->{default}{$strip} = $merge{$strip};
282 4 last;
283 };
284 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
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
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
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
BranchTrueFalseTotal
unless _validate_value($self, $self->{"default"}{$strip}, $spec->{"valid"}) 1 26 27
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
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 4 4 8
@_ % 2 == 0 ? : 3 1 4
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
BranchTrueFalseTotal
/switch|counter/ and do { $self->{"default"}{$strip} = $replace{$strip} || 0; last } 14 31 45
332 14 ▶ $self->{default}{$strip} = $replace{$strip} || 0;
Condition100%2/2
$replace{$strip} || 0
l!l
68
MC/DC100%1/1
$replace{$strip} || 0
$replace{$strip}
Truth table100%2/2
A
A= $replace{$strip}
Aresult
11
00
333 14 last;
334 };
335 31 ▶ /parameter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/parameter/ and do { $self->{"default"}{$strip} = $replace{$strip}; last } 18 13 31
336 18 $self->{default}{$strip} = $replace{$strip};
337 18 last;
338 };
339 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
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
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
342 5 ▶ if ( exists $replace{$strip} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $replace{$strip}) { } 4 1 5
343 $replace{$strip} = ref($replace{$strip}) eq 'ARRAY' ?
344 4 ▶ $replace{$strip} : [ $replace{$strip} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $replace{$strip} eq 'ARRAY' ? : 2 2 4
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
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
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
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
354 6 ▶ $replace{$strip} = {} unless exists $replace{$strip};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $replace{$strip} 2 4 6
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
BranchTrueFalseTotal
unless _validate_value($self, $self->{"default"}{$strip}, $spec->{"valid"}) 1 42 43
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
BranchTrueFalseTotal
@$list == 1 ? : 8 0 8
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
BranchTrueFalseTotal
if @$list 8 6 14
length $list->[0] > 1 ? : 6 2 8
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
BranchTrueFalseTotal
defined $doc ? : 3 4 7
428 7 ▶ return $str unless defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default 3 4 7
429 4 ▶ $str .= " " if length $str;
Branch: 1 branch
BranchTrueFalseTotal
if length $str 1 3 4
430 4 $str .= "(default: ";
431 4 ▶ if ($type eq 'list') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'list') { } 1 3 4
elsif ($type eq 'keypair') { } 1 2 3
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
BranchTrueFalseTotal
unless $self->{"seen"}{$key} 98 105 203
452 105 ▶ next unless exists $self->{spec}{$key}{needs};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"spec"}{$key}{"needs"} 95 10 105
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
BranchTrueFalseTotal
unless $self->{"seen"}{$_} 5 7 12
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
BranchTrueFalseTotal
if defined $val 2 80 82
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
BranchTrueFalseTotal
unless $self->{"strict"} 314 30 344
480 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
481  
482 66 for ( keys %{$self->{alias_nocase}} ) {
483 14 ▶ return $self->{alias_nocase}{$_} if $arg =~ /^$_$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $arg =~ /^$_$/i 4 10 14
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
BranchTrueFalseTotal
if ($neg) { } 4 20 24
497 4 $key = $val;
498 }
499 else {
500 20 ▶ my $value = defined $val ? $val : shift @{$self->{target}};
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 3 17 20
501 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
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
BranchTrueFalseTotal
unless $value =~ /[^=]+=.+/ 3 16 19
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
BranchTrueFalseTotal
unless _validate_value($self, {$key, $data}, $self->{"spec"}{$arg}{"valid"}) 4 12 16
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
BranchTrueFalseTotal
if ($neg) { } 4 16 20
523 4 $value = $val;
524 }
525 else {
526 16 ▶ $value = defined $val ? $val : shift @{$self->{target}};
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 3 13 16
527 16 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 13 3 16
528 13 ▶ if (! defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 1 12 13
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
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
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
BranchTrueFalseTotal
unless _validate_value($self, $value, $self->{"spec"}{$arg}{"valid"}) 1 13 14
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
BranchTrueFalseTotal
if ($neg) { } 6 46 52
550 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
551 if defined $val;
552 }
553 else {
554 46 ▶ $value = defined $val ? $val : shift @{$self->{target}};
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 7 39 46
555 46 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 39 7 46
556 39 ▶ if (! defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 37 39
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
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
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
BranchTrueFalseTotal
unless _validate_value($self, $value, $self->{"spec"}{$arg}{"valid"}) 4 37 41
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
BranchTrueFalseTotal
unless $type eq "Getopt::Lucid::Spec" 0 541 541
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
BranchTrueFalseTotal
unless $self->{"strict"} 501 32 533
590 533 for (@names) {
591 848 $self->{alias_hr}{$_} = $names[0];
592 848 ▶ $self->{alias_nocase}{$_} = $names[0] if $opt->{nocase};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"nocase"} 16 832 848
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
BranchTrueFalseTotal
ref $d eq 'ARRAY' ? : 48 415 463
ref $d eq 'HASH' ? : 49 366 415
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
BranchTrueFalseTotal
/switch/ and do { $result{$strip} = $neg ? 0 : $value; last } 59 215 274
617 59 ▶ $result{$strip} = $neg ? 0 : $value;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 4 55 59
618 59 last;
619 };
620 215 ▶ /counter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/counter/ and do { $result{$strip} = $neg ? 0 : $result{$strip} + $value; last } 102 113 215
621 102 ▶ $result{$strip} = $neg ? 0 : $result{$strip} + $value;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 3 99 102
622 102 last;
623 };
624 113 ▶ /parameter/ && do {
Branch: 1 branch
BranchTrueFalseTotal
/parameter/ and do { $result{$strip} = $neg ? "" : $value; last } 42 71 113
625 42 ▶ $result{$strip} = $neg ? "" : $value;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 5 37 42
626 42 last;
627 };
628 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
629 40 ▶ if ($neg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neg) { } 4 36 40
630 $result{$strip} = $value ?
631 4 ▶ [ grep { $_ ne $value } @{$result{$strip}} ] :
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 2 4
632 [];
633 }
634 36 else { push @{$result{$strip}}, $value }
635 40 last;
636 };
637 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
638 31 ▶ if ($neg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neg) { } 4 27 31
639 4 ▶ if ($value->[0]) { delete $result{$strip}{$value->[0]} }
Branch: 1 branch
BranchTrueFalseTotal
if ($value->[0]) { } 2 2 4
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
BranchTrueFalseTotal
unless defined $valid 3 171 174
657 171 ▶ if ( ref($valid) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid eq 'CODE') { } 10 161 171
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
BranchTrueFalseTotal
exists $spec->{'default'} ? : 236 352 588
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
BranchTrueFalseTotal
if $type eq "list" and defined $d and ref $d ne "ARRAY" 0 588 588
Condition83%5/6
$type eq "list" and defined $d and ref $d ne "ARRAY"
!ll&&!rl&&r
555330
$type eq "list" and defined $d
!ll&&!rl&&r
5332233
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
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
BranchTrueFalseTotal
if $type eq "keypair" and defined $d and ref $d ne "HASH" 1 587 588
Condition100%6/6
$type eq "keypair" and defined $d and ref $d ne "HASH"
!ll&&!rl&&r
554331
$type eq "keypair" and defined $d
!ll&&!rl&&r
5252934
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
680 if ( $type eq "keypair" && defined $d && ref($d) ne "HASH" );
681 587 ▶ if (defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) 221 366 587
682 throw_spec("Default '$spec->{canon}' = '$d' fails to validate")
683 221 ▶ unless _validate_value($self, $d, $spec->{valid});
Branch: 1 branch
BranchTrueFalseTotal
unless _validate_value($self, $d, $spec->{"valid"}) 1 220 221
684 }
685 586 $default{$strip} = do {
686 586 local $_ = $type;
687 586 ▶ /switch/ ? (defined $d ? $d: 0) :
Branch: 9 branches
BranchTrueFalseTotal
/switch/ ? : 155 431 586
defined $d ? : 39 116 155
/counter/ ? : 140 291 431
defined $d ? : 52 88 140
/parameter/ ? : 174 117 291
/list/ ? : 55 62 117
defined $d ? : 33 22 55
/keypair/ ? : 62 0 62
defined $d ? : 33 29 62
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
BranchTrueFalseTotal
if ($raw =~ /^($NEGATIVE?$VALID_NAME|$SHORT_BUNDLE)=(.*)/) { } 27 245 272
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
BranchTrueFalseTotal
if defined $val 2 52 54
721 if defined $val;
722 52 ▶ if (! $neg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($neg) 48 4 52
723 throw_argv("Switch used twice: $self->{spec}{$arg}{canon}")
724 48 ▶ if $self->{seen}{$arg} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"seen"}{$arg} > 1 1 47 48
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
BranchTrueFalseTotal
if ($arg =~ /^$SHORT_BUNDLE$/) 24 248 272
736 24 my @flags = split(//,substr($arg,1));
737 24 ▶ unshift @{$self->{target}}, ("-" . pop(@flags) . "=" . $val)
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 4 20 24
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
BranchTrueFalseTotal
unless exists $self->{"spec"}{$key}{"needs"} 512 18 530
755 18 my $needs = $self->{spec}{$key}{needs};
756 18 ▶ my @prereq = ref($needs) eq 'ARRAY' ? @$needs : ( $needs );
Branch: 1 branch
BranchTrueFalseTotal
ref $needs eq 'ARRAY' ? : 18 0 18
757 18 for (@prereq) {
758 24 ▶ throw_spec("Prerequisite '$_' for '$self->{spec}{$key}{canon}' is not recognized")
Branch: 1 branch
BranchTrueFalseTotal
unless _find_arg($self, $_) 1 23 24
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
BranchTrueFalseTotal
unless $self->{"strict"} 809 48 857
776 857 ▶ throw_spec(
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$VALID_NAME$/ 2 855 857
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
BranchTrueFalseTotal
if exists $self->{"alias_hr"}{$alt_name} 3 852 855
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
BranchTrueFalseTotal
if grep {$strip eq $_;} values %{$$self{"strip"};} 0 852 852
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
BranchTrueFalseTotal
unless grep {$key eq $_;} @valid_keys 1 1909 1910
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
BranchTrueFalseTotal
unless grep {$type eq $_;} @valid_types 2 533 535
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
BranchTrueFalseTotal
unless defined $valid 287 102 389
806 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
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
BranchTrueFalseTotal
if (() = each %$value) 44 26 70
810 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
811 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
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
BranchTrueFalseTotal
_regex_or_code($_, $valid) or return 0 0 36 36
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
BranchTrueFalseTotal
if $name eq "DESTROY" 190 15 205
833 15 my ($action, $maybe_opt) = $name =~ /^(get|set)_(.+)/ ;
834 15 ▶ if ($action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action) 15 0 15
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
BranchTrueFalseTotal
if ($maybe_opt eq $known_opt) 12 33 45
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
BranchTrueFalseTotal
if ($maybe_opt eq $fuzzy_opt) 3 30 33
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
BranchTrueFalseTotal
unless $opt 0 15 15
853 if ! $opt;
854  
855 # handle the accessor if an option was found
856 15 ▶ if ($action eq "set") {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "set") 5 10 15
857 $self->{options}{$opt} =
858 ref($self->{options}{$opt}) eq 'ARRAY' ? [@_] :
859 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
860  
861 }
862 15 my $ans = $self->{options}{$opt};
863 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
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__
↑ Summary Lucid/Exception.pm »