blib/lib/MooX/Options.pm

filter: Statementstmt 94.3% 134 / 142 Branchbran 81.8% 36 / 44 Conditioncond 95.2% 20 / 21 MC/DCmcdc 91.6% 11 / 12 Subroutinesub 100% 30 / 30 Podpod n/a totaltotal 92.7% 231 / 249 CC 32 SCAR 16.6
CC
32
Cov
92%
_validate_and_filter_options
17.0
import
9.1
__ANON__
6.9
CRAP 5.2
« blib/lib/MooX/Options/Role.pm ↑ Summary /usr/local/bin/perldoc »
1 package MooX::Options;
2  
3 32 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 23 our $VERSION = "4.103";
6  
7 31 ▶use Carp ('croak');
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Module::Runtime qw(use_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 my @OPTIONS_ATTRIBUTES
11 23 = qw/format short repeatable negatable autosplit autorange doc long_doc order json hidden spacer_before spacer_after/;
12  
13 70 ▶sub import {
Subroutine import: called CC=9 SCAR=22.1
14 70 my ( undef, @import ) = @_;
15 70 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 70 my $target = caller;
33 70 for my $needed_methods (qw/with around has/) {
34 203 ▶ next if $target->can($needed_methods);
Branch: 1 branch
BranchTrueFalseTotal
if $target->can($needed_methods) 198 3 201
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 67 my $with = $target->can('with');
41 67 my $around = $target->can('around');
42 67 my $has = $target->can('has');
43  
44 67 my @target_isa;
45 25 ▶ { no strict 'refs'; @target_isa = @{"${target}::ISA"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 67 ▶ if (@target_isa) { #only in the main class, not a role
Branch: 1 branch
BranchTrueFalseTotal
if (@target_isa) { } 60 5 65
48  
49 ## no critic (ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval, ValuesAndExpressions::ProhibitImplicitNewlines)
50 62 eval "#line ${\(__LINE__+1 . ' ' . __FILE__)}\n" . '{
51 package ' . $target . ';
52 use MRO::Compat ();
53 25 ▶ 
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
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
54 sub _options_data {
55 77 ▶ my ( $class, @meta ) = @_;
Subroutine _options_data: called CC=1 SCAR=0.0
Subroutine _options_data: called CC=1 SCAR=0.0
Subroutine _options_data: called CC=1 SCAR=0.0
56 77 return $class->maybe::next::method(@meta);
57 77 }
58  
59 sub _options_config {
60 112 ▶ my ( $class, @params ) = @_;
Subroutine _options_config: called CC=1 SCAR=0.0
Subroutine _options_config: called CC=1 SCAR=0.0
Subroutine _options_config: called CC=1 SCAR=0.0
61 112 return $class->maybe::next::method(@params);
62 112 }
63  
64 1;
65 }';
66  
67 62 ▶ croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 59 60
68  
69 $around->(
70 229 ▶ _options_config => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 227 my ( $orig, $self ) = ( shift, shift );
72 227 return $self->$orig(@_), %$options_config;
73 }
74 61 );
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 64 my $options_data = {};
87 64 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 64 64
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 145 ▶ my $apply_modifiers = sub {
Subroutine __ANON__: called CC=5 SCAR=19.3
101 143 ▶ return if $target->can('new_with_options');
Branch: 1 branch
BranchTrueFalseTotal
if $target->can("new_with_options") 78 65 143
102 65 $with->('MooX::Options::Role');
103 65 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 65 65
104 0 $with->('MooX::ConfigFromFile::Role');
105 }
106 65 ▶ if ( $options_config->{with_locale_textdomain_oo} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_locale_textdomain_oo"}) 0 65 65
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 247 ▶ _options_data => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 245 my ( $orig, $self ) = ( shift, shift );
117 245 return ( $self->$orig(@_), %$options_data );
118 }
119 65 );
120 64 };
121  
122 my @banish_keywords
123 64 = qw/h help man usage option new_with_options parse_options options_usage _options_data _options_config/;
124 64 ▶ if ( $options_config->{with_config_from_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_config->{"with_config_from_file"}) 0 64 64
125 0 push @banish_keywords, qw/config_files config_prefix config_dirs/;
126 }
127  
128 92 ▶ my $option = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
129 90 my ( $name, %attributes ) = @_;
130 90 for my $ban (@banish_keywords) {
131 864 ▶ croak(
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $ban 9 855 864
132 "You cannot use an option with the name '$ban', it is implied by MooX::Options"
133 ) if $name eq $ban;
134 }
135  
136 81 my %_moo_attrs = _filter_attributes(%attributes);
137 81 ▶ $has->( $name => %_moo_attrs ) if %_moo_attrs;
Branch: 1 branch
BranchTrueFalseTotal
if %_moo_attrs 80 1 81
138  
139 ## no critic (RegularExpressions::RequireExtendedFormatting)
140 81 $name =~ s/^\+//; # one enhances an attribute being an option
141 81 $options_data->{$name}
142 = { _validate_and_filter_options(%attributes) };
143  
144 79 $apply_modifiers->();
145 79 return;
146 64 };
147  
148 64 ▶ if ( my $info = $Role::Tiny::INFO{$target} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $Role::Tiny::INFO{$target}) 5 59 64
149 5 $info->{not_methods}{$option} = $option;
150 }
151  
152 25 ▶ { no strict 'refs'; *{"${target}::option"} = $option; }
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 64 $apply_modifiers->();
155  
156 64 return;
157 }
158  
159 23 my %filter_key = map { $_ => 1 } ( @OPTIONS_ATTRIBUTES, 'negativable' );
160  
161 83 ▶sub _filter_attributes {
Subroutine _filter_attributes: called CC=1 SCAR=0.0
162 81 my %attributes = @_;
163 106 return map { ( $_ => $attributes{$_} ) }
164 81 grep { !exists $filter_key{$_} } keys %attributes;
165 }
166  
167 81 ▶sub _validate_and_filter_options {
Subroutine _validate_and_filter_options: called CC=17 SCAR=28.3
168 81 my (%options) = @_;
169 81 ▶ $options{doc} = $options{documentation} if !defined $options{doc};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"doc"} 72 9 81
170 81 ▶ $options{order} = 0 if !defined $options{order};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"order"} 75 6 81
171  
172 81 ▶ if ( $options{json}
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"json"} or defined $options{"format"} and $options{"format"} eq "json") 3 78 81
Condition100%6/6
$options{"json"} or defined $options{"format"} and $options{"format"} eq "json"
l!l&&r!l&&!r
1278
defined $options{"format"} and $options{"format"} eq "json"
!ll&&!rl&&r
48302
MC/DC100%3/3
$options{"json"} or defined $options{"format"} and $options{"format"} eq "json"
$options{"json"} defined $options{"format"} $options{"format"} eq "json"
Truth table100%4/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 3 delete $options{repeatable};
176 3 delete $options{autosplit};
177 3 delete $options{autorange};
178 3 delete $options{negativable};
179 3 delete $options{negatable};
180 3 $options{json} = 1;
181 3 $options{format} = 's';
182 }
183  
184 81 ▶ if ( $options{autorange} and not defined $options{autosplit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"autorange"} and not defined $options{"autosplit"}) 5 76 81
Condition100%3/3
$options{"autorange"} and not defined $options{"autosplit"}
!ll&&!rl&&r
7515
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
185  
186 # XXX maybe we should warn here since a previously beloved feature isn't enabled automatically
187 5 eval { use_module("Data::Record"); use_module("Regexp::Common"); }
188 5 ▶ and $options{autosplit} = ',';
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &use_module("Data::Record"); &use_module("Regexp::Common") } } and $options{"autosplit"} = "," 5 0 5
189 }
190  
191 exists $options{negativable}
192 81 ▶ and $options{negatable} = delete $options{negativable};
Branch: 1 branch
BranchTrueFalseTotal
exists $options{"negativable"} and $options{"negatable"} = delete $options{"negativable"} 2 79 81
193  
194 244 my %cmdline_options = map { ( $_ => $options{$_} ) }
195 81 grep { exists $options{$_} } @OPTIONS_ATTRIBUTES, 'required';
196  
197 $cmdline_options{repeatable} = 1
198 81 ▶ if $cmdline_options{autosplit} or $cmdline_options{autorange};
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline_options{"autosplit"} or $cmdline_options{"autorange"} 14 67 81
Condition66%2/3
$cmdline_options{"autosplit"} or $cmdline_options{"autorange"}
l!l&&r!l&&!r
14067
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 81 ▶ && 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 "\@" 9 72 81
Condition100%6/6
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"} and substr($cmdline_options{"format"}, -1) ne "\@"
!ll&&!rl&&r
6759
$cmdline_options{"repeatable"} and defined $cmdline_options{"format"}
!ll&&!rl&&r
66114
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
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 81 ▶ and defined $cmdline_options{format};
Branch: 1 branch
BranchTrueFalseTotal
if $cmdline_options{"negatable"} and defined $cmdline_options{"format"} 2 79 81
Condition100%3/3
$cmdline_options{"negatable"} and defined $cmdline_options{"format"}
!ll&&!rl&&r
7722
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
209  
210 79 return %cmdline_options;
211 }
212  
213 23 1;
214  
215 __END__
« blib/lib/MooX/Options/Role.pm ↑ Summary /usr/local/bin/perldoc »