Cmd/Command.pm

filter: Statementstmt 97.2% 71 / 73 Branchbran 61.5% 16 / 26 Conditioncond 81.8% 9 / 11 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 19 / 19 Podpod 100% 12 / 12 totaltotal 88.4% 130 / 147 CC 18 SCAR 16.9
CC
18
Cov
87%
abstract
12.2
execute
4.5
description
4.1
CRAP 5.4
« Cmd/Subdispatch.pm ↑ Summary Cmd/Tester.pm »
1 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package App::Cmd::Command 0.340;
5  
6 17 ▶use App::Cmd::ArgProcessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶BEGIN { our @ISA = 'App::Cmd::ArgProcessor' };
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: a base class for App::Cmd commands
10  
11 17 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #pod =method prepare
14 #pod
15 #pod my ($cmd, $opt, $args) = $class->prepare($app, @args);
16 #pod
17 #pod This method is the primary way in which App::Cmd::Command objects are built.
18 #pod Given the remaining command line arguments meant for the command, it returns
19 #pod the Command object, parsed options (as a hashref), and remaining arguments (as
20 #pod an arrayref).
21 #pod
22 #pod In the usage above, C<$app> is the App::Cmd object that is invoking the
23 #pod command.
24 #pod
25 #pod =cut
26  
27 33 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod documented
28 33 my ($class, $app, @args) = @_;
29  
30 33 my ($opt, $args, %fields)
31 = $class->_process_args(\@args, $class->_option_processing_params($app));
32  
33 return (
34 31 $class->new({ app => $app, %fields }),
35 $opt,
36 @$args,
37 );
38 }
39  
40 33 ▶sub _option_processing_params {
Subroutine _option_processing_params: called CC=1 SCAR=0.0
41 33 my ($class, @args) = @_;
42  
43 return (
44 33 $class->usage_desc(@args),
45 $class->opt_spec(@args),
46 );
47 }
48  
49 #pod =method new
50 #pod
51 #pod This returns a new instance of the command plugin. Probably only C<prepare>
52 #pod should use this.
53 #pod
54 #pod =cut
55  
56 33 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
57 33 my ($class, $arg) = @_;
58 33 bless $arg => $class;
59 }
60  
61 #pod =method execute
62 #pod
63 #pod $command_plugin->execute(\%opt, \@args);
64 #pod
65 #pod This method does whatever it is the command should do! It is passed a hash
66 #pod reference of the parsed command-line options and an array reference of left
67 #pod over arguments.
68 #pod
69 #pod If no C<execute> method is defined, it will try to call C<run> -- but it will
70 #pod warn about this behavior during testing, to remind you to fix the method name!
71 #pod
72 #pod =cut
73  
74 1 ▶sub execute {
Subroutine execute: called CC=3 SCAR=15.1
Pod documented
75 1 my $class = shift;
76  
77 1 ▶ if (my $run = $class->can('run')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $run = $class->can("run")) 0 1 1
78 warn "App::Cmd::Command subclasses should implement ->execute not ->run"
79 0 ▶ if $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HARNESS_ACTIVE"} 0 0 0
80  
81 0 return $class->$run(@_);
82 }
83  
84 1 Carp::croak ref($class) . " does not implement mandatory method 'execute'\n";
85 }
86  
87 #pod =method app
88 #pod
89 #pod This method returns the App::Cmd object into which this command is plugged.
90 #pod
91 #pod =cut
92  
93 60 ▶sub app { $_[0]->{app}; }
Subroutine app: called CC=1 SCAR=0.0
Pod documented
94  
95 #pod =method usage
96 #pod
97 #pod This method returns the usage object for this command. (See
98 #pod L<Getopt::Long::Descriptive>).
99 #pod
100 #pod =cut
101  
102 10 ▶sub usage { $_[0]->{usage}; }
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
103  
104 #pod =method command_names
105 #pod
106 #pod This method returns a list of command names handled by this plugin. The
107 #pod first item returned is the 'canonical' name of the command.
108 #pod
109 #pod If this method is not overridden by an App::Cmd::Command subclass, it will
110 #pod return the last part of the plugin's package name, converted to lowercase.
111 #pod For example, YourApp::Cmd::Command::Init will, by default, handle the command
112 #pod "init".
113 #pod
114 #pod Subclasses should generally get the superclass value of C<command_names>
115 #pod and then append aliases.
116 #pod
117 #pod =cut
118  
119 159 ▶sub command_names {
Subroutine command_names: called CC=2 SCAR=7.1
Pod documented
120 # from UNIVERSAL::moniker
121 159 ▶ (ref( $_[0] ) || $_[0]) =~ /([^:]+)$/;
Condition66%2/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
31560
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table66%2/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
122 159 return lc $1;
123 }
124  
125 #pod =method usage_desc
126 #pod
127 #pod This method should be overridden to provide a usage string. (This is the first
128 #pod argument passed to C<describe_options> from Getopt::Long::Descriptive.)
129 #pod
130 #pod If not overridden, it returns "%c COMMAND %o"; COMMAND is the first item in
131 #pod the result of the C<command_names> method.
132 #pod
133 #pod =cut
134  
135 26 ▶sub usage_desc {
Subroutine usage_desc: called CC=1 SCAR=0.0
Pod documented
136 26 my ($self) = @_;
137  
138 26 my ($command) = $self->command_names;
139 26 return "%c $command %o"
140 }
141  
142 #pod =method opt_spec
143 #pod
144 #pod This method should be overridden to provide option specifications. (This is
145 #pod list of arguments passed to C<describe_options> from Getopt::Long::Descriptive,
146 #pod after the first.)
147 #pod
148 #pod If not overridden, it returns an empty list.
149 #pod
150 #pod =cut
151  
152 19 ▶sub opt_spec {
Subroutine opt_spec: called CC=1 SCAR=0.0
Pod documented
153 19 return;
154 }
155  
156 #pod =method validate_args
157 #pod
158 #pod $command_plugin->validate_args(\%opt, \@args);
159 #pod
160 #pod This method is passed a hashref of command line options (as processed by
161 #pod Getopt::Long::Descriptive) and an arrayref of leftover arguments. It may throw
162 #pod an exception (preferably by calling C<usage_error>, below) if they are invalid,
163 #pod or it may do nothing to allow processing to continue.
164 #pod
165 #pod =cut
166  
167 22 ▶sub validate_args { }
Subroutine validate_args: called CC=1 SCAR=0.0
Pod documented
168  
169 #pod =method usage_error
170 #pod
171 #pod $self->usage_error("This command must not be run by root!");
172 #pod
173 #pod This method should be called to die with human-friendly usage output, during
174 #pod C<validate_args>.
175 #pod
176 #pod =cut
177  
178 1 ▶sub usage_error {
Subroutine usage_error: called CC=1 SCAR=0.0
Pod documented
179 1 my ( $self, $error ) = @_;
180 1 die "Error: $error\nUsage: " . $self->_usage_text;
181 }
182  
183 1 ▶sub _usage_text {
Subroutine _usage_text: called CC=1 SCAR=0.0
184 1 my ($self) = @_;
185 1 local $@;
186 1 join "\n", eval { $self->app->_usage_text }, eval { $self->usage->text };
187 }
188  
189 #pod =method abstract
190 #pod
191 #pod This method returns a short description of the command's purpose. If this
192 #pod method is not overridden, it will return the abstract from the module's Pod.
193 #pod If it can't find the abstract, it will look for a comment starting with
194 #pod "ABSTRACT:" like the ones used by L<Pod::Weaver::Section::Name>.
195 #pod
196 #pod =cut
197  
198 # stolen from ExtUtils::MakeMaker
199 33 ▶sub abstract {
Subroutine abstract: called CC=12 SCAR=25.0
Pod documented
200 33 my ($class) = @_;
201 33 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 33 33
202  
203 33 my $result;
204 my $weaver_abstract;
205  
206 # classname to filename
207 33 (my $pm_file = $class) =~ s!::!/!g;
208 33 $pm_file .= '.pm';
209 33 ▶ $pm_file = $INC{$pm_file} or return "(unknown)";
Branch: 1 branch
BranchTrueFalseTotal
$pm_file = $INC{$pm_file} or return "(unknown)" 33 0 33
210  
211 # if the pm file exists, open it and parse it
212 33 ▶ open my $fh, "<", $pm_file or return "(unknown)";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pm_file or return "(unknown)" 33 0 33
213  
214 33 local $/ = "\n";
215 33 my $inpod;
216  
217 33 ▶ while (local $_ = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(local $_ = readline $fh)) 1986 12 1998
218 # =cut toggles, it doesn't end :-/
219 1986 ▶ $inpod = /^=cut/ ? !$inpod : $inpod || /^=(?!cut)/;
Branch: 1 branch
BranchTrueFalseTotal
/^=cut/ ? : 0 1986 1986
Condition100%3/3
$inpod or /^=(?!cut)/
l!l&&r!l&&!r
78211887
MC/DC100%2/2
$inpod or /^=(?!cut)/
$inpod /^=(?!cut)/
Truth table100%3/3
A or B
A= $inpodB= /^=(?!cut)/
ABresult
1X1
011
000
220  
221 1986 ▶ if (/#+\s*ABSTRACT: (.*)/){
Branch: 1 branch
BranchTrueFalseTotal
if (/#+\s*ABSTRACT: (.*)/) 9 1977 1986
222 # takes ABSTRACT: ... if no POD defined yet
223 9 $weaver_abstract = $1;
224 }
225  
226 1986 ▶ next unless $inpod;
Branch: 1 branch
BranchTrueFalseTotal
unless $inpod 1887 99 1986
227 99 chomp;
228  
229 99 ▶ next unless /^(?:$class\s-\s)(.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:$class\s-\s)(.*)/ 78 21 99
230  
231 21 $result = $1;
232 21 last;
233 }
234  
235 33 ▶ return $result || $weaver_abstract || "(unknown)";
Condition80%4/5
$result || $weaver_abstract || '(unknown)'
l!l
2112
$result or $weaver_abstract
l!l&&r!l&&!r
21012
MC/DC50%1/2
$result || $weaver_abstract || '(unknown)'
$result $weaver_abstract
Truth table66%2/3
A or B
A= $resultB= $weaver_abstract
ABresult
1X1
011
000
236 }
237  
238 #pod =method description
239 #pod
240 #pod This method can be overridden to provide full option description. It
241 #pod is used by the built-in L<help|App::Cmd::Command::help> command.
242 #pod
243 #pod If not overridden, it uses L<Pod::Usage> to extract the description
244 #pod from the module's Pod DESCRIPTION section or the empty string.
245 #pod
246 #pod =cut
247  
248 4 ▶sub description {
Subroutine description: called CC=4 SCAR=14.0
Pod documented
249 4 my ($class) = @_;
250 4 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 4 0 4
251  
252 # classname to filename
253 4 (my $pm_file = $class) =~ s!::!/!g;
254 4 $pm_file .= '.pm';
255 4 ▶ $pm_file = $INC{$pm_file} or return '';
Branch: 1 branch
BranchTrueFalseTotal
$pm_file = $INC{$pm_file} or return "" 4 0 4
256  
257 4 ▶ open my $input, "<", $pm_file or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $input, "<", $pm_file or return "" 4 0 4
258  
259 4 my $descr = "";
260 4 open my $output, ">", \$descr;
261  
262 4 require Pod::Usage;
263 4 Pod::Usage::pod2usage( -input => $input,
264 -output => $output,
265 -exit => "NOEXIT",
266 -verbose => 99,
267 -sections => "DESCRIPTION",
268 indent => 0
269 );
270 4 $descr =~ s/Description:\n//m;
271 4 chomp $descr;
272  
273 4 return $descr;
274 }
275  
276 17 1;
277  
278 __END__
« Cmd/Subdispatch.pm ↑ Summary Cmd/Tester.pm »