App/Meta/Role/Class/Base.pm

filter: Statementstmt 95.7% 429 / 448 Branchbran 87.6% 142 / 162 Conditioncond 60.3% 70 / 116 MC/DCmcdc 33.7% 25 / 74 Subroutinesub 100% 40 / 40 Podpod 90.4% 19 / 21 totaltotal 84.2% 725 / 861 CC 165 SCAR 27.3
CC
165
Cov
88%
command_args
35.3
command_parser_hints
28.1
command_parse_options
20.0
CRAP 15.3
« App/Meta/Role/Attribute/Option.pm ↑ Summary App/Message/BlockColor.pm »
1 # ============================================================================
2 package MooseX::App::Meta::Role::Class::Base;
3 # ============================================================================
4  
5 16 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 16 ▶use List::Util qw(max first);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 16 ▶use MooseX::App::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Module::Pluggable::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 ▶use File::Basename qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 16 has 'app_messageclass' => (
18 is => 'rw',
19 isa => 'ClassName',
20 lazy => 1,
21 builder => '_build_app_messageclass',
22 );
23  
24 16 has 'app_namespace' => (
25 is => 'rw',
26 isa => 'MooseX::App::Types::List',
27 coerce => 1,
28 lazy => 1,
29 builder => '_build_app_namespace',
30 );
31  
32 has 'app_exclude' => (
33 is => 'rw',
34 isa => 'MooseX::App::Types::List',
35 coerce => 1,
36 51 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 16 );
38  
39 has 'app_base' => (
40 is => 'rw',
41 isa => 'Str',
42 lazy => 1,
43 10 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
44 10 return File::Basename::basename($0);
45 },
46 16 );
47  
48 has 'app_strict' => (
49 is => 'rw',
50 isa => 'Bool',
51 51 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
52 16 );
53  
54 has 'app_fuzzy' => (
55 is => 'rw',
56 isa => 'Bool',
57 51 ▶ default => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 16 );
59  
60 has 'app_command_name' => (
61 is => 'rw',
62 isa => 'CodeRef',
63 51 ▶ default => sub { \&MooseX::App::Utils::class_to_command },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 16 );
65  
66 has 'app_prefer_commandline' => (
67 is => 'rw',
68 isa => 'Bool',
69 51 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
70 16 );
71  
72 has 'app_permute' => (
73 is => 'rw',
74 isa => 'Bool',
75 51 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
76 16 );
77  
78 16 has 'app_commands' => (
79 is => 'rw',
80 isa => 'HashRef[Str]',
81 traits => ['Hash'],
82 handles => {
83 command_register => 'set',
84 command_get => 'get',
85 command_classes => 'values',
86 command_list => 'shallow_clone',
87 },
88 lazy => 1,
89 builder => '_build_app_commands',
90 );
91  
92 13 ▶sub _build_app_messageclass {
Subroutine _build_app_messageclass: called CC=1 SCAR=0.0
93 13 my ($self) = @_;
94 13 return 'MooseX::App::Message::Block'
95 }
96  
97 11 ▶sub _build_app_namespace {
Subroutine _build_app_namespace: called CC=1 SCAR=0.0
98 11 my ($self) = @_;
99 11 return [ $self->name ];
100 }
101  
102 11 ▶sub _build_app_commands {
Subroutine _build_app_commands: called CC=5 SCAR=16.1
103 11 my ($self) = @_;
104  
105 11 my (@list);
106 # Process namespace list
107 11 foreach my $namespace ( @{ $self->app_namespace } ) {
108 11 push(@list,$self->command_scan_namespace($namespace));
109 }
110 11 my $commands = { @list };
111  
112 # Process excludes
113 11 foreach my $exclude ( @{ $self->app_exclude } ) {
114 3 foreach my $command (keys %{$commands}) {
115 delete $commands->{$command}
116 21 ▶ if $commands->{$command} =~ m/^\Q$exclude\E(::|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $commands->{$command} =~ /^\Q$exclude\E(::|$)/ 3 18 21
117 }
118 }
119  
120 11 return $commands;
121 }
122  
123 83 ▶sub command_check {
Subroutine command_check: called CC=2 SCAR=6.9
Pod documented
124 83 my ($self) = @_;
125  
126 83 foreach my $attribute ($self->command_usage_attributes($self,'all')) {
127 204 $attribute->cmd_check();
128 }
129 82 return;
130 }
131  
132 11 ▶sub command_scan_namespace {
Subroutine command_scan_namespace: called CC=3 SCAR=11.0
Pod documented
133 11 my ($self,$namespace) = @_;
134  
135 # Find all packages in namespace
136 11 my $mpo = Module::Pluggable::Object->new(
137 search_path => [ $namespace ],
138 );
139  
140 11 my $commandsub = $self->app_command_name;
141  
142 11 my %return;
143 # Loop all packages
144 11 foreach my $command_class ($mpo->plugins) {
145 46 my $command_class_name = substr($command_class,length($namespace)+2);
146  
147 # subcommands support
148 46 $command_class_name =~ s/::/ /g;
149  
150 # Extract command name
151 46 $command_class_name =~ s/^\Q$namespace\E:://;
152 46 $command_class_name =~ s/^.+::([^:]+)$/$1/;
153 46 my $command = $commandsub->($command_class_name,$command_class);
154  
155 # Check if command was loaded
156 46 ▶ $return{$command} = $command_class
Branch: 1 branch
BranchTrueFalseTotal
if defined $command 46 0 46
157 if defined $command;
158 }
159  
160 11 return %return;
161 }
162  
163 69 ▶sub command_args {
Subroutine command_args: called CC=26 SCAR=35.7
Pod documented
164 69 my ($self,$metaclass) = @_;
165  
166 69 ▶ $metaclass ||= $self;
Condition33%1/3
$metaclass ||= $self
l!l&&r!l&&!r
6900
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table33%1/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
167 69 my $parsed_argv = MooseX::App::ParsedArgv->instance;
168  
169 69 ▶ unless ($metaclass->does_role('MooseX::App::Role::Common')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($metaclass->does_role("MooseX::App::Role::Common")) 0 69 69
170 0 Moose->throw_error('Class '.$metaclass->name.' is not a proper MooseX::App::Command class. You either need to use MooseX::App::Command or exclude this class via app_exclude')
171 }
172  
173 # Process options
174 69 my @attributes_option = $self->command_usage_attributes($metaclass,'option');
175  
176 69 my ($return,$errors) = $self->command_parse_options(\@attributes_option);
177  
178 68 my %raw_error;
179 # Loop all left over options
180 68 foreach my $option ($parsed_argv->available('option')) {
181 2 my $key = $option->key;
182 2 my $raw = $option->original;
183 2 my $message;
184 next
185 2 ▶ if defined $raw_error{$raw};
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw_error{$raw} 0 2 2
186  
187 # Get possible options with double dash - might be missing
188 2 ▶ if (length $key == 1
Branch: 1 branch
BranchTrueFalseTotal
if (length $key == 1 and $raw =~ /^-(\w+)$/) 0 2 2
Condition33%1/3
length $key == 1 and $raw =~ /^-(\w+)$/
!ll&&!rl&&r
200
MC/DC0%0/2
length $key == 1 and $raw =~ /^-(\w+)$/
length $key == 1 $raw =~ /^-(\w+)$/
Truth table33%1/3
A and B
A= length $key == 1B= $raw =~ /^-(\w+)$/
ABresult
0X0
100
111
189 && $raw =~ m/^-(\w+)$/) {
190 POSSIBLE_ATTRIBUTES:
191 0 foreach my $attribute ($self->command_usage_attributes($metaclass,[qw(option proto)])) {
192 0 foreach my $name ($attribute->cmd_name_possible) {
193 # TODO fuzzy match
194 0 ▶ if ($name eq $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $1) 0 0 0
195 0 $raw_error{$raw} = 1;
196 0 $message = "Did you mean '--$name'?";
197 0 last POSSIBLE_ATTRIBUTES;
198 }
199 }
200 }
201 }
202  
203 # Handle error messages
204 2 my $error;
205 2 ▶ if (defined $message) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message) { } 0 2 2
206 0 $error = $self->command_message(
207 header => "Unknown option '".$raw."'", # LOCALIZE
208 body => $message,
209 type => "error",
210 );
211 } else {
212 2 $error = $self->command_message(
213 header => "Unknown option '".$option->key."'", # LOCALIZE
214 type => "error",
215 );
216 }
217 2 unshift(@{$errors},$error);
218 }
219  
220 # Process positional parameters
221 68 my @attributes_parameter = $self->command_usage_attributes($metaclass,'parameter');
222  
223 68 foreach my $attribute (@attributes_parameter) {
224 36 my $element = $parsed_argv->consume('parameter');
225 last
226 36 ▶ unless defined $element;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $element 14 22 36
227  
228 22 my ($parameter_value,$parameter_errors) = $self->command_process_attribute($attribute, [ $element->key ] );
229 22 push(@{$errors},@{$parameter_errors});
230 22 $return->{$attribute->name} = $parameter_value;
231 }
232  
233 # Handle all unconsumed parameters and options
234 68 ▶ if ($self->app_strict || $metaclass->command_strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app_strict or $metaclass->command_strict) 34 34 68
Condition66%2/3
$self->app_strict or $metaclass->command_strict
l!l&&r!l&&!r
34034
MC/DC50%1/2
$self->app_strict or $metaclass->command_strict
$self->app_strict $metaclass->command_strict
Truth table66%2/3
A or B
A= $self->app_strictB= $metaclass->command_strict
ABresult
1X1
011
000
235 34 foreach my $parameter ($parsed_argv->available('parameter')) {
236 2 unshift(@{$errors},
237 $self->command_message(
238 header => "Unknown parameter '".$parameter->key."'", # LOCALIZE
239 type => "error",
240 )
241 );
242 }
243 }
244  
245 # Handle ENV
246 68 foreach my $attribute ($self->command_usage_attributes($metaclass,'all')) {
247 next
248 378 ▶ unless $attribute->can('has_cmd_env')
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute->can("has_cmd_env") and $attribute->has_cmd_env 349 29 378
Condition66%2/3
$attribute->can("has_cmd_env") and $attribute->has_cmd_env
!ll&&!rl&&r
034929
MC/DC50%1/2
$attribute->can("has_cmd_env") and $attribute->has_cmd_env
$attribute->can("has_cmd_env") $attribute->has_cmd_env
Truth table66%2/3
A and B
A= $attribute->can("has_cmd_env")B= $attribute->has_cmd_env
ABresult
0X0
100
111
249 && $attribute->has_cmd_env;
250  
251 29 my $cmd_env = $attribute->cmd_env;
252  
253 29 ▶ if (exists $ENV{$cmd_env}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{$cmd_env} and not defined $$return{$attribute->name}) 3 26 29
Condition100%3/3
exists $ENV{$cmd_env} and not defined $$return{$attribute->name}
!ll&&!rl&&r
2513
MC/DC100%2/2
exists $ENV{$cmd_env} and not defined $$return{$attribute->name}
exists $ENV{$cmd_env} not defined $$return{$attribute->name}
Truth table100%3/3
A and not(B)
A= exists $ENV{$cmd_env}B= not defined $$return{$attribute->name}
ABresult
0X0
100
111
254 && ! defined $return->{$attribute->name}) {
255  
256 3 my $value = $ENV{$cmd_env};
257  
258 3 ▶ if ($attribute->has_type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_type_constraint) 3 0 3
259 3 my $type_constraint = $attribute->type_constraint;
260 3 ▶ if ($attribute->should_coerce
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->should_coerce and $type_constraint->has_coercion) 1 2 3
Condition66%2/3
$attribute->should_coerce and $type_constraint->has_coercion
!ll&&!rl&&r
201
MC/DC50%1/2
$attribute->should_coerce and $type_constraint->has_coercion
$attribute->should_coerce $type_constraint->has_coercion
Truth table66%2/3
A and B
A= $attribute->should_coerceB= $type_constraint->has_coercion
ABresult
0X0
100
111
261 && $type_constraint->has_coercion) {
262 1 my $coercion = $type_constraint->coercion;
263 1 ▶ $value = $coercion->coerce($value) // $value;
Condition33%1/3
$coercion->coerce($value) // $value
l!l&&r!l&&!r
100
MC/DC0%0/2
$coercion->coerce($value) // $value
$coercion->coerce($value) $value
Truth table33%1/3
A // B
A= $coercion->coerce($value)B= $value
ABresult
1X1
011
000
264 }
265 }
266  
267 3 $return->{$attribute->name} = $value;
268 3 my $error = $attribute->cmd_type_constraint_check($value);
269 3 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 2 3
270 1 push(@{$errors},
271 $self->command_message(
272 header => "Invalid environment value for '".$cmd_env."'", # LOCALIZE
273 type => "error",
274 body => $error,
275 )
276 );
277 }
278 }
279 }
280  
281 68 return ($return,$errors);
282 }
283  
284 75 ▶sub command_proto {
Subroutine command_proto: called CC=5 SCAR=17.8
Pod documented
285 75 my ($self,$metaclass) = @_;
286  
287 75 ▶ $metaclass ||= $self;
Condition33%1/3
$metaclass ||= $self
l!l&&r!l&&!r
7500
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table33%1/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
288  
289 75 my @attributes;
290 75 foreach my $attribute ($self->command_usage_attributes($metaclass,'proto')) {
291 next
292 96 ▶ unless $attribute->does('MooseX::App::Meta::Role::Attribute::Option')
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type 0 96 96
Condition33%1/3
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type
!ll&&!rl&&r
0096
MC/DC0%0/2
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") $attribute->has_cmd_type
Truth table33%1/3
A and B
A= $attribute->does("MooseX::App::Meta::Role::Attribute::Option")B= $attribute->has_cmd_type
ABresult
0X0
100
111
293 && $attribute->has_cmd_type;
294 96 push(@attributes,$attribute);
295 }
296  
297 75 return $self->command_parse_options(\@attributes);
298 }
299  
300 144 ▶sub command_parse_options {
Subroutine command_parse_options: called CC=20 SCAR=30.0
Pod documented
301 144 my ($self,$attributes) = @_;
302  
303 # Build attribute lookup hash
304 144 my %option_to_attribute;
305 144 foreach my $attribute (@{$attributes}) {
306 336 foreach my $name ($attribute->cmd_name_possible) {
307 574 ▶ if (defined $option_to_attribute{$name}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $option_to_attribute{$name} and $option_to_attribute{$name} != $attribute) 1 573 574
Condition66%2/3
defined $option_to_attribute{$name} and $option_to_attribute{$name} != $attribute
!ll&&!rl&&r
57301
MC/DC50%1/2
defined $option_to_attribute{$name} and $option_to_attribute{$name} != $attribute
defined $option_to_attribute{$name} $option_to_attribute{$name} != $attribute
Truth table66%2/3
A and B
A= defined $option_to_attribute{$name}B= $option_to_attribute{$name} != $attribute
ABresult
0X0
100
111
308 && $option_to_attribute{$name} != $attribute) {
309 1 Moose->throw_error('Command line option conflict: '.$name);
310 }
311 573 $option_to_attribute{$name} = $attribute;
312 }
313 }
314  
315 143 my $match = {};
316 143 my $return = {};
317 143 my @errors;
318  
319 # Get ARGV
320 143 my $parsed_argv = MooseX::App::ParsedArgv->instance;
321  
322 # Loop all exact matches
323 143 foreach my $option ($parsed_argv->available('option')) {
324 203 ▶ if (my $attribute = $option_to_attribute{$option->key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attribute = $option_to_attribute{$option->key}) 89 114 203
325 89 $option->consume($attribute);
326 89 $match->{$attribute->name} = [ $option ];
327 }
328 }
329  
330 # Process fuzzy matches
331 143 ▶ if ($self->app_fuzzy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app_fuzzy) 139 4 143
332 # Loop all options (sorted by length)
333 139 foreach my $option (sort { length($b->key) <=> length($a->key) } $parsed_argv->available('option')) {
334  
335 # No fuzzy matching for one-letter flags
336 109 my $option_length = length($option->key);
337 next
338 109 ▶ if $option_length == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $option_length == 1 4 105 109
339  
340 105 my ($match_attributes) = [];
341  
342 # Try to match attributes
343 105 foreach my $name (keys %option_to_attribute) {
344 next
345 466 ▶ if ($option_length >= length($name));
Branch: 1 branch
BranchTrueFalseTotal
if $option_length >= length $name 355 111 466
346  
347 111 my $name_short = lc(substr($name,0,$option_length));
348  
349 # Partial match
350 111 ▶ if (lc($option->key) eq $name_short) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $option->key eq $name_short) 16 95 111
351 16 my $attribute = $option_to_attribute{$name};
352 16 ▶ unless (grep { $attribute == $_ } @{$match_attributes}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$attribute == $_;} @{$match_attributes;}) 16 0 16
353 16 push(@{$match_attributes},$attribute);
354 }
355 }
356 }
357  
358 # Process matches
359 # No match
360 105 ▶ if(@{$match_attributes} == 0) {}
Branch: 2 branches
BranchTrueFalseTotal
if (@{$match_attributes;} == 0) { } 90 15 105
elsif (@{$match_attributes;} == 1) { } 14 1 15
361 # One match
362 15 elsif(@{$match_attributes} == 1) {
363 14 my $attribute = $match_attributes->[0];
364 14 $option->consume();
365 14 ▶ $match->{$attribute->name} ||= [];
Condition100%2/2
$$match{$attribute->name} ||= []
l!l
59
MC/DC100%1/1
$$match{$attribute->name} ||= []
$$match{$attribute->name}
Truth table100%2/2
A
A= $$match{$attribute->name}
Aresult
11
00
366 14 push(@{$match->{$attribute->name}},$option);
367 }
368 # Multiple matches
369 else {
370 1 $option->consume();
371 push(@errors,
372 $self->command_message(
373 header => "Ambiguous option '".$option->key."'", # LOCALIZE
374 type => "error",
375 body => "Could be\n".MooseX::App::Utils::format_list( # LOCALIZE
376 2 map { [ $_ ] }
377 sort
378 2 map { $_->cmd_name_primary }
379 1 @{$match_attributes}
380 ),
381 )
382 );
383 }
384 }
385 }
386  
387 # Check all attributes
388 143 foreach my $attribute (@{$attributes}) {
389  
390 next
391 334 ▶ unless exists $match->{$attribute->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$match{$attribute->name} 239 95 334
392  
393 95 my @mapped_values;
394 95 foreach my $element (@{$match->{$attribute->name}}) {
395 100 push(@mapped_values,$element->all_values);
396 }
397  
398 my $values = [
399 110 map { $_->value }
400 95 sort { $a->position <=> $b->position }
401 @mapped_values
402 ];
403  
404 #warn Data::Dumper::Dumper($raw);
405 95 my ($value,$errors) = $self->command_process_attribute( $attribute, $values );
406 95 push(@errors,@{$errors});
407  
408 95 $return->{$attribute->name} = $value;
409 }
410  
411 143 return ($return,\@errors);
412 }
413  
414 117 ▶sub command_process_attribute {
Subroutine command_process_attribute: called CC=17 SCAR=28.5
Pod documented
415 117 my ($self,$attribute,$raw) = @_;
416  
417 117 ▶ $raw = [ $raw ]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $raw eq "ARRAY" 0 117 117
418 unless ref($raw) eq 'ARRAY';
419  
420 117 my @errors;
421 my $value;
422  
423 # Attribute with split
424 117 ▶ if ($attribute->has_cmd_split) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_cmd_split) 1 116 117
425 1 my @raw_unfolded;
426 1 foreach (@{$raw}) {
427 2 push(@raw_unfolded,split($attribute->cmd_split,$_));
428 }
429 1 $raw = \@raw_unfolded;
430 }
431  
432 # Attribute with counter - transform value count into value
433 117 ▶ if ($attribute->cmd_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->cmd_count) 1 116 117
434 1 $value = $raw = [ scalar(@$raw) ];
435 }
436  
437 # Attribute with type constraint
438 117 ▶ if ($attribute->has_type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_type_constraint) { } 111 6 117
439 111 my $type_constraint = $attribute->type_constraint;
440  
441 111 ▶ if ($type_constraint->is_a_type_of('ArrayRef')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type_constraint->is_a_type_of('ArrayRef')) { } 5 106 111
elsif ($type_constraint->is_a_type_of('HashRef')) { } 4 102 106
elsif ($type_constraint->is_a_type_of('Bool')) { } 26 76 102
442 5 $value = $raw;
443 } elsif ($type_constraint->is_a_type_of('HashRef')) {
444 4 $value = {};
445 4 foreach my $element (@{$raw}) {
446 6 ▶ if ($element =~ m/^([^=]+)=(.+?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($element =~ /^([^=]+)=(.+?)$/) { } 5 1 6
447 5 ▶ $value->{$1} ||= $2;
Condition33%1/3
$value->{$1} ||= $2
l!l&&r!l&&!r
050
MC/DC0%0/2
$value->{$1} ||= $2
$value->{$1} $2
Truth table33%1/3
A ||= B
A= $value->{$1}B= $2
ABresult
1X1
011
000
448 } else {
449 1 push(@errors,
450 $self->command_message(
451 header => "Invalid value for '".$attribute->cmd_name_primary."'", # LOCALIZE
452 type => "error",
453 body => "Value must be supplied as 'key=value' (not '$element')", # LOCALIZE
454 )
455 );
456 }
457 }
458 } elsif ($type_constraint->is_a_type_of('Bool')) {
459 26 $value = $raw->[-1];
460  
461 # if ($self->has_default
462 # && ! $self->is_default_a_coderef
463 # && $self->default == 1) {
464  
465 } else {
466 76 $value = $raw->[-1];
467 }
468  
469 111 ▶ unless(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 109 2 111
470 2 push(@errors,
471 $self->command_message(
472 header => "Missing value for '".$attribute->cmd_name_primary."'", # LOCALIZE
473 type => "error",
474 )
475 );
476 } else {
477 109 ▶ if ($attribute->should_coerce
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->should_coerce and $type_constraint->has_coercion) 1 108 109
Condition66%2/3
$attribute->should_coerce and $type_constraint->has_coercion
!ll&&!rl&&r
10801
MC/DC50%1/2
$attribute->should_coerce and $type_constraint->has_coercion
$attribute->should_coerce $type_constraint->has_coercion
Truth table66%2/3
A and B
A= $attribute->should_coerceB= $type_constraint->has_coercion
ABresult
0X0
100
111
478 && $type_constraint->has_coercion) {
479 1 my $coercion = $type_constraint->coercion;
480 1 ▶ $value = $coercion->coerce($value) // $value;
Condition33%1/3
$coercion->coerce($value) // $value
l!l&&r!l&&!r
100
MC/DC0%0/2
$coercion->coerce($value) // $value
$coercion->coerce($value) $value
Truth table33%1/3
A // B
A= $coercion->coerce($value)B= $value
ABresult
1X1
011
000
481 }
482 109 my $error = $attribute->cmd_type_constraint_check($value);
483 109 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 6 103 109
484 6 push(@errors,
485 $self->command_message(
486 header => "Invalid value for '".$attribute->cmd_name_primary."'", # LOCALIZE
487 type => "error",
488 body => $error,
489 )
490 );
491 }
492 }
493  
494 } else {
495 6 $value = $raw->[-1];
496 }
497  
498 117 return ($value,\@errors);
499 }
500  
501 45 ▶sub command_candidates {
Subroutine command_candidates: called CC=4 SCAR=13.9
Pod documented
502 45 my ($self,$command) = @_;
503  
504 45 my $lc_command = lc($command);
505 45 my $commands = $self->app_commands;
506  
507 45 my @candidates;
508 45 my $candidate_length = length($command);
509  
510 # Compare all commands to find matching candidates
511 45 foreach my $command_name (keys %$commands) {
512 320 ▶ if ($command_name eq $lc_command) {
Branch: 2 branches
BranchTrueFalseTotal
if ($command_name eq $lc_command) { } 1 319 320
elsif ($lc_command eq substr($command_name, 0, $candidate_length)) { } 32 287 319
513 1 return $command_name;
514 } elsif ($lc_command eq substr($command_name,0,$candidate_length)) {
515 32 push(@candidates,$command_name);
516 }
517 }
518  
519 44 return [ sort @candidates ];
520 }
521  
522 65 ▶sub command_find {
Subroutine command_find: called CC=14 SCAR=27.6
Pod documented
523 65 my ($self,$commands) = @_;
524  
525 65 my $parsed_argv = MooseX::App::ParsedArgv->instance;
526 65 my $all_commands = $self->app_commands;
527  
528 # Get parts
529 65 my (@parts,@command_parts);
530 65 ▶ if (defined $commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $commands) { } 1 64 65
531 1 ▶ if (ref($commands) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $commands eq 'ARRAY') { } 0 1 1
532 0 @parts = map { lc } @{$commands};
533 } else {
534 1 @parts = ( lc($commands) );
535 }
536 } else {
537 64 @parts = $parsed_argv->elements_argv;
538 }
539  
540 # Extract possible parts
541 65 foreach my $part (@parts) {
542 # Anyting staring with a dash cannot be a command
543 last
544 145 ▶ if $part =~ m/^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $part =~ /^-/ 57 88 145
545 88 push(@command_parts,lc($part));
546 }
547  
548 # Shortcut
549 return
550 65 ▶ unless scalar @command_parts;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @command_parts 0 65 65
551  
552 # basically do a longest-match search
553 65 for my $index (reverse(0..$#command_parts)) {
554 86 my $command = join ' ', @command_parts[0..$index];
555 86 ▶ if( $all_commands->{$command} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_commands->{$command}) 37 49 86
556 37 $parsed_argv->shift_argv for 0..$index;
557 37 return $command;
558 }
559 }
560  
561 # didn't find an exact match, let's go to plan B
562 28 foreach my $index (reverse(0..$#command_parts)) {
563 42 my $command = join ' ', @command_parts[0..$index];
564 42 my $candidate = $self->command_candidates($command);
565 42 ▶ if (ref $candidate eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $candidate eq "") 0 42 42
566 0 $parsed_argv->shift_argv;
567 0 return $candidate;
568 }
569 42 ▶ if (@{$candidate} == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@{$candidate;} == 0) { } 15 27 42
elsif (@{$candidate;} == 1) { } 27 0 27
570 15 next;
571 }
572 27 elsif (@{$candidate} == 1) {
573 27 ▶ if ($self->app_fuzzy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app_fuzzy) { } 26 1 27
574 26 $parsed_argv->shift_argv;
575 26 return $candidate->[0];
576 } else {
577 1 return $self->command_message(
578 header => "Unknown command '$command'", # LOCALIZE
579 type => "error",
580 body => "Did you mean '".$candidate->[0]."'?", # LOCALIZE
581 );
582 }
583 }
584 else {
585 return $self->command_message(
586 header => "Ambiguous command '$command'", # LOCALIZE
587 type => "error",
588 body => "Which command did you mean?\n". # LOCALIZE
589 0 MooseX::App::Utils::format_list(map { [ $_ ] } sort @{$candidate}),
590 );
591 }
592 }
593  
594 1 my $command = $command_parts[0];
595 1 return $self->command_message(
596 header => "Unknown command '$command'", # LOCALIZE
597 type => "error",
598 );
599 }
600  
601 75 ▶sub command_parser_hints {
Subroutine command_parser_hints: called CC=28 SCAR=33.4
Pod documented
602 75 my ($self,$metaclass) = @_;
603  
604 75 ▶ $metaclass ||= $self;
Condition33%1/3
$metaclass ||= $self
l!l&&r!l&&!r
7500
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table33%1/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
605  
606 75 my %hints;
607 my %names;
608 75 my $return = { permute => [], novalue => [], fixedvalue => {} };
609 75 foreach my $attribute ($self->command_usage_attributes($metaclass,[qw(option proto)])) {
610 352 my $permute = 0;
611 352 my $bool = 0;
612 352 my $type_constraint = $attribute->type_constraint;
613 352 ▶ if ($type_constraint) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type_constraint) 313 39 352
614 313 ▶ $permute = 1
Branch: 1 branch
BranchTrueFalseTotal
if $type_constraint->is_a_type_of("ArrayRef") or $type_constraint->is_a_type_of("HashRef") 28 285 313
Condition100%3/3
$type_constraint->is_a_type_of("ArrayRef") or $type_constraint->is_a_type_of("HashRef")
l!l&&r!l&&!r
208285
MC/DC100%2/2
$type_constraint->is_a_type_of("ArrayRef") or $type_constraint->is_a_type_of("HashRef")
$type_constraint->is_a_type_of("ArrayRef") $type_constraint->is_a_type_of("HashRef")
Truth table100%3/3
A or B
A= $type_constraint->is_a_type_of("ArrayRef")B= $type_constraint->is_a_type_of("HashRef")
ABresult
1X1
011
000
615 if $type_constraint->is_a_type_of('ArrayRef')
616 || $type_constraint->is_a_type_of('HashRef');
617  
618 313 ▶ $bool = 1
Branch: 1 branch
BranchTrueFalseTotal
if $type_constraint->is_a_type_of("Bool") 145 168 313
619 if $type_constraint->is_a_type_of('Bool');
620 }
621  
622 352 ▶ my $hint = {
Condition100%3/3
$bool || $attribute->cmd_count
l!l&&r!l&&!r
1456201
MC/DC100%2/2
$bool || $attribute->cmd_count
$bool $attribute->cmd_count
Truth table100%3/3
A || B
A= $boolB= $attribute->cmd_count
ABresult
1X1
011
000
623 name => $attribute->name,
624 bool => $bool,
625 novalue => $bool || $attribute->cmd_count,
626 permute => $permute,
627 };
628  
629 352 foreach my $name ($attribute->cmd_name_list) {
630 577 $names{$name} = $hints{$name} = $hint;
631 }
632  
633 # Negated values
634 352 ▶ if ($bool) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bool) { } 145 207 352
elsif ($attribute->cmd_count) { } 6 201 207
635 145 $hint->{fixedvalue} = 1;
636 145 ▶ if ($attribute->has_cmd_negate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->has_cmd_negate) 11 134 145
637 11 my $hint_neg = { %{$hint} }; # shallow copy
638 11 $hint_neg->{fixedvalue} = 0;
639 11 foreach my $name (@{$attribute->cmd_negate}) {
640 14 $names{$name} = $hints{$name} = $hint_neg;
641 }
642 }
643 } elsif ($attribute->cmd_count) {
644 6 $hint->{fixedvalue} = 1;
645 }
646 }
647  
648 75 ▶ if ($self->app_fuzzy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app_fuzzy) 73 2 75
649 73 ▶ my $length = max(map { length($_) } keys %names) // 0;
Condition50%1/2
&max(map({length $_;} keys %names)) // 0
l!l
730
MC/DC0%0/1
&max(map({length $_;} keys %names)) // 0
&max(map({length $_;} keys %names))
Truth table50%1/2
A
A= &max(map({length $_;} keys %names))
Aresult
11
00
650 73 foreach my $l (reverse(2..$length)) {
651 638 my %tmp;
652 638 foreach my $name (keys %names) {
653 next
654 5080 ▶ if length($name) < $l;
Branch: 1 branch
BranchTrueFalseTotal
if length $name < $l 2793 2287 5080
655 2287 my $short_name = substr($name,0,$l);
656 next
657 2287 ▶ if defined $hints{$short_name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $hints{$short_name} 423 1864 2287
658 1864 ▶ $tmp{$short_name} ||= [];
Condition100%2/2
$tmp{$short_name} ||= []
l!l
2651599
MC/DC100%1/1
$tmp{$short_name} ||= []
$tmp{$short_name}
Truth table100%2/2
A
A= $tmp{$short_name}
Aresult
11
00
659 next
660 if defined $tmp{$short_name}->[0]
661 1864 ▶ && $tmp{$short_name}->[0]->{name} eq $names{$name}->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp{$short_name}[0] and $tmp{$short_name}[0]{"name"} eq $names{$name}{"name"} 0 1864 1864
Condition66%2/3
defined $tmp{$short_name}[0] and $tmp{$short_name}[0]{"name"} eq $names{$name}{"name"}
!ll&&!rl&&r
15992650
MC/DC0%0/2
defined $tmp{$short_name}[0] and $tmp{$short_name}[0]{"name"} eq $names{$name}{"name"}
defined $tmp{$short_name}[0] $tmp{$short_name}[0]{"name"} eq $names{$name}{"name"}
Truth table66%2/3
A and B
A= defined $tmp{$short_name}[0]B= $tmp{$short_name}[0]{"name"} eq $names{$name}{"name"}
ABresult
0X0
100
111
662 1864 push(@{$tmp{$short_name}},$names{$name})
663 }
664 638 foreach my $short_name (keys %tmp) {
665 next
666 1599 ▶ if scalar @{$tmp{$short_name}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$tmp{$short_name};} > 1 249 1350 1599
667 1350 $hints{$short_name} = $tmp{$short_name}->[0];
668 }
669 }
670 }
671  
672 75 foreach my $name (keys %hints) {
673 1940 ▶ if ($hints{$name}->{novalue}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hints{$name}{"novalue"}) 1105 835 1940
674 1105 push(@{$return->{novalue}},$name);
675 }
676 1940 ▶ if ($hints{$name}->{permute}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hints{$name}{"permute"}) 97 1843 1940
677 97 push(@{$return->{permute}},$name);
678 }
679 1940 ▶ if (defined $hints{$name}->{fixedvalue}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hints{$name}{"fixedvalue"}) 1105 835 1940
680 1105 $return->{fixedvalue}{$name} = $hints{$name}->{fixedvalue};
681 }
682 }
683  
684  
685 #warn Data::Dumper::Dumper($return);
686 75 return $return;
687 }
688  
689 159 ▶sub command_message {
Subroutine command_message: called CC=1 SCAR=0.0
Pod documented
690 159 my ($self,@args) = @_;
691 159 my $messageclass = $self->app_messageclass;
692 159 Class::Load::load_class($messageclass);
693 159 return $messageclass->new(@args);
694 }
695  
696 68 ▶sub command_check_attributes {
Subroutine command_check_attributes: called CC=7 SCAR=19.5
Pod documented
697 68 my ($self,$command_meta,$errors,$params) = @_;
698  
699 68 ▶ $command_meta ||= $self;
Condition33%1/3
$command_meta ||= $self
l!l&&r!l&&!r
6800
MC/DC0%0/2
$command_meta ||= $self
$command_meta $self
Truth table33%1/3
A ||= B
A= $command_metaB= $self
ABresult
1X1
011
000
700  
701 # Check required values
702 68 foreach my $attribute ($self->command_usage_attributes($command_meta,[qw(option proto parameter)])) {
703 378 ▶ if ($attribute->is_required
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->is_required and not exists $$params{$attribute->name} and not $attribute->has_default) 7 371 378
Condition100%6/6
$attribute->is_required and not exists $$params{$attribute->name} and not $attribute->has_default
!ll&&!rl&&r
36837
$attribute->is_required and not exists $$params{$attribute->name}
!ll&&!rl&&r
3244410
MC/DC100%3/3
$attribute->is_required and not exists $$params{$attribute->name} and not $attribute->has_default
$attribute->is_required not exists $$params{$attribute->name} not $attribute->has_default
Truth table100%4/4
A and not(B) and not(C)
A= $attribute->is_requiredB= not exists $$params{$attribute->name}C= not $attribute->has_default
ABCresult
0XX0
10X0
1100
1111
704 && ! exists $params->{$attribute->name}
705 && ! $attribute->has_default) {
706 7 ▶ push(@{$errors},
Branch: 1 branch
BranchTrueFalseTotal
$attribute->cmd_type eq 'parameter' ? : 1 6 7
707 $self->command_message(
708 header => "Required ".($attribute->cmd_type eq 'parameter' ? 'parameter':'option')." '".$attribute->cmd_name_primary."' missing", # LOCALIZE
709 type => "error",
710 )
711 );
712 }
713 }
714  
715 68 return $errors;
716 }
717  
718 636 ▶sub command_usage_attributes {
Subroutine command_usage_attributes: called CC=10 SCAR=23.3
Pod documented
719 636 my ($self,$metaclass,$types) = @_;
720  
721 636 ▶ $metaclass ||= $self;
Condition66%2/3
$metaclass ||= $self
l!l&&r!l&&!r
63510
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table66%2/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
722 636 ▶ $types ||= [qw(option proto)];
Condition100%2/2
$types ||= ["option", "proto"]
l!l
6351
MC/DC100%1/1
$types ||= ["option", "proto"]
$types
Truth table100%2/2
A
A= $types
Aresult
11
00
723 636 ▶ if ('' eq ref $types) {
Branch: 1 branch
BranchTrueFalseTotal
if ("" eq ref $types) 450 186 636
724 450 $types = [$types];
725 }
726  
727 636 ▶ unless ($metaclass->does_role('MooseX::App::Role::Common')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($metaclass->does_role("MooseX::App::Role::Common")) 0 636 636
728 0 Moose->throw_error('Class '.$metaclass->name.' is not a proper MooseX::App::Command class. You either need to use MooseX::App::Command or exclude this class via app_exclude')
729 }
730  
731 636 my @return;
732 636 foreach my $attribute ($metaclass->get_all_attributes) {
733 next
734 4678 ▶ unless $attribute->does('MooseX::App::Meta::Role::Attribute::Option')
Branch: 1 branch
BranchTrueFalseTotal
unless $attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type 1451 3227 4678
Condition100%3/3
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type
!ll&&!rl&&r
7357163227
MC/DC100%2/2
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") and $attribute->has_cmd_type
$attribute->does("MooseX::App::Meta::Role::Attribute::Option") $attribute->has_cmd_type
Truth table100%3/3
A and B
A= $attribute->does("MooseX::App::Meta::Role::Attribute::Option")B= $attribute->has_cmd_type
ABresult
0X0
100
111
735 && $attribute->has_cmd_type;
736  
737 next
738 3227 ▶ unless first { $attribute->cmd_type eq $_ or 'all' eq $_ } @$types;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
unless &first(sub { $attribute->cmd_type eq $_ or "all" eq $_; } , @$types) 1202 2025 3227
$attribute->cmd_type eq $_ or "all" eq $_ 1371 2286 3657
739  
740 2025 push(@return,$attribute);
741 }
742  
743 return (sort {
744 636 ▶ $a->cmd_position <=> $b->cmd_position ||
Condition33%1/3
$a->cmd_position <=> $b->cmd_position || $a->cmd_usage_name cmp $b->cmd_usage_name
l!l&&r!l&&!r
285900
MC/DC0%0/2
$a->cmd_position <=> $b->cmd_position || $a->cmd_usage_name cmp $b->cmd_usage_name
$a->cmd_position <=> $b->cmd_position $a->cmd_usage_name cmp $b->cmd_usage_name
Truth table33%1/3
A || B
A= $a->cmd_position <=> $b->cmd_positionB= $a->cmd_usage_name cmp $b->cmd_usage_name
ABresult
1X1
011
000
745 $a->cmd_usage_name cmp $b->cmd_usage_name
746 } @return);
747 }
748  
749 35 ▶sub command_usage_options {
Subroutine command_usage_options: called CC=5 SCAR=17.0
Pod documented
750 35 my ($self,$metaclass,$headline) = @_;
751  
752 35 ▶ $headline ||= 'options:'; # LOCALIZE
Condition50%1/2
$headline ||= "options:"
l!l
350
MC/DC0%0/1
$headline ||= "options:"
$headline
Truth table50%1/2
A
A= $headline
Aresult
11
00
753 35 ▶ $metaclass ||= $self;
Condition33%1/3
$metaclass ||= $self
l!l&&r!l&&!r
3500
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table33%1/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
754  
755 35 my @options;
756 35 foreach my $attribute ($self->command_usage_attributes($metaclass,[qw(option proto)])) {
757 172 push(@options,[
758 $attribute->cmd_usage_name(),
759 $attribute->cmd_usage_description()
760 ]);
761 }
762  
763 return
764 35 ▶ unless scalar @options > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @options > 0 0 35 35
765  
766 35 return $self->command_message(
767 header => $headline,
768 body => MooseX::App::Utils::format_list(@options),
769 );
770 }
771  
772 35 ▶sub command_usage_parameters {
Subroutine command_usage_parameters: called CC=5 SCAR=16.4
Pod documented
773 35 my ($self,$metaclass,$headline) = @_;
774  
775 35 ▶ $headline ||= 'parameter:'; # LOCALIZE
Condition50%1/2
$headline ||= "parameter:"
l!l
350
MC/DC0%0/1
$headline ||= "parameter:"
$headline
Truth table50%1/2
A
A= $headline
Aresult
11
00
776 35 ▶ $metaclass ||= $self;
Condition33%1/3
$metaclass ||= $self
l!l&&r!l&&!r
3500
MC/DC0%0/2
$metaclass ||= $self
$metaclass $self
Truth table33%1/3
A ||= B
A= $metaclassB= $self
ABresult
1X1
011
000
777  
778 35 my @parameters;
779 35 foreach my $attribute (
780 21 sort { $a->cmd_position <=> $b->cmd_position }
781 $self->command_usage_attributes($metaclass,'parameter')
782 ) {
783 21 push(@parameters,[
784 $attribute->cmd_usage_name(),
785 $attribute->cmd_usage_description()
786 ]);
787 }
788  
789 return
790 35 ▶ unless scalar @parameters > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @parameters > 0 28 7 35
791  
792 7 return $self->command_message(
793 header => $headline,
794 body => MooseX::App::Utils::format_list(@parameters),
795 );
796 }
797  
798 29 ▶sub command_usage_header {
Subroutine command_usage_header: called CC=8 SCAR=20.8
Pod documented
799 29 my ($self,$command_meta_class) = @_;
800  
801 29 my $caller = $self->app_base;
802  
803 29 my ($command_name,$usage);
804 29 ▶ if ($command_meta_class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_meta_class) { } 23 6 29
805 23 $command_name = $self->command_class_to_command($command_meta_class->name);
806 } else {
807 6 $command_name = '<command>';
808 }
809  
810 29 ▶ $command_meta_class ||= $self;
Condition66%2/3
$command_meta_class ||= $self
l!l&&r!l&&!r
2360
MC/DC0%0/2
$command_meta_class ||= $self
$command_meta_class $self
Truth table66%2/3
A ||= B
A= $command_meta_classB= $self
ABresult
1X1
011
000
811 29 ▶ if ($command_meta_class->can('command_usage')
Branch: 1 branch
BranchTrueFalseTotal
if ($command_meta_class->can("command_usage") and $command_meta_class->command_usage_predicate) 1 28 29
Condition66%2/3
$command_meta_class->can("command_usage") and $command_meta_class->command_usage_predicate
!ll&&!rl&&r
0281
MC/DC50%1/2
$command_meta_class->can("command_usage") and $command_meta_class->command_usage_predicate
$command_meta_class->can("command_usage") $command_meta_class->command_usage_predicate
Truth table66%2/3
A and B
A= $command_meta_class->can("command_usage")B= $command_meta_class->command_usage_predicate
ABresult
0X0
100
111
812 && $command_meta_class->command_usage_predicate) {
813 1 $usage = MooseX::App::Utils::format_text($command_meta_class->command_usage);
814 }
815  
816 29 ▶ unless (defined $usage) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $usage) 28 1 29
817 # LOCALIZE
818 28 $usage = "$caller $command_name ";
819 28 my @parameter= $self->command_usage_attributes($command_meta_class,'parameter');
820 28 foreach my $attribute (@parameter) {
821 21 ▶ if ($attribute->is_required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute->is_required) { } 2 19 21
822 2 $usage .= "<".$attribute->cmd_usage_name.'> ';
823 } else {
824 19 $usage .= '['.$attribute->cmd_usage_name.'] ';
825 }
826 }
827 28 $usage .= "[long options...]
828 $caller help
829 $caller $command_name --help";
830 28 $usage = MooseX::App::Utils::format_text($usage);
831 }
832  
833 29 return $self->command_message(
834 header => 'usage:', # LOCALIZE
835 body => $usage,
836 );
837 }
838  
839 36 ▶sub command_usage_description {
Subroutine command_usage_description: called CC=6 SCAR=18.5
Pod documented
840 36 my ($self,$command_meta_class) = @_;
841  
842 36 ▶ $command_meta_class ||= $self;
Condition33%1/3
$command_meta_class ||= $self
l!l&&r!l&&!r
3600
MC/DC0%0/2
$command_meta_class ||= $self
$command_meta_class $self
Truth table33%1/3
A ||= B
A= $command_meta_classB= $self
ABresult
1X1
011
000
843 36 ▶ if ($command_meta_class->can('command_long_description')
Branch: 2 branches
BranchTrueFalseTotal
if ($command_meta_class->can('command_long_description') and $command_meta_class->command_long_description_predicate) { } 13 23 36
elsif ($command_meta_class->can('command_short_description') and $command_meta_class->command_short_description_predicate) { } 1 22 23
Condition66%2/3
$command_meta_class->can("command_long_description") and $command_meta_class->command_long_description_predicate
!ll&&!rl&&r
02313
MC/DC50%1/2
$command_meta_class->can("command_long_description") and $command_meta_class->command_long_description_predicate
$command_meta_class->can("command_long_description") $command_meta_class->command_long_description_predicate
Truth table66%2/3
A and B
A= $command_meta_class->can("command_long_description")B= $command_meta_class->command_long_description_predicate
ABresult
0X0
100
111
844 && $command_meta_class->command_long_description_predicate) {
845 13 return $self->command_message(
846 header => 'description:', # LOCALIZE
847 body => MooseX::App::Utils::format_text($command_meta_class->command_long_description),
848 );
849 ▶ } elsif ($command_meta_class->can('command_short_description')
Condition66%2/3
$command_meta_class->can("command_short_description") and $command_meta_class->command_short_description_predicate
!ll&&!rl&&r
0221
MC/DC50%1/2
$command_meta_class->can("command_short_description") and $command_meta_class->command_short_description_predicate
$command_meta_class->can("command_short_description") $command_meta_class->command_short_description_predicate
Truth table66%2/3
A and B
A= $command_meta_class->can("command_short_description")B= $command_meta_class->command_short_description_predicate
ABresult
0X0
100
111
850 && $command_meta_class->command_short_description_predicate) {
851 1 return $self->command_message(
852 header => 'short description:', # LOCALIZE
853 body => MooseX::App::Utils::format_text($command_meta_class->command_short_description),
854 );
855 }
856 22 return;
857 }
858  
859 55 ▶sub command_class_to_command {
Subroutine command_class_to_command: called CC=3 SCAR=11.0
Pod documented
860 55 my ($self,$command_class) = @_;
861  
862 55 my $commands = $self->app_commands;
863 55 foreach my $element (keys %$commands) {
864 164 ▶ if ($command_class eq $commands->{$element}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_class eq $commands->{$element}) 55 109 164
865 55 return $element;
866 }
867 }
868  
869 0 return;
870 }
871  
872 29 ▶sub command_subcommands {
Subroutine command_subcommands: called CC=5 SCAR=16.2
Pod undocumented
873 29 my ($self,$command_meta_class) = @_;
874  
875 29 ▶ $command_meta_class ||= $self;
Condition33%1/3
$command_meta_class ||= $self
l!l&&r!l&&!r
2900
MC/DC0%0/2
$command_meta_class ||= $self
$command_meta_class $self
Truth table33%1/3
A ||= B
A= $command_meta_classB= $self
ABresult
1X1
011
000
876 29 my $command_class = $command_meta_class->name;
877 29 my $command_name = $self->command_class_to_command($command_class);
878  
879 29 my $commands = $self->app_commands;
880 29 my $subcommands = {};
881 29 foreach my $command (keys %{$commands}) {
882 next
883 145 ▶ if $command eq $command_name
Branch: 1 branch
BranchTrueFalseTotal
if $command eq $command_name or not $command =~ /^\Q$command_name\E\s(.+)/ 143 2 145
Condition100%3/3
$command eq $command_name or not $command =~ /^\Q$command_name\E\s(.+)/
l!l&&r!l&&!r
291142
MC/DC100%2/2
$command eq $command_name or not $command =~ /^\Q$command_name\E\s(.+)/
$command eq $command_name not $command =~ /^\Q$command_name\E\s(.+)/
Truth table100%3/3
A or not(B)
A= $command eq $command_nameB= not $command =~ /^\Q$command_name\E\s(.+)/
ABresult
1X1
011
000
884 || $command !~ m/^\Q$command_name\E\s(.+)/;
885 2 $subcommands->{$1} = $commands->{$command};
886 }
887  
888 29 return $subcommands;
889 }
890  
891 29 ▶sub command_usage_command {
Subroutine command_usage_command: called CC=2 SCAR=7.0
Pod documented
892 29 my ($self,$command_meta_class) = @_;
893  
894 29 ▶ $command_meta_class ||= $self;
Condition33%1/3
$command_meta_class ||= $self
l!l&&r!l&&!r
2900
MC/DC0%0/2
$command_meta_class ||= $self
$command_meta_class $self
Truth table33%1/3
A ||= B
A= $command_meta_classB= $self
ABresult
1X1
011
000
895  
896 29 my @usage;
897 29 push(@usage,$self->command_usage_header($command_meta_class));
898 29 push(@usage,$self->command_usage_description($command_meta_class));
899 29 push(@usage,$self->command_usage_parameters($command_meta_class,'parameters:')); # LOCALIZE
900 29 push(@usage,$self->command_usage_options($command_meta_class,'options:')); # LOCALIZE
901 29 push(@usage,$self->command_usage_subcommands('available subcommands:',$self->command_subcommands($command_meta_class))); # LOCALIZE
902  
903 29 return @usage;
904 }
905  
906 6 ▶sub command_usage_global {
Subroutine command_usage_global: called CC=2 SCAR=6.9
Pod documented
907 6 my ($self) = @_;
908  
909 6 my @usage;
910 6 push(@usage,$self->command_usage_header());
911  
912 6 my $description = $self->command_usage_description($self);
913 6 ▶ push(@usage,$description)
Branch: 1 branch
BranchTrueFalseTotal
if $description 3 3 6
914 if $description;
915 6 push(@usage,$self->command_usage_parameters($self,'global parameters:')); # LOCALIZE
916 6 push(@usage,$self->command_usage_options($self,'global options:')); # LOCALIZE
917 6 push(@usage,$self->command_usage_subcommands('available commands:',$self->app_commands)); # LOCALIZE
918  
919 6 return @usage;
920 }
921  
922 35 ▶sub command_usage_subcommands {
Subroutine command_usage_subcommands: called CC=7 SCAR=19.9
Pod undocumented
923 35 my ($self,$headline,$commands) = @_;
924  
925 35 my @commands;
926  
927 35 foreach my $command (keys %$commands) {
928 25 my $class = $commands->{$command};
929 25 Class::Load::load_class($class);
930 }
931  
932 35 foreach my $command (keys %$commands) {
933 25 my $class = $commands->{$command};
934  
935 25 ▶ unless ($class->can('meta')
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("meta") and $class->DOES("MooseX::App::Role::Common")) 0 25 25
Condition33%1/3
$class->can("meta") and $class->DOES("MooseX::App::Role::Common")
!ll&&!rl&&r
0025
MC/DC0%0/2
$class->can("meta") and $class->DOES("MooseX::App::Role::Common")
$class->can("meta") $class->DOES("MooseX::App::Role::Common")
Truth table33%1/3
A and B
A= $class->can("meta")B= $class->DOES("MooseX::App::Role::Common")
ABresult
0X0
100
111
936 && $class->DOES('MooseX::App::Role::Common')) {
937 0 Moose->throw_error('Class '.$class.' is not a proper MooseX::App::Command class. You either need to use MooseX::App::Command or exclude this class via app_exclude')
938 }
939  
940 25 my $command_description;
941 25 ▶ $command_description = $class->meta->command_short_description
Branch: 1 branch
BranchTrueFalseTotal
if $class->meta->can("command_short_description") 25 0 25
942 if $class->meta->can('command_short_description');
943  
944 25 ▶ $command_description ||= '';
Condition100%2/2
$command_description ||= ""
l!l
196
MC/DC100%1/1
$command_description ||= ""
$command_description
Truth table100%2/2
A
A= $command_description
Aresult
11
00
945 25 push(@commands,[$command,$command_description]);
946 }
947  
948 35 @commands = sort { $a->[0] cmp $b->[0] } @commands;
949 35 push(@commands,['help','Prints this usage information']); # LOCALIZE
950  
951 35 return $self->command_message(
952 header => $headline,
953 body => MooseX::App::Utils::format_list(@commands),
954 );
955 }
956  
957 16 1;
958  
959 __END__
« App/Meta/Role/Attribute/Option.pm ↑ Summary App/Message/BlockColor.pm »