App/ParsedArgv.pm

filter: Statementstmt 95.3% 143 / 150 Branchbran 89.1% 41 / 46 Conditioncond 70.5% 12 / 17 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 100% 23 / 23 Podpod 62.5% 5 / 8 totaltotal 89.8% 229 / 255 CC 35 SCAR 20.4
CC
35
Cov
92%
_build_elements
18.0
extra
5.1
available
5.0
CRAP 7.7
« App/Utils.pm ↑ Summary App/Role/Base.pm »
1 # ============================================================================
2 package MooseX::App::ParsedArgv;
3 # ============================================================================
4  
5 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use MooseX::App::ParsedArgv::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use MooseX::App::ParsedArgv::Value;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 my $SINGLETON;
16  
17 has 'argv' => (
18 is => 'ro',
19 isa => 'ArrayRef[Str]',
20 traits => ['Array'],
21 handles => {
22 length_argv => 'count',
23 elements_argv => 'elements',
24 _shift_argv => 'shift',
25 },
26 4 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
27 4 my @argv;
28 4 @argv = eval {
29 4 require I18N::Langinfo;
30 4 I18N::Langinfo->import(qw(langinfo CODESET));
31 4 my $codeset = langinfo(CODESET());
32 # TODO Not sure if this is the right place?
33 4 ▶ if ($codeset =~ m/^UTF-?8$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($codeset =~ /^UTF-?8$/i) 0 4 4
34 0 binmode(STDOUT, ":encoding(UTF-8)");
35 0 binmode(STDERR, ":encoding(UTF-8)");
36 }
37 4 return map { decode($codeset,$_) } @ARGV;
38 };
39 # Fallback to standard
40 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
41 0 @argv = @ARGV;
42 }
43 4 return \@argv;
44 },
45 17 );
46  
47 has 'hints_novalue' => (
48 is => 'rw',
49 isa => 'ArrayRef[Str]',
50 77 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 17 ); # No value hints for the parser (such as for flags)
52  
53 has 'hints_permute' => (
54 is => 'rw',
55 isa => 'ArrayRef[Str]',
56 77 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
57 17 ); # Permute hints for the parser
58  
59 has 'hints_fixedvalue' => (
60 is => 'rw',
61 isa => 'HashRef[Str]',
62 78 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 17 ); # fixed value hints for the parser
64  
65 17 has 'elements' => (
66 is => 'ro',
67 isa => 'ArrayRef[MooseX::App::ParsedArgv::Element]',
68 lazy => 1,
69 builder => '_build_elements',
70 clearer => 'reset_elements',
71 );
72  
73 78 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
74 78 my ($self) = @_;
75  
76 # Register singleton
77 78 $SINGLETON = $self;
78 78 return $self;
79 }
80  
81 63 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=3 SCAR=12.5
Pod undocumented
82 63 my ($self) = @_;
83  
84 # Unregister singleton if it is stll the same
85 63 ▶ $SINGLETON = undef
Branch: 1 branch
BranchTrueFalseTotal
if defined $SINGLETON and $SINGLETON == $self 0 63 63
Condition33%1/3
defined $SINGLETON and $SINGLETON == $self
!ll&&!rl&&r
0630
MC/DC0%0/2
defined $SINGLETON and $SINGLETON == $self
defined $SINGLETON $SINGLETON == $self
Truth table33%1/3
A and B
A= defined $SINGLETONB= $SINGLETON == $self
ABresult
0X0
100
111
86 if defined $SINGLETON
87 && $SINGLETON == $self;
88  
89 63 return;
90 }
91  
92 426 ▶sub instance {
Subroutine instance: called CC=2 SCAR=6.9
Pod documented
93 426 my ($class) = @_;
94 426 ▶ unless (defined $SINGLETON) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SINGLETON) 4 422 426
95 4 return $class->new();
96 }
97 422 return $SINGLETON;
98 }
99  
100 68 ▶sub first_argv {
Subroutine first_argv: called CC=1 SCAR=0.0
Pod documented
101 68 my ($self) = @_;
102 68 return ($self->elements_argv)[0];
103 }
104  
105 65 ▶sub shift_argv {
Subroutine shift_argv: called CC=1 SCAR=0.0
Pod undocumented
106 65 my ($self) = @_;
107 65 $self->reset_elements;
108 65 return $self->_shift_argv;
109 }
110  
111 71 ▶sub _build_elements {
Subroutine _build_elements: called CC=18 SCAR=28.9
112 71 my ($self) = @_;
113  
114 71 my (@elements);
115  
116 my %options;
117 71 my $lastkey;
118 71 my $lastelement;
119 71 my $stopprocessing = 0; # Flag that is set after ' -- ' and inticated end of processing
120 71 my $position = 0; # Argument position
121 71 my $expecting = 0; # Flag that indicates that a value is expected
122  
123 # Loop all elements of our ARGV copy
124 71 foreach my $element ($self->elements_argv) {
125 # We are behind first ' -- ' occurrence: Do not process further
126 249 ▶ if ($stopprocessing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stopprocessing) { } 4 245 249
127 4 push (@elements,MooseX::App::ParsedArgv::Element->new(
128 key => $element,
129 type => 'extra',
130 ));
131 # Process element
132 } else {
133 # Flags with only one leading dash (-h or -vh)
134 245 ▶ if ($element =~ m/^-([^-][[:alnum:]]*)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($element =~ /^-([^-][[:alnum:]]*)$/) { } 4 241 245
elsif ($element =~ /^--([^-=][^=]+)=(.+)$/) { } 2 239 241
elsif ($element =~ /^--?([^-].+)/) { } 118 121 239
elsif ($element eq '--') { } 2 119 121
135 4 undef $lastkey;
136 4 undef $lastelement;
137 4 $expecting = 0;
138 # Split into single letter flags
139 4 foreach my $flag (split(//,$1)) {
140 8 ▶ unless (defined $options{$flag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{$flag}) 7 1 8
141 7 $options{$flag} = MooseX::App::ParsedArgv::Element->new(
142 key => $flag,
143 type => 'option',
144 raw => $element,
145 );
146 7 push(@elements,$options{$flag});
147 }
148 8 $options{$flag}->add_value(
149 1,
150 $position,
151 $element,
152 );
153 8 $lastkey = $options{$flag};
154 8 $lastelement = $element;
155 }
156 }
157 # Key-value combined (--key=value)
158 elsif ($element =~ m/^--([^-=][^=]+)=(.+)$/) {
159 2 undef $lastkey;
160 2 undef $lastelement;
161 2 $expecting = 0;
162 2 my ($key,$value) = ($1,$2);
163 2 ▶ unless (defined $options{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{$key}) 1 1 2
164 1 $options{$key} = MooseX::App::ParsedArgv::Element->new(
165 key => $key,
166 type => 'option',
167 raw => $element,
168 );
169 1 push(@elements,$options{$key});
170 }
171 2 $options{$key}->add_value(
172 $value,
173 $position,
174 $element,
175 );
176 }
177 # Ordinary key
178 elsif ($element =~ m/^--?([^-].+)/) {
179 118 my $key = $1;
180  
181 118 ▶ unless (defined $options{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $options{$key}) 107 11 118
182 107 $options{$key} = MooseX::App::ParsedArgv::Element->new(
183 key => $key,
184 type => 'option',
185 raw => $element,
186 );
187 107 push(@elements,$options{$key});
188 }
189 # This is a boolean or counter key that does not expect a value
190 118 ▶ if (first {$key eq $_} @{$self->hints_novalue}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { $key eq $_; } , @{$self->hints_novalue;})) { } 32 86 118
191 $options{$key}->add_value(
192 32 ▶ ($self->hints_fixedvalue->{$key} // 1),
Condition100%2/2
$self->hints_fixedvalue->{$key} // 1
l!l
311
MC/DC100%1/1
$self->hints_fixedvalue->{$key} // 1
$self->hints_fixedvalue->{$key}
Truth table100%2/2
A
A= $self->hints_fixedvalue->{$key}
Aresult
11
00
193 $position,
194 $element
195 );
196 32 $expecting = 0;
197 # We are expecting a value
198 } else {
199 86 $expecting = 1;
200 86 $lastelement = $element;
201 86 $lastkey = $options{$key};
202 }
203 }
204 # Extra values - stop processing after this token
205 elsif ($element eq '--') {
206 2 undef $lastkey;
207 2 undef $lastelement;
208 2 $stopprocessing = 1;
209 2 $expecting = 0;
210 }
211 # Value
212 else {
213 119 ▶ if (defined $lastkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastkey) { } 84 35 119
214 # This is a parameter - last key was a flag
215 84 ▶ if (first {$lastkey->key eq $_} @{$self->hints_novalue}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
if (&first(sub { $lastkey->key eq $_; } , @{$self->hints_novalue;})) { } 0 84 84
elsif (&first(sub { $lastkey->key eq $_; } , @{$self->hints_permute;})) { } 18 66 84
216 0 push(@elements,MooseX::App::ParsedArgv::Element->new( key => $element, type => 'parameter' ));
217 0 undef $lastkey;
218 0 undef $lastelement;
219 0 $expecting = 0;
220 # Permute values
221 84 ▶ } elsif (first {$lastkey->key eq $_} @{$self->hints_permute}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 18 $expecting = 0;
223 18 $lastkey->add_value(
224 $element,
225 $position,
226 $lastelement
227 );
228 # Has value
229 } else {
230 66 $expecting = 0;
231 66 $lastkey->add_value($element,$position);
232 66 undef $lastkey;
233 66 undef $lastelement;
234 }
235 } else {
236 35 push(@elements,MooseX::App::ParsedArgv::Element->new( key => $element, type => 'parameter' ));
237 }
238 }
239 }
240 249 $position++;
241 }
242  
243 # Fill up last value
244 71 ▶ if (defined $lastkey
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lastkey and $expecting) 3 68 71
Condition100%3/3
defined $lastkey and $expecting
!ll&&!rl&&r
6443
MC/DC100%2/2
defined $lastkey and $expecting
defined $lastkey $expecting
Truth table100%3/3
A and B
A= defined $lastkeyB= $expecting
ABresult
0X0
100
111
245 && $expecting) {
246 3 $lastkey->add_value(undef,$position,$lastelement);
247 3 $position++;
248 }
249  
250 71 return \@elements;
251 }
252  
253 384 ▶sub available {
Subroutine available: called CC=5 SCAR=16.1
Pod documented
254 384 my ($self,$type) = @_;
255  
256 384 my @elements;
257 384 foreach my $element (@{$self->elements}) {
258 next
259 733 ▶ if $element->consumed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->consumed 248 485 733
260 next
261 485 ▶ if defined $type
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $element->type ne $type 169 316 485
Condition66%2/3
defined $type and $element->type ne $type
!ll&&!rl&&r
0316169
MC/DC50%1/2
defined $type and $element->type ne $type
defined $type $element->type ne $type
Truth table66%2/3
A and B
A= defined $typeB= $element->type ne $type
ABresult
0X0
100
111
262 && $element->type ne $type;
263 316 push(@elements,$element);
264 }
265 384 return @elements;
266 }
267  
268 36 ▶sub consume {
Subroutine consume: called CC=5 SCAR=16.1
Pod documented
269 36 my ($self,$type) = @_;
270  
271 36 foreach my $element (@{$self->elements}) {
272 next
273 76 ▶ if $element->consumed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->consumed 52 24 76
274 next
275 24 ▶ if defined $type
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $element->type ne $type 2 22 24
Condition66%2/3
defined $type and $element->type ne $type
!ll&&!rl&&r
0222
MC/DC50%1/2
defined $type and $element->type ne $type
defined $type $element->type ne $type
Truth table66%2/3
A and B
A= defined $typeB= $element->type ne $type
ABresult
0X0
100
111
276 && $element->type ne $type;
277 22 $element->consume;
278 22 return $element;
279 }
280 14 return;
281 }
282  
283 45 ▶sub extra {
Subroutine extra: called CC=5 SCAR=16.2
Pod documented
284 45 my ($self) = @_;
285  
286 45 my @extra;
287 45 foreach my $element (@{$self->elements}) {
288 next
289 108 ▶ if $element->consumed;
Branch: 1 branch
BranchTrueFalseTotal
if $element->consumed 99 9 108
290 next
291 9 ▶ unless $element->type eq 'parameter'
Branch: 1 branch
BranchTrueFalseTotal
unless $element->type eq "parameter" or $element->type eq "extra" 0 9 9
Condition66%2/3
$element->type eq "parameter" or $element->type eq "extra"
l!l&&r!l&&!r
720
MC/DC0%0/2
$element->type eq "parameter" or $element->type eq "extra"
$element->type eq "parameter" $element->type eq "extra"
Truth table66%2/3
A or B
A= $element->type eq "parameter"B= $element->type eq "extra"
ABresult
1X1
011
000
292 || $element->type eq 'extra';
293 9 push(@extra,$element->key);
294 }
295  
296 45 return @extra;
297 }
298  
299 17 __PACKAGE__->meta->make_immutable;
300 17 1;
301  
302 __END__
« App/Utils.pm ↑ Summary App/Role/Base.pm »