Commando/Command.pm

filter: Statementstmt 86.9% 113 / 130 Branchbran 86.3% 19 / 22 Conditioncond 45.4% 10 / 22 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 83.3% 25 / 30 Podpod 0.0% 0 / 18 totaltotal 71.0% 167 / 235 CC 23 SCAR 11.9
CC
23
Cov
82%
__ANON__
12.0
execute
7.3
syntax
4.0
CRAP 3.3
↑ Summary Commando/Option.pm »
1 package App::Commando::Command;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 ▶use App::Commando::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use App::Commando::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use App::Commando::Presenter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 has 'actions' => ( is => 'rw' );
15 7 has 'aliases' => ( is => 'ro' );
16 7 has 'commands' => ( is => 'rw' );
17 7 has 'description' => ( is => 'rw' );
18 7 has 'map' => ( is => 'ro' );
19 7 has 'name' => ( is => 'ro' );
20 7 has 'options' => ( is => 'rw' );
21 7 has 'parent' => ( is => 'rw' );
22  
23 13 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
24 13 my ($class, $name, $parent) = @_;
25  
26 return {
27 13 actions => [],
28 aliases => [],
29 commands => {},
30 map => {},
31 name => $name,
32 options => [],
33 parent => $parent,
34 };
35 }
36  
37 # Gets or sets the command version
38 10 ▶sub version {
Subroutine version: called CC=2 SCAR=6.9
Pod undocumented
39 10 my ($self, $version) = @_;
40  
41 10 ▶ $self->{_version} = $version if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 2 8 10
42 10 return $self->{_version};
43 }
44  
45 6 ▶sub syntax {
Subroutine syntax: called CC=4 SCAR=13.9
Pod undocumented
46 6 my ($self, $syntax) = @_;
47  
48 6 ▶ $self->{_syntax} = $syntax if defined $syntax;
Branch: 1 branch
BranchTrueFalseTotal
if defined $syntax 1 5 6
49  
50 6 my @syntax_list = ();
51  
52 6 ▶ if ($self->parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parent) 1 5 6
53 1 my $parent_syntax = $self->parent->syntax;
54 1 $parent_syntax =~ s/<[\w\s-]+>|\[[\w\s-]+\]//g;
55 1 $parent_syntax =~ s/^\s+|\s+$//g;
56 1 push @syntax_list, $parent_syntax;
57 }
58 6 ▶ push @syntax_list, ($self->{_syntax} || $self->name);
Condition66%2/3
$self->{'_syntax'} || $self->name
l!l&&r!l&&!r
240
MC/DC0%0/2
$self->{'_syntax'} || $self->name
$self->{'_syntax'} $self->name
Truth table66%2/3
A || B
A= $self->{'_syntax'}B= $self->name
ABresult
1X1
011
000
59  
60 6 return join ' ', @syntax_list;
61 }
62  
63 3 ▶sub default_command {
Subroutine default_command: called CC=3 SCAR=11.0
Pod undocumented
64 3 my ($self, $command_name) = @_;
65  
66 3 ▶ if ($command_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_name) { } 2 1 3
67 2 ▶ if (exists $self->commands->{$command_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->commands->{$command_name}) { } 1 1 2
68 1 return $self->{_default_command} = $self->commands->{$command_name};
69 }
70 else {
71 1 croak "$command_name couldn't be found in this command's list of " .
72 "commands.";
73 }
74 }
75 else {
76 1 return $self->{_default_command};
77 }
78 }
79  
80 36 ▶sub option {
Subroutine option: called CC=1 SCAR=0.0
Pod undocumented
81 36 my ($self, $config_key, @info) = @_;
82  
83 36 my $option = App::Commando::Option->new($config_key, @info);
84 36 push @{$self->options}, $option;
85 36 $self->map->{$option} = $config_key;
86  
87 36 return $option;
88 }
89  
90 0 ▶sub command {
Subroutine command: not called CC=1 SCAR=6.9
Pod undocumented
91 0 my ($self, $command_name) = @_;
92  
93 0 my $cmd = App::Commando::Command->new($command_name, $self);
94 0 $self->commands->{$command_name} = $cmd;
95  
96 0 return $cmd;
97 }
98  
99 2 ▶sub alias {
Subroutine alias: called CC=2 SCAR=6.9
Pod undocumented
100 2 my ($self, $command_name) = @_;
101  
102 2 $self->logger->debug("adding alias to parent for self: $command_name");
103 2 push @{$self->aliases}, $command_name;
104 2 ▶ $self->parent->commands->{$command_name} = $self if defined $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->parent 1 1 2
105 }
106  
107 8 ▶sub action {
Subroutine action: called CC=1 SCAR=0.0
Pod undocumented
108 8 my ($self, $code) = @_;
109  
110 8 push @{$self->actions}, $code;
111 }
112  
113 11 ▶sub logger {
Subroutine logger: called CC=2 SCAR=7.5
Pod undocumented
114 11 my ($self) = @_;
115  
116 11 ▶ unless ($self->{_logger}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_logger"}) 11 0 11
117 11 $self->{_logger} = App::Commando::Logger->new(*STDOUT);
118 11 $self->{_logger}->level('info');
119 0 ▶ $self->{_logger}->formatter(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 my ($level, $message) = @_;
121  
122 0 return $self->identity . ' | ' .
123 sprintf("%-7s", ucfirst lc $level) . ": $message\n";
124 11 });
125 }
126  
127 11 return $self->{_logger};
128 }
129  
130 9 ▶sub go {
Subroutine go: called CC=3 SCAR=13.1
Pod undocumented
131 9 my ($self, $argv, $config) = @_;
132  
133 9 ▶ if (defined $argv->[0] && exists $self->commands->{$argv->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $argv->[0] and exists $self->commands->{$argv->[0]}) { } 0 9 9
Condition66%2/3
defined $argv->[0] and exists $self->commands->{$argv->[0]}
!ll&&!rl&&r
180
MC/DC0%0/2
defined $argv->[0] and exists $self->commands->{$argv->[0]}
defined $argv->[0] exists $self->commands->{$argv->[0]}
Truth table66%2/3
A and B
A= defined $argv->[0]B= exists $self->commands->{$argv->[0]}
ABresult
0X0
100
111
134 0 my $cmd = $self->commands->{$argv->[0]};
135 0 $self->logger->debug("Found subcommand " . $cmd->name);
136 0 shift @$argv;
137 0 $cmd->go($argv, $config);
138 }
139 else {
140 9 $self->logger->debug('No additional command found, time to exec');
141 9 $self->process_options($config);
142 9 return $self;
143 }
144 }
145  
146 9 ▶sub process_options {
Subroutine process_options: called CC=2 SCAR=6.9
Pod undocumented
147 9 my ($self, $config) = @_;
148  
149 9 my %options_spec = ();
150  
151 9 for my $option (@{$self->options}) {
152 6 ▶ $options_spec{$option->for_get_options} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 6 my ($name, $value) = @_;
154 6 $config->{$self->map->{$option}} = $value;
155 16 };
156 }
157  
158 9 %options_spec = $self->add_default_options(%options_spec);
159  
160 9 Getopt::Long::Configure('pass_through');
161 9 GetOptions(%options_spec);
162 }
163  
164 9 ▶sub add_default_options {
Subroutine add_default_options: called CC=1 SCAR=1.4
Pod undocumented
165 9 my ($self, %options_spec) = @_;
166  
167 9 my $option;
168  
169 9 $option = $self->option('show_help', '-h', '--help', 'Show this message');
170 0 ▶ $options_spec{$option->for_get_options} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 print $self->as_string . "\n";
172 0 exit(0);
173 9 };
174  
175 9 $option = $self->option('show_version', '-v', '--version',
176 'Print the name and version');
177 0 ▶ $options_spec{$option->for_get_options} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
178 0 ▶ print(($self->name || '') . " " . ($self->version || '') . "\n");
Condition0%0/4
$self->name || ''
l!l
00
$self->version || ''
l!l
00
MC/DC0%0/1
$self->name || ''
$self->name
MC/DC0%0/1
$self->version || ''
$self->version
Truth table0%0/2
A
A= $self->name
Aresult
11
00
Truth table0%0/2
A
A= $self->version
Aresult
11
00
179 0 exit(0);
180 9 };
181  
182 9 return %options_spec;
183 }
184  
185 8 ▶sub execute {
Subroutine execute: called CC=6 SCAR=19.9
Pod undocumented
186 8 my ($self, $argv, $config) = @_;
187  
188 8 ▶ $argv //= [];
Condition50%1/2
$argv //= []
l!l
80
MC/DC0%0/1
$argv //= []
$argv
Truth table50%1/2
A
A= $argv
Aresult
11
00
189 8 ▶ $config //= {};
Condition50%1/2
$config //= {}
l!l
80
MC/DC0%0/1
$config //= {}
$config
Truth table50%1/2
A
A= $config
Aresult
11
00
190  
191 8 ▶ if (!@{$self->actions} && defined $self->default_command) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->actions;} and defined $self->default_command) { } 0 8 8
Condition33%1/3
not @{$self->actions;} and defined $self->default_command
!ll&&!rl&&r
800
MC/DC0%0/2
not @{$self->actions;} and defined $self->default_command
not @{$self->actions;} defined $self->default_command
Truth table33%1/3
not(A) and B
A= not @{$self->actions;}B= defined $self->default_command
ABresult
0X0
100
111
192 0 $self->default_command->execute;
193 }
194 else {
195 8 for my $action (@{$self->actions}) {
196 8 &$action($argv, $config);
197 }
198 }
199 }
200  
201 4 ▶sub identity {
Subroutine identity: called CC=2 SCAR=6.9
Pod undocumented
202 4 my ($self) = @_;
203  
204 4 ▶ return $self->full_name .
Branch: 1 branch
BranchTrueFalseTotal
defined $self->version ? : 2 2 4
205 (defined $self->version ? ' ' . $self->version : '');
206 }
207  
208 12 ▶sub full_name {
Subroutine full_name: called CC=3 SCAR=11.1
Pod undocumented
209 12 my ($self) = @_;
210  
211 return
212 12 ▶ ($self->parent && $self->parent->full_name ?
Branch: 1 branch
BranchTrueFalseTotal
$self->parent && $self->parent->full_name ? : 3 9 12
Condition66%2/3
$self->parent and $self->parent->full_name
!ll&&!rl&&r
930
MC/DC0%0/2
$self->parent and $self->parent->full_name
$self->parent $self->parent->full_name
Truth table66%2/3
A and B
A= $self->parentB= $self->parent->full_name
ABresult
0X0
100
111
213 $self->parent->full_name . ' ' : '') .
214 $self->name;
215 }
216  
217 4 ▶sub names_and_aliases {
Subroutine names_and_aliases: called CC=1 SCAR=0.0
Pod undocumented
218 4 my ($self) = @_;
219  
220 4 return join ', ', $self->name, @{$self->aliases};
221 }
222  
223 3 ▶sub summarize {
Subroutine summarize: called CC=2 SCAR=7.2
Pod undocumented
224 3 my ($self) = @_;
225  
226 3 ▶ return sprintf " %-20s %s", $self->names_and_aliases,
Condition50%1/2
$self->description || ''
l!l
30
MC/DC0%0/1
$self->description || ''
$self->description
Truth table50%1/2
A
A= $self->description
Aresult
11
00
227 ($self->description || '');
228 }
229  
230 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod undocumented
231 0 my ($self) = @_;
232  
233 0 return App::Commando::Presenter->new($self)->command_presentation;
234 }
235  
236 7 1;
237  
238 __END__
↑ Summary Commando/Option.pm »