Simple/Options.pm

filter: Statementstmt 98.4% 188 / 191 Branchbran 83.1% 133 / 160 Conditioncond 60.7% 68 / 112 MC/DCmcdc 36.5% 23 / 63 Subroutinesub 100% 16 / 16 Podpod 100% 3 / 3 totaltotal 79.0% 431 / 545 CC 130 SCAR 38.6
CC
130
Cov
84%
_check_option_constraints
96.9
_validate_option_value
26.7
finalize_options
18.6
CRAP 47.7
« Simple.pm ↑ Summary Simple/TempDir.pm »
1 package CLI::Simple::Options;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 our $VERSION = '2.7.0';
13  
14 ########################################################################
15 72 ▶sub normalize_option_specs {
Subroutine normalize_option_specs: called CC=17 SCAR=28.5
Pod documented
16 ########################################################################
17 72 my ( $self, $declarations ) = @_;
18  
19 72 ▶ croak 'Option declarations must be an array or hash reference'
Branch: 1 branch
BranchTrueFalseTotal
if not ref $declarations or builtin::reftype($declarations) ne "ARRAY" and builtin::reftype($declarations) ne "HASH" 3 69 72
Condition66%4/6
not ref $declarations or builtin::reftype($declarations) ne "ARRAY" and builtin::reftype($declarations) ne "HASH"
l!l&&r!l&&!r
3069
builtin::reftype($declarations) ne "ARRAY" and builtin::reftype($declarations) ne "HASH"
!ll&&!rl&&r
37320
MC/DC33%1/3
not ref $declarations or builtin::reftype($declarations) ne "ARRAY" and builtin::reftype($declarations) ne "HASH"
not ref $declarations builtin::reftype($declarations) ne "ARRAY" builtin::reftype($declarations) ne "HASH"
Truth table75%3/4
not(A) or B and C
A= not ref $declarationsB= builtin::reftype($declarations) ne "ARRAY"C= builtin::reftype($declarations) ne "HASH"
ABCresult
1XX1
0111
00X0
0100
20 if !ref $declarations || ( reftype($declarations) ne 'ARRAY' && reftype($declarations) ne 'HASH' );
21  
22 69 my @specs;
23 my %metadata;
24  
25 69 my @declarations;
26  
27 69 ▶ if ( reftype($declarations) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($declarations) eq 'ARRAY') { } 37 32 69
28 37 foreach my $spec ( @{$declarations} ) {
29 29 ▶ croak 'Option specifications must be strings'
Branch: 1 branch
BranchTrueFalseTotal
if ref $spec 0 29 29
30 if ref $spec;
31 29 push @declarations, [ $spec, {} ];
32 }
33 }
34 else {
35 32 foreach my $spec ( keys %{$declarations} ) {
36 43 my $attributes = $declarations->{$spec};
37 43 ▶ croak sprintf q{Attributes for option '%s' must be a hash reference}, $spec
Branch: 1 branch
BranchTrueFalseTotal
if not ref $attributes or builtin::reftype($attributes) ne "HASH" 1 42 43
Condition66%2/3
not ref $attributes or builtin::reftype($attributes) ne "HASH"
l!l&&r!l&&!r
0142
MC/DC50%1/2
not ref $attributes or builtin::reftype($attributes) ne "HASH"
not ref $attributes builtin::reftype($attributes) ne "HASH"
Truth table66%2/3
not(A) or B
A= not ref $attributesB= builtin::reftype($attributes) ne "HASH"
ABresult
1X1
011
000
38 if !ref $attributes || reftype($attributes) ne 'HASH';
39 42 push @declarations, [ $spec, $attributes ];
40 }
41 }
42  
43 68 foreach my $declaration (@declarations) {
44 71 my ( $spec, $attributes ) = @{$declaration};
45 71 my ($name) = $spec =~ /\A([\w-]+)/;
46 71 ▶ croak sprintf q{Invalid option specification: %s}, $spec
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 71 71
47 if !defined $name;
48  
49 71 $name =~ s/-/_/g;
50 croak sprintf q{Duplicate canonical option name: %s}, $name
51 71 ▶ if exists $metadata{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $metadata{$name} 1 70 71
52  
53 70 my $inferred_type = _infer_option_type($spec);
54 70 my %entry = ( spec => $spec, %{$attributes} );
55  
56 70 ▶ if ( defined $inferred_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $inferred_type) 69 1 70
57 69 $entry{inferred_type} = $inferred_type;
58 69 ▶ $entry{type} //= $inferred_type;
Condition66%2/3
$entry{"type"} //= $inferred_type
l!l&&r!l&&!r
10590
MC/DC0%0/2
$entry{"type"} //= $inferred_type
$entry{"type"} $inferred_type
Truth table66%2/3
A //= B
A= $entry{"type"}B= $inferred_type
ABresult
1X1
011
000
59 }
60  
61 70 ▶ $entry{collection} = 'array' if $spec =~ /[=:][sif]@\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /[=:][sif]\@\z/ 3 67 70
62 70 ▶ $entry{collection} = 'hash' if $spec =~ /[=:][sif]%\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /[=:][sif]%\z/ 2 68 70
63 70 _check_option_constraints( $name, \%entry );
64  
65 61 push @specs, $spec;
66 61 $metadata{$name} = \%entry;
67 }
68  
69 58 return ( \@specs, \%metadata );
70 }
71  
72 ########################################################################
73 70 ▶sub _infer_option_type {
Subroutine _infer_option_type: called CC=8 SCAR=20.8
74 ########################################################################
75 70 my ($spec) = @_;
76  
77 # Only infer from recognized Getopt::Long value modifiers. Unknown
78 # suffixes are left uninterpreted rather than guessing a type.
79 # Extract the suffix after the option names, including optional
80 # arguments and multi-value (@ or %) destinations.
81 70 my ($modifier) = $spec =~ /[=:]([sif])(?:[@%])?\z/;
82 70 ▶ if ( defined $modifier ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $modifier) 57 13 70
83 57 ▶ return 'string' if $modifier eq 's';
Branch: 1 branch
BranchTrueFalseTotal
if $modifier eq "s" 36 21 57
84 21 ▶ return 'integer' if $modifier eq 'i';
Branch: 1 branch
BranchTrueFalseTotal
if $modifier eq "i" 20 1 21
85 1 ▶ return 'number' if $modifier eq 'f';
Branch: 1 branch
BranchTrueFalseTotal
if $modifier eq "f" 1 0 1
86 }
87  
88 13 ▶ return 'integer'
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /\+\z/ 1 12 13
89 if $spec =~ /\+\z/;
90  
91 12 ▶ return 'boolean'
Branch: 1 branch
BranchTrueFalseTotal
if $spec =~ /!\z/ or $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/ 11 1 12
Condition100%3/3
$spec =~ /!\z/ or $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/
l!l&&r!l&&!r
1101
MC/DC100%2/2
$spec =~ /!\z/ or $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/
$spec =~ /!\z/ $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/
Truth table100%3/3
A or B
A= $spec =~ /!\z/B= $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/
ABresult
1X1
011
000
92 if $spec =~ /!\z/ || $spec =~ /\A[\w-]+(?:[|][\w-]+)*\z/;
93  
94 1 return;
95 }
96  
97 ########################################################################
98 70 ▶sub _check_option_constraints {
Subroutine _check_option_constraints: called CC=56 SCAR=45.7
99 ########################################################################
100 70 my ( $name, $entry ) = @_;
101  
102 70 my %allowed = map { $_ => 1 } qw(
103 spec description inferred_type type collection required min max
104 min_length max_length enum finalize slurp
105 );
106 70 foreach my $key ( keys %{$entry} ) {
107 croak sprintf q{Unknown attribute '%s' for option '%s'}, $key, $name
108 259 ▶ if !exists $allowed{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $allowed{$key} 0 259 259
109 }
110  
111 70 ▶ if ( exists $entry->{slurp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"slurp"}) 9 61 70
112 croak sprintf q{slurp must be 0 or 1 for option '%s'}, $name
113 if !defined $entry->{slurp}
114 || ref $entry->{slurp}
115 9 ▶ || $entry->{slurp} !~ /\A[01]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $entry->{"slurp"} or ref $entry->{"slurp"} or not $entry->{"slurp"} =~ /\A[01]\z/ 1 8 9
Condition50%3/6
not defined $entry->{"slurp"} or ref $entry->{"slurp"} or not $entry->{"slurp"} =~ /\A[01]\z/
l!l&&r!l&&!r
018
not defined $entry->{"slurp"} or ref $entry->{"slurp"}
l!l&&r!l&&!r
009
MC/DC33%1/3
not defined $entry->{"slurp"} or ref $entry->{"slurp"} or not $entry->{"slurp"} =~ /\A[01]\z/
not defined $entry->{"slurp"} ref $entry->{"slurp"} not $entry->{"slurp"} =~ /\A[01]\z/
Truth table50%2/4
not(A) or B or not(C)
A= not defined $entry->{"slurp"}B= ref $entry->{"slurp"}C= not $entry->{"slurp"} =~ /\A[01]\z/
ABCresult
1XX1
01X1
0011
0000
116  
117 croak sprintf q{slurp requires type 'file' for option '%s'}, $name
118 8 ▶ if $entry->{slurp} && ( $entry->{type} // q{} ) ne 'file';
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"slurp"} and ($entry->{"type"} // "") ne "file" 1 7 8
Condition60%3/5
$entry->{"slurp"} and ($entry->{"type"} // "") ne "file"
!ll&&!rl&&r
071
$entry->{"type"} // ""
l!l
80
MC/DC0%0/2
$entry->{"slurp"} and ($entry->{"type"} // "") ne "file"
$entry->{"slurp"} $entry->{"type"}
Truth table33%1/3
A and B
A= $entry->{"slurp"}B= $entry->{"type"}
ABresult
0X0
100
111
119  
120 croak sprintf q{slurp does not support collection options: %s}, $name
121 7 ▶ if $entry->{slurp} && $entry->{collection};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"slurp"} and $entry->{"collection"} 1 6 7
Condition66%2/3
$entry->{"slurp"} and $entry->{"collection"}
!ll&&!rl&&r
061
MC/DC50%1/2
$entry->{"slurp"} and $entry->{"collection"}
$entry->{"slurp"} $entry->{"collection"}
Truth table66%2/3
A and B
A= $entry->{"slurp"}B= $entry->{"collection"}
ABresult
0X0
100
111
122 }
123  
124 67 ▶ if ( exists $entry->{finalize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"finalize"}) 9 58 67
125 croak sprintf q{finalize must be a code reference for option '%s'}, $name
126 9 ▶ if !ref $entry->{finalize} || reftype( $entry->{finalize} ) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not ref $entry->{"finalize"} or builtin::reftype($entry->{"finalize"}) ne "CODE" 1 8 9
Condition66%2/3
not ref $entry->{"finalize"} or builtin::reftype($entry->{"finalize"}) ne "CODE"
l!l&&r!l&&!r
108
MC/DC50%1/2
not ref $entry->{"finalize"} or builtin::reftype($entry->{"finalize"}) ne "CODE"
not ref $entry->{"finalize"} builtin::reftype($entry->{"finalize"}) ne "CODE"
Truth table66%2/3
not(A) or B
A= not ref $entry->{"finalize"}B= builtin::reftype($entry->{"finalize"}) ne "CODE"
ABresult
1X1
011
000
127 }
128  
129 66 ▶ if ( exists $entry->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"required"}) 6 60 66
130 croak sprintf q{required must be 0 or 1 for option '%s'}, $name
131 6 ▶ if !defined $entry->{required} || $entry->{required} !~ /\A[01]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"required"} and $entry->{"required"} =~ /\A[01]\z/ 1 5 6
Condition66%2/3
defined $entry->{"required"} and $entry->{"required"} =~ /\A[01]\z/
!ll&&!rl&&r
015
MC/DC50%1/2
defined $entry->{"required"} and $entry->{"required"} =~ /\A[01]\z/
defined $entry->{"required"} $entry->{"required"} =~ /\A[01]\z/
Truth table66%2/3
A and B
A= defined $entry->{"required"}B= $entry->{"required"} =~ /\A[01]\z/
ABresult
0X0
100
111
132 }
133  
134 65 my $type = $entry->{type};
135 65 my %valid_types = map { $_ => 1 } qw(string integer number boolean file directory);
136 65 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 64 1 65
137 croak sprintf q{Unknown type '%s' for option '%s'}, $type, $name
138 64 ▶ if !exists $valid_types{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_types{$type} 0 64 64
139 }
140  
141 65 my $inferred = $entry->{inferred_type};
142 65 ▶ if ( defined $type && defined $inferred ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and defined $inferred) 64 1 65
Condition66%2/3
defined $type and defined $inferred
!ll&&!rl&&r
1064
MC/DC50%1/2
defined $type and defined $inferred
defined $type defined $inferred
Truth table66%2/3
A and B
A= defined $typeB= defined $inferred
ABresult
0X0
100
111
143 my $compatible
144 = $type eq $inferred
145 64 ▶ || ( $inferred eq 'string' && any { $type eq $_ } qw(file directory) )
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition66%8/12
$type eq $inferred || $inferred eq 'string' && &any(sub { $type eq $_; } , 'file', 'directory') || $inferred eq 'integer' && $type eq 'number'
l!l&&r!l&&!r
6301
$type eq $inferred or $inferred eq "string" and &any(sub { $type eq $_; } , "file", "directory")
l!l&&r!l&&!r
5671
$inferred eq "string" and &any(sub { $type eq $_; } , "file", "directory")
!ll&&!rl&&r
107
$inferred eq "integer" and $type eq "number"
!ll&&!rl&&r
010
MC/DC40%2/5
$type eq $inferred || $inferred eq 'string' && &any(sub { $type eq $_; } , 'file', 'directory') || $inferred eq 'integer' && $type eq 'number'
$type eq $inferred $inferred eq "string" &any(sub { $type eq $_; } , "file", "directory") $inferred eq "integer" $type eq "number"
Truth table37%3/8
A or B and C || D and E
A= $type eq $inferredB= $inferred eq "string"C= &any(sub { $type eq $_; } , "file", "directory")D= $inferred eq "integer"E= $type eq "number"
ABCDEresult
1XXXX1
011XX1
00X111
010111
00X0X0
00X100
0100X0
010100
146 || ( $inferred eq 'integer' && $type eq 'number' );
147 64 ▶ croak sprintf q{Type '%s' conflicts with specification for option '%s'}, $type, $name
Branch: 1 branch
BranchTrueFalseTotal
unless $compatible 1 63 64
148 if !$compatible;
149 }
150  
151 64 foreach my $key (qw(min max min_length max_length)) {
152 256 ▶ next if !exists $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{$key} 241 15 256
153 croak sprintf q{%s must be numeric for option '%s'}, $key, $name
154 if !defined $entry->{$key}
155 || ref $entry->{$key}
156 15 ▶ || $entry->{$key} !~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $entry->{$key} or ref $entry->{$key} or not $entry->{$key} =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/ 0 15 15
Condition33%2/6
not defined $entry->{$key} or ref $entry->{$key} or not $entry->{$key} =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/
l!l&&r!l&&!r
0015
not defined $entry->{$key} or ref $entry->{$key}
l!l&&r!l&&!r
0015
MC/DC0%0/3
not defined $entry->{$key} or ref $entry->{$key} or not $entry->{$key} =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/
not defined $entry->{$key} ref $entry->{$key} not $entry->{$key} =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/
Truth table25%1/4
not(A) or B or not(C)
A= not defined $entry->{$key}B= ref $entry->{$key}C= not $entry->{$key} =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)\z/
ABCresult
1XX1
01X1
0011
0000
157 }
158  
159 64 ▶ if ( exists $entry->{min} || exists $entry->{max} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"min"} or exists $entry->{"max"}) 8 56 64
Condition66%2/3
exists $entry->{"min"} or exists $entry->{"max"}
l!l&&r!l&&!r
8056
MC/DC50%1/2
exists $entry->{"min"} or exists $entry->{"max"}
exists $entry->{"min"} exists $entry->{"max"}
Truth table66%2/3
A or B
A= exists $entry->{"min"}B= exists $entry->{"max"}
ABresult
1X1
011
000
160 croak sprintf q{Numeric bounds require a numeric option: %s}, $name
161 8 ▶ if !defined $type || !any { $type eq $_ } qw(integer number);
Subroutine __ANON__: called CC=1 SCAR=0.8
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and &any(sub { $type eq $_; } , "integer", "number") 0 8 8
Condition33%1/3
defined $type and &any(sub { $type eq $_; } , "integer", "number")
!ll&&!rl&&r
008
MC/DC0%0/2
defined $type and &any(sub { $type eq $_; } , "integer", "number")
defined $type &any(sub { $type eq $_; } , "integer", "number")
Truth table33%1/3
A and B
A= defined $typeB= &any(sub { $type eq $_; } , "integer", "number")
ABresult
0X0
100
111
162 croak sprintf q{min exceeds max for option '%s'}, $name
163 8 ▶ if exists $entry->{min} && exists $entry->{max} && $entry->{min} > $entry->{max};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entry->{"min"} and exists $entry->{"max"} and $entry->{"min"} > $entry->{"max"} 1 7 8
Condition83%5/6
exists $entry->{"min"} and exists $entry->{"max"} and $entry->{"min"} > $entry->{"max"}
!ll&&!rl&&r
431
exists $entry->{"min"} and exists $entry->{"max"}
!ll&&!rl&&r
044
MC/DC66%2/3
exists $entry->{"min"} and exists $entry->{"max"} and $entry->{"min"} > $entry->{"max"}
exists $entry->{"min"} exists $entry->{"max"} $entry->{"min"} > $entry->{"max"}
Truth table75%3/4
A and B and C
A= exists $entry->{"min"}B= exists $entry->{"max"}C= $entry->{"min"} > $entry->{"max"}
ABCresult
0XX0
10X0
1100
1111
164 }
165  
166 63 ▶ if ( exists $entry->{min_length} || exists $entry->{max_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"min_length"} or exists $entry->{"max_length"}) 2 61 63
Condition66%2/3
exists $entry->{"min_length"} or exists $entry->{"max_length"}
l!l&&r!l&&!r
2061
MC/DC50%1/2
exists $entry->{"min_length"} or exists $entry->{"max_length"}
exists $entry->{"min_length"} exists $entry->{"max_length"}
Truth table66%2/3
A or B
A= exists $entry->{"min_length"}B= exists $entry->{"max_length"}
ABresult
1X1
011
000
167 2 ▶ croak sprintf q{Length bounds require a string option: %s}, $name
Branch: 1 branch
BranchTrueFalseTotal
if not defined $type or $type ne "string" 1 1 2
Condition66%2/3
not defined $type or $type ne "string"
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $type or $type ne "string"
not defined $type $type ne "string"
Truth table66%2/3
not(A) or B
A= not defined $typeB= $type ne "string"
ABresult
1X1
011
000
168 if !defined $type || $type ne 'string';
169 1 foreach my $key (qw(min_length max_length)) {
170 2 ▶ next if !exists $entry->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $entry->{$key} 0 2 2
171 croak sprintf q{%s must be a nonnegative integer for option '%s'}, $key, $name
172 2 ▶ if $entry->{$key} !~ /\A\d+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->{$key} =~ /\A\d+\z/ 0 2 2
173 }
174 croak sprintf q{min_length exceeds max_length for option '%s'}, $name
175 if exists $entry->{min_length}
176 && exists $entry->{max_length}
177 1 ▶ && $entry->{min_length} > $entry->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if exists $entry->{"min_length"} and exists $entry->{"max_length"} and $entry->{"min_length"} > $entry->{"max_length"} 0 1 1
Condition33%2/6
exists $entry->{"min_length"} and exists $entry->{"max_length"} and $entry->{"min_length"} > $entry->{"max_length"}
!ll&&!rl&&r
010
exists $entry->{"min_length"} and exists $entry->{"max_length"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $entry->{"min_length"} and exists $entry->{"max_length"} and $entry->{"min_length"} > $entry->{"max_length"}
exists $entry->{"min_length"} exists $entry->{"max_length"} $entry->{"min_length"} > $entry->{"max_length"}
Truth table25%1/4
A and B and C
A= exists $entry->{"min_length"}B= exists $entry->{"max_length"}C= $entry->{"min_length"} > $entry->{"max_length"}
ABCresult
0XX0
10X0
1100
1111
178 }
179  
180 62 ▶ if ( exists $entry->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $entry->{"enum"}) 3 59 62
181 croak sprintf q{enum must be a nonempty array reference for option '%s'}, $name
182 if !ref $entry->{enum}
183 || reftype( $entry->{enum} ) ne 'ARRAY'
184 3 ▶ || !@{ $entry->{enum} };
Branch: 1 branch
BranchTrueFalseTotal
if not ref $entry->{"enum"} or builtin::reftype($entry->{"enum"}) ne "ARRAY" or not @{$entry->{"enum"};} 1 2 3
Condition50%3/6
not ref $entry->{"enum"} or builtin::reftype($entry->{"enum"}) ne "ARRAY" or not @{$entry->{"enum"};}
l!l&&r!l&&!r
012
not ref $entry->{"enum"} or builtin::reftype($entry->{"enum"}) ne "ARRAY"
l!l&&r!l&&!r
003
MC/DC33%1/3
not ref $entry->{"enum"} or builtin::reftype($entry->{"enum"}) ne "ARRAY" or not @{$entry->{"enum"};}
not ref $entry->{"enum"} builtin::reftype($entry->{"enum"}) ne "ARRAY" not @{$entry->{"enum"};}
Truth table50%2/4
not(A) or B or not(C)
A= not ref $entry->{"enum"}B= builtin::reftype($entry->{"enum"}) ne "ARRAY"C= not @{$entry->{"enum"};}
ABCresult
1XX1
01X1
0011
0000
185 2 foreach my $item ( @{ $entry->{enum} } ) {
186 4 ▶ croak sprintf q{enum entries must be defined scalars for option '%s'}, $name
Branch: 1 branch
BranchTrueFalseTotal
if not defined $item or ref $item 0 4 4
Condition33%1/3
not defined $item or ref $item
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $item or ref $item
not defined $item ref $item
Truth table33%1/3
not(A) or B
A= not defined $itemB= ref $item
ABresult
1X1
011
000
187 if !defined $item || ref $item;
188 }
189 }
190  
191 61 return;
192 }
193  
194 ########################################################################
195 58 ▶sub validate_options {
Subroutine validate_options: called CC=13 SCAR=25.9
Pod documented
196 ########################################################################
197 58 my ($self) = @_;
198  
199 58 ▶ my $metadata = $self->{_cli_simple_option_metadata} // {};
Condition50%1/2
$self->{"_cli_simple_option_metadata"} // {}
l!l
580
MC/DC0%0/1
$self->{"_cli_simple_option_metadata"} // {}
$self->{"_cli_simple_option_metadata"}
Truth table50%1/2
A
A= $self->{"_cli_simple_option_metadata"}
Aresult
11
00
200  
201 58 foreach my $name ( sort keys %{$metadata} ) {
202 55 my $rule = $metadata->{$name};
203 55 my $getter = 'get_' . $name;
204 55 ▶ my $value = $self->can($getter) ? $self->$getter() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->can($getter) ? : 55 0 55
205  
206 55 ▶ if ( !defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 10 45 55
207 croak sprintf q{Required option '%s' is undefined}, $name
208 10 ▶ if $rule->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"required"} 2 8 10
209 8 next;
210 }
211  
212 45 my @values = ($value);
213 45 ▶ if ( $rule->{collection} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->{'collection'}) { } 6 39 45
elsif (ref $value) { } 0 39 39
214 6 my $collection = $rule->{collection};
215 6 ▶ croak sprintf q{Option '%s' requires a %s reference}, $name, $collection
Branch: 2 branches
BranchTrueFalseTotal
if not ref $value or builtin::reftype($value) ne uc($collection eq "array" ? "array" : "hash") 1 5 6
$collection eq 'array' ? : 4 2 6
Condition66%2/3
not ref $value or builtin::reftype($value) ne uc($collection eq "array" ? "array" : "hash")
l!l&&r!l&&!r
015
MC/DC50%1/2
not ref $value or builtin::reftype($value) ne uc($collection eq "array" ? "array" : "hash")
not ref $value builtin::reftype($value) ne uc($collection eq "array" ? "array" : "hash")
Truth table66%2/3
not(A) or B
A= not ref $valueB= builtin::reftype($value) ne uc($collection eq "array" ? "array" : "hash")
ABresult
1X1
011
000
216 if !ref $value || reftype($value) ne uc( $collection eq 'array' ? 'array' : 'hash' );
217 5 ▶ @values = $collection eq 'array' ? @{$value} : values %{$value};
Branch: 1 branch
BranchTrueFalseTotal
$collection eq 'array' ? : 4 1 5
218 }
219 elsif ( ref $value ) {
220 0 croak sprintf q{Option '%s' requires a scalar value}, $name;
221 }
222  
223 44 foreach my $item (@values) {
224 47 _validate_option_value( $name, $item, $rule );
225 }
226 }
227  
228 48 return $self;
229 }
230  
231 ########################################################################
232 45 ▶sub finalize_options {
Subroutine finalize_options: called CC=18 SCAR=29.2
Pod documented
233 ########################################################################
234 45 my ($self) = @_;
235  
236 45 ▶ my $metadata = $self->{_cli_simple_option_metadata} // {};
Condition50%1/2
$self->{"_cli_simple_option_metadata"} // {}
l!l
450
MC/DC0%0/1
$self->{"_cli_simple_option_metadata"} // {}
$self->{"_cli_simple_option_metadata"}
Truth table50%1/2
A
A= $self->{"_cli_simple_option_metadata"}
Aresult
11
00
237  
238 45 foreach my $name ( sort keys %{$metadata} ) {
239 37 my $rule = $metadata->{$name};
240 37 my $finalizer = $rule->{finalize};
241 37 ▶ my $slurp = $rule->{slurp} // 0;
Condition100%2/2
$rule->{"slurp"} // 0
l!l
631
MC/DC100%1/1
$rule->{"slurp"} // 0
$rule->{"slurp"}
Truth table100%2/2
A
A= $rule->{"slurp"}
Aresult
11
00
242 37 ▶ next if !$slurp && !defined $finalizer;
Branch: 1 branch
BranchTrueFalseTotal
unless $slurp or defined $finalizer 24 13 37
Condition100%3/3
$slurp or defined $finalizer
l!l&&r!l&&!r
6724
MC/DC100%2/2
$slurp or defined $finalizer
$slurp defined $finalizer
Truth table100%3/3
A or B
A= $slurpB= defined $finalizer
ABresult
1X1
011
000
243  
244 13 my $getter = 'get_' . $name;
245 13 my $setter = 'set_' . $name;
246  
247 13 ▶ croak sprintf q{Missing accessor for option '%s'}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can($getter) and $self->can($setter) 0 13 13
Condition33%1/3
$self->can($getter) and $self->can($setter)
!ll&&!rl&&r
0013
MC/DC0%0/2
$self->can($getter) and $self->can($setter)
$self->can($getter) $self->can($setter)
Truth table33%1/3
A and B
A= $self->can($getter)B= $self->can($setter)
ABresult
0X0
100
111
248 if !$self->can($getter) || !$self->can($setter);
249  
250 13 my $value = $self->$getter();
251 13 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 11 13
252  
253 11 ▶ if ($slurp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($slurp) 5 6 11
254 5 my $content;
255  
256 5 my $ok = eval {
257 5 ▶ open my $fh, '<', $value
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $value or die sprintf(q[Cannot open '%s': %s], $value, $CLI::Simple::Options::OS_ERROR) 4 1 5
258 or die sprintf q{Cannot open '%s': %s}, $value, $OS_ERROR;
259  
260 4 local $RS = undef;
261  
262 4 $content = <$fh>;
263  
264 4 ▶ die sprintf q{Cannot read '%s': %s}, $value, $OS_ERROR
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content or eof $fh 0 4 4
Condition33%1/3
defined $content or eof $fh
l!l&&r!l&&!r
400
MC/DC0%0/2
defined $content or eof $fh
defined $content eof $fh
Truth table33%1/3
A or B
A= defined $contentB= eof $fh
ABresult
1X1
011
000
265 if !defined $content && !eof $fh;
266  
267 4 ▶ $content //= q{};
Condition50%1/2
$content //= ""
l!l
40
MC/DC0%0/1
$content //= ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
268  
269 4 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die sprintf(q[Cannot close '%s': %s], $value, $CLI::Simple::Options::OS_ERROR) 4 0 4
270 or die sprintf q{Cannot close '%s': %s}, $value, $OS_ERROR;
271 4 1;
272 };
273  
274 5 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 4 5
275 1 my $error = $EVAL_ERROR;
276 1 croak sprintf q{Slurp failed for option '%s': %s}, $name, $error;
277 }
278  
279 4 $value = $content;
280 }
281  
282 10 ▶ if ( defined $finalizer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $finalizer) 8 2 10
283 8 my $result;
284 8 my $ok = eval {
285 8 $result = $finalizer->( $self, $value );
286 7 1;
287 };
288  
289 8 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 7 8
290 1 my $error = $EVAL_ERROR;
291 1 croak sprintf q{Finalization failed for option '%s': %s}, $name, $error;
292 }
293 7 $value = $result;
294 }
295  
296 9 $self->$setter($value);
297 }
298  
299 43 return $self;
300 }
301  
302 ########################################################################
303 47 ▶sub _validate_option_value {
Subroutine _validate_option_value: called CC=23 SCAR=32.8
304 ########################################################################
305 47 my ( $name, $value, $rule ) = @_;
306  
307 47 ▶ croak sprintf q{Option '%s' contains an undefined value}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 47 47
308 if !defined $value;
309  
310 47 ▶ croak sprintf q{Option '%s' contains a reference instead of a scalar}, $name
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 47 47
311 if ref $value;
312  
313 47 my $type = $rule->{type};
314 47 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 47 0 47
315 47 ▶ if ( $type eq 'integer' || $type eq 'boolean' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq 'integer' or $type eq 'boolean') { } 22 25 47
elsif ($type eq 'number') { } 0 25 25
elsif ($type eq 'file') { } 5 20 25
elsif ($type eq 'directory') { } 0 20 20
Condition100%3/3
$type eq "integer" or $type eq "boolean"
l!l&&r!l&&!r
16625
MC/DC100%2/2
$type eq "integer" or $type eq "boolean"
$type eq "integer" $type eq "boolean"
Truth table100%3/3
A or B
A= $type eq "integer"B= $type eq "boolean"
ABresult
1X1
011
000
316 22 ▶ croak sprintf q{Option '%s' must be an integer}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[+-]?\d+\z/ 1 21 22
317 if $value !~ /\A[+-]?\d+\z/;
318 }
319 elsif ( $type eq 'number' ) {
320 0 ▶ croak sprintf q{Option '%s' must be a number}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/ 0 0 0
321 if $value !~ /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/;
322 }
323 elsif ( $type eq 'file' ) {
324 5 ▶ croak sprintf q{Option '%s' must be an existing file}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless -f $value 1 4 5
325 if !-f $value;
326 }
327 elsif ( $type eq 'directory' ) {
328 0 ▶ croak sprintf q{Option '%s' must be an existing directory}, $name
Branch: 1 branch
BranchTrueFalseTotal
unless -d $value 0 0 0
329 if !-d $value;
330 }
331 }
332  
333 45 ▶ if ( exists $rule->{min} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"min"}) 12 33 45
334 croak sprintf q{Option '%s' must be >= %s}, $name, $rule->{min}
335 12 ▶ if $value < $rule->{min};
Branch: 1 branch
BranchTrueFalseTotal
if $value < $rule->{"min"} 3 9 12
336 }
337 42 ▶ if ( exists $rule->{max} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"max"}) 2 40 42
338 croak sprintf q{Option '%s' must be <= %s}, $name, $rule->{max}
339 2 ▶ if $value > $rule->{max};
Branch: 1 branch
BranchTrueFalseTotal
if $value > $rule->{"max"} 0 2 2
340 }
341 42 ▶ if ( exists $rule->{min_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"min_length"}) 2 40 42
342 croak sprintf q{Option '%s' is shorter than %s characters}, $name, $rule->{min_length}
343 2 ▶ if length($value) < $rule->{min_length};
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $rule->{"min_length"} 1 1 2
344 }
345 41 ▶ if ( exists $rule->{max_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"max_length"}) 1 40 41
346 croak sprintf q{Option '%s' exceeds %s characters}, $name, $rule->{max_length}
347 1 ▶ if length($value) > $rule->{max_length};
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $rule->{"max_length"} 0 1 1
348 }
349 41 ▶ if ( exists $rule->{enum} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"enum"}) 3 38 41
350 croak sprintf q{Option '%s' has an unrecognized value}, $name
351 3 ▶ if !any { $_ eq $value } @{ $rule->{enum} };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $value; } , @{$$rule{"enum"};}) 1 2 3
352 }
353  
354 40 return;
355 }
356  
357 21 1;
358  
359 ## no critic
360  
361 __END__
« Simple.pm ↑ Summary Simple/TempDir.pm »