lib/Getopt/Type/Tiny.pm

filter: Statementstmt 95.5% 106 / 111 Branchbran 85.7% 72 / 84 Conditioncond 74.3% 29 / 39 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 85.3% 228 / 267 CC 62 SCAR 33.9
CC
62
Cov
88%
get_opts
45.1
_infer_getopt_modifier
12.6
_validate_multi_value_type
12.0
CRAP 29.6
↑ Summary blib/script/example.pl »
1 package Getopt::Type::Tiny;
2  
3 # ABSTRACT: Clean Getopt::Long wrapper with Type::Tiny support
4  
5 2 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Type::Library -extends => [
Subroutine BEGIN: called CC=1 SCAR=0.0
10 qw(
11 Types::Standard
12 Types::Common::Numeric
13 Types::Common::String
14 )
15 2 ];
16  
17 2 ▶use Getopt::Long 'GetOptionsFromArray';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Pod::Usage qw(pod2usage);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our $VERSION = '0.03';
22  
23 2 push our @EXPORT_OK, qw(get_opts);
24  
25 30 ▶sub _infer_getopt_modifier ( $type, $coerce = 0 ) {
Subroutine _infer_getopt_modifier: called CC=12 SCAR=25.3
26  
27 # Use display_name since parameterized types have __ANON__ as name
28 30 my $name = $type->display_name;
29  
30 # Check if it's ArrayRef or HashRef
31 30 ▶ return unless $name =~ /^(ArrayRef|HashRef)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^(ArrayRef|HashRef)/u 9 21 30
32  
33 21 my $container = $1;
34 21 ▶ my $suffix = $container eq 'ArrayRef' ? '@' : '%';
Branch: 1 branch
BranchTrueFalseTotal
$container eq 'ArrayRef' ? : 16 5 21
35  
36 # Get inner type parameter
37 21 my $param = $type->type_parameter;
38 21 ▶ return unless $param; # Bare ArrayRef/HashRef - will error later
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 21 21
39  
40 # Check ancestry (order matters: Int before Num since Int is subtype of Num)
41 21 ▶ my $sigil
Branch: 5 branches
BranchTrueFalseTotal
$param->is_a_type_of(&Int()) ? : 7 14 21
$param->is_a_type_of(&Num()) ? : 2 12 14
$param->is_a_type_of(&Str()) ? : 10 2 12
$param->equals(&Any()) ? : 1 1 2
$coerce && $param->has_coercion ? : 1 0 1
Condition33%1/3
$coerce and $param->has_coercion
!ll&&!rl&&r
001
MC/DC0%0/2
$coerce and $param->has_coercion
$coerce $param->has_coercion
Truth table33%1/3
A and B
A= $coerceB= $param->has_coercion
ABresult
0X0
100
111
42 = $param->is_a_type_of(Int) ? 'i'
43 : $param->is_a_type_of(Num) ? 'f'
44 : $param->is_a_type_of(Str) ? 's'
45 : $param->equals(Any) ? 's'
46 : $coerce && $param->has_coercion ? 's'
47 : undef;
48  
49 21 ▶ return unless $sigil;
Branch: 1 branch
BranchTrueFalseTotal
unless $sigil 0 21 21
50 21 return "=$sigil$suffix";
51 }
52  
53 34 ▶sub _validate_multi_value_type ( $name, $type, $coerce = 0 ) {
Subroutine _validate_multi_value_type: called CC=12 SCAR=24.9
54  
55 # Use display_name since parameterized types have __ANON__ as name
56 34 my $type_name = $type->display_name;
57  
58 # Only validate ArrayRef and HashRef
59 34 ▶ return unless $type_name =~ /^(ArrayRef|HashRef)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $type_name =~ /^(ArrayRef|HashRef)/u 9 25 34
60  
61 25 my $container = $1;
62 25 my $param = $type->type_parameter;
63  
64 25 ▶ unless ($param) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($param) 2 23 25
65 2 ▶ my $suffix = $container eq 'ArrayRef' ? '@' : '%';
Branch: 1 branch
BranchTrueFalseTotal
$container eq 'ArrayRef' ? : 1 1 2
66 2 croak <<"END_ERROR";
67 Unsupported type '$type_name' for option '$name'.
68  
69 ArrayRef and HashRef require a type parameter (e.g., ArrayRef[Str]).
70  
71 To fix this, either:
72 1. Use explicit GetOpt::Long syntax: '$name=s$suffix' => { isa => $type_name }
73 2. Specify the inner type: ArrayRef[Str], ArrayRef[Int], or ArrayRef[Num]
74 END_ERROR
75 }
76  
77 23 ▶ my $is_supported
Condition93%14/15
$param->is_a_type_of(&Int()) || $param->is_a_type_of(&Num()) || $param->is_a_type_of(&Str()) || $param->equals(&Any()) || $coerce && $param->has_coercion
l!l&&r!l&&!r
2012
$param->is_a_type_of(&Int()) or $param->is_a_type_of(&Num()) or $param->is_a_type_of(&Str()) or $param->equals(&Any())
l!l&&r!l&&!r
1913
$param->is_a_type_of(&Int()) or $param->is_a_type_of(&Num()) or $param->is_a_type_of(&Str())
l!l&&r!l&&!r
9104
$param->is_a_type_of(&Int()) or $param->is_a_type_of(&Num())
l!l&&r!l&&!r
7214
$coerce and $param->has_coercion
!ll&&!rl&&r
201
MC/DC83%5/6
$param->is_a_type_of(&Int()) || $param->is_a_type_of(&Num()) || $param->is_a_type_of(&Str()) || $param->equals(&Any()) || $coerce && $param->has_coercion
$param->is_a_type_of(&Int()) $param->is_a_type_of(&Num()) $param->is_a_type_of(&Str()) $param->equals(&Any()) $coerce $param->has_coercion
Truth table85%6/7
A or B or C or D || E and F
A= $param->is_a_type_of(&Int())B= $param->is_a_type_of(&Num())C= $param->is_a_type_of(&Str())D= $param->equals(&Any())E= $coerceF= $param->has_coercion
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
0000111
00000X0
0000100
78 = $param->is_a_type_of(Int)
79 || $param->is_a_type_of(Num)
80 || $param->is_a_type_of(Str)
81 || $param->equals(Any)
82 || $coerce && $param->has_coercion;
83  
84 23 ▶ unless ($is_supported) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_supported) 2 21 23
85 2 ▶ my $suffix = $container eq 'ArrayRef' ? '@' : '%';
Branch: 1 branch
BranchTrueFalseTotal
$container eq 'ArrayRef' ? : 1 1 2
86 2 croak <<"END_ERROR";
87 Unsupported type '$type_name' for option '$name'.
88  
89 GetOpt::Long only supports ArrayRef and HashRef with inner types that are
90 subtypes of Str, Int, or Num.
91  
92 To fix this, either:
93 1. Use explicit GetOpt::Long syntax: '$name=s$suffix' => { isa => $type_name }
94 2. Simplify your type to ArrayRef[Str], ArrayRef[Int], or ArrayRef[Num]
95 END_ERROR
96 }
97 }
98  
99 34 ▶sub get_opts (@arg_specs) {
Subroutine get_opts: called CC=40 SCAR=38.1
Pod documented
100  
101 # forces Carp to ignore this package
102 34 local $Carp::Internal{ +__PACKAGE__ } = 1;
103 34 my %opt_for; # store the options
104 my @getopt_specs; # the option specs
105 34 my %defaults; # default values
106 34 my %renames; # rename option keys
107 34 my %types; # type constraints
108 34 my %coerce; # should coerce?
109 34 my %required; # required options
110  
111 34 ▶ while (@arg_specs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg_specs) 38 30 68
112 38 my $this_getopt_spec = shift @arg_specs;
113 38 ▶ my $options = is_HashRef( $arg_specs[0] ) ? shift @arg_specs : {};
Branch: 1 branch
BranchTrueFalseTotal
is_HashRef($arg_specs[0]) ? : 35 3 38
114  
115 38 my ($name) = $this_getopt_spec =~ /^([\w_]+)/;
116  
117 38 ▶ if ( exists $options->{isa} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"isa"}) 34 4 38
118 34 $types{$name} = $options->{isa};
119  
120 34 ▶ if ( $options->{coerce} && !$types{$name}->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"coerce"} and not $types{$name}->has_coercion) 0 34 34
Condition66%2/3
$options->{"coerce"} and not $types{$name}->has_coercion
!ll&&!rl&&r
3220
MC/DC0%0/2
$options->{"coerce"} and not $types{$name}->has_coercion
$options->{"coerce"} not $types{$name}->has_coercion
Truth table66%2/3
A and not(B)
A= $options->{"coerce"}B= not $types{$name}->has_coercion
ABresult
0X0
100
111
121 0 croak("Cannot coerce to a type without a coercion");
122 }
123  
124 # Validate multi-value types (croaks if unsupported)
125 _validate_multi_value_type(
126 $name, $types{$name},
127 $options->{coerce}
128 34 );
129  
130 # Check for multi-value type inference
131 my $inferred_modifier
132 30 = _infer_getopt_modifier( $types{$name}, $options->{coerce} );
133  
134 30 ▶ if ($inferred_modifier) {
Branch: 2 branches
BranchTrueFalseTotal
if ($inferred_modifier) { } 21 9 30
elsif ($types{$name}->name ne 'Bool' and not $this_getopt_spec =~ /=/u) { } 9 0 9
135  
136 # Multi-value type detected
137 21 ▶ if ( $this_getopt_spec =~ /=[a-z][@%]$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_getopt_spec =~ /=[a-z][\@%]$/u) { } 6 15 21
138  
139 # Explicit modifier exists - check for mismatch
140 6 my ($explicit_modifier) = $this_getopt_spec =~ /(=[a-z][@%])$/;
141 6 ▶ if ( $explicit_modifier ne $inferred_modifier
Branch: 1 branch
BranchTrueFalseTotal
if ($explicit_modifier ne $inferred_modifier and not $options->{"nowarn"}) 1 5 6
Condition100%3/3
$explicit_modifier ne $inferred_modifier and not $options->{"nowarn"}
!ll&&!rl&&r
411
MC/DC100%2/2
$explicit_modifier ne $inferred_modifier and not $options->{"nowarn"}
$explicit_modifier ne $inferred_modifier not $options->{"nowarn"}
Truth table100%3/3
A and not(B)
A= $explicit_modifier ne $inferred_modifierB= not $options->{"nowarn"}
ABresult
0X0
100
111
142 && !$options->{nowarn} )
143 {
144 warn "Option '$name' has explicit spec '$explicit_modifier' but type '"
145 1 . $types{$name}->display_name
146 . "' suggests '$inferred_modifier'.\n"
147 . "Type::Tiny will still validate the values. "
148 . "Use 'nowarn => 1' to suppress this warning.\n";
149 }
150 }
151 else {
152 # No explicit modifier - append inferred one
153 15 $this_getopt_spec .= $inferred_modifier;
154 }
155 }
156 ▶ elsif ($types{$name}->name ne 'Bool'
Condition33%1/3
$types{$name}->name ne "Bool" and not $this_getopt_spec =~ /=/u
!ll&&!rl&&r
009
MC/DC0%0/2
$types{$name}->name ne "Bool" and not $this_getopt_spec =~ /=/u
$types{$name}->name ne "Bool" not $this_getopt_spec =~ /=/u
Truth table33%1/3
A and not(B)
A= $types{$name}->name ne "Bool"B= not $this_getopt_spec =~ /=/u
ABresult
0X0
100
111
157 && $this_getopt_spec !~ /=/ )
158 {
159  
160 # Non-Bool scalar type without modifier
161 9 ▶ if ( $this_getopt_spec =~ s/=[a-z]$/=s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_getopt_spec =~ s/=[a-z]$/=s/u) { } 0 9 9
162  
163 # Replaced explicit type with =s
164 }
165 else {
166 9 $this_getopt_spec .= '=s';
167 }
168 }
169 }
170 34 push @getopt_specs, $this_getopt_spec;
171  
172 34 ▶ if ( exists $options->{default} && exists $options->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options->{"default"} and exists $options->{"required"}) 0 34 34
Condition66%2/3
exists $options->{"default"} and exists $options->{"required"}
!ll&&!rl&&r
2860
MC/DC0%0/2
exists $options->{"default"} and exists $options->{"required"}
exists $options->{"default"} exists $options->{"required"}
Truth table66%2/3
A and B
A= exists $options->{"default"}B= exists $options->{"required"}
ABresult
0X0
100
111
173 0 croak("Option '$name' cannot be both required and have a default value");
174 }
175 34 ▶ if ( exists $options->{default} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options->{'default'}) { } 6 28 34
elsif (exists $options->{'isa'}) { } 25 3 28
176 6 $defaults{$name} = $options->{default};
177 }
178 elsif ( exists $options->{isa} ) {
179  
180 # Auto-default for ArrayRef and HashRef
181 25 my $type_name = $options->{isa}->display_name;
182 25 ▶ if ( $type_name =~ /^ArrayRef/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type_name =~ /^ArrayRef/u) { } 14 11 25
elsif ($type_name =~ /^HashRef/u) { } 4 7 11
183 14 $defaults{$name} = [];
184 }
185 elsif ( $type_name =~ /^HashRef/ ) {
186 4 $defaults{$name} = {};
187 }
188 }
189 34 ▶ $renames{$name} = $options->{rename} if exists $options->{rename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"rename"} 1 33 34
190 $required{$name} = $options->{required}
191 34 ▶ if exists $options->{required};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"required"} 2 32 34
192 34 ▶ $coerce{$name} = $options->{coerce} if exists $options->{coerce};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"coerce"} 2 32 34
193  
194 # If no type is specified and the option doesn't have =s or =i,
195 # assume Bool
196 34 ▶ $types{$name} //= Bool unless $this_getopt_spec =~ /=/;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_getopt_spec =~ /=/u 4 30 34
Condition33%1/3
$types{$name} //= &Bool()
l!l&&r!l&&!r
040
MC/DC0%0/2
$types{$name} //= &Bool()
$types{$name} &Bool()
Truth table33%1/3
A //= B
A= $types{$name}B= &Bool()
ABresult
1X1
011
000
197 }
198  
199 # this has proven so incredibly useful that it's now a default,
200 # but perhaps it should be optional. Will need to think of the cleanest
201 # interface for that.
202 30 push @getopt_specs, 'help|?';
203 30 push @getopt_specs, 'man';
204  
205 # Note: this will mutate @ARGV if any options are present
206 30 my $result = GetOptionsFromArray( \@ARGV, \%opt_for, @getopt_specs );
207  
208 30 ▶ if ( $opt_for{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_for{"help"}) 0 30 30
209 0 pod2usage(1);
210 }
211  
212 30 ▶ if ( $opt_for{man} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_for{"man"}) 0 30 30
213 0 pod2usage( -exitval => 0, -verbose => 2 );
214 }
215  
216 30 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 30 30
217 0 croak( pod2usage(2) );
218 }
219  
220 # Apply defaults
221 30 for my $name ( keys %defaults ) {
222 24 ▶ next if exists $opt_for{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt_for{$name} 16 8 24
223 $opt_for{$name}
224 = is_CodeRef( $defaults{$name} )
225 ? $defaults{$name}->()
226 8 ▶ : $defaults{$name};
Branch: 1 branch
BranchTrueFalseTotal
is_CodeRef($defaults{$name}) ? : 3 5 8
227 }
228  
229 # Type checking
230 30 for my $name ( keys %types ) {
231 34 my $type = $types{$name};
232 34 my $coerce = $coerce{$name};
233 34 ▶ if ( exists $opt_for{$name} && !$type->check( $opt_for{$name} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt_for{$name} and not $type->check($opt_for{$name})) 4 30 34
Condition100%3/3
exists $opt_for{$name} and not $type->check($opt_for{$name})
!ll&&!rl&&r
1294
MC/DC100%2/2
exists $opt_for{$name} and not $type->check($opt_for{$name})
exists $opt_for{$name} not $type->check($opt_for{$name})
Truth table100%3/3
A and not(B)
A= exists $opt_for{$name}B= not $type->check($opt_for{$name})
ABresult
0X0
100
111
234 4 ▶ if ($coerce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coerce) 2 2 4
235 2 my $new_val = $type->coerce( $opt_for{$name} );
236 2 ▶ if ( $type->check($new_val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->check($new_val)) 2 0 2
237 2 $opt_for{$name} = $new_val;
238 2 next;
239 }
240 }
241 2 croak( "Invalid value for option '$name': " . $type->get_message( $opt_for{$name} ) );
242 }
243 }
244  
245 # Check required options
246 28 for my $name ( keys %required ) {
247 2 ▶ if ( $required{$name} && !exists $opt_for{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required{$name} and not exists $opt_for{$name}) 1 1 2
Condition66%2/3
$required{$name} and not exists $opt_for{$name}
!ll&&!rl&&r
011
MC/DC50%1/2
$required{$name} and not exists $opt_for{$name}
$required{$name} not exists $opt_for{$name}
Truth table66%2/3
A and not(B)
A= $required{$name}B= not exists $opt_for{$name}
ABresult
0X0
100
111
248 1 croak("Required option '$name' is missing");
249 }
250 }
251  
252 # Rename keys
253 27 for my $name ( keys %renames ) {
254 $opt_for{ $renames{$name} } = delete $opt_for{$name}
255 1 ▶ if exists $opt_for{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt_for{$name} 1 0 1
256 }
257  
258 27 return %opt_for;
259 }
260 2 1;
261  
262 __END__
↑ Summary blib/script/example.pl »