Yath/Instance.pm

filter: Statementstmt 71.1% 242 / 340 Branchbran 56.8% 100 / 176 Conditioncond 44.3% 55 / 124 MC/DCmcdc 16.9% 12 / 71 Subroutinesub 73.3% 22 / 30 Podpod 0.0% 0 / 12 totaltotal 57.2% 431 / 753 CC 155 SCAR 48.8
CC
155
Cov
62%
process_args
229.9
include
210.0
option_groups
30.0
CRAP 132.3
↑ Summary Yath/Option.pm »
1 package Getopt::Yath::Instance;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '2.000007';
6  
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Getopt::Yath::Util qw/mod2file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Getopt::Yath::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Yath::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Getopt::Yath::Term qw/USE_COLOR color/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 use Getopt::Yath::HashBase qw{
16 <options <included
17 <posts
18 <stops
19  
20 <class
21  
22 +dedup
23  
24 +options_groups_cache
25 +options_map_cache
26 +cache_key
27  
28 category_sort_map
29 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub init {
32 1 ▶ my $self = shift;
Subroutine init: called CC=5 SCAR=17.2
Pod undocumented
33  
34 1 ▶ $self->{+OPTIONS} //= []; # List of option instances
Condition50%1/2
$self->{"options"} //= []
l!l
01
MC/DC0%0/1
$self->{"options"} //= []
$self->{"options"}
Truth table50%1/2
A
A= $self->{"options"}
Aresult
11
00
35 1 ▶ $self->{+POSTS} //= {}; # weight => {...}
Condition50%1/2
$self->{"posts"} //= {}
l!l
01
MC/DC0%0/1
$self->{"posts"} //= {}
$self->{"posts"}
Truth table50%1/2
A
A= $self->{"posts"}
Aresult
11
00
36 1 ▶ $self->{+INCLUDED} //= {}; # type => [$inst],
Condition50%1/2
$self->{"included"} //= {}
l!l
01
MC/DC0%0/1
$self->{"included"} //= {}
$self->{"included"}
Truth table50%1/2
A
A= $self->{"included"}
Aresult
11
00
37  
38 1 ▶ $self->{+CATEGORY_SORT_MAP} //= {'NO CATEGORY - FIX ME' => 99999};
Condition50%1/2
$self->{"category_sort_map"} //= {"NO CATEGORY - FIX ME", 99999}
l!l
01
MC/DC0%0/1
$self->{"category_sort_map"} //= {"NO CATEGORY - FIX ME", 99999}
$self->{"category_sort_map"}
Truth table50%1/2
A
A= $self->{"category_sort_map"}
Aresult
11
00
39  
40 1 $self->{+DEDUP} = {};
41 }
42  
43 sub add_option {
44 0 ▶ my $self = shift;
Subroutine add_option: not called CC=1 SCAR=6.9
Pod undocumented
45 0 my $option = Getopt::Yath::Option->create(trace => [caller()], @_);
46 0 return $self->_option($option);
47 }
48  
49 sub add_post_process {
50 0 ▶ my $self = shift;
Subroutine add_post_process: not called CC=1 SCAR=6.9
Pod undocumented
51 0 return $self->_post([caller()], @_);
52 }
53  
54 sub _post {
55 4 ▶ my $self = shift;
Subroutine _post: called CC=3 SCAR=11.2
56 4 my ($caller, $weight, $applicable, $cb) = @_;
57  
58 4 ▶ $weight //= 0;
Condition50%1/2
$weight //= 0
l!l
40
MC/DC0%0/1
$weight //= 0
$weight
Truth table50%1/2
A
A= $weight
Aresult
11
00
59  
60 4 ▶ return if $self->{+DEDUP}->{$cb}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dedup"}{$cb}++ 0 4 4
61 4 push @{$self->{+POSTS}->{$weight}} => {caller => $caller, weight => $weight, applicable => $applicable, callback => $cb};
62 }
63  
64 sub include {
65 0 ▶ my $self = shift;
Subroutine include: not called CC=14 SCAR=53.5
Pod undocumented
66 0 my ($other, $list) = @_;
67  
68 0 ▶ return unless $other;
Branch: 1 branch
BranchTrueFalseTotal
unless $other 0 0 0
69 0 ▶ return if $self->{+DEDUP}->{$other}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dedup"}{$other}++ 0 0 0
70  
71 0 ▶ push @{$self->included->{ref($other)} //= []} => $other;
Condition0%0/2
$self->included->{ref $other} //= []
l!l
00
MC/DC0%0/1
$self->included->{ref $other} //= []
$self->included->{ref $other}
Truth table0%0/2
A
A= $self->included->{ref $other}
Aresult
11
00
72  
73 0 ▶ if (my $other_include = $other->included) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $other_include = $other->included) 0 0 0
74 0 for my $key (keys %{$other_include}) {
75 0 ▶ push @{$self->included->{$key}} => @{$other_include->{$key} // []};
Condition0%0/2
$other_include->{$key} // []
l!l
00
MC/DC0%0/1
$other_include->{$key} // []
$other_include->{$key}
Truth table0%0/2
A
A= $other_include->{$key}
Aresult
11
00
76 }
77 }
78  
79 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
80 0 my %want = map {$_ => 1} @$list;
81 0 ▶ $self->_option($_) for grep { $want{$_->title} || $want{$_->field} || $want{$_->name} } @{$other->options};
Branch: 1 branch
BranchTrueFalseTotal
$want{$_->title} or $want{$_->field} or $want{$_->name} 0 0 0
Condition0%0/3
$want{$_->title} or $want{$_->field}
l!l&&r!l&&!r
000
MC/DC0%0/2
$want{$_->title} or $want{$_->field}
$want{$_->title} $want{$_->field}
Truth table0%0/3
A or B
A= $want{$_->title}B= $want{$_->field}
ABresult
1X1
011
000
82 }
83 else {
84 0 $self->_option($_) for @{$other->options};
85 }
86  
87 0 for my $set (values %{$other->posts}) {
88 0 for my $post (@$set) {
89 0 $self->_post(@{$post}{qw/caller weight applicable callback/});
90 }
91 }
92  
93 0 $self->clear_cache;
94 }
95  
96 sub _option {
97 15 ▶ my $self = shift;
Subroutine _option: called CC=3 SCAR=11.2
98 15 my ($option) = @_;
99  
100 15 ▶ my $options = $self->{+OPTIONS} //= []; # List of option instances
Condition50%1/2
$self->{"options"} //= []
l!l
150
MC/DC0%0/1
$self->{"options"} //= []
$self->{"options"}
Truth table50%1/2
A
A= $self->{"options"}
Aresult
11
00
101  
102 15 ▶ return if $self->{+DEDUP}->{$option}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dedup"}{$option}++ 0 15 15
103 15 push @{$options} => $option;
104 }
105  
106 sub clear_cache {
107 0 ▶ my $self = shift;
Subroutine clear_cache: not called CC=1 SCAR=6.9
Pod undocumented
108  
109 0 delete $self->{+OPTIONS_GROUPS_CACHE};
110 0 delete $self->{+OPTIONS_MAP_CACHE};
111 0 delete $self->{+CACHE_KEY};
112 }
113  
114 sub check_cache {
115 0 ▶ my $self = shift;
Subroutine check_cache: not called CC=3 SCAR=24.8
Pod undocumented
116  
117 0 my $options = $self->options;
118 0 my $new_key = @$options;
119 0 ▶ my $old_key = $self->{+CACHE_KEY} //= 0;
Condition0%0/2
$self->{"cache_key"} //= 0
l!l
00
MC/DC0%0/1
$self->{"cache_key"} //= 0
$self->{"cache_key"}
Truth table0%0/2
A
A= $self->{"cache_key"}
Aresult
11
00
120  
121 0 ▶ return 1 if $old_key == $new_key;
Branch: 1 branch
BranchTrueFalseTotal
if $old_key == $new_key 0 0 0
122  
123 0 $self->clear_cache();
124  
125 0 $self->{+CACHE_KEY} = $new_key;
126  
127 0 return 0;
128 }
129  
130 sub have_group {
131 0 ▶ my $self = shift;
Subroutine have_group: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my ($name) = @_;
133 0 ▶ return 1 if $self->option_groups->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->option_groups->{$name} 0 0 0
134 0 return 0;
135 }
136  
137 sub option_groups {
138 0 ▶ my $self = shift;
Subroutine option_groups: not called CC=5 SCAR=34.0
Pod undocumented
139 0 my ($in_options) = @_;
140  
141 0 my $options;
142 0 ▶ if ($in_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_options) { } 0 0 0
143 0 $options = $in_options;
144 }
145 else {
146 0 $options = $self->options;
147  
148 return $self->{+OPTIONS_GROUPS_CACHE}
149 0 ▶ if $self->{+OPTIONS_GROUPS_CACHE}
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options_groups_cache"} and $self->check_cache 0 0 0
Condition0%0/3
$self->{"options_groups_cache"} and $self->check_cache
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"options_groups_cache"} and $self->check_cache
$self->{"options_groups_cache"} $self->check_cache
Truth table0%0/3
A and B
A= $self->{"options_groups_cache"}B= $self->check_cache
ABresult
0X0
100
111
150 && $self->check_cache();
151 }
152  
153 0 my $groups = { map {($_->group() => 1)} @$options };
154  
155 0 ▶ return $groups if $in_options;
Branch: 1 branch
BranchTrueFalseTotal
if $in_options 0 0 0
156 0 return $self->{+OPTIONS_GROUPS_CACHE} = $groups;
157 }
158  
159 sub option_map {
160 64 ▶ my $self = shift;
Subroutine option_map: called CC=10 SCAR=31.1
Pod undocumented
161 64 my ($in_options) = @_;
162  
163 64 my $options;
164 64 ▶ if ($in_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_options) { } 64 0 64
165 64 $options = $in_options;
166 }
167 else {
168 0 $options = $self->options;
169  
170 return $self->{+OPTIONS_MAP_CACHE}
171 0 ▶ if $self->{+OPTIONS_MAP_CACHE}
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options_map_cache"} and $self->check_cache 0 0 0
Condition0%0/3
$self->{"options_map_cache"} and $self->check_cache
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"options_map_cache"} and $self->check_cache
$self->{"options_map_cache"} $self->check_cache
Truth table0%0/3
A and B
A= $self->{"options_map_cache"}B= $self->check_cache
ABresult
0X0
100
111
172 && $self->check_cache();
173 }
174  
175 64 my $map = {
176 custom_match => [],
177 # --whatever => $option
178 };
179  
180 64 for my $option (@$options) {
181 452 ▶ push @{$map->{custom_match}} => $option->custom_matches
Branch: 1 branch
BranchTrueFalseTotal
if $option->can("custom_matches") 0 452 452
182 if $option->can('custom_matches');
183  
184 452 for my $form (keys %{$option->forms}) {
185 1435 ▶ if (my $existing = $map->{$form}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing = $map->{$form}) 0 1435 1435
186 0 ▶ croak "Option form '$form' defined twice, first in '" . $existing->trace_string . "' and again in '" . $option->trace_string . "'" if $existing ne $option;
Branch: 1 branch
BranchTrueFalseTotal
if $existing ne $option 0 0 0
187 0 next;
188 }
189  
190 1435 $map->{$form} = $option;
191 }
192 }
193  
194 64 ▶ return $map if $in_options;
Branch: 1 branch
BranchTrueFalseTotal
if $in_options 64 0 64
195 0 return $self->{+OPTIONS_MAP_CACHE} = $map;
196 }
197  
198 sub process_args {
199 64 ▶ my $self = shift;
Subroutine process_args: called CC=88 SCAR=54.4
Pod undocumented
200 64 my ($args, %params) = @_;
201  
202 64 ▶ croak "Must provide an argv arrayref" unless $args && ref($args) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $args and ref $args eq "ARRAY" 0 64 64
Condition33%1/3
$args and ref $args eq "ARRAY"
!ll&&!rl&&r
0064
MC/DC0%0/2
$args and ref $args eq "ARRAY"
$args ref $args eq "ARRAY"
Truth table33%1/3
A and B
A= $argsB= ref $args eq "ARRAY"
ABresult
0X0
100
111
203  
204 64 my $argv = [@$args]; # Make a copy
205  
206 64 ▶ my $settings = $params{settings} // Getopt::Yath::Settings->new({});
Condition33%1/3
$params{"settings"} // "Getopt::Yath::Settings"->new({})
l!l&&r!l&&!r
0640
MC/DC0%0/2
$params{"settings"} // "Getopt::Yath::Settings"->new({})
$params{"settings"} "Getopt::Yath::Settings"->new({})
Truth table33%1/3
A // B
A= $params{"settings"}B= "Getopt::Yath::Settings"->new({})
ABresult
1X1
011
000
207 64 ▶ my $stops = $params{stops} // [];
Condition100%2/2
$params{"stops"} // []
l!l
163
MC/DC100%1/1
$params{"stops"} // []
$params{"stops"}
Truth table100%2/2
A
A= $params{"stops"}
Aresult
11
00
208 64 ▶ my $groups = $params{groups} // {};
Condition50%1/2
$params{"groups"} // {}
l!l
064
MC/DC0%0/1
$params{"groups"} // {}
$params{"groups"}
Truth table50%1/2
A
A= $params{"groups"}
Aresult
11
00
209 64 ▶ $stops = { map { ($_ => 1) } @$stops } if $stops && ref($stops) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $stops and ref $stops eq "ARRAY" 64 0 64
Condition33%1/3
$stops and ref $stops eq "ARRAY"
!ll&&!rl&&r
0064
MC/DC0%0/2
$stops and ref $stops eq "ARRAY"
$stops ref $stops eq "ARRAY"
Truth table33%1/3
A and B
A= $stopsB= ref $stops eq "ARRAY"
ABresult
0X0
100
111
210  
211 64 ▶ my $options = [ grep { $_->is_applicable($self, $settings) } @{$self->options // []} ];
Condition50%1/2
$self->options // []
l!l
640
MC/DC0%0/1
$self->options // []
$self->options
Truth table50%1/2
A
A= $self->options
Aresult
11
00
212  
213 64 my @skip;
214 my $state = {
215 settings => $settings,
216 skipped => \@skip,
217 remains => $argv,
218 env => $params{env} // {},
219 cleared => $params{cleared} // {},
220 modules => $params{modules} // {},
221 64 ▶ stop => undef,
Condition50%3/6
$params{"env"} // {}
l!l
064
$params{"cleared"} // {}
l!l
064
$params{"modules"} // {}
l!l
064
MC/DC0%0/1
$params{"env"} // {}
$params{"env"}
MC/DC0%0/1
$params{"cleared"} // {}
$params{"cleared"}
MC/DC0%0/1
$params{"modules"} // {}
$params{"modules"}
Truth table50%1/2
A
A= $params{"env"}
Aresult
11
00
Truth table50%1/2
A
A= $params{"cleared"}
Aresult
11
00
Truth table50%1/2
A
A= $params{"modules"}
Aresult
11
00
222 };
223  
224 64 for my $opt (@$options) {
225 475 my $group = $settings->group($opt->group, 1);
226 475 my $ref = $group->option_ref($opt->field, 1);
227 475 ▶ unless(defined ${$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$ref;}) 475 0 475
228 475 my $val = $opt->get_initial_value($settings);
229 475 my $rt = ref($val);
230 475 ▶ if (!defined($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 329 146 475
elsif ($rt) { } 86 60 146
231 329 $val = [];
232 }
233 elsif ($rt) {
234 86 ▶ $val = [ $rt eq 'ARRAY' ? @$val : %$val ];
Branch: 1 branch
BranchTrueFalseTotal
$rt eq 'ARRAY' ? : 32 54 86
235 }
236 else {
237 60 $val = [$val];
238 }
239 475 $opt->trigger(action => 'initialize', ref => $ref, val => $val, state => $state, options => $self, settings => $settings, group => $group);
240 475 $opt->add_value($ref, @$val);
241 }
242  
243 475 $opt->init_settings($state, $settings, $group, $ref);
244 }
245  
246 64 ▶ my $invalid = $params{invalid_opt_callback} // sub { die "'$_[0]' is not a valid option.\n" };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$params{"invalid_opt_callback"} // sub { die "'$_[0]' is not a valid option.\n"; }
l!l&&r!l&&!r
0640
MC/DC0%0/2
$params{"invalid_opt_callback"} // sub { die "'$_[0]' is not a valid option.\n"; }
$params{"invalid_opt_callback"} sub { die "'$_[0]' is not a valid option.\n"; }
Truth table33%1/3
A // B
A= $params{"invalid_opt_callback"}B= sub { die "'$_[0]' is not a valid option.\n"; }
ABresult
1X1
011
000
247  
248 64 my $parse_group;
249 $parse_group = sub {
250 0 ▶ my $end = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
251  
252 0 my $group = [];
253 0 ▶ while (@$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) 0 0 0
254 0 my $arg = shift(@$argv);
255 0 ▶ return $group if $arg eq $end;
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq $end 0 0 0
256  
257 0 ▶ if (my $nest = $groups->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nest = $groups->{$arg}) 0 0 0
258 0 $arg = $parse_group->($nest);
259 }
260  
261 0 push @$group => $arg;
262 }
263  
264 0 die "Could not find end token '$end' before end of arguments.\n";
265 64 };
266  
267 64 ▶ while (@$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) 64 58 122
268 64 my $map = $self->option_map($options);
269 64 my $base = shift @$argv;
270  
271 64 ▶ if (my $end = $groups->{$base}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $end = $groups->{$base}) 0 64 64
272 0 push @skip => $parse_group->($end);
273 0 next;
274 }
275  
276 64 ▶ if ($stops->{$base}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stops->{$base}) 1 63 64
277 1 $state->{stop} = $base;
278 1 last;
279 }
280  
281 63 ▶ if ($base !~ m/^-/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base =~ /^-/) 3 60 63
282 3 ▶ if ($params{stop_at_non_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"stop_at_non_opts"}) 0 3 3
283 0 $state->{stop} = $base;
284 0 last;
285 }
286  
287 3 ▶ if ($params{skip_non_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"skip_non_opts"}) 1 2 3
288 1 push @skip => $base;
289 1 next;
290 }
291  
292 2 $invalid->($base);
293 }
294  
295 60 my ($first, $set, $arg, $opt, $delta);
296  
297 60 ▶ if ($base =~ m/^(-[^-])(=?)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ /^(-[^-])(=?)(.*)$/) { } 33 27 60
298 33 my ($other, $eq);
299 33 ($first, $set, $other) = ($1, $2, $3);
300  
301 33 ▶ if ($opt = $map->{$first}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt = $map->{$first}) 33 0 33
302 33 ▶ if ($opt->allows_shortval && ($set || $other)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt->allows_shortval and $set || $other) { } 10 23 33
elsif ($set) { } 4 19 23
Condition100%6/6
$opt->allows_shortval and $set || $other
!ll&&!rl&&r
121110
$set or $other
l!l&&r!l&&!r
8211
MC/DC100%3/3
$opt->allows_shortval and $set || $other
$opt->allows_shortval $set $other
Truth table100%4/4
A and B or C
A= $opt->allows_shortvalB= $setC= $other
ABCresult
0XX0
1000
11X1
1011
303 10 $set = 1;
304 10 $arg = $other;
305 }
306 elsif ($set) {
307 4 $arg = $other;
308 }
309 else {
310 19 ▶ unshift @$argv => "-$other" if $other;
Branch: 1 branch
BranchTrueFalseTotal
if $other 3 16 19
311 }
312 }
313 }
314 else {
315 27 ($first, $set, $arg) = split(/(=)/, $base, 2);
316 27 $opt = $map->{$first};
317 }
318  
319 60 ▶ unless ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt) 2 58 60
320 2 ▶ if (my $list = $map->{custom_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $list = $map->{"custom_match"}) 2 0 2
321 2 for my $match (@$list) {
322 0 ($opt, $delta, $arg) = $match->($base, $state);
323 0 ▶ next unless $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt 0 0 0
324 0 $set = 1;
325 0 last;
326 }
327 }
328 }
329  
330 60 ▶ die "Use of 'arg=val' form without a value is not valid in option '$base'.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $set and not defined $arg 0 60 60
Condition66%2/3
$set and not defined $arg
!ll&&!rl&&r
43170
MC/DC0%0/2
$set and not defined $arg
$set not defined $arg
Truth table66%2/3
A and not(B)
A= $setB= not defined $arg
ABresult
0X0
100
111
331 if $set && !defined($arg);
332  
333 60 ▶ unless ($opt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt) 2 58 60
334 2 ▶ if ($params{skip_invalid_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"skip_invalid_opts"}) 1 1 2
335 1 push @skip => $base;
336 1 next;
337 }
338  
339 1 ▶ if ($params{stop_at_invalid_opts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"stop_at_invalid_opts"}) 0 1 1
340 0 $state->{stop} = $base;
341 0 last;
342 }
343  
344 1 $invalid->($base);
345 }
346  
347 58 ▶ die "Use of 'arg=val' form is not allowed in option '$base'. Arguments are not allowed for this option type.\n"
Branch: 1 branch
BranchTrueFalseTotal
if $set and not $opt->allows_arg 1 57 58
Condition100%3/3
$set and not $opt->allows_arg
!ll&&!rl&&r
41161
MC/DC100%2/2
$set and not $opt->allows_arg
$set not $opt->allows_arg
Truth table100%3/3
A and not(B)
A= $setB= not $opt->allows_arg
ABresult
0X0
100
111
348 if $set && !$opt->allows_arg;
349  
350 57 ▶ $delta //= $opt->forms->{$first};
Condition33%1/3
$delta //= $opt->forms->{$first}
l!l&&r!l&&!r
0570
MC/DC0%0/2
$delta //= $opt->forms->{$first}
$delta $opt->forms->{$first}
Truth table33%1/3
A //= B
A= $deltaB= $opt->forms->{$first}
ABresult
1X1
011
000
351  
352 57 ▶ $state->{modules}->{$opt->module}++ unless $opt->no_module;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->no_module 1 56 57
353  
354 57 my $group_name = $opt->group;
355 57 my $field_name = $opt->field;
356 57 my $group = $settings->group($group_name, 1);
357 57 my $ref = $group->option_ref($field_name, 1);
358  
359 57 ▶ if ($delta < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 0) 12 45 57
360 12 $opt->clear_field($ref);
361 12 $opt->trigger(action => 'clear', ref => $ref, val => undef, state => $state, options => $self, settings => $settings, group => $group);
362 12 $state->{cleared}->{$group_name}->{$field_name} = 1;
363 12 ▶ next unless $set;
Branch: 1 branch
BranchTrueFalseTotal
unless $set 12 0 12
364 }
365  
366 45 ▶ delete $state->{cleared}->{$group_name}->{$field_name} if $state->{cleared}->{$group_name};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cleared"}{$group_name} 6 39 45
367  
368 45 ▶ if ($opt->requires_arg && !$set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->requires_arg and not $set) 9 36 45
Condition100%3/3
$opt->requires_arg and not $set
!ll&&!rl&&r
3159
MC/DC100%2/2
$opt->requires_arg and not $set
$opt->requires_arg not $set
Truth table100%3/3
A and not(B)
A= $opt->requires_argB= not $set
ABresult
0X0
100
111
369 9 ▶ die "No argument provided to '$first'.\n" unless @$argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @$argv 1 8 9
370 8 $arg = shift(@$argv);
371 }
372  
373 44 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 23 21 44
374 23 ▶ if (my $end = $groups->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $end = $groups->{$arg}) 0 23 23
375 0 $arg = $parse_group->($end);
376 }
377 }
378  
379 44 ▶ if (ref($arg) && @$arg > 1 && !$opt->allows_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg and @$arg > 1 and not $opt->allows_list) 0 44 44
Condition33%2/6
ref $arg and @$arg > 1 and not $opt->allows_list
!ll&&!rl&&r
4400
ref $arg and @$arg > 1
!ll&&!rl&&r
4400
MC/DC0%0/3
ref $arg and @$arg > 1 and not $opt->allows_list
ref $arg @$arg > 1 not $opt->allows_list
Truth table25%1/4
A and B and not(C)
A= ref $argB= @$arg > 1C= not $opt->allows_list
ABCresult
0XX0
10X0
1100
1111
380 0 die "Option '$first' cannot take multiple values, got: [" . join(', ' => @$arg) . "].\n";
381 }
382  
383 44 my $from = '';
384 44 my @val;
385 44 ▶ if (defined $arg) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $arg) { } 24 20 44
elsif ($opt->allows_autofill) { } 7 13 20
386 24 $from = 'arg';
387 24 ▶ @val = $opt->normalize_value(ref($arg) ? @$arg : $arg);
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 0 24 24
388 }
389 elsif ($opt->allows_autofill) {
390 7 $from = 'autofill';
391 7 @val = $opt->get_autofill_value($settings);
392 }
393 else {
394 13 $from = 'no_arg';
395 13 @val = $opt->no_arg_value($settings);
396 }
397  
398 44 ▶ if ($opt->mod_adds_options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->mod_adds_options) 0 44 44
399 0 my ($class) = @val;
400 0 require(mod2file($class));
401 0 ▶ if ($class->can('options')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can("options")) 0 0 0
402 0 ▶ if (my $add = $class->options) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $add = $class->options) 0 0 0
403 0 $self->include($add);
404 }
405 }
406 }
407  
408 44 $opt->trigger(action => 'set', ref => $ref, val => \@val, state => $state, options => $self, settings => $settings, group => $group, set_from => $from);
409 44 my @bad = $opt->check_value(\@val);
410 44 ▶ if (@bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) 0 44 44
411 0 ▶ die "Invalid value(s) for option '$first': " . join(', ' => map {defined($_) ? "'$_'" : 'undef' } @bad) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
412 }
413 44 $opt->add_value($ref, @val);
414 }
415  
416 59 for my $opt (@$options) {
417 447 my $group_name = $opt->group;
418 447 my $field_name = $opt->field;
419 447 my $group = $settings->group($group_name, 1);
420 447 my $ref = $group->option_ref($field_name, 1);
421  
422 # Do not set the default if the --no-OPT form was used.
423 447 ▶ next if $state->{cleared} && $state->{cleared}->{$group_name} && $state->{cleared}->{$group_name}->{$field_name};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"cleared"} and $state->{"cleared"}{$group_name} and $state->{"cleared"}{$group_name}{$field_name} 10 437 447
Condition83%5/6
$state->{"cleared"} and $state->{"cleared"}{$group_name} and $state->{"cleared"}{$group_name}{$field_name}
!ll&&!rl&&r
3756210
$state->{"cleared"} and $state->{"cleared"}{$group_name}
!ll&&!rl&&r
037572
MC/DC66%2/3
$state->{"cleared"} and $state->{"cleared"}{$group_name} and $state->{"cleared"}{$group_name}{$field_name}
$state->{"cleared"} $state->{"cleared"}{$group_name} $state->{"cleared"}{$group_name}{$field_name}
Truth table75%3/4
A and B and C
A= $state->{"cleared"}B= $state->{"cleared"}{$group_name}C= $state->{"cleared"}{$group_name}{$field_name}
ABCresult
0XX0
10X0
1100
1111
424 437 ▶ next if $opt->is_populated($ref);
Branch: 1 branch
BranchTrueFalseTotal
if $opt->is_populated($ref) 83 354 437
425 354 $opt->add_value($ref, $opt->get_default_value($settings));
426 }
427  
428 59 ▶ unless ($params{skip_posts}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"skip_posts"}) 59 0 59
429 59 for my $weight (sort { $a <=> $b } keys %{$self->{+POSTS}}) {
430 9 for my $set (@{$self->{+POSTS}->{$weight}}) {
431 12 ▶ next if $set->{applicable} && !$set->{applicable}->($set, $self, $settings);
Branch: 1 branch
BranchTrueFalseTotal
if $set->{"applicable"} and not $set->{"applicable"}->($set, $self, $settings) 0 12 12
Condition33%1/3
$set->{"applicable"} and not $set->{"applicable"}->($set, $self, $settings)
!ll&&!rl&&r
1200
MC/DC0%0/2
$set->{"applicable"} and not $set->{"applicable"}->($set, $self, $settings)
$set->{"applicable"} not $set->{"applicable"}->($set, $self, $settings)
Truth table33%1/3
A and not(B)
A= $set->{"applicable"}B= not $set->{"applicable"}->($set, $self, $settings)
ABresult
0X0
100
111
432 12 $set->{callback}->($self, $state);
433 }
434 }
435 }
436  
437 59 for my $opt (@$options) {
438 447 my $group = $settings->group($opt->group, 1);
439 447 my $ref = $group->option_ref($opt->field, 1);
440  
441 447 ▶ for my $env (@{$opt->clear_env_vars // []}) {
Condition100%2/2
$opt->clear_env_vars // []
l!l
12435
MC/DC100%1/1
$opt->clear_env_vars // []
$opt->clear_env_vars
Truth table100%2/2
A
A= $opt->clear_env_vars
Aresult
11
00
442 12 $state->{env}->{$env} = undef;
443 12 ▶ delete $ENV{$env} unless $params{no_set_env};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"no_set_env"} 12 0 12
444 }
445  
446 447 $opt->finalize_settings($state, $settings, $group, $ref);
447  
448 447 ▶ next unless $opt->can_set_env;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->can_set_env 80 367 447
449  
450 367 ▶ my $to_set = $opt->set_env_vars or next;
Branch: 1 branch
BranchTrueFalseTotal
my $to_set = $opt->set_env_vars or next 355 12 367
451 12 ▶ next unless @$to_set;
Branch: 1 branch
BranchTrueFalseTotal
unless @$to_set 0 12 12
452  
453 12 ▶ next unless $opt->is_populated($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $opt->is_populated($ref) 3 9 12
454  
455 9 for my $name (@$to_set) {
456 9 my $env = "$name";
457 9 $env =~ s/^(!)//;
458 9 my $neg = $1;
459 9 ▶ my @val = $opt->get_env_value($env => $ref) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@val) = $opt->get_env_value($env, $ref) or next 0 9 9
460 9 ▶ if (@val > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@val > 1) 0 9 9
461 0 my $title = $opt->title;
462 0 ▶ my $trace = $opt->trace // ['', 'unknown', 'n/a'];
Condition0%0/2
$opt->trace // ["", "unknown", "n/a"]
l!l
00
MC/DC0%0/1
$opt->trace // ["", "unknown", "n/a"]
$opt->trace
Truth table0%0/2
A
A= $opt->trace
Aresult
11
00
463 0 die "Option '$title' defined in $trace->[1] line $trace->[2] returned more than one value when get_env_value($env) was called.\n";
464 }
465  
466 9 my $setval = $val[0];
467 9 ▶ $setval = $setval ? 0 : 1 if $neg;
Branch: 2 branches
BranchTrueFalseTotal
if $neg 2 7 9
$setval ? : 1 1 2
468  
469 9 $state->{env}->{$env} = $val[0];
470 9 ▶ $ENV{$env} = $val[0] unless $params{no_set_env};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"no_set_env"} 9 0 9
471 }
472 }
473  
474 59 return $state;
475 }
476  
477 my %DOC_FORMATS = (
478 'cli' => [
479 'cli_docs', # Method to call on opt
480 "\n", # how to join lines
481 1 ▶ sub { $_[4] ? "\n" . color('bold underline white') . $_[1] . color('reset') . " ($_[3])" : "\n$_[1] ($_[3])" }, # how to render the category
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[4] ? : 0 1 1
482 14 ▶ sub { $_[0] =~ s/^/ /mg; "$_[0]\n" }, # transform the value from the opt
Subroutine __ANON__: called CC=1 SCAR=0.0
483 ▶ sub { }, # add this at the end
Subroutine __ANON__: called CC=1 SCAR=0.0
484 ],
485 'pod' => [
486 'pod_docs', # Method to call on opt
487 "\n\n", # how to join lines
488 1 ▶ sub { ($_[0] ? ("=back") : (), "=head$_[2] $_[1]", "=over 4") }, # how to render the category
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 1 1
489 14 ▶ sub { $_[0] }, # transform the value from the opt
Subroutine __ANON__: called CC=1 SCAR=0.0
490 1 ▶ sub { $_[0] ? ("=back\n") : () }, # add this at the end
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 1 0 1
491 1 ],
492 );
493  
494 sub docs {
495 2 ▶ my $self = shift;
Subroutine docs: called CC=14 SCAR=28.0
Pod undocumented
496 2 my ($format, %params) = @_;
497  
498 2 ▶ $params{color} //= USE_COLOR() && -t STDOUT;
Condition33%1/3
$params{"color"} //= -t STDOUT
l!l&&r!l&&!r
020
MC/DC0%0/2
$params{"color"} //= -t STDOUT
$params{"color"} -t STDOUT
Truth table33%1/3
A //= B
A= $params{"color"}B= -t STDOUT
ABresult
1X1
011
000
499  
500 2 my $settings = $params{settings};
501 2 ▶ my $opts = [ grep { $params{applicable} || $_->is_applicable($self, $settings) } @{$self->options // []} ];
Branch: 1 branch
BranchTrueFalseTotal
$params{"applicable"} or $_->is_applicable($self, $settings) 28 0 28
Condition50%1/2
$self->options // []
l!l
20
MC/DC0%0/1
$self->options // []
$self->options
Truth table50%1/2
A
A= $self->options
Aresult
11
00
502  
503 2 ▶ $format //= "UNDEFINED";
Condition50%1/2
$format //= "UNDEFINED"
l!l
20
MC/DC0%0/1
$format //= "UNDEFINED"
$format
Truth table50%1/2
A
A= $format
Aresult
11
00
504 2 ▶ my $fset = $DOC_FORMATS{$format} or croak "Invalid documentation format '$format'";
Branch: 1 branch
BranchTrueFalseTotal
my $fset = $DOC_FORMATS{$format} or croak("Invalid documentation format '${format}'") 0 2 2
505 2 my ($fmeth, $join, $fcat, $ftrans, $fend) = @$fset;
506  
507 2 ▶ return unless $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts 0 2 2
508 2 ▶ return unless @$opts;
Branch: 1 branch
BranchTrueFalseTotal
unless @$opts 0 2 2
509  
510 2 my @render = @$opts;
511  
512 2 ▶ @render = grep { $_->group eq $params{group} } @render if $params{group};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"group"} 0 2 2
513  
514 2 ▶ return "\n\n!! Invalid option group: $params{group} !!"
Branch: 1 branch
BranchTrueFalseTotal
unless @render 0 2 2
515 unless @render;
516  
517 2 @render = sort { $self->doc_sort_ops($a, $b) } @render;
518  
519 2 my @out;
520  
521 my $cat;
522 2 for my $opt (@render) {
523 28 ▶ if (!$cat || $opt->category ne $cat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cat or $opt->category ne $cat) 2 26 28
Condition66%2/3
not $cat or $opt->category ne $cat
l!l&&r!l&&!r
2026
MC/DC50%1/2
not $cat or $opt->category ne $cat
not $cat $opt->category ne $cat
Truth table66%2/3
not(A) or B
A= not $catB= $opt->category ne $cat
ABresult
1X1
011
000
524 2 push @out => $fcat->($cat, $opt->category, $params{head}, $opt->group, $params{color});
525 2 $cat = $opt->category;
526 }
527  
528 28 my $help = $opt->$fmeth(%params);
529 28 push @out => $ftrans->($help);
530 }
531  
532 2 push @out => $fend->($cat);
533 2 s/[ \t]+$//gm for @out;
534  
535 2 return join $join => @out;
536 }
537  
538 sub doc_sort_ops {
539 74 ▶ my $self = shift;
Subroutine doc_sort_ops: called CC=12 SCAR=34.0
Pod undocumented
540 74 my ($a, $b, %params) = @_;
541  
542 74 my $map = $self->{+CATEGORY_SORT_MAP};
543 74 ▶ my $aw = $map->{$a->category} || 0;
Condition50%1/2
$$map{$a->category} || 0
l!l
074
MC/DC0%0/1
$$map{$a->category} || 0
$$map{$a->category}
Truth table50%1/2
A
A= $$map{$a->category}
Aresult
11
00
544 74 ▶ my $bw = $map->{$b->category} || 0;
Condition50%1/2
$$map{$b->category} || 0
l!l
074
MC/DC0%0/1
$$map{$b->category} || 0
$$map{$b->category}
Truth table50%1/2
A
A= $$map{$b->category}
Aresult
11
00
545  
546 74 my $ret = $aw <=> $bw;
547 74 ▶ if ($params{group_first}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'group_first'}) { } 0 74 74
548 0 ▶ $ret ||= $a->group cmp $b->group;
Condition0%0/3
$ret ||= $a->group cmp $b->group
l!l&&r!l&&!r
000
MC/DC0%0/2
$ret ||= $a->group cmp $b->group
$ret $a->group cmp $b->group
Truth table0%0/3
A ||= B
A= $retB= $a->group cmp $b->group
ABresult
1X1
011
000
549 0 ▶ $ret ||= $a->category cmp $b->category;
Condition0%0/3
$ret ||= $a->category cmp $b->category
l!l&&r!l&&!r
000
MC/DC0%0/2
$ret ||= $a->category cmp $b->category
$ret $a->category cmp $b->category
Truth table0%0/3
A ||= B
A= $retB= $a->category cmp $b->category
ABresult
1X1
011
000
550 }
551 else {
552 74 ▶ $ret ||= $a->category cmp $b->category;
Condition33%1/3
$ret ||= $a->category cmp $b->category
l!l&&r!l&&!r
0074
MC/DC0%0/2
$ret ||= $a->category cmp $b->category
$ret $a->category cmp $b->category
Truth table33%1/3
A ||= B
A= $retB= $a->category cmp $b->category
ABresult
1X1
011
000
553 74 ▶ $ret ||= $a->group cmp $b->group;
Condition33%1/3
$ret ||= $a->group cmp $b->group
l!l&&r!l&&!r
0074
MC/DC0%0/2
$ret ||= $a->group cmp $b->group
$ret $a->group cmp $b->group
Truth table33%1/3
A ||= B
A= $retB= $a->group cmp $b->group
ABresult
1X1
011
000
554 }
555 74 ▶ $ret ||= ($a->prefix || '') cmp ($b->prefix || '');
Condition42%3/7
$ret ||= ($a->prefix || "") cmp ($b->prefix || "")
l!l&&r!l&&!r
0074
$a->prefix || ''
l!l
074
$b->prefix || ''
l!l
074
MC/DC0%0/2
$ret ||= ($a->prefix || "") cmp ($b->prefix || "")
$ret $a->prefix
MC/DC0%0/1
$b->prefix || ''
$b->prefix
Truth table33%1/3
A ||= B
A= $retB= $a->prefix
ABresult
1X1
011
000
Truth table50%1/2
A
A= $b->prefix
Aresult
11
00
556 74 ▶ $ret ||= $a->name cmp $b->name;
Condition33%1/3
$ret ||= $a->name cmp $b->name
l!l&&r!l&&!r
0740
MC/DC0%0/2
$ret ||= $a->name cmp $b->name
$ret $a->name cmp $b->name
Truth table33%1/3
A ||= B
A= $retB= $a->name cmp $b->name
ABresult
1X1
011
000
557  
558 74 return $ret;
559 }
560  
561 1 1;
562  
563 __END__
↑ Summary Yath/Option.pm »