Options.pm

filter: Statementstmt 83.3% 90 / 108 Branchbran 72.7% 32 / 44 Conditioncond 57.1% 12 / 21 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 75.2% 149 / 198 SCAR 37.8
CC
32
Cov
77%
_validate_and_filter_options
33.8
import
9.3
__ANON__
6.9
CRAP 43.7
« Options/Role.pm ↑ Summary Options/Descriptive.pm »
1 package MooX::Options;
2  
3 30 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 22 our $VERSION = "4.102";
6  
7 30 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my @OPTIONS_ATTRIBUTES
11 22 = qw/format short repeatable negatable autosplit autorange doc long_doc order json hidden spacer_before spacer_after/;
12  
13 sub import {
14 68 ▶ my ( undef, @import ) = @_;
Subroutine import: called CC=9 SCAR=22.3
15 68 my $options_config = {
16 protect_argv => 1,
17 flavour => [],
18 skip_options => [],
19 prefer_commandline => 0,
20 with_config_from_file => 0,
21 with_locale_textdomain_oo => 0,
22 usage_string => undef,
23  
24 #long description (manual)
25 description => undef,
26 authors => [],
27 synopsis => undef,
28 spacer => " ",
29 @import
30 };
31  
32 68 my $target = caller;
33 68 for my $needed_methods (qw/with around has/) {
34 197 ▶ next if $target->can($needed_methods);
Branch: 1 branch
BranchTrueFalseTotal
if $target->can($needed_methods) 192 3 195
35 5 croak( "Can't find the method <$needed_methods> in <$target>!\n"
36 . "Ensure to load a Role::Tiny compatible module like Moo or Moose before using MooX::Options."
37 );
38 }
39  
40 65 my $with = $target->can('with');
41 65 my $around = $target->can('around');
42 65 my $has = $target->can('has');
43  
44 65 my @target_isa;
45 24 ▶ { no strict 'refs'; @target_isa = @{"${target}::ISA"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 65 ▶ if (@target_isa) { #only in the main class, not a role
Branch: 1 branch
BranchTrueFalseTotal
if (@target_isa) { } 58 5 63
48  
49 ## no critic (ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval, ValuesAndExpressions::ProhibitImplicitNewlines)
50 60 eval "#line ${\(__LINE__+1 . ' ' . __FILE__)}\n" . '{
51 package ' . $target . ';
52 use MRO::Compat ();
53 24 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
54 sub _options_data {
55 my ( $class, @meta ) = @_;
56 return $class->maybe::next::method(@meta);
57 }
58  
59 sub _options_config {
60 my ( $class, @params ) = @_;
61 return $class->maybe::next::method(@params);
62 }
63  
64 1;
65 }';
66  
67 60 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 57 58
68  
69 $around->(
70 _options_config => sub {
71 105 ▶ my ( $orig, $self ) = ( shift, shift );
Subroutine __ANON__: called CC=1 SCAR=0.0
72 105 return $self->$orig(@_), %$options_config;
73 }
74 59 );
75  
76 ## use critic
77 }
78 else {
79 5 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 5 5
80 0 croak(
81 "Please, don't use the option <with_config_from_file> into a role."
82 );
83 }
84 }
85  
86 62 my $options_data = {};
87 62 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 62 62
88 $options_data->{config_prefix} = {
89 0 format => 's',
90 doc => 'config prefix',
91 order => 0,
92 };
93 $options_data->{config_files} = {
94 0 format => 's@',
95 doc => 'config files',
96 order => 0,
97 };
98 }
99  
100 my $apply_modifiers = sub {
101 107 ▶ return if $target->can('new_with_options');
Subroutine __ANON__: called CC=5 SCAR=19.3
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("new_with_options") 44 63 107
102 63 $with->('MooX::Options::Role');
103 63 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 63 63
104 0 $with->('MooX::ConfigFromFile::Role');
105 }
106 63 ▶ if ( $options_config->{with_locale_textdomain_oo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_locale_textdomain_oo"}) 0 63 63
107 0 $with->('MooX::Locale::TextDomain::OO');
108 0 use_module("MooX::Options::Descriptive::Usage");
109 0 ▶ MooX::Options::Descriptive::Usage->can("localizer")
Branch: 1 branch
BranchTrueFalseTotal
"MooX::Options::Descriptive::Usage"->can("localizer") or "MooX::Options::Descriptive::Usage"->can("with")->("MooX::Locale::TextDomain::OO") 0 0 0
110 or MooX::Options::Descriptive::Usage->can("with")
111 ->("MooX::Locale::TextDomain::OO");
112 }
113  
114 $around->(
115 _options_data => sub {
116 121 ▶ my ( $orig, $self ) = ( shift, shift );
Subroutine __ANON__: called CC=1 SCAR=0.0
117 121 return ( $self->$orig(@_), %$options_data );
118 }
119 63 );
120 62 };
121  
122 my @banish_keywords
123 62 = qw/h help man usage option new_with_options parse_options options_usage _options_data _options_config/;
124 62 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 62 62
125 0 push @banish_keywords, qw/config_files config_prefix config_dirs/;
126 }
127  
128 my $option = sub {
129 56 ▶ my ( $name, %attributes ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
130 56 for my $ban (@banish_keywords) {
131 524 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $ban 9 515 524
132 "You cannot use an option with the name '$ban', it is implied by MooX::Options"
133 ) if $name eq $ban;
134 }
135  
136 47 my %_moo_attrs = _filter_attributes(%attributes);
137 47 ▶ $has->( $name => %_moo_attrs ) if %_moo_attrs;
Branch: 1 branch
BranchTrueFalseTotal
if %_moo_attrs 46 1 47
138  
139 ## no critic (RegularExpressions::RequireExtendedFormatting)
140 47 $name =~ s/^\+//; # one enhances an attribute being an option
141 47 $options_data->{$name}
142 = { _validate_and_filter_options(%attributes) };
143  
144 45 $apply_modifiers->();
145 45 return;
146 62 };
147  
148 62 ▶ if ( my $info = $Role::Tiny::INFO{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $Role::Tiny::INFO{$target}) 5 57 62
149 5 $info->{not_methods}{$option} = $option;
150 }
151  
152 24 ▶ { no strict 'refs'; *{"${target}::option"} = $option; }
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 62 $apply_modifiers->();
155  
156 62 return;
157 }
158  
159 22 my %filter_key = map { $_ => 1 } ( @OPTIONS_ATTRIBUTES, 'negativable' );
160  
161 sub _filter_attributes {
162 47 ▶ my %attributes = @_;
Subroutine _filter_attributes: called CC=1 SCAR=0.0
163 66 return map { ( $_ => $attributes{$_} ) }
164 47 grep { !exists $filter_key{$_} } keys %attributes;
165 }
166  
167 sub _validate_and_filter_options {
168 47 ▶ my (%options) = @_;
Subroutine _validate_and_filter_options: called CC=17 SCAR=35.2
169 47 ▶ $options{doc} = $options{documentation} if !defined $options{doc};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"doc"} 39 8 47
170 47 ▶ $options{order} = 0 if !defined $options{order};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"order"} 41 6 47
171  
172 47 ▶ if ( $options{json}
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"json"} or defined $options{"format"} and $options{"format"} eq "json") 0 47 47
Condition50%3/6
$options{"json"} or defined $options{"format"} and $options{"format"} eq "json"
l!l&&r!l&&!r
0047
defined $options{"format"} and $options{"format"} eq "json"
!ll&&!rl&&r
34130
MC/DC0%0/3
$options{"json"} or defined $options{"format"} and $options{"format"} eq "json"
$options{"json"} defined $options{"format"} $options{"format"} eq "json"
Truth table25%1/4
A or B and C
A= $options{"json"}B= defined $options{"format"}C= $options{"format"} eq "json"
ABCresult
1XX1
0111
00X0
0100
173 || ( defined $options{format} && $options{format} eq 'json' ) )
174 {
175 0 delete $options{repeatable};
176 0 delete $options{autosplit};
177 0 delete $options{autorange};
178 0 delete $options{negativable};
179 0 delete $options{negatable};
180 0 $options{json} = 1;
181 0 $options{format} = 's';
182 }
183  
184 47 ▶ if ( $options{autorange} and not defined $options{autosplit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"autorange"} and not defined $options{"autosplit"}) 0 47 47
Condition33%1/3
$options{"autorange"} and not defined $options{"autosplit"}
!ll&&!rl&&r
4700
MC/DC0%0/2
$options{"autorange"} and not defined $options{"autosplit"}
$options{"autorange"} not defined $options{"autosplit"}
Truth table33%1/3
A and not(B)
A= $options{"autorange"}B= not defined $options{"autosplit"}
ABresult
0X0
100
111
185  
186 # XXX maybe we should warn here since a previously beloved feature isn't enabled automatically
187 0 eval { use_module("Data::Record"); use_module("Regexp::Common"); }
188 0 ▶ and $options{autosplit} = ',';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &use_module("Data::Record"); &use_module("Regexp::Common") } } and $options{"autosplit"} = "," 0 0 0
189 }
190  
191 exists $options{negativable}
192 47 ▶ and $options{negatable} = delete $options{negativable};
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"negativable"} and $options{"negatable"} = delete $options{"negativable"} 1 46 47
193  
194 118 my %cmdline_options = map { ( $_ => $options{$_} ) }
195 47 grep { exists $options{$_} } @OPTIONS_ATTRIBUTES, 'required';
196  
197 $cmdline_options{repeatable} = 1
198 47 ▶ if $cmdline_options{autosplit} or $cmdline_options{autorange};
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline_options{"autosplit"} or $cmdline_options{"autorange"} 3 44 47
Condition66%2/3
$cmdline_options{"autosplit"} or $cmdline_options{"autorange"}
l!l&&r!l&&!r
3044
MC/DC50%1/2
$cmdline_options{"autosplit"} or $cmdline_options{"autorange"}
$cmdline_options{"autosplit"} $cmdline_options{"autorange"}
Truth table66%2/3
A or B
A= $cmdline_options{"autosplit"}B= $cmdline_options{"autorange"}
ABresult
1X1
011
000
199 $cmdline_options{format} .= "@"
200 if $cmdline_options{repeatable}
201 && defined $cmdline_options{format}
202 47 ▶ && substr( $cmdline_options{format}, -1 ) ne '@';
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@" 0 47 47
Condition66%4/6
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@"
!ll&&!rl&&r
4430
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"}
!ll&&!rl&&r
4403
MC/DC0%0/3
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@"
$cmdline_options{"repeatable"} defined $cmdline_options{"format"} substr($cmdline_options{"format"}, -1) ne "\@"
Truth table50%2/4
A and B and C
A= $cmdline_options{"repeatable"}B= defined $cmdline_options{"format"}C= substr($cmdline_options{"format"}, -1) ne "\@"
ABCresult
0XX0
10X0
1100
1111
203  
204 croak(
205 "Negatable params is not usable with non boolean value, don't pass format to use it !"
206 )
207 if ( $cmdline_options{negatable} )
208 47 ▶ and defined $cmdline_options{format};
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline_options{"negatable"} and defined $cmdline_options{"format"} 2 45 47
Condition66%2/3
$cmdline_options{"negatable"} and defined $cmdline_options{"format"}
!ll&&!rl&&r
4502
MC/DC50%1/2
$cmdline_options{"negatable"} and defined $cmdline_options{"format"}
$cmdline_options{"negatable"} defined $cmdline_options{"format"}
Truth table66%2/3
A and B
A= $cmdline_options{"negatable"}B= defined $cmdline_options{"format"}
ABresult
0X0
100
111
209  
210 45 return %cmdline_options;
211 }
212  
213 22 1;
214  
215 __END__
« Options/Role.pm ↑ Summary Options/Descriptive.pm »