blib/lib/Getopt/Modular.pm

filter: Statementstmt 94.4% 291 / 308 Branchbran 86.0% 117 / 136 Conditioncond 63.4% 52 / 82 MC/DCmcdc 34.0% 17 / 50 Subroutinesub 93.0% 40 / 43 Podpod 100% 12 / 12 totaltotal 83.8% 529 / 631 CC 112 SCAR 21.8
CC
112
Cov
87%
_setOpt
19.9
getOpt
13.6
getHelpRaw
13.3
CRAP 8.9
↑ Summary
1 package Getopt::Modular;
2 11 $Getopt::Modular::VERSION = '0.13';
3 #ABSTRACT: Modular access to Getopt::Long
4  
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use List::MoreUtils qw(any uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Scalar::Util qw(reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 use Exception::Class
12 11 ▶ 'Getopt::Modular::Exception' => {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 description => 'Exception in commandline parsing/handling',
14 fields => [ qw(type option value warning valid expected) ],
15 },
16 'Getopt::Modular::Internal' => {
17 description => 'Internal Exception in commandline parsing/handling',
18 fields => [ qw(type option) ]
19 11 };
20 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 sub import
24 10 ▶{
Subroutine import: called CC=8 SCAR=21.6
25 10 my $class = shift;
26 10 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 10 20
27 {
28 10 my $opt = shift;
29 10 ▶ if ($opt eq '-namespace')
Branch: 2 branches
BranchTrueFalseTotal
if ($opt eq '-namespace') { } 9 1 10
elsif ($opt eq '-getOpt') { } 1 0 1
30 {
31 9 ▶ my $ns = shift || die "No namespace given";
Condition50%1/2
shift() || die('No namespace given')
l!l
90
MC/DC0%0/1
shift() || die('No namespace given')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
32  
33 # I could do this without eval, but I'm too lazy today.
34 9 ▶ eval qq{
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $ns;\n \@${ns}::ISA = List::MoreUtils::uniq('Getopt::Modular', \@${ns}::ISA);\n 1; " or die $@ 9 0 9
35 package $ns;
36 \@${ns}::ISA = List::MoreUtils::uniq('Getopt::Modular', \@${ns}::ISA);
37 1; } or die $@;
38 }
39 elsif ($opt eq '-getOpt')
40 {
41 1 ▶ my $package = caller || 'main';
Condition50%1/2
caller || 'main'
l!l
10
MC/DC0%0/1
caller || 'main'
caller
Truth table50%1/2
A
A= caller
Aresult
11
00
42  
43 # again, too lazy right now.
44 1 ▶ eval qq{
Subroutine getOpt: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
eval "\n package $package;\n sub getOpt { Getopt::Modular->getOpt(\@_) }\n 1; " or die $@ 1 0 1
45 package $package;
46 sub getOpt { Getopt::Modular->getOpt(\@_) }
47 1; } or die $@;
48 }
49 }
50 }
51  
52  
53 11 my $global;
54 9 ▶sub new {
Subroutine new: called CC=3 SCAR=12.9
Pod documented
55 9 my $class = shift;
56 9 my $self = {};
57 9 bless $self, $class;
58  
59 # do we have a global one yet?
60 9 ▶ $global ||= $self;
Condition33%1/3
$global ||= $self
l!l&&r!l&&!r
090
MC/DC0%0/2
$global ||= $self
$global $self
Truth table33%1/3
A ||= B
A= $globalB= $self
ABresult
1X1
011
000
61  
62 0 ▶ if (any {'global' eq lc} @_)
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { "global" eq lc $_; } , @_)) 0 9 9
63 {
64 0 $global = $self;
65 0 @_ = grep { 'global' ne lc } @_;
66 }
67  
68 9 $self->setBoolHelp(qw(off on));
69  
70 9 $self->init(@_);
71  
72 9 $self;
73 }
74  
75 sub _self_or_global
76 990 ▶{
Subroutine _self_or_global: called CC=6 SCAR=18.5
77 990 my $underscore = shift;
78 990 my $self = $underscore->[0];
79  
80 # is it an object? use it.
81 990 ▶ eval { ref $self && $self->isa(__PACKAGE__) } && return shift @$underscore;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { ref $self and $self->isa(__PACKAGE__) } } and return shift @$underscore 864 126 990
Condition66%2/3
ref $self && $self->isa(__PACKAGE__)
!ll&&!rl&&r
1260864
MC/DC50%1/2
ref $self && $self->isa(__PACKAGE__)
ref $self $self->isa(__PACKAGE__)
Truth table66%2/3
A && B
A= ref $selfB= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
82  
83 # passed in via class method? skip it.
84 126 ▶ eval { not ref $self && $self->isa(__PACKAGE__) } && shift @$underscore;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { not ref $self && $self->isa(__PACKAGE__) } } and shift @$underscore 126 0 126
Condition33%1/3
ref $self && $self->isa(__PACKAGE__)
!ll&&!rl&&r
12600
MC/DC0%0/2
ref $self && $self->isa(__PACKAGE__)
ref $self $self->isa(__PACKAGE__)
Truth table33%1/3
A && B
A= ref $selfB= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
85  
86 # have global? use it.
87 126 ▶ $global ? $global :
Branch: 1 branch
BranchTrueFalseTotal
$global ? : 118 8 126
88 # otherwise, create new.
89 $self->new();
90 }
91  
92 sub _accepts_opt
93 83 ▶{
Subroutine _accepts_opt: called CC=1 SCAR=0.0
94 83 my $self = _self_or_global(\@_);
95 83 my $opt = shift;
96 83 return exists $self->{accept_opts}{$opt};
97 }
98  
99 sub _opt
100 561 ▶{
Subroutine _opt: called CC=3 SCAR=11.1
101 561 my $self = _self_or_global(\@_);
102 561 my $opt = shift;
103
104 561 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 39 522 561
105 {
106 39 $self->{accept_opts}{$opt} = shift;
107 39 return;
108 }
109  
110 522 ▶ unless (exists $self->{accept_opts}{$opt})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"accept_opts"}{$opt}) 0 522 522
111 {
112 0 Getopt::Modular::Internal->throw(
113 type => 'unknown-option',
114 message => "Unknown option: $opt",
115 option => $opt,
116 );
117 }
118 522 return $self->{accept_opts}{$opt};
119 }
120  
121  
122 sub init
123 9 ▶{
Subroutine init: called CC=2 SCAR=7.1
Pod documented
124 9 my $self = shift;
125 9 ▶ $self->setMode(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
126 9 1;
127 }
128  
129  
130 11 my %_known_modes = map { $_ => 1 } qw(
131 strict
132 );
133  
134 sub setMode
135 5 ▶{
Subroutine setMode: called CC=3 SCAR=11.0
Pod documented
136 5 my $self = _self_or_global(\@_);
137  
138 5 foreach my $mode (@_)
139 {
140 5 ▶ if ($_known_modes{$mode})
Branch: 1 branch
BranchTrueFalseTotal
if ($_known_modes{$mode}) { } 3 2 5
141 {
142 3 $self->{mode}{$mode}++;
143 }
144 else
145 {
146 2 croak "Unknown mode: $@";
147 }
148 }
149 }
150  
151  
152 sub setBoolHelp
153 9 ▶{
Subroutine setBoolHelp: called CC=1 SCAR=0.0
Pod documented
154 9 my $self = _self_or_global(\@_);
155 9 $self->{bool_strings} = [ @_[0,1] ];
156 }
157  
158  
159 sub acceptParam
160 17 ▶{
Subroutine acceptParam: called CC=12 SCAR=24.8
Pod documented
161 17 my $self = _self_or_global(\@_);
162 17 ▶ while (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 42 16 58
163 {
164 42 my $param = shift;
165 42 my $opts = shift;
166  
167 42 ▶ my $aliases = exists $opts->{aliases} ? ref $opts->{aliases} ? $opts->{aliases} : [ $opts->{aliases} ] : [];
Branch: 2 branches
BranchTrueFalseTotal
exists $opts->{'aliases'} ? : 5 37 42
ref $opts->{'aliases'} ? : 3 2 5
168  
169 42 ▶ if ($param =~ /\|/)
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /\|/) 10 32 42
170 {
171 10 ($param, my @aliases) = split /\|/, $param;
172 10 unshift @$aliases, @aliases;
173 }
174  
175 # if any of the aliases have pipes, split them up. Needed to provide
176 # the help screen.
177 $opts->{aliases} = [
178 uniq
179 eval {
180 42 ▶ my $o = $self->_accepts_opt($param) ? $self->_opt($param) : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->_accepts_opt($param) ? : 2 40 42
181 42 ▶ @{$o->{aliases} || []};
Condition100%2/2
$o->{'aliases'} || []
l!l
240
MC/DC100%1/1
$o->{'aliases'} || []
$o->{'aliases'}
Truth table100%2/2
A
A= $o->{'aliases'}
Aresult
11
00
182 },
183 42 map { split /\|/, $_ } @$aliases
184 ];
185  
186 # check if this flag already exists (other than as main name)
187 42 for (@{$opts->{aliases}})
188 {
189 24 ▶ if (exists $self->{all_opts}{$_} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"all_opts"}{$_} and $self->{"all_opts"}{$_} ne $param) 1 23 24
Condition100%3/3
exists $self->{"all_opts"}{$_} and $self->{"all_opts"}{$_} ne $param
!ll&&!rl&&r
2031
MC/DC100%2/2
exists $self->{"all_opts"}{$_} and $self->{"all_opts"}{$_} ne $param
exists $self->{"all_opts"}{$_} $self->{"all_opts"}{$_} ne $param
Truth table100%3/3
A and B
A= exists $self->{"all_opts"}{$_}B= $self->{"all_opts"}{$_} ne $param
ABresult
0X0
100
111
190 $self->{all_opts}{$_} ne $param)
191 {
192 1 croak "$_ already used by $self->{all_opts}{$_}";
193 }
194 # save this as the owner
195 23 $self->{all_opts}{$_} = $param;
196 }
197  
198 41 delete $self->{unacceptable}{$param};
199 41 ▶ if ($self->_accepts_opt($param))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_accepts_opt($param)) { } 2 39 41
200 {
201 2 my $opt = $self->_opt($param);
202 2 @{$self->_opt($param)}{keys %$opts} = values %$opts;
203 }
204 else
205 {
206 # set some defaults ...
207 39 ▶ $opts->{spec} ||= '';
Condition100%2/2
$opts->{"spec"} ||= ""
l!l
363
MC/DC100%1/1
$opts->{"spec"} ||= ""
$opts->{"spec"}
Truth table100%2/2
A
A= $opts->{"spec"}
Aresult
11
00
208  
209 39 $self->_opt($param, $opts);
210 }
211 }
212 }
213  
214  
215 sub unacceptParam
216 2 ▶{
Subroutine unacceptParam: called CC=2 SCAR=6.9
Pod documented
217 2 my $self = _self_or_global(\@_);
218 2 for my $param (@_)
219 {
220 2 $self->{unacceptable}{$param} = 1;
221 my @x =
222 2 delete @{$self->{all_opts}}{@{$self->{accept_opts}{$param}{aliases}}};
223 }
224 }
225  
226  
227 sub parseArgs
228 5 ▶{
Subroutine parseArgs: called CC=10 SCAR=23.1
Pod documented
229 5 my $self = _self_or_global(\@_);
230  
231 # first, gather up for the call to Getopt::Long.
232 5 my %opts;
233 5 my $accept = $self->{accept_opts};
234 5 my $unaccept = $self->{unacceptable};
235 my @params = map {
236 15 my $param = join '|', $_, @{$accept->{$_}{aliases}};
237 15 ▶ $param . ($accept->{$_}{spec} || '');
Condition50%1/2
$accept->{$_}{'spec'} || ''
l!l
150
MC/DC0%0/1
$accept->{$_}{'spec'} || ''
$accept->{$_}{'spec'}
Truth table50%1/2
A
A= $accept->{$_}{'spec'}
Aresult
11
00
238 } grep {
239 # skip unaccepted parameters
240 5 ▶ $unaccept and not $unaccept->{$_}
Condition66%2/3
$unaccept && !$unaccept->{$_}
!ll&&!rl&&r
0115
MC/DC50%1/2
$unaccept && !$unaccept->{$_}
$unaccept !$unaccept->{$_}
Truth table66%2/3
A && not(B)
A= $unacceptB= !$unaccept->{$_}
ABresult
0X0
100
111
241 } keys %$accept;
242  
243 # parse them
244 5 my $warnings;
245 5 my $success = do {
246 5 ▶ local $SIG{__WARN__} = sub { $warnings .= "@_";};
Subroutine __ANON__: called CC=1 SCAR=0.0
247 5 Getopt::Long::Configure("bundling");
248 5 GetOptions(\%opts, @params);
249 };
250 5 ▶ if (not $success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 1 4 5
251 {
252 1 Getopt::Modular::Exception->throw(
253 message => "Bad command-line: $warnings",
254 type => 'getopt-long-failure',
255 warning => $warnings,
256 );
257 }
258  
259 # now validate everything that was passed in, and save it.
260 4 for my $opt (keys %$accept)
261 {
262 11 ▶ if (exists $opts{$opt})
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{$opt}) { } 6 5 11
elsif ($accept->{$opt}{'mandatory'}) { } 2 3 5
263 {
264 6 $self->setOpt($opt, $opts{$opt});
265 }
266 # if it's mandatory, get it - that will call the default and
267 # set it.
268 elsif ($accept->{$opt}{mandatory})
269 {
270 # setting via default.
271 2 $self->getOpt($opt);
272 }
273 }
274  
275 # if passed in a hash ref to populate, fill it.
276 3 ▶ if (@_ && ref $_[0] eq 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0] eq "HASH") 1 2 3
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
201
MC/DC50%1/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
277 {
278 1 my $opts = shift;
279 1 for my $opt (keys %{$self->{accept_opts}})
280 {
281 2 $opts->{$opt} = $self->getOpt($opt);
282 }
283 }
284 }
285  
286  
287 sub getOpt
288 66 ▶{
Subroutine getOpt: called CC=13 SCAR=26.1
Pod documented
289 66 my $self = _self_or_global(\@_);
290 66 ▶ my $opt = shift || Getopt::Modular::Exception->throw(
Condition33%1/3
shift() || 'Getopt::Modular::Exception'->throw('message', 'No option given?', 'type', 'dev-error')
l!l&&r!l&&!r
6600
MC/DC0%0/2
shift() || 'Getopt::Modular::Exception'->throw('message', 'No option given?', 'type', 'dev-error')
shift() 'Getopt::Modular::Exception'->throw('message', 'No option given?', 'type', 'dev-error')
Truth table33%1/3
A || B
A= shift()B= 'Getopt::Modular::Exception'->throw('message', 'No option given?', 'type', 'dev-error')
ABresult
1X1
011
000
291 message => 'No option given?',
292 type => 'dev-error',
293 );
294  
295 66 ▶ if (not exists $self->{accept_opts}{$opt})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"accept_opts"}{$opt}) 5 61 66
296 {
297 5 ▶ if ($self->{mode}{strict})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"mode"}{"strict"}) 3 2 5
298 {
299 3 Getopt::Modular::Exception->throw(
300 message => "No such option: $opt",
301 type => 'no-such-option',
302 option => $opt,
303 value => undef,
304 );
305 }
306 }
307  
308 # If we don't have it yet, check if there's a default.
309 63 ▶ if (not exists $self->{options}{$opt} and
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"options"}{$opt} and exists $self->{"accept_opts"}{$opt} and exists $self->{"accept_opts"}{$opt}{"default"}) 15 48 63
Condition100%6/6
not exists $self->{"options"}{$opt} and exists $self->{"accept_opts"}{$opt} and exists $self->{"accept_opts"}{$opt}{"default"}
!ll&&!rl&&r
381015
not exists $self->{"options"}{$opt} and exists $self->{"accept_opts"}{$opt}
!ll&&!rl&&r
36225
MC/DC100%3/3
not exists $self->{"options"}{$opt} and exists $self->{"accept_opts"}{$opt} and exists $self->{"accept_opts"}{$opt}{"default"}
not exists $self->{"options"}{$opt} exists $self->{"accept_opts"}{$opt} exists $self->{"accept_opts"}{$opt}{"default"}
Truth table100%4/4
not(A) and B and C
A= not exists $self->{"options"}{$opt}B= exists $self->{"accept_opts"}{$opt}C= exists $self->{"accept_opts"}{$opt}{"default"}
ABCresult
0XX0
10X0
1100
1111
310 exists $self->{accept_opts}{$opt} and
311 exists $self->{accept_opts}{$opt}{default})
312 {
313 15 my @default = $self->{accept_opts}{$opt}{default};
314 15 ▶ if (ref $default[0] and ref $default[0] eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default[0] and ref $default[0] eq "CODE") 7 8 15
Condition66%2/3
ref $default[0] and ref $default[0] eq "CODE"
!ll&&!rl&&r
807
MC/DC50%1/2
ref $default[0] and ref $default[0] eq "CODE"
ref $default[0] ref $default[0] eq "CODE"
Truth table66%2/3
A and B
A= ref $default[0]B= ref $default[0] eq "CODE"
ABresult
0X0
100
111
315 {
316 7 @default = $default[0]->();
317 }
318 15 $self->setOpt($opt, @default);
319 }
320  
321 # should have one now ... check and return
322 63 ▶ if (exists $self->{options}{$opt})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"options"}{$opt}) 51 12 63
323 {
324 51 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 8 43 51
325 {
326 return
327 8 ▶ ref $self->{options}{$opt} eq 'ARRAY' ? @{$self->{options}{$opt}} :
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'options'}{$opt} eq 'HASH' ? : 0 0 0
328 0 ref $self->{options}{$opt} eq 'HASH' ? %{$self->{options}{$opt}} :
329 8 ▶ $self->{options}{$opt};
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'options'}{$opt} eq 'ARRAY' ? : 8 0 8
330 }
331 43 return $self->{options}{$opt}
332 }
333  
334 12 return;
335 }
336  
337 sub _getType
338 92 ▶{
Subroutine _getType: called CC=4 SCAR=13.9
339 92 my $self = _self_or_global(\@_);
340 92 my $opt = shift;
341  
342 92 ▶ unless (exists $self->_opt($opt)->{_GMTYPE})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_opt($opt)->{"_GMTYPE"}) 32 60 92
343 {
344 32 my $type = $self->_opt($opt)->{spec};
345 32 $self->_opt($opt)->{_GMTYPE} = ''; #scalar
346 32 ▶ if ($type =~ /\@/)
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\@/) { } 6 26 32
elsif ($type =~ /\%/) { } 3 23 26
347 {
348 6 $self->_opt($opt)->{_GMTYPE} = 'ARRAY';
349 }
350 elsif ($type =~ /\%/)
351 {
352 3 $self->_opt($opt)->{_GMTYPE} = 'HASH';
353 }
354 }
355 $self->_opt($opt)->{_GMTYPE}
356 92 }
357  
358 sub _bool_val
359 8 ▶{
Subroutine _bool_val: called CC=1 SCAR=0.0
360 # technically, perl allows anything to be boolean.
361 #my ($opt,$val) = @_;
362 }
363  
364 sub _int_val
365 24 ▶{
Subroutine _int_val: called CC=2 SCAR=6.9
366 24 my ($opt,$val) = @_;
367 24 ▶ if ($val !~ /^[-+]?\d+$/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $val =~ /^[-+]?\d+$/) 4 20 24
368 {
369 4 Getopt::Modular::Exception->throw(
370 message => "Trying to set '$opt' (an integer-only parameter) to '$val'",
371 type => 'set-int-failure',
372 option => $opt,
373 value => $val
374 );
375 }
376 }
377  
378 sub _real_val
379 17 ▶{
Subroutine _real_val: called CC=2 SCAR=6.9
380 17 my ($opt,$val) = @_;
381  
382 17 ▶ unless (looks_like_number $val)
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($val)) 5 12 17
383 {
384 5 Getopt::Modular::Exception->throw(
385 message => "Trying to set '$opt' (a real-number parameter) to '$val'",
386 type => 'set-real-failure',
387 option => $opt,
388 value => $val
389 );
390 }
391 }
392  
393 my %_valtypes = (
394 '!' => { val => \&_bool_val },
395 '+' => { val => \&_int_val },
396 46 ▶ 's' => { val => sub {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
397 11 'i' => { val => \&_int_val },
398 'o' => { val => \&_int_val },
399 'f' => { val => \&_real_val },
400 );
401  
402 sub _setOpt
403 67 ▶{
Subroutine _setOpt: called CC=18 SCAR=29.9
404 67 my $self = _self_or_global(\@_);
405 67 my $opt = shift;
406 67 my $val = shift;
407  
408 # check known types before passing on to user-specified validation.
409  
410 67 my $type = $self->_opt($opt)->{spec};
411 67 ▶ if ($type eq '' || $type eq '!') # boolean
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq '' or $type eq '!') { } 8 59 67
Condition66%2/3
$type eq "" or $type eq "!"
l!l&&r!l&&!r
0859
MC/DC50%1/2
$type eq "" or $type eq "!"
$type eq "" $type eq "!"
Truth table66%2/3
A or B
A= $type eq ""B= $type eq "!"
ABresult
1X1
011
000
412 {
413 8 _bool_val($opt,$val);
414 # extra information should not be stored in a boolean.
415 8 $val = !!$val;
416 }
417 else
418 {
419 59 for (split //, $type)
420 {
421 138 ▶ if (my $info = $_valtypes{$_})
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $_valtypes{$_}) 59 79 138
422 {
423 59 ▶ if ($type =~ /\@/)
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /\@/) { } 15 44 59
elsif ($type =~ /\%/) { } 7 37 44
424 {
425 15 $info->{val}->($opt,$_) for @$val;
426 }
427 elsif ($type =~ /\%/)
428 {
429 7 $info->{val}->($opt,$_) for values %$val;
430 }
431 else
432 {
433 37 $info->{val}->($opt,$val);
434 }
435 }
436 }
437 }
438  
439 58 ▶ if ($self->_opt($opt)->{validate})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_opt($opt)->{"validate"}) 26 32 58
440 {
441 26 local $_ = $val;
442 26 ▶ unless ($self->_opt($opt)->{validate}->())
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_opt($opt)->{"validate"}->()) 7 18 25
443 {
444 7 ▶ if (ref $val)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 4 3 7
445 {
446 4 ▶ $val = join ',', @$val if ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 3 1 4
447 4 ▶ $val = join ',', map { "$_=$val->{$_}" } sort keys %$val if ref $val eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "HASH" 1 3 4
448 }
449 Getopt::Modular::Exception->throw(
450 7 message => "'$val' is an invalid value for $opt",
451 type => 'validate-failure',
452 option => $opt,
453 value => $val,
454 );
455 }
456 }
457  
458 50 ▶ if (my $valid = $self->_opt($opt)->{valid_values})
Branch: 1 branch
BranchTrueFalseTotal
if (my $valid = $self->_opt($opt)->{"valid_values"}) 1 49 50
459 {
460 1 ▶ if (ref $valid eq 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid eq "CODE") 0 1 1
461 {
462 0 my @valid = $valid->();
463 0 $valid = \@valid;
464 0 $self->_opt($opt)->{valid_values} = $valid; # cache for next time.
465 }
466  
467 1 ▶ if (ref $valid eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid eq 'ARRAY') { } 0 1 1
468 {
469 0 ▶ unless (any { $_ eq $val } @$valid)
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ eq $val; } , @$valid)) 0 0 0
470 {
471 0 Getopt::Modular::Exception->throw(
472 message => "'$val' is an invalid value for $opt",
473 type => 'validate-failure',
474 option => $opt,
475 value => $val,
476 valid => $valid,
477 );
478 }
479 }
480 else
481 {
482 1 Getopt::Modular::Exception->throw(
483 message => "'valid_values requires either an array ref or a code ref to generate the list of valid values.",
484 type => 'valid-values-error',
485 option => $opt,
486 );
487 }
488 }
489  
490 49 $self->{options}{$opt} = $val;
491 }
492  
493  
494 sub setOpt
495 70 ▶{
Subroutine setOpt: called CC=8 SCAR=20.8
Pod documented
496 70 my $self = _self_or_global(\@_);
497 70 my $opt = shift;
498 70 my $val = do {
499 70 ▶ if (ref $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 9 61 70
500 {
501 9 ▶ Getopt::Modular::Exception->throw(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_getType($opt) eq builtin::reftype($_[0]) 3 6 9
Condition83%5/6
$self->_getType($opt) || 'SCALAR'
l!l
12
builtin::reftype($_[0]) || 'SCALAR'
l!l
30
$self->_getType($opt) || 'SCALAR'
l!l
12
MC/DC100%1/1
$self->_getType($opt) || 'SCALAR'
$self->_getType($opt)
MC/DC0%0/1
builtin::reftype($_[0]) || 'SCALAR'
builtin::reftype($_[0])
MC/DC100%1/1
$self->_getType($opt) || 'SCALAR'
$self->_getType($opt)
Truth table100%2/2
A
A= $self->_getType($opt)
Aresult
11
00
Truth table50%1/2
A
A= builtin::reftype($_[0])
Aresult
11
00
Truth table100%2/2
A
A= $self->_getType($opt)
Aresult
11
00
502 type => 'wrong-type',
503 message => "Wrong type of data for $opt. Expected: " .
504 ($self->_getType($opt) || 'SCALAR') .
505 " got: " . (reftype $_[0] || 'SCALAR'),
506 expected => ($self->_getType($opt) || 'SCALAR'),
507 option => $opt,
508 value => $_[0],
509 )
510 unless $self->_getType($opt) eq reftype $_[0];
511  
512 # if it's a reference, pass it in unchanged.
513 6 shift;
514 }
515 else
516 {
517 # scalars get passed in, but hashes and arrays need to
518 # be referencised.
519  
520 61 ▶ ! $self->_getType($opt) ? shift :
Branch: 2 branches
BranchTrueFalseTotal
!$self->_getType($opt) ? : 45 16 61
$self->_getType($opt) eq 'HASH' ? : 6 10 16
521 $self->_getType($opt) eq 'HASH' ? { @_ } : [ @_ ];
522 }
523 };
524  
525 67 $self->_setOpt($opt, $val);
526 }
527  
528  
529 sub getHelpRaw
530 7 ▶{
Subroutine getHelpRaw: called CC=13 SCAR=25.9
Pod documented
531 7 my $self = _self_or_global(\@_);
532  
533 # get the list of parameters ...
534 7 my $accept = $self->{accept_opts};
535 7 my $unaccept = $self->{unacceptable};
536 my @params = sort grep {
537 # skip unaccepted parameters
538 7 ▶ $unaccept and not $unaccept->{$_}
Condition33%1/3
$unaccept && !$unaccept->{$_}
!ll&&!rl&&r
0022
MC/DC0%0/2
$unaccept && !$unaccept->{$_}
$unaccept !$unaccept->{$_}
Truth table33%1/3
A && not(B)
A= $unacceptB= !$unaccept->{$_}
ABresult
0X0
100
111
539 } keys %$accept;
540  
541 # start figuring it out.
542 7 my @raw;
543 7 for my $param (@params)
544 {
545 22 my %opt;
546  
547 22 my $param_info = $accept->{$param};
548 22 ▶ my @keys = ($param, @{$param_info->{aliases}||[]});
Condition50%1/2
$param_info->{'aliases'} || []
l!l
220
MC/DC0%0/1
$param_info->{'aliases'} || []
$param_info->{'aliases'}
Truth table50%1/2
A
A= $param_info->{'aliases'}
Aresult
11
00
549  
550 # booleans get the "no" version.
551 22 ▶ if ($param_info->{spec} =~ /!/)
Branch: 1 branch
BranchTrueFalseTotal
if ($param_info->{"spec"} =~ /!/) 14 8 22
552 {
553 14 ▶ @keys = map { length > 1 ? ($_, "no$_") : $_ } @keys;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 14 14 28
554 }
555  
556 # anything with more than one letter gets a double-dash.
557 22 ▶ @keys = map { length > 1 ? "--$_" : "-$_" } @keys;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 36 21 57
558 22 $opt{param} = \@keys;
559  
560 $opt{help} = ref $param_info->{help} ?
561 22 ▶ $param_info->{help}->() : $param_info->{help};
Branch: 1 branch
BranchTrueFalseTotal
ref $param_info->{'help'} ? : 0 22 22
562  
563 # determine default (or set value)
564 22 my $default;
565 22 eval {
566 22 $opt{default} = $self->getOpt($param);
567  
568 22 my $type = $self->_opt($param)->{spec};
569 22 ▶ if ($type eq '' || $type eq '!') # boolean
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "" or $type eq "!") 14 8 22
Condition66%2/3
$type eq "" or $type eq "!"
l!l&&r!l&&!r
0148
MC/DC50%1/2
$type eq "" or $type eq "!"
$type eq "" $type eq "!"
Truth table66%2/3
A or B
A= $type eq ""B= $type eq "!"
ABresult
1X1
011
000
570 {
571 14 ▶ my $bools = ( $self->_opt($param)->{help_bool} or $self->{bool_strings} );
Condition66%2/3
$self->_opt($param)->{'help_bool'} || $self->{'bool_strings'}
l!l&&r!l&&!r
770
MC/DC0%0/2
$self->_opt($param)->{'help_bool'} || $self->{'bool_strings'}
$self->_opt($param)->{'help_bool'} $self->{'bool_strings'}
Truth table66%2/3
A || B
A= $self->_opt($param)->{'help_bool'}B= $self->{'bool_strings'}
ABresult
1X1
011
000
572  
573 14 ▶ $opt{default} = $bools->[$opt{default} ? 1 : 0];
Branch: 1 branch
BranchTrueFalseTotal
$opt{'default'} ? : 14 0 14
574 }
575 };
576  
577 # determine valid values.
578 22 eval {
579 22 $opt{valid_values} = $self->_opt($param)->{valid_values};
580  
581 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
582 # if it's not a code ref, the eval will exit, but we'll already
583 # have what we want anyway.
584 22 $opt{valid_values} = [ $opt{valid_values}->() ];
585 };
586  
587 # is it hidden? It's still part of the raw output.
588 22 ▶ $opt{hidden} = $param_info->{hidden} if exists $param_info->{hidden};
Branch: 1 branch
BranchTrueFalseTotal
if exists $param_info->{"hidden"} 2 20 22
589  
590 22 push @raw, \%opt;
591 }
592 7 return @raw;
593 }
594  
595  
596 sub getHelp
597 3 ▶{
Subroutine getHelp: called CC=7 SCAR=19.5
Pod documented
598 3 my $self = _self_or_global(\@_);
599 3 my @raw = grep { not $_->{hidden} } $self->getHelpRaw;
600 3 ▶ my $cbs = shift || {};
Condition100%2/2
shift() || {}
l!l
12
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
601  
602 3 require Text::Table;
603  
604 3 my $tb = Text::Table->new();
605 3 for my $param (@raw)
606 {
607 9 my $opt = join ",\n ", @{$param->{param}};
608 9 my $txt = $param->{help};
609 11 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
610  
611 9 ▶ $txt .= "\n " . ($cbs->{current_value} || sub { "Current value: [". shift(). "]" })->($param->{default}) if exists $param->{default};
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"default"} 9 0 9
Condition66%2/3
$cbs->{'current_value'} || sub { 'Current value: [' . (shift()) . ']'; }
l!l&&r!l&&!r
450
MC/DC0%0/2
$cbs->{'current_value'} || sub { 'Current value: [' . (shift()) . ']'; }
$cbs->{'current_value'} sub { 'Current value: [' . (shift()) . ']'; }
Truth table66%2/3
A || B
A= $cbs->{'current_value'}B= sub { 'Current value: [' . (shift()) . ']'; }
ABresult
1X1
011
000
612  
613 9 ▶ if ($param->{valid_values})
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"valid_values"}) 3 6 9
614 {
615 # if it's a code ref, de-ref it. If not, ignore the exception.
616 3 eval { $param->{valid_values} = [ $param->{valid_values}->() ] };
617 3 ▶ $txt .= "\n " . ($cbs->{valid_values} || sub { "Valid values: [". join(',', @_). "]" })->(@{$param->{valid_values}});
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition66%2/3
$cbs->{'valid_values'} || sub { 'Valid values: [' . join(',', @_) . ']'; }
l!l&&r!l&&!r
210
MC/DC0%0/2
$cbs->{'valid_values'} || sub { 'Valid values: [' . join(',', @_) . ']'; }
$cbs->{'valid_values'} sub { 'Valid values: [' . join(',', @_) . ']'; }
Truth table66%2/3
A || B
A= $cbs->{'valid_values'}B= sub { 'Valid values: [' . join(',', @_) . ']'; }
ABresult
1X1
011
000
618 }
619  
620 9 $tb->add($opt, $txt);
621 }
622 3 $tb;
623 }
624  
625  
626 sub getHelpWrap
627 2 ▶{
Subroutine getHelpWrap: called CC=8 SCAR=21.6
Pod documented
628 2 my $self = _self_or_global(\@_);
629 2 ▶ my $width = (@_ && not ref $_[0]) ? shift : 80;
Branch: 1 branch
BranchTrueFalseTotal
@_ && !ref($_[0]) ? : 1 1 2
Condition66%2/3
@_ and not ref $_[0]
!ll&&!rl&&r
101
MC/DC50%1/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
630 2 ▶ my $cbs = shift || {};
Condition50%1/2
shift() || {}
l!l
02
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
631 2 my @raw = grep { not $_->{hidden} } $self->getHelpRaw;
632  
633 2 require Text::Table;
634  
635 my $wrap = eval {
636 2 require Text::WrapI18N;
637 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
638 6 local $Text::WrapI18N::columns = shift;
639 6 local $Text::WrapI18N::unexpand;
640  
641 6 Text::WrapI18N::wrap('', '', @_);
642 2 };
643 2 ▶ } || do {
Condition33%1/3
eval { do { require Text::WrapI18N; sub { local $Text::WrapI18N::columns = shift(); local $Text::WrapI18N::unexpand; Text::WrapI18N::wrap('', '', @_); } } } || do { require Text::Wrap; sub { local $Text::Wrap::columns = shift(); local $Text::Wrap::unexpand; Text::Wrap::wrap('', '', @_); } }
l!l&&r!l&&!r
200
MC/DC0%0/2
eval { do { require Text::WrapI18N; sub { local $Text::WrapI18N::columns = shift(); local $Text::WrapI18N::unexpand; Text::WrapI18N::wrap('', '', @_); } } } || do { require Text::Wrap; sub { local $Text::Wrap::columns = shift(); local $Text::Wrap::unexpand; Text::Wrap::wrap('', '', @_); } }
eval { do { require Text::WrapI18N; sub { local $Text::WrapI18N::columns = shift(); local $Text::WrapI18N::unexpand; Text::WrapI18N::wrap('', '', @_); } } } do { require Text::Wrap; sub { local $Text::Wrap::columns = shift(); local $Text::Wrap::unexpand; Text::Wrap::wrap('', '', @_); } }
Truth table33%1/3
A || B
A= eval { do { require Text::WrapI18N; sub { local $Text::WrapI18N::columns = shift(); local $Text::WrapI18N::unexpand; Text::WrapI18N::wrap('', '', @_); } } }B= do { require Text::Wrap; sub { local $Text::Wrap::columns = shift(); local $Text::Wrap::unexpand; Text::Wrap::wrap('', '', @_); } }
ABresult
1X1
011
000
644 0 require Text::Wrap;
645 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
646 0 local $Text::Wrap::columns = shift;
647 0 local $Text::Wrap::unexpand;
648  
649 0 Text::Wrap::wrap('', '', @_);
650 }
651 0 };
652  
653 2 my $tb = Text::Table->new();
654 12 ▶ my $load_data = sub {
Subroutine __ANON__: called CC=6 SCAR=18.3
655 12 my $tb = shift;
656 12 my $param = shift;
657  
658 12 my $opt = join ",\n ", @{$param->{param}};
659 12 my $txt = shift;
660  
661 12 my $available = shift;
662  
663 11 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
664  
665 12 ▶ $txt .= "\n " . ($cbs->{current_value} || sub { "Current value: [". shift(). "]" })->($param->{default}) if exists $param->{default};
Subroutine __ANON__: called CC=1 SCAR=0.8
Branch: 1 branch
BranchTrueFalseTotal
if exists $param->{"default"} 12 0 12
Condition33%1/3
$cbs->{'current_value'} || sub { 'Current value: [' . (shift()) . ']'; }
l!l&&r!l&&!r
0120
MC/DC0%0/2
$cbs->{'current_value'} || sub { 'Current value: [' . (shift()) . ']'; }
$cbs->{'current_value'} sub { 'Current value: [' . (shift()) . ']'; }
Truth table33%1/3
A || B
A= $cbs->{'current_value'}B= sub { 'Current value: [' . (shift()) . ']'; }
ABresult
1X1
011
000
666 12 ▶ $txt .= "\n " . ($cbs->{valid_values} || sub { "Valid values: [". join(',', @_). "]" })->(@{$param->{valid_values}}) if $param->{valid_values};
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"valid_values"} 4 8 12
Condition33%1/3
$cbs->{'valid_values'} || sub { 'Valid values: [' . join(',', @_) . ']'; }
l!l&&r!l&&!r
040
MC/DC0%0/2
$cbs->{'valid_values'} || sub { 'Valid values: [' . join(',', @_) . ']'; }
$cbs->{'valid_values'} sub { 'Valid values: [' . join(',', @_) . ']'; }
Truth table33%1/3
A || B
A= $cbs->{'valid_values'}B= sub { 'Valid values: [' . join(',', @_) . ']'; }
ABresult
1X1
011
000
667  
668 # wrap all lines
669 12 ▶ $txt = $wrap->($available, $txt) if $available;
Branch: 1 branch
BranchTrueFalseTotal
if $available 6 6 12
670  
671 12 $tb->add($opt, $txt);
672 2 };
673  
674 2 for my $param (@raw)
675 {
676 6 $load_data->($tb, $param, $param->{help});
677 }
678  
679 2 ▶ if ($tb->width > $width)
Branch: 1 branch
BranchTrueFalseTotal
if ($tb->width > $width) 2 0 2
680 {
681 # rebuild, wrapped.
682 2 my @colrange = $tb->colrange(0);
683 2 my $available = $width - $colrange[1] - 1; # 1 for extra space between columns
684  
685 2 $tb->clear();
686 2 for my $param (@raw)
687 {
688 6 $load_data->($tb, $param, $param->{help}, $available);
689 }
690 }
691  
692 # if the current value or valid values are a block of text too long, we don't want all
693 # lines to be too long, so clobber the extra spaces that Text::Table puts in at the end.
694 2 (my $txt = "".$tb) =~ s/\s+$//msg;
695 2 $txt .= "\n";
696 2 $txt;
697 }
698  
699  
700 11 1; # End of Getopt::Modular
701  
702 __END__
↑ Summary