CLI/Command.pm

filter: Statementstmt 98.3% 120 / 122 Branchbran 78.5% 22 / 28 Conditioncond 60.0% 12 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 23 / 23 Podpod 66.6% 8 / 12 totaltotal 86.1% 187 / 217 CC 26 SCAR 14.7
CC
26
Cov
91%
loc_text
9.1
subcommand
6.1
cascadable
6.1
CRAP 4.4
« CLI/Command/Help.pm ↑ Summary CLI/Helper.pm »
1 package App::CLI::Command;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Locale::Maketext::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use App::CLI::Helper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Class::Load qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 App::CLI::Command - Base class for App::CLI commands
13  
14 =head1 SYNOPSIS
15  
16 package MyApp::List;
17 use base qw(App::CLI::Command);
18  
19 use constant options => (
20 'verbose' => 'verbose',
21 'n|name=s' => 'name',
22 );
23  
24 sub run {
25 my ( $self, $arg ) = @_;
26  
27 print "verbose" if $self->{verbose};
28  
29 my $name = $self->{name}; # get arg following long option --name
30  
31 # anything you want this command do
32 }
33  
34 # See App::CLI for information of how to invoke (sub)command.
35  
36 =head1 DESCRIPTION
37  
38  
39 =cut
40  
41 2 ▶use constant subcommands => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use constant options => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 15 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
45 15 my $class = shift;
46 15 bless {@_}, $class;
47 }
48  
49 20 ▶sub command_options {
Subroutine command_options: called CC=1 SCAR=0.0
Pod undocumented
50 20 ( ( map { $_ => $_ } $_[0]->subcommands ), $_[0]->options );
51 }
52  
53 20 ▶sub run_command {
Subroutine run_command: called CC=1 SCAR=0.0
Pod undocumented
54 20 my $self = shift;
55 20 $self->run(@_);
56 }
57  
58 1 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
59 1 my $class = shift;
60 1 Carp::croak ref($class) . " does not implement mandatory method 'run'\n";
61 }
62  
63 =head3 subcommand()
64  
65 return old genre subcommand of $self;
66  
67 =cut
68  
69 20 ▶sub subcommand {
Subroutine subcommand: called CC=6 SCAR=18.1
Pod documented
70 20 my $self = shift;
71 20 my @cmd = $self->subcommands;
72 0 ▶ @cmd = values %{ { $self->options } } if @cmd && $cmd[0] eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if @cmd and $cmd[0] eq "*" 0 20 20
Condition66%2/3
@cmd and $cmd[0] eq "*"
!ll&&!rl&&r
1370
MC/DC0%0/2
@cmd and $cmd[0] eq "*"
@cmd $cmd[0] eq "*"
Truth table66%2/3
A and B
A= @cmdB= $cmd[0] eq "*"
ABresult
0X0
100
111
73 20 my $subcmd = undef;
74 20 for ( grep { $self->{$_} } @cmd ) {
75 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶ if ( exists ${ ref($self) . '::' }{ $_ . '::' } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists ${ref($self) . '::';}{$_ . '::'}) 1 0 1
77 1 my %data = %{$self};
78 1 $subcmd = bless( {%data}, ( ref($self) . "::$_" ) );
79 1 last;
80 }
81 }
82 20 ▶ $subcmd ? $subcmd : $self;
Branch: 1 branch
BranchTrueFalseTotal
$subcmd ? : 1 19 20
83 }
84  
85 =head3 cascading()
86  
87 Return instance of cascading subcommand invoked if it was listed in your
88 constant subcommands.
89  
90 =cut
91  
92 6 ▶sub cascading {
Subroutine cascading: called CC=2 SCAR=7.1
Pod documented
93 6 my $self = shift;
94 6 ▶ if ( my $subcmd = $self->cascadable ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subcmd = $self->cascadable) { } 6 0 6
95 6 shift @ARGV;
96 6 my %data = %{$self};
97 6 return bless {%data}, $subcmd;
98 }
99 else {
100 0 die $self->error_cmd( $ARGV[0] );
101 }
102 }
103  
104 =head3 cascadable()
105  
106 Return package name of subcommand if the subcommand invoked is in your
107 constant subcommands, otherwise, return C<undef>.
108  
109 =cut
110  
111 32 ▶sub cascadable {
Subroutine cascadable: called CC=6 SCAR=18.0
Pod documented
112 32 my $self = shift;
113 32 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
3200
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
114 32 for ( $self->subcommands ) {
115 2 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
116 30 my $package_name = $class . '::' . $_;
117 30 load_class $package_name;
118 30 ▶ if ( $ARGV[0]
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] and ucfirst $ARGV[0] eq $_ and exists ${$class . '::';}{$_ . '::'}) 12 18 30
Condition83%5/6
$ARGV[0] and ucfirst $ARGV[0] eq $_ and exists ${$class . '::';}{$_ . '::'}
!ll&&!rl&&r
18012
$ARGV[0] and ucfirst $ARGV[0] eq $_
!ll&&!rl&&r
41412
MC/DC66%2/3
$ARGV[0] and ucfirst $ARGV[0] eq $_ and exists ${$class . '::';}{$_ . '::'}
$ARGV[0] ucfirst $ARGV[0] eq $_ exists ${$class . '::';}{$_ . '::'}
Truth table75%3/4
A and B and C
A= $ARGV[0]B= ucfirst $ARGV[0] eq $_C= exists ${$class . '::';}{$_ . '::'}
ABCresult
0XX0
10X0
1100
1111
119 && (ucfirst( $ARGV[0] ) eq $_)
120 12 && exists ${ $class . '::' }{ $_ . '::' } )
121 {
122 12 return $package_name;
123 }
124 }
125 20 return undef;
126 }
127  
128 =head3 app
129  
130 Return the object referring to the current app.
131  
132 =cut
133  
134 70 ▶sub app {
Subroutine app: called CC=2 SCAR=6.9
Pod documented
135 70 my $self = shift;
136  
137 70 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 44 26 70
138 44 $self->{app} = shift;
139 44 weaken( $self->{app} );
140 }
141  
142 70 return $self->{app};
143 }
144  
145 =head3 brief_usage ($file)
146  
147 Display a one-line brief usage of the command object. Optionally, a file
148 could be given to extract the usage from the POD.
149  
150 =cut
151  
152 6 ▶sub brief_usage {
Subroutine brief_usage: called CC=5 SCAR=16.4
Pod documented
153 6 my ( $self, $file ) = @_;
154 6 ▶ open my ($podfh), '<', ( $file || $self->filename ) or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $podfh, "<", $file || $self->filename or return 6 0 6
Condition66%2/3
$file || $self->filename
l!l&&r!l&&!r
420
MC/DC0%0/2
$file || $self->filename
$file $self->filename
Truth table66%2/3
A || B
A= $fileB= $self->filename
ABresult
1X1
011
000
155 6 local $/ = undef;
156 6 my $buf = <$podfh>;
157 6 my $base = ref $self->app;
158 6 my $indent = " ";
159 6 ▶ if ( $buf =~ /^=head1\s+NAME\s*\Q$base\E::(\w+)( - .+)$/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^=head1\s+NAME\s*\Q$base\E::(\w+)( - .+)$/m) { } 4 2 6
160 4 print $indent, loc( lc($1) . $2 ), "\n";
161 }
162 else {
163 2 ▶ my $cmd = $file || $self->filename;
Condition33%1/3
$file || $self->filename
l!l&&r!l&&!r
020
MC/DC0%0/2
$file || $self->filename
$file $self->filename
Truth table33%1/3
A || B
A= $fileB= $self->filename
ABresult
1X1
011
000
164 2 $cmd =~ s/^(?:.*)\/(.*?).pm$/$1/;
165 2 print $indent, lc($cmd), " - ", loc("undocumented") . "\n";
166 }
167 6 close $podfh;
168 }
169  
170 =head3 usage ($want_detail)
171  
172 Display usage. If C<$want_detail> is true, the C<DESCRIPTION>
173 section is displayed as well.
174  
175 =cut
176  
177 3 ▶sub usage {
Subroutine usage: called CC=2 SCAR=6.9
Pod documented
178 3 my ( $self, $want_detail ) = @_;
179 3 my $fname = $self->filename;
180 3 my ($cmd) = $fname =~ m{\W(\w+)\.pm$};
181 3 require Pod::Simple::Text;
182 3 my $parser = Pod::Simple::Text->new;
183 3 my $buf;
184 3 $parser->output_string( \$buf );
185 3 $parser->parse_file($fname);
186  
187 3 my $base = ref $self->app;
188 3 $buf =~ s/\Q$base\E::(\w+)/\l$1/g;
189 3 $buf =~ s/^AUTHORS.*//sm;
190 3 ▶ $buf =~ s/^DESCRIPTION.*//sm unless $want_detail;
Branch: 1 branch
BranchTrueFalseTotal
unless $want_detail 2 1 3
191 3 print $self->loc_text($buf);
192 }
193  
194 =head3 loc_text $text
195  
196 Localizes the body of (formatted) text in C<$text> and returns the
197 localized version.
198  
199 =cut
200  
201 4 ▶sub loc_text {
Subroutine loc_text: called CC=9 SCAR=22.1
Pod documented
202 4 my $self = shift;
203 4 my $buf = shift;
204  
205 4 my $out = "";
206 4 foreach my $line ( split( /\n\n+/, $buf, -1 ) ) {
207 11 ▶ if ( my @lines = $line =~ /^( {4}\s+.+\s*)$/mg ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my(@lines) = $line =~ /^( {4}\s+.+\s*)$/gm) { } 1 10 11
elsif ($line =~ /^(\s+)(\w+ - .*)$/) { } 2 8 10
elsif (length $line) { } 5 3 8
208 1 foreach my $chunk (@lines) {
209 1 ▶ $chunk =~ /^(\s*)(.+?)( *)(: .+?)?(\s*)$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$chunk =~ /^(\s*)(.+?)( *)(: .+?)?(\s*)$/ or next 1 0 1
210 1 my $spaces = $3;
211 1 ▶ my $loc = $1 . loc( $2 . ( $4 || '' ) ) . $5;
Condition50%1/2
$4 || ''
l!l
01
MC/DC0%0/1
$4 || ''
$4
Truth table50%1/2
A
A= $4
Aresult
11
00
212 1 ▶ $loc =~ s/: /$spaces: / if $spaces;
Branch: 1 branch
BranchTrueFalseTotal
if $spaces 0 1 1
213 1 $out .= $loc . "\n";
214 }
215 1 $out .= "\n";
216 }
217 elsif ( $line =~ /^(\s+)(\w+ - .*)$/ ) {
218 2 $out .= $1 . loc($2) . "\n\n";
219 }
220 elsif ( length $line ) {
221 5 $out .= loc($line) . "\n\n";
222 }
223 }
224 4 return $out;
225 }
226  
227 =head3 filename
228  
229 Return the filename for the command module.
230  
231 =cut
232  
233 7 ▶sub filename {
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
234 7 my $self = shift;
235 7 my $fname = ref($self);
236 7 $fname =~ s{::[a-z]+$}{}; # subcommand
237 7 $fname =~ s{::}{/}g;
238 7 return $INC{"$fname.pm"};
239 }
240  
241 =head1 SEE ALSO
242  
243 L<App::CLI>, L<Getopt::Long>
244  
245 =head1 AUTHORS
246  
247 Chia-liang Kao E<lt>clkao@clkao.orgE<gt>
248  
249 Cornelius Lin E<lt>cornelius.howl@gmail.comE<gt>
250  
251 Shelling E<lt>navyblueshellingford@gmail.comE<gt>
252  
253 Paul Cochrane E<lt>paul@liekut.deE<gt> (current maintainer)
254  
255 =head1 COPYRIGHT
256  
257 Copyright 2005-2006 by Chia-liang Kao E<lt>clkao@clkao.orgE<gt>.
258  
259 This program is free software; you can redistribute it and/or modify it
260 under the same terms as Perl itself.
261  
262 See L<http://www.perl.com/perl/misc/Artistic.html>
263  
264 =cut
265  
266 2 1;
« CLI/Command/Help.pm ↑ Summary CLI/Helper.pm »