blib/lib/MooX/Options.pm

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