Opts.pm

filter: Statementstmt 99.2% 127 / 128 Branchbran 88.2% 60 / 68 Conditioncond 83.8% 26 / 31 MC/DCmcdc 55.5% 10 / 18 Subroutinesub 100% 20 / 20 Podpod 75.0% 6 / 8 totaltotal 91.2% 249 / 273 CC 50 SCAR 24.4
CC
50
Cov
94%
opt
24.0
_validate
10.0
parse
9.0
CRAP 11.5
« Args.pm ↑ Summary CLI.pm »
1 package Declare::Opts;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = "0.009";
6  
7 7 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 ▶use Exporter::Declare qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 import
11 gen_default_export
12 default_export
13 7 };
14  
15 7 ▶gen_default_export 'OPTS_META' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 7 my ( $class, $caller ) = @_;
17 7 my $meta = $class->new();
18 7 $meta->{class} = $caller;
19 7 ▶ return sub { $meta };
Subroutine __ANON__: called CC=1 SCAR=0.0
20 7 };
21  
22 20 ▶default_export opt => sub { caller->OPTS_META->opt( @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
23 12 ▶default_export parse_opts => sub { caller->OPTS_META->parse( @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 ▶default_export opt_info => sub { caller->OPTS_META->info };
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 1 ▶sub class { shift->{class} }
Subroutine class: called CC=1 SCAR=0.0
Pod documented
27 145 ▶sub opts { shift->{opts} }
Subroutine opts: called CC=1 SCAR=0.0
Pod undocumented
28 9 ▶sub default { shift->{default} }
Subroutine default: called CC=1 SCAR=0.0
Pod documented
29  
30 7 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
31 7 my $class = shift;
32 7 my ( %opts ) = @_;
33  
34 7 my $self = bless { opts => {}, default => {} } => $class;
35 7 $self->opt( $_, $opts{$_} ) for keys %opts;
36  
37 7 return $self;
38 }
39  
40 24 ▶sub valid_opt_params {
Subroutine valid_opt_params: called CC=1 SCAR=0.0
Pod undocumented
41 24 return qr/^(alias|list|bool|default|check|transform|description)$/;
42 }
43  
44 20 ▶sub opt {
Subroutine opt: called CC=22 SCAR=31.8
Pod documented
45 20 my $self = shift;
46 20 my ( $name, %config ) = @_;
47  
48 croak "opt '$name' already defined"
49 20 ▶ if $self->opts->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opts->{$name} 0 20 20
50  
51 20 for my $prop ( keys %config ) {
52 24 ▶ next if $prop =~ $self->valid_opt_params;
Branch: 1 branch
BranchTrueFalseTotal
if $prop =~ $self->valid_opt_params 24 0 24
53 0 croak "invalid opt property: '$prop'";
54 }
55  
56 20 $config{name} = $name;
57  
58 croak "'check' cannot be used with 'bool'"
59 20 ▶ if $config{bool} && $config{check};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"bool"} and $config{"check"} 0 20 20
Condition66%2/3
$config{"bool"} and $config{"check"}
!ll&&!rl&&r
1730
MC/DC0%0/2
$config{"bool"} and $config{"check"}
$config{"bool"} $config{"check"}
Truth table66%2/3
A and B
A= $config{"bool"}B= $config{"check"}
ABresult
0X0
100
111
60  
61 croak "'transform' cannot be used with 'bool'"
62 20 ▶ if $config{bool} && $config{transform};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"bool"} and $config{"transform"} 0 20 20
Condition66%2/3
$config{"bool"} and $config{"transform"}
!ll&&!rl&&r
1730
MC/DC0%0/2
$config{"bool"} and $config{"transform"}
$config{"bool"} $config{"transform"}
Truth table66%2/3
A and B
A= $config{"bool"}B= $config{"transform"}
ABresult
0X0
100
111
63  
64 croak "opt properties 'list' and 'bool' are mutually exclusive"
65 20 ▶ if $config{list} && $config{bool};
Branch: 1 branch
BranchTrueFalseTotal
if $config{"list"} and $config{"bool"} 1 19 20
Condition100%3/3
$config{"list"} and $config{"bool"}
!ll&&!rl&&r
1541
MC/DC100%2/2
$config{"list"} and $config{"bool"}
$config{"list"} $config{"bool"}
Truth table100%3/3
A and B
A= $config{"list"}B= $config{"bool"}
ABresult
0X0
100
111
66  
67 19 ▶ if (exists $config{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{"default"}) 2 17 19
68 croak "References cannot be used in default, wrap them in a sub."
69 2 ▶ if ref $config{default} && ref $config{default} ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $config{"default"} and ref $config{"default"} ne "CODE" 0 2 2
Condition33%1/3
ref $config{"default"} and ref $config{"default"} ne "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
ref $config{"default"} and ref $config{"default"} ne "CODE"
ref $config{"default"} ref $config{"default"} ne "CODE"
Truth table33%1/3
A and B
A= ref $config{"default"}B= ref $config{"default"} ne "CODE"
ABresult
0X0
100
111
70 2 $self->default->{$name} = $config{default};
71 }
72  
73 19 ▶ if ( exists $config{check} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{"check"}) 9 10 19
74 9 my $ref = ref $config{check};
75 croak "'$config{check}' is not a valid value for 'check'"
76 if ($ref && $ref !~ m/^(CODE|Regexp)$/)
77 9 ▶ || (!$ref && $config{check} !~ m/^(file|dir|number)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $ref and not $ref =~ /^(CODE|Regexp)$/ or not $ref || $config{"check"} =~ /^(file|dir|number)$/ 2 7 9
Condition100%9/9
$ref and not $ref =~ /^(CODE|Regexp)$/ or not $ref || $config{"check"} =~ /^(file|dir|number)$/
l!l&&r!l&&!r
117
$ref and not $ref =~ /^(CODE|Regexp)$/
!ll&&!rl&&r
621
$ref || $config{'check'} =~ /^(file|dir|number)$/
l!l&&r!l&&!r
251
MC/DC100%4/4
$ref and not $ref =~ /^(CODE|Regexp)$/ or not $ref || $config{"check"} =~ /^(file|dir|number)$/
$ref not $ref =~ /^(CODE|Regexp)$/ $ref $config{'check'} =~ /^(file|dir|number)$/
Truth table57%4/7
A and not(B) or not(C || D)
A= $refB= not $ref =~ /^(CODE|Regexp)$/C= $refD= $config{'check'} =~ /^(file|dir|number)$/
ABCDresult
11XX1
0X001
10001
0X1X0
0X010
101X0
10010
78 }
79  
80 17 ▶ if ( exists $config{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{"alias"}) 2 15 17
81 my $aliases = ref $config{alias} ? $config{alias}
82 2 ▶ : [ $config{alias} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $config{'alias'} ? : 1 1 2
83  
84 2 $config{_alias} = { map { $_ => 1 } @$aliases };
85  
86 2 for my $alias ( @$aliases ) {
87 croak "Cannot use alias '$alias', name is already taken by another opt."
88 3 ▶ if $self->opts->{$alias};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opts->{$alias} 0 3 3
89  
90 3 $self->opts->{$alias} = \%config;
91 }
92 }
93  
94 17 $self->opts->{$name} = \%config;
95 }
96  
97 12 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.0
Pod documented
98 12 my $self = shift;
99 12 my @opts = @_;
100  
101 12 my $params = [];
102 12 my $flags = {};
103 12 my $no_flags = 0;
104  
105 12 ▶ while ( my $opt = shift @opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = shift @opts) 33 5 38
106 33 ▶ if ( $opt eq '--' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt eq '--') { } 3 30 33
elsif ($opt =~ /^-+([^-=]+)(?:=(.+))?$/ and not $no_flags) { } 24 6 30
107 3 $no_flags++;
108 }
109 ▶ elsif ( $opt =~ m/^-+([^-=]+)(?:=(.+))?$/ && !$no_flags ) {
Condition100%3/3
$opt =~ /^-+([^-=]+)(?:=(.+))?$/ and not $no_flags
!ll&&!rl&&r
3324
MC/DC100%2/2
$opt =~ /^-+([^-=]+)(?:=(.+))?$/ and not $no_flags
$opt =~ /^-+([^-=]+)(?:=(.+))?$/ not $no_flags
Truth table100%3/3
A and not(B)
A= $opt =~ /^-+([^-=]+)(?:=(.+))?$/B= not $no_flags
ABresult
0X0
100
111
110 24 my ( $key, $value ) = ( $1, $2 );
111  
112 24 my $name = $self->_flag_name( $key );
113 22 my $values = $self->_flag_value(
114 $name,
115 $value,
116 \@opts
117 );
118  
119 17 ▶ if( $self->opts->{$name}->{list} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opts->{$name}{'list'}) { } 5 12 17
120 5 push @{$flags->{$name}} => @$values;
121 }
122 else {
123 12 $flags->{$name} = $values->[0];
124 }
125 }
126 else {
127 6 push @$params => $opt;
128 }
129 }
130  
131 # Add defaults for opts not provided
132 5 for my $opt ( keys %{ $self->default } ) {
133 4 ▶ next if exists $flags->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $flags->{$opt} 2 2 4
134 2 my $val = $self->default->{$opt};
135 2 ▶ $flags->{$opt} = ref $val ? $val->() : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 0 2 2
136 }
137  
138 5 return ( $params, $flags );
139 }
140  
141 2 ▶sub info {
Subroutine info: called CC=2 SCAR=6.9
Pod documented
142 2 my $self = shift;
143 return {
144 4 ▶ map { $self->opts->{$_}->{name} => $self->opts->{$_}->{description} || "No Description" }
Condition100%2/2
$self->opts->{$_}{'description'} || 'No Description'
l!l
22
MC/DC100%1/1
$self->opts->{$_}{'description'} || 'No Description'
$self->opts->{$_}{'description'}
Truth table100%2/2
A
A= $self->opts->{$_}{'description'}
Aresult
11
00
145 2 keys %{ $self->opts }
146 };
147 }
148  
149 22 ▶sub _flag_value {
Subroutine _flag_value: called CC=7 SCAR=19.5
150 22 my $self = shift;
151 22 my ( $flag, $value, $opts ) = @_;
152  
153 22 my $spec = $self->opts->{$flag};
154  
155 22 ▶ if ( $spec->{bool} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"bool"}) 3 19 22
156 3 ▶ return [$value] if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 2 3
157 2 ▶ return [$spec->{default} ? 0 : 1];
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'default'} ? : 1 1 2
158 }
159  
160 19 ▶ my $val = defined $value ? $value : shift @$opts;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 5 14 19
161  
162 19 ▶ my $out = $spec->{list} ? [ split /\s*,\s*/, $val ]
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'list'} ? : 7 12 19
163 : [ $val ];
164  
165 19 $self->_validate( $flag, $spec, $out );
166  
167 14 ▶ return $out unless $spec->{transform};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"transform"} 12 2 14
168 2 return [ map { $spec->{transform}->($_) } @$out ];
169 }
170  
171 19 ▶sub _validate {
Subroutine _validate: called CC=10 SCAR=23.0
172 19 my $self = shift;
173 19 my ( $flag, $spec, $value ) = @_;
174  
175 19 my $check = $spec->{check};
176 19 ▶ return unless $check;
Branch: 1 branch
BranchTrueFalseTotal
unless $check 7 12 19
177 12 ▶ my $ref = ref $check || "";
Condition100%2/2
ref $check || ''
l!l
48
MC/DC100%1/1
ref $check || ''
ref $check
Truth table100%2/2
A
A= ref $check
Aresult
11
00
178  
179 12 my @bad;
180  
181 12 ▶ if ( $ref eq 'Regexp' ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 2 10 12
elsif ($ref eq 'CODE') { } 2 8 10
elsif ($check eq 'file') { } 2 6 8
elsif ($check eq 'dir') { } 2 4 6
elsif ($check eq 'number') { } 4 0 4
182 2 @bad = grep { $_ !~ $check } @$value;
183 }
184 elsif ( $ref eq 'CODE' ) {
185 2 @bad = grep { !$check->( $_ ) } @$value;
186 }
187 elsif ( $check eq 'file' ) {
188 2 @bad = grep { ! -f $_ } @$value;
189 }
190 elsif ( $check eq 'dir' ) {
191 2 @bad = grep { ! -d $_ } @$value;
192 }
193 elsif ( $check eq 'number' ) {
194 4 @bad = grep { m/\D/ } @$value;
195 }
196  
197 12 ▶ return unless @bad;
Branch: 1 branch
BranchTrueFalseTotal
unless @bad 7 5 12
198 5 ▶ my $type = $ref || $check;
Condition66%2/3
$ref || $check
l!l&&r!l&&!r
230
MC/DC0%0/2
$ref || $check
$ref $check
Truth table66%2/3
A || B
A= $refB= $check
ABresult
1X1
011
000
199 5 die "Validation Failed for '$flag=$type': " . join( ", ", @bad ) . "\n";
200 }
201  
202 24 ▶sub _flag_name {
Subroutine _flag_name: called CC=4 SCAR=13.9
203 24 my $self = shift;
204 24 my ( $key ) = @_;
205  
206 # Exact match
207 return $self->opts->{$key}->{name}
208 24 ▶ if $self->opts->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->opts->{$key} 20 4 24
209  
210 4 my %matches = map { $self->opts->{$_}->{name} => 1 }
211 22 grep { m/^$key/ }
212 4 keys %{ $self->opts };
213 4 my @matches = keys %matches;
214  
215 4 ▶ die "partial option '$key' is ambiguous, could be: " . join( ", " => @matches ) . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if @matches > 1 1 3 4
216 if @matches > 1;
217  
218 3 ▶ die "unknown option '$key'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @matches 1 2 3
219 unless @matches;
220  
221 2 return $matches[0];
222 }
223  
224 7 1;
225  
226 __END__
« Args.pm ↑ Summary CLI.pm »