blib/lib/opts.pm

filter: Statementstmt 97.2% 104 / 107 Branchbran 88.4% 46 / 52 Conditioncond 61.5% 8 / 13 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 88.6% 172 / 194 CC 34 SCAR 28.2
CC
34
Cov
92%
opts
25.1
_compile_rule
11.7
BEGIN
1.0
CRAP 16.7
↑ Summary
1 package opts;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 our $VERSION = '0.08';
5 13 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use PadWalker qw/var_name/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our @EXPORT = qw/opts/;
11  
12 13 our $TYPE_CONSTRAINT = {
13 'Bool' => '!',
14 'Str' => '=s',
15 'Int' => '=i',
16 'Num' => '=f',
17 'ArrayRef' => '=s@',
18 'HashRef' => '=s%',
19 };
20  
21 13 my %is_invocant = map{ $_ => undef } qw($self $class);
22  
23 13 my $coerce_type_map = {
24 Multiple => 'ArrayRef',
25 };
26  
27 my $coerce_generater = {
28 3 ▶ Multiple => sub { [ split(qr{,}, join(q{,}, @{ $_[0] })) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 13 };
30  
31 29 ▶sub opts {
Subroutine opts: called CC=25 SCAR=32.2
Pod undocumented
32 {
33 29 package DB;
34 # call of caller in DB package sets @DB::args,
35 # which requires list context, but does not use return values
36 29 () = caller(1);
37 }
38  
39 # method call
40 29 ▶ if(exists $is_invocant{ var_name(1, \$_[0]) || '' }){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $is_invocant{var_name(1, \$_[0]) or ""}) 6 23 29
Condition50%1/2
var_name(1, \$_[0]) || ''
l!l
290
MC/DC0%0/1
var_name(1, \$_[0]) || ''
var_name(1, \$_[0])
Truth table50%1/2
A
A= var_name(1, \$_[0])
Aresult
11
00
41 6 $_[0] = shift @DB::args;
42 6 shift;
43 # XXX: should we provide ways to check the type of invocant?
44 }
45  
46 # track our coderef defaults
47 29 my %default_subs;
48  
49 29 my @options = ('help|h!' => \my $help);
50 29 my %requireds;
51 my %generaters;
52 29 my $usage;
53 29 my @option_help;
54 29 ▶ for(my $i = 0; $i < @_; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 37 29 66
55  
56 37 ▶ (my $name = var_name(1, \$_[$i]))
Branch: 1 branch
BranchTrueFalseTotal
my $name = var_name(1, \$_[$i]) or Carp::croak("usage: opts my \$var => TYPE, ...") 37 0 37
57 or Carp::croak('usage: opts my $var => TYPE, ...');
58  
59 37 $name =~ s/^\$//;
60  
61 37 my $rule = _compile_rule($_[$i+1]);
62  
63 37 ▶ if ($name =~ /_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /_/) 5 32 37
64  
65 # Name has underscores in it, which is annoying for command line
66 # arguments. Swap them and create / add to alias.
67 5 (my $newname = $name) =~ s/_/-/g;
68  
69 $rule->{alias}
70 = $rule->{alias}
71 ? $name . q{|} . $rule->{alias}
72 5 ▶ : $name
Branch: 1 branch
BranchTrueFalseTotal
$rule->{'alias'} ? : 3 2 5
73 ;
74  
75 5 $name = $newname;
76 }
77  
78 37 ▶ if (exists $rule->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"default"}) 2 35 37
79  
80 2 ▶ if (ref $rule->{default} && ref $rule->{default} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rule->{'default'} and ref $rule->{'default'} eq 'CODE') { } 1 1 2
Condition66%2/3
ref $rule->{"default"} and ref $rule->{"default"} eq "CODE"
!ll&&!rl&&r
101
MC/DC50%1/2
ref $rule->{"default"} and ref $rule->{"default"} eq "CODE"
ref $rule->{"default"} ref $rule->{"default"} eq "CODE"
Truth table66%2/3
A and B
A= ref $rule->{"default"}B= ref $rule->{"default"} eq "CODE"
ABresult
0X0
100
111
81 1 $default_subs{$i} = $rule->{default};
82 1 $_[$i] = undef;
83 }
84 else {
85 1 $_[$i] = $rule->{default};
86 }
87 }
88  
89 37 ▶ if (exists $rule->{required}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{"required"}) 3 34 37
90 3 $requireds{$name} = $i;
91 }
92  
93
94 37 ▶ my $comment = $rule->{comment} || "";
Condition100%2/2
$rule->{'comment'} || ''
l!l
136
MC/DC100%1/1
$rule->{'comment'} || ''
$rule->{'comment'}
Truth table100%2/2
A
A= $rule->{'comment'}
Aresult
11
00
95 37 my @names = (substr($name,0,1), $name);
96 37 ▶ push @names, $rule->{alias} if $rule->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"alias"} 8 29 37
97 37 ▶ my $optname = join(', ', map { (length($_) > 1 ? '--' : '-').$_ } @names);
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 23 59 82
98 37 push @option_help, [ $optname, ucfirst($comment) ];
99  
100 37 ▶ if (my $gen = $coerce_generater->{$rule->{isa}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gen = $$coerce_generater{$rule->{"isa"}}) 4 33 37
101 4 $generaters{$name} = { idx => $i, gen => $gen };
102 }
103  
104 37 ▶ $name .= '|' . $rule->{alias} if $rule->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"alias"} 8 29 37
105 37 push @options, $name . $rule->{type} => \$_[$i];
106  
107 37 ▶ $i++ if defined $_[$i+1]; # discard type info
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[$i + 1] 33 4 37
108 }
109
110 {
111 29 my $err;
112 29 ▶ local $SIG{__WARN__} = sub { $err = shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
113 29 ▶ GetOptions(@options) or Carp::croak($err);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(@options) or Carp::croak($err) 27 2 29
114 27 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 1 26 27
115 1 $usage = "usage: $0 [options]\n\n";
116  
117 1 ▶ if (@option_help) {
Branch: 1 branch
BranchTrueFalseTotal
if (@option_help) 1 0 1
118 1 require Text::Table;
119 1 push @option_help, ['-h, --help', 'This help message'];
120 1 my $sep = \' ';
121 1 $usage .= "options:\n";
122 1 $usage .= Text::Table->new($sep, '', $sep, '')->load(@option_help)->stringify."\n";
123 }
124  
125 1 die $usage;
126 }
127  
128 1 ▶ do { $_[$_] = $default_subs{$_}->() unless defined $_[$_] }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[$_] 1 0 1
129 26 for keys %default_subs;
130  
131 26 ▶ while ( my ($name, $idx) = each %requireds ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %requireds) 3 25 28
132 3 ▶ unless (defined($_[$idx])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[$idx]) 1 2 3
133 1 Carp::croak("missing mandatory parameter named '\$$name'");
134 }
135 }
136 25 ▶ while ( my ($name, $val) = each %generaters ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %generaters) 4 25 29
137 4 $_[$val->{idx}] = $val->{gen}->($_[$val->{idx}]);
138 }
139 }
140 }
141  
142 1 ▶sub coerce ($$&) { ## no critic
Subroutine coerce: called CC=1 SCAR=0.0
Pod undocumented
143 1 my ($isa, $type, $generater) = @_;
144  
145 1 $coerce_type_map->{$isa} = $type;
146 1 $coerce_generater->{$isa} = $generater;
147 }
148  
149 37 ▶sub _compile_rule {
Subroutine _compile_rule: called CC=10 SCAR=24.6
150 37 my ($rule) = @_;
151 37 ▶ if (!defined $rule) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $rule) { } 4 33 37
elsif (not ref $rule) { } 16 17 33
152 4 return +{ type => "!", isa => 'Bool' };
153 }
154 elsif (!ref $rule) { # single, non-ref parameter is a type name
155 my $tc = _get_type_constraint($rule) ||
156 16 ▶ _get_type_constraint($coerce_type_map->{$rule}) or
Branch: 1 branch
BranchTrueFalseTotal
my $tc = _get_type_constraint($rule) || _get_type_constraint($coerce_type_map->{$rule}) or Carp::croak("cannot find type constraint '${rule}'") 16 0 16
Condition66%2/3
_get_type_constraint($rule) || _get_type_constraint($coerce_type_map->{$rule})
l!l&&r!l&&!r
1240
MC/DC0%0/2
_get_type_constraint($rule) || _get_type_constraint($coerce_type_map->{$rule})
_get_type_constraint($rule) _get_type_constraint($coerce_type_map->{$rule})
Truth table66%2/3
A || B
A= _get_type_constraint($rule)B= _get_type_constraint($coerce_type_map->{$rule})
ABresult
1X1
011
000
157 Carp::croak("cannot find type constraint '$rule'");
158 16 return +{ type => $tc, isa => $rule };
159 }
160 else {
161 17 my %ret;
162 17 ▶ if ($rule->{isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{'isa'}) { } 17 0 17
163 17 $ret{isa} = $rule->{isa};
164 my $tc = _get_type_constraint($rule->{isa}) ||
165 17 ▶ _get_type_constraint($coerce_type_map->{$rule->{isa}}) or
Branch: 1 branch
BranchTrueFalseTotal
my $tc = _get_type_constraint($rule->{"isa"}) || _get_type_constraint($$coerce_type_map{$rule->{"isa"}}) or Carp::croak(qq[cannot find type constraint '@{[$rule->{"isa"}];}']) 17 0 17
Condition33%1/3
_get_type_constraint($rule->{'isa'}) || _get_type_constraint($$coerce_type_map{$rule->{'isa'}})
l!l&&r!l&&!r
1700
MC/DC0%0/2
_get_type_constraint($rule->{'isa'}) || _get_type_constraint($$coerce_type_map{$rule->{'isa'}})
_get_type_constraint($rule->{'isa'}) _get_type_constraint($$coerce_type_map{$rule->{'isa'}})
Truth table33%1/3
A || B
A= _get_type_constraint($rule->{'isa'})B= _get_type_constraint($$coerce_type_map{$rule->{'isa'}})
ABresult
1X1
011
000
166 0 Carp::croak("cannot find type constraint '@{[$rule->{isa}]}'");
167 17 $ret{type} = $tc;
168 } else {
169 0 $ret{isa} = 'Bool';
170 0 $ret{type} = "!";
171 }
172 17 for my $key (qw(alias default required comment)) {
173 68 ▶ if (exists $rule->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rule->{$key}) 12 56 68
174 12 $ret{$key} = $rule->{$key};
175 }
176 }
177 17 return \%ret;
178 }
179 }
180  
181 37 ▶sub _get_type_constraint {
Subroutine _get_type_constraint: called CC=1 SCAR=0.0
182 37 my $isa = shift;
183  
184 37 $TYPE_CONSTRAINT->{$isa};
185 }
186  
187 13 1;
188 __END__
↑ Summary