blib/lib/Getopt/O2.pm

filter: Statementstmt 95.2% 260 / 273 Branchbran 93.2% 138 / 148 Conditioncond 82.8% 29 / 35 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 100% 32 / 32 Podpod 41.6% 5 / 12 totaltotal 91.3% 474 / 519 SCAR 45.7
CC
94
Cov
94%
getopt
39.1
getValue
14.3
parseRules
13.0
CRAP 96.3
↑ Summary Build »
1 ##------------------------------------------------------------------------------
2 ## $Id: O2.pm 887 2016-08-29 12:57:34Z schieche $
3 ##------------------------------------------------------------------------------
4 package Getopt::O2;
5
6 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 2 ▶no if $] >= 5.017011, warnings => 'experimental::smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 2 our $VERSION = '1.0.20';
13 ##------------------------------------------------------------------------------
14 2 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 Readonly my $USAGE_MARGIN => 80;
17 2 Readonly my $USAGE_OPTIONS_LENGTH => 29;
18
19 2 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 ##------------------------------------------------------------------------------
22 sub new
23 {
24 27 ▶ my $class = ref $_[0] ? ref $_[0] : $_[0];
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 26 27
25 27 my $self = bless {
26 shortOptions => {},
27 longOptions => {},
28 options => {}
29 }, $class;
30
31 27 return $self
32 }
33 ##------------------------------------------------------------------------------
34 sub getopt ## no critic (Subroutines::ProhibitExcessComplexity)
35 {
36 27 ▶ my $self = shift;
Subroutine getopt: called CC=39 SCAR=36.6
Pod documented
37 27 my $dest = shift;
38 27 my $args = shift;
39 27 my ($arg,$key,$rule,%context,@arguments);
40
41 27 $self->{'options'} = {%$dest};
42 27 $self->parseRules();
43
44 22 ▶ PROCESS_ARGUMENTS: while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 46 8 54
45 46 $arg = shift @ARGV;
46
47 46 ▶ if (!defined $arg || !length $arg || '-' eq $arg || $arg !~ /^-/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $arg && length $arg or '-' eq $arg or not $arg =~ /^-/) { } 5 41 46
elsif ('--' eq $arg) { } 2 39 41
Condition100%9/9
not defined $arg && length $arg or "-" eq $arg or not $arg =~ /^-/
l!l&&r!l&&!r
3241
not defined $arg && length $arg or "-" eq $arg
l!l&&r!l&&!r
2143
defined $arg && length $arg
!ll&&!rl&&r
1144
MC/DC100%4/4
not defined $arg && length $arg or "-" eq $arg or not $arg =~ /^-/
defined $arg length $arg "-" eq $arg not $arg =~ /^-/
Truth table100%5/5
not(A && B) or C or not(D)
A= defined $argB= length $argC= "-" eq $argD= not $arg =~ /^-/
ABCDresult
0XXX1
10XX1
111X1
11011
11000
48 5 push @arguments, $arg;
49 5 next PROCESS_ARGUMENTS;
50 } elsif ('--' eq $arg) {
51 2 push @arguments, @ARGV;
52 2 last PROCESS_ARGUMENTS;
53 }
54
55 39 ▶ if ($arg !~ /^--/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $arg =~ /^--/) { } 14 25 39
56 14 $key = (substr $arg, 1, 1);
57 14 $rule = $self->{'shortOptions'}->{$key};
58 14 ▶ $self->error('No such option "-%s"', $key)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 1 13 14
59 unless defined $rule;
60 13 $rule = $self->{'longOptions'}->{$rule};
61
62 13 ▶ if (length $arg > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $arg > 2) 5 8 13
63 5 ▶ if ($rule->type) { ## no critic (ControlStructures::ProhibitDeepNests)
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->type) { } 1 4 5
64 1 unshift @ARGV, (substr $arg, 2);
65 } else {
66 4 unshift @ARGV, '-'.(substr $arg, 2);
67 }
68 }
69 } else {
70 25 $key = (substr $arg, 2);
71
72 25 ▶ if (~(index $key, '=')) {
Branch: 1 branch
BranchTrueFalseTotal
if (~index($key, "=")) 1 24 25
73 1 ($key,$arg) = (split /=/, $key, 2);
74 1 unshift @ARGV, $arg;
75 }
76
77 25 $rule = $self->{'longOptions'}->{$key};
78 25 ▶ unless (defined $rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule) 5 20 25
79 5 ▶ $self->error('No such option "--%s"', $key)
Branch: 1 branch
BranchTrueFalseTotal
if 0 != index($key, "no-") 2 3 5
80 if 0 != (index $key, 'no-');
81 3 $key = (substr $key, 3);
82 3 $rule = $self->{'longOptions'}->{$key};
83
84 3 ▶ $self->error('No such option "--no-%s" or negatable "--%s"', $key, $key)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule and $rule->negatable 2 1 3
Condition100%3/3
defined $rule and $rule->negatable
!ll&&!rl&&r
111
MC/DC100%2/2
defined $rule and $rule->negatable
defined $rule $rule->negatable
Truth table100%3/3
A and B
A= defined $ruleB= $rule->negatable
ABresult
0X0
100
111
85 unless defined $rule && $rule->negatable;
86 1 $rule->{'_negate'} = 1;
87 }
88 }
89
90 34 ▶ if (defined $rule->context) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->context) 1 33 34
91 1 foreach (@{$rule->context->{'need'}}) {
92 $self->error('Option "--%s" cannot be used in this context.', $rule->long)
93 1 ▶ unless exists $context{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $context{$_} 1 0 1
94 }
95
96 0 delete $context{$_} foreach @{$rule->context->{'clear'}};
97 0 $context{$_} = 1 foreach @{$rule->context->{'set'}};
98 }
99
100 33 ▶ if ($rule->multiple) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rule->multiple) { } 3 30 33
elsif (not defined $rule->type) { } 6 24 30
101 $self->{'options'}->{$rule->long} = 0
102 3 ▶ unless exists $self->{'options'}->{$rule->long};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"options"}{$rule->long} 1 2 3
103 3 ++$self->{'options'}->{$rule->long};
104 3 next PROCESS_ARGUMENTS;
105 } elsif(!defined $rule->type) {
106 6 $arg = undef;
107 } else {
108 24 $arg = $self->getValue();
109 24 ▶ $self->error('Option "--%s" needs a mandatory value.', $rule->long)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 2 22 24
110 unless defined $arg;
111
112 22 ▶ delete $self->{'options'}->{$rule->long}
Branch: 1 branch
BranchTrueFalseTotal
if $rule->is_unused 14 8 22
113 if $rule->is_unused;
114 22 $rule->mark_used;
115
116 $self->{'options'}->{$rule->long} = []
117 22 ▶ if $rule->is_list && !defined $self->{'options'}->{$rule->long};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->is_list and not defined $self->{"options"}{$rule->long} 5 17 22
Condition100%3/3
$rule->is_list and not defined $self->{"options"}{$rule->long}
!ll&&!rl&&r
985
MC/DC100%2/2
$rule->is_list and not defined $self->{"options"}{$rule->long}
$rule->is_list not defined $self->{"options"}{$rule->long}
Truth table100%3/3
A and not(B)
A= $rule->is_listB= not defined $self->{"options"}{$rule->long}
ABresult
0X0
100
111
118
119 22 given($rule->type) {
120 22 when('s') {
121 }
122
123 11 when('i') {
124 2 ▶ $self->error('Argument "%s" to "--%s" isn\'t numeric', $arg, $rule->long)
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($arg) 1 1 2
125 unless looks_like_number($arg);
126 1 $arg = int $arg;
127 }
128
129 9 when('?') {
130 $self->error('Value "%s" to argument "--%s" is invalid.', $arg, $rule->long)
131 9 ▶ unless $arg ~~ @{$rule->values || []};
Branch: 2 branches
BranchTrueFalseTotal
unless $arg ~~ \@{$rule->values or [];} 2 7 9
$rule->values or [] 1 8 9
132 }
133 }
134
135 19 ▶ if ($rule->is_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->is_list) 13 6 19
136 13 ▶ if ('?' ne $rule->type) { ## no critic (ControlStructures::ProhibitDeepNests)
Branch: 1 branch
BranchTrueFalseTotal
if ('?' ne $rule->type) { } 7 6 13
137 7 push @{$self->{'options'}->{$rule->long}}, $arg;
138 } else {
139 5 push @{$self->{'options'}->{$rule->long}}, $arg
140 6 ▶ unless ($rule->keep_unique && $arg ~~ @{$self->{'options'}->{$rule->long}});
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->keep_unique and $arg ~~ \@{$self->{"options"}{$rule->long};} 5 1 6
Condition100%3/3
$rule->keep_unique and $arg ~~ \@{$self->{"options"}{$rule->long};}
!ll&&!rl&&r
231
MC/DC100%2/2
$rule->keep_unique and $arg ~~ \@{$self->{"options"}{$rule->long};}
$rule->keep_unique $arg ~~ \@{$self->{"options"}{$rule->long};}
Truth table100%3/3
A and B
A= $rule->keep_uniqueB= $arg ~~ \@{$self->{"options"}{$rule->long};}
ABresult
0X0
100
111
141 }
142 13 next PROCESS_ARGUMENTS;
143 }
144 }
145
146 12 ▶ if (defined $rule->action) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->action) { } 1 11 12
147 1 $arg = $rule->action->($arg, $key, $rule);
148 } else {
149 11 ▶ $arg = $rule->{'_negate'} ? '' : 1
Branch: 2 branches
BranchTrueFalseTotal
unless defined $arg 5 6 11
$rule->{'_negate'} ? : 1 4 5
150 unless defined $arg;
151 }
152
153 11 $self->{'options'}->{$rule->long} = $arg;
154 }
155
156 10 %$dest = %{$self->{'options'}};
157 10 ▶ @$args = @arguments if ref $args;
Branch: 1 branch
BranchTrueFalseTotal
if ref $args 2 8 10
158 10 $self->{'options'} = {};
159 10 return $self
160 }
161 ##------------------------------------------------------------------------------
162 sub error
163 {
164 1 ▶ return shift->usage(1, shift(), @_);
Subroutine error: called CC=1 SCAR=0.0
Pod documented
165 }
166 ##------------------------------------------------------------------------------
167 sub getProgram
168 {
169 5 ▶ my $program = $ENV{_};
Subroutine getProgram: called CC=2 SCAR=7.0
Pod undocumented
170 5 $program =~ s{.*/([^/]+)$}{$1};
171 5 ▶ $program = $PROGRAM_NAME if 'perl' eq $program;
Branch: 1 branch
BranchTrueFalseTotal
if "perl" eq $program 0 5 5
172 5 return $program;
173 }
174 ##------------------------------------------------------------------------------
175 sub getProgramDescription
176 {
177 2 ▶ my $class = ref $_[0];
Subroutine getProgramDescription: called CC=1 SCAR=0.0
Pod documented
178 2 return qq{another example of this programmer's lazyness: it forgot the description (and should implement ${class}::getProgramDescription())}
179 }
180 ##------------------------------------------------------------------------------
181 sub getValue
182 {
183 24 ▶ return unless @ARGV;
Subroutine getValue: called CC=8 SCAR=26.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 1 23 24
184 23 my $value = $ARGV[0];
185 return shift @ARGV
186 23 ▶ if !defined $value || !length $value || '-' eq $value || $value !~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value && length $value or "-" eq $value or not $value =~ /^-/ 21 0 21
Condition88%8/9
not defined $value && length $value or "-" eq $value or not $value =~ /^-/
l!l&&r!l&&!r
3200
not defined $value && length $value or "-" eq $value
l!l&&r!l&&!r
2120
defined $value && length $value
!ll&&!rl&&r
1121
MC/DC0%0/4
not defined $value && length $value or "-" eq $value or not $value =~ /^-/
defined $value length $value "-" eq $value not $value =~ /^-/
Truth table80%4/5
not(A && B) or C or not(D)
A= defined $valueB= length $valueC= "-" eq $valueD= not $value =~ /^-/
ABCDresult
0XXX1
10XX1
111X1
11011
11000
187 0 ▶ return if $value ne '--';
Branch: 1 branch
BranchTrueFalseTotal
if $value ne "--" 0 0 0
188 0 shift @ARGV;
189 0 ▶ return unless @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 0 0
190 0 $value = shift @ARGV;
191 0 unshift @ARGV, '--';
192 0 return $value;
193 }
194 ##------------------------------------------------------------------------------
195 sub getOptionRules
196 {
197 7 ▶ my $self = shift;
Subroutine getOptionRules: called CC=1 SCAR=0.0
Pod documented
198
199 return
200 7 ▶ 'h|help' => ['Display this help message', sub {$self->usage(0)}],
Subroutine __ANON__: called CC=1 SCAR=0.0
201 'v|verbose+' => 'Increase program verbosity',
202 undef
203 }
204 ##------------------------------------------------------------------------------
205 sub parseRules ## no critic (Subroutines::ProhibitExcessComplexity)
206 {
207 30 ▶ my $self = shift;
Subroutine parseRules: called CC=13 SCAR=25.6
Pod undocumented
208 30 my @rules = $self->getOptionRules();
209
210 ## Perl Critic false positive on "$}" at the end of the reg-ex
211 ## no critic (Variables::ProhibitPunctuationVars)
212 30 state $pattern = qr{^
213 (?:(?P<negatable>!))?
214 (?:(?P<short>[[:alpha:]])[|])?
215 (?P<long>[[:alpha:]](?:[[:alpha:]-]*)[[:alpha:]])
216 (?:
217 (?:=(?P<type>[si?]@?))
218 |
219 (?P<multiple>[+])
220 )?
221 $}x;
222 ## use critic
223
224 30 my ($arg,$opt,@parsed);
225
226 30 ▶ while (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) 68 25 93
227 68 $arg = shift @rules;
228 68 ▶ unless (defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 9 59 68
229 9 ▶ push @parsed, undef if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 4 5 9
230 9 next;
231 }
232 59 $opt = $arg;
233 59 ▶ confess('Not enough rules') unless @rules;
Branch: 1 branch
BranchTrueFalseTotal
unless @rules 1 58 59
234 58 $arg = shift @rules;
235
236 58 ▶ $arg = [$arg] unless ref $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg 36 22 58
237 58 ▶ confess("Invalid rule pattern '$opt'") if $opt !~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt =~ /$pattern/ 1 57 58
238 57 my $rule = Getopt::O2::Rule->new($arg, %LAST_PAREN_MATCH);
239
240 confess(sprintf q{Option spec '%s' redefines long option '%s'}, $opt, $rule->long)
241 56 ▶ if exists $self->{'longOptions'}->{$rule->long};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"longOptions"}{$rule->long} 1 55 56
242
243 55 ▶ if (defined $rule->short) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->short) 34 21 55
244 confess(sprintf q{Option spec '%s' redefines short option '%s'}, $opt, $rule->short)
245 34 ▶ if exists $self->{'shortOptions'}->{$rule->short};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"shortOptions"}{$rule->short} 1 33 34
246 33 $self->{'shortOptions'}->{$rule->short} = $rule->long;
247 }
248
249 54 ▶ if (defined $rule->default) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->default) 4 50 54
250 4 $self->{'options'}->{$rule->long} = $rule->default;
251 }
252
253 54 $self->{'longOptions'}->{$rule->long} = $rule;
254 54 ▶ push @parsed, $rule if wantarray
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 13 41 54
255 }
256
257 25 ▶ return $self unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 22 3 25
258 3 return @parsed;
259 }
260 ##------------------------------------------------------------------------------
261 sub showOptionDefaultValues
262 {
263 3 ▶ return;
Subroutine showOptionDefaultValues: called CC=1 SCAR=0.0
Pod undocumented
264 }
265 ##------------------------------------------------------------------------------
266 sub usage ## no critic (Subroutines::ProhibitExcessComplexity)
267 {
268 3 ▶ my $self = shift;
Subroutine usage: called CC=10 SCAR=23.0
Pod documented
269 3 my ($exitCode,$message,@args) = @_;
270
271 3 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 1 2 3
272 1 $message = sprintf "Error: $message", @args;
273 } else {
274 2 $message = sprintf '%s - %s', $self->getProgram(), $self->getProgramDescription();
275 }
276
277 print STDERR "$_\n"
278 3 foreach wrapString($message, 0, 8, $USAGE_MARGIN);
279 3 printf STDERR "\nUsage: %s [options...]\n\nValid options:\n\n", $self->getProgram();
280
281 ## no critic (Variables::ProhibitLocalVars)
282 3 local $self->{'longOptions'} = undef;
283 3 local $self->{'shortOptions'} = undef;
284 ## use critic
285
286 3 my @rules = $self->parseRules();
287 3 my ($rule,$line,$long,$len,$show_default);
288
289 3 $show_default = $self->showOptionDefaultValues();
290
291 3 ▶ PROCESS_RULES: while (@rules) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rules) 17 3 20
292 #@type Getopt::O2::Rule
293 17 $rule = shift @rules;
294
295 17 ▶ unless (defined $rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rule) 4 13 17
296 4 print STDERR "\n";
297 4 next PROCESS_RULES;
298 }
299
300 13 $line = ' ';
301 13 $long = $rule->long;
302 13 ▶ $long = "(no-)$long" if $rule->negatable;
Branch: 1 branch
BranchTrueFalseTotal
if $rule->negatable 1 12 13
303
304 13 ▶ unless (defined $rule->short) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->short) { } 11 2 13
305 2 $long = "--$long";
306 } else {
307 11 $long = " [--$long]";
308 11 $line .= '-'.$rule->short;
309 }
310
311 13 $line = "$line$long";
312 13 ▶ $line .= ' ARG' if defined $rule->type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rule->type 3 10 13
313
314 13 ▶ $line .= ' ' x ($USAGE_OPTIONS_LENGTH - $len)
Branch: 1 branch
BranchTrueFalseTotal
if $USAGE_OPTIONS_LENGTH > ($len = length($line) + 2) 12 1 13
315 if $USAGE_OPTIONS_LENGTH > ($len = length($line) + 2);
316 13 $line = "$line: ";
317 13 print STDERR $line;
318
319 print STDERR "$_\n"
320 13 foreach wrapString($rule->help($show_default), length $line, $USAGE_OPTIONS_LENGTH, $USAGE_MARGIN);
321 }
322
323 3 print STDERR "\n";
324 3 exit $exitCode;
325 }
326 ##------------------------------------------------------------------------------
327 sub wrapString
328 {
329 16 ▶ my ($string,$firstIndent,$leftIndent,$wrapAt) = @_;
Subroutine wrapString: called CC=6 SCAR=17.9
Pod undocumented
330 16 my (@lines,$len,$pos,$nChars);
331
332 16 ▶ for ($nChars = $wrapAt - $firstIndent; length $string; $nChars = $wrapAt - $leftIndent) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $string) 22 0 22
333 22 $len = length $string;
334
335 22 ▶ if ($len < $nChars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len < $nChars) 16 6 22
336 16 push @lines, $string;
337 16 last;
338 }
339
340 6 $pos = strrpos((substr $string, 0, $nChars), ' ');
341 6 ▶ if (-1 == $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == $pos) { } 1 5 6
342 1 push @lines, (substr $string, 0, $nChars);
343 1 $string = (substr $string, $nChars);
344 } else {
345 5 push @lines, (substr $string, 0, $pos);
346 5 $string = (substr $string, $pos + 1);
347 }
348 }
349
350 16 ▶ if (@lines > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines > 1) 3 13 16
351 3 my $indent = ' ' x $leftIndent;
352 3 $lines[$_] = "$indent$lines[$_]" foreach (1..$#lines);
353 }
354
355 return @lines
356 16 }
357 ##------------------------------------------------------------------------------
358 sub strrpos
359 {
360 6 ▶ my ($string,$find) = @_;
Subroutine strrpos: called CC=3 SCAR=11.0
Pod undocumented
361 6 my ($length) = length $find;
362
363 6 ▶ for (my $pos = length($string) - 1; $pos >= 0; --$pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos >= 0) 101 1 102
364 101 ▶ return $pos if $find eq (substr $string, $pos, $length);
Branch: 1 branch
BranchTrueFalseTotal
if $find eq substr($string, $pos, $length) 5 96 101
365 }
366
367 1 return -1
368 }
369 ##------------------------------------------------------------------------------
370 package Getopt::O2::Rule; ## no critic (Modules::ProhibitMultiplePackages)
371
372 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
373 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
374 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
375
376 2 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
377
378 BEGIN {
379 ## no critic (TestingAndDebugging::ProhibitNoStrict)
380 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
381 2 ▶ foreach my $method (qw(action context default is_list keep_unique long multiple negatable short type values)) {
Subroutine BEGIN: called CC=2 SCAR=6.9
382 22 ▶ *{__PACKAGE__."::$method"} = sub {shift->{$method}}
Subroutine __ANON__: called CC=1 SCAR=0.0
383 22 }
384 ## use critic
385 }
386
387 sub new ## no critic (Subroutines::ProhibitExcessComplexity)
388 {
389 57 ▶ my $class = shift;
Subroutine new: called CC=12 SCAR=24.9
390 57 my ($arg, %options) = @_;
391 57 my (%rule);
392
393 57 $rule{'long'} = $options{'long'};
394 57 ▶ $rule{'short'} = $options{'short'} if exists $options{'short'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options{"short"} 35 22 57
395
396 57 ▶ $rule{'negatable'} = 1 if $options{'negatable'};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"negatable"} 4 53 57
397 57 ▶ if ($options{'multiple'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'multiple'}) { } 8 49 57
elsif ($options{'type'}) { } 22 27 49
398 8 $rule{'multiple'} = 1
399 } elsif ($options{'type'}) {
400 22 $rule{'type'} = (substr $options{'type'}, 0, 1);
401 22 $rule{'is_list'} = ~(index $options{'type'}, '@');
402 $rule{'keep_unique'} = $options{'keep_unique'} // 1
403 22 ▶ if $rule{'is_list'};
Branch: 1 branch
BranchTrueFalseTotal
if $rule{"is_list"} 6 16 22
Condition50%1/2
$options{"keep_unique"} // 1
l!l
06
MC/DC0%0/1
$options{"keep_unique"} // 1
$options{"keep_unique"}
Truth table50%1/2
A
A= $options{"keep_unique"}
Aresult
11
00
404 }
405
406 57 $rule{'help'} = shift @$arg;
407 57 $rule{'help'} =~ s/^\s+|\s+$//g;
408 57 $rule{'help'} =~ s/\s+/ /g;
409 57 ▶ $rule{'help'} .= '.' if $rule{'help'} !~ /[.]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule{"help"} =~ /[.]$/ 55 2 57
410
411 57 ▶ if (@$arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$arg) 22 35 57
412 22 ▶ $rule{'action'} = shift @$arg
Branch: 1 branch
BranchTrueFalseTotal
if "CODE" eq ref $arg->[0] 7 15 22
413 if 'CODE' eq ref $arg->[0];
414 22 ▶ confess('Invalid rule options; the remainder is a list with uneven members')
Branch: 1 branch
BranchTrueFalseTotal
if 0 != @$arg % 2 1 21 22
415 if 0 != (@$arg % 2);
416 21 %rule = (%rule, @$arg);
417 }
418
419 56 ▶ if (defined $rule{'context'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule{"context"}) 2 54 56
420 2 $rule{'context'} = [split /,/, $rule{'context'}];
421 $rule{'context'} = {
422 1 set => [map {(substr $_, 1)} grep {/^[+]/} @{$rule{'context'}}],
423 0 clear => [map {(substr $_, 1)} grep {/^-/} @{$rule{'context'}}],
424 2 need => [grep {/^[^+-]/} @{$rule{'context'}}],
425 };
426 }
427
428 56 $rule{'_used'} = 0;
429
430 56 return bless \%rule, $class
431 }
432 ##------------------------------------------------------------------------------
433 sub is_unused
434 {
435 22 ▶ return !shift->{'_used'};
Subroutine is_unused: called CC=1 SCAR=0.0
436 }
437 ##------------------------------------------------------------------------------
438 sub mark_used
439 {
440 22 ▶ my $self = shift;
Subroutine mark_used: called CC=1 SCAR=0.0
441 22 $self->{'_used'} = 1;
442 22 return $self;
443 }
444 ##------------------------------------------------------------------------------
445 sub help
446 {
447 13 ▶ my $self = shift;
Subroutine help: called CC=7 SCAR=21.1
448 13 my $show_default = shift;
449
450 13 ▶ unless (defined $self->{'type'}) { # flags
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'type'}) { } 3 10 13
if (do { '?' ne $self->{'type'} }) { } 2 1 3
451 10 return $self->{'help'};
452 } elsif ('?' ne $self->{'type'}) { # anything but ENUM
453 2 my $helpstr = $self->{'help'};
454
455 2 ▶ return $helpstr unless $show_default && defined $self->{'default'};
Branch: 1 branch
BranchTrueFalseTotal
unless $show_default and defined $self->{"default"} 2 0 2
Condition33%1/3
$show_default and defined $self->{"default"}
!ll&&!rl&&r
200
MC/DC0%0/2
$show_default and defined $self->{"default"}
$show_default defined $self->{"default"}
Truth table33%1/3
A and B
A= $show_defaultB= defined $self->{"default"}
ABresult
0X0
100
111
456
457 0 $helpstr =~ s/\s*[.]\s*$//;
458 0 return sprintf '%s (default: "%s").', $helpstr, $self->{'default'};
459 } else {
460 1 my @values = map {qq{"$_"}} @{$self->values};
461 my $default_value = ($show_default && defined $self->{'default'})
462 1 ▶ ? (sprintf ' [default: "%s"]', $self->{'default'})
Branch: 1 branch
BranchTrueFalseTotal
$show_default && defined $self->{'default'} ? : 0 1 1
Condition33%1/3
$show_default and defined $self->{"default"}
!ll&&!rl&&r
100
MC/DC0%0/2
$show_default and defined $self->{"default"}
$show_default defined $self->{"default"}
Truth table33%1/3
A and B
A= $show_defaultB= defined $self->{"default"}
ABresult
0X0
100
111
463 : '';
464 1 return $self->{'help'} . (sprintf ' (ARG must be %s or %s)%s',
465 (join ', ', @values[0..$#values-1]), $values[-1], $default_value);
466 }
467 }
468 ##------------------------------------------------------------------------------
469 2 1;
470 __END__
↑ Summary Build »