lib/CLI/Dispatch/Help.pm

filter: Statementstmt 99.6% 279 / 280 Branchbran 28.8% 15 / 52 Conditioncond 15.7% 3 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 71 / 71 Podpod 100% 9 / 9 totaltotal 85.2% 377 / 442 CC 41 SCAR 20.7
CC
41
Cov
85%
list_commands
49.1
_lookup
11.2
output
5.5
CRAP 7.9
« blib/lib/CLI/Dispatch/Command.pm ↑ Summary lib/CLI/Dispatch/Command.pm »
1 package CLI::Dispatch::Help;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
5 13 ▶use base qw( CLI::Dispatch::Command );
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
6 13 ▶use Class::Unload;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
7 13 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8 13 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
9 13 ▶use Pod::Simple::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
10 13 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
11 13 ▶use String::CamelCase;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
12 13 ▶use Term::Encoding ();
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
13 13 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
14  
15 9 ▶my $term_encoding = eval {
Condition50%1/2
eval { do { &find_encoding(scalar Term::Encoding::get_encoding()) } } || 'utf8'
l!l
90
MC/DC0%0/1
eval { do { &find_encoding(scalar Term::Encoding::get_encoding()) } } || 'utf8'
eval { do { &find_encoding(scalar Term::Encoding::get_encoding()) } }
Truth table50%1/2
A
A= eval { do { &find_encoding(scalar Term::Encoding::get_encoding()) } }
Aresult
11
00
16 9 find_encoding(Term::Encoding::get_encoding())
17 } || 'utf8';
18  
19  
20 8 ▶sub options {qw( from|decode=s to|encode=s )}
Subroutine options: called CC=1 SCAR=0.0
Pod documented
21  
22 ▶sub extra_namespaces {}
Subroutine extra_namespaces: called CC=1 SCAR=0.0
Pod documented
23  
24 sub run {
25 8 ▶ my ($self, @args) = @_;
Subroutine run: called CC=2 SCAR=7.0
Pod documented
26  
27 8 my $text;
28 8 ▶ if ( @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 4 0 4
29 8 $text = $self->extract_pod( @args );
30 }
31 else {
32 4 $text = $self->list_commands;
33 }
34  
35 8 $self->output( $text );
36 }
37  
38 sub output {
39 14 ▶ my ($self, $text, $no_print) = @_;
Subroutine output: called CC=5 SCAR=17.0
Pod documented
40  
41 14 ▶ unless ( Encode::is_utf8( $text ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Encode::is_utf8($text)) 6 4 10
42 10 ▶ $text = decode( $self->option('from') || 'utf8', $text )
Condition50%1/2
$self->option('from') || 'utf8'
l!l
06
MC/DC0%0/1
$self->option('from') || 'utf8'
$self->option('from')
Truth table50%1/2
A
A= $self->option('from')
Aresult
11
00
43 }
44 14 ▶ $text = encode( $self->option('to') || $term_encoding, $text );
Condition33%1/3
$self->option('to') || $term_encoding
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->option('to') || $term_encoding
$self->option('to') $term_encoding
Truth table33%1/3
A || B
A= $self->option('to')B= $term_encoding
ABresult
1X1
011
000
45  
46 14 ▶ print $text unless $no_print;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_print 10 0 10
47  
48 14 return $text;
49 }
50  
51 sub extract_pod {
52 8 ▶ my ($self, $command) = @_;
Subroutine extract_pod: called CC=3 SCAR=11.4
Pod documented
53  
54 8 my $content = $self->_lookup( $command );
55  
56 8 ▶ unless ( $content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 0 4 4
57 4 ▶ $self->logger(1) unless $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->logger 0 0 0
58 4 $self->log( warn => "$command is not found" );
59 4 return $self->list_commands;
60 }
61  
62 8 my $pod = $self->_parse_pod($content);
63  
64 8 return $self->extract_pod_body($pod);
65 }
66  
67 sub extract_pod_body {
68 8 ▶ my ($self, $pod) = @_;
Subroutine extract_pod_body: called CC=1 SCAR=0.0
Pod documented
69  
70 # remove the first ("NAME") section as the command does not
71 # always belong to the same namespace as the dispatcher/script.
72 # (default CLI::Dispatch namespace may be confusing for end users)
73 8 $pod =~ s/^\S+\s+(.+?)\n(?=\S)//s;
74  
75 8 return $pod;
76 }
77  
78 sub list_commands {
79 4 ▶ my $self = shift;
Subroutine list_commands: called CC=19 SCAR=38.9
Pod documented
80  
81 4 my @paths = map { s{::}{/}g; $_ } $self->_namespaces;
82  
83 4 my %found;
84 my %classes;
85 4 my $maxlength = 0;
86 4 foreach my $inc ( @INC ) {
87 4 foreach my $path ( @paths ) {
88 4 my $dir = path( $inc, $path );
89 4 ▶ next unless $dir->exists && $dir->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->exists and $dir->is_dir 0 0 0
Condition0%0/3
$dir->exists and $dir->is_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$dir->exists and $dir->is_dir
$dir->exists $dir->is_dir
Truth table0%0/3
A and B
A= $dir->existsB= $dir->is_dir
ABresult
0X0
100
111
90 4 my $iter = $dir->iterator({recurse => 1});
91 4 ▶ while (my $file = $iter->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = &$iter()) 0 0 0
92 4 ▶ next if $file->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $file->is_dir 0 0 0
93  
94 4 my $basename = $file->basename;
95 4 $basename =~ s/\.(?:pm|pod)$//;
96  
97 4 ▶ next if defined $found{$basename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $found{$basename} 0 0 0
98  
99 4 (my $class = $path) =~ s{/}{::}g;
100 4 $class .= '::'.$basename;
101 4 $classes{$class} = 1;
102  
103 # ignore base class
104 4 ▶ next if $class eq 'CLI::Dispatch::Command';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "CLI::Dispatch::Command" 0 0 0
105  
106 4 my $podfile = $file->parent->child($basename . '.pod');
107 4 my $pmfile = $file->parent->child($basename . '.pm');
108  
109 # should always parse .pod file if it exists
110 4 ▶ my $pod = $self->_parse_pod($podfile->exists ? $podfile->slurp : $file->slurp);
Branch: 1 branch
BranchTrueFalseTotal
$podfile->exists ? : 0 0 0
111  
112 4 $basename = $self->convert_command($basename);
113  
114 4 ▶ $found{$basename} ||= $self->extract_brief_description($pod, $class);
Condition0%0/3
$found{$basename} ||= $self->extract_brief_description($pod, $class)
l!l&&r!l&&!r
000
MC/DC0%0/2
$found{$basename} ||= $self->extract_brief_description($pod, $class)
$found{$basename} $self->extract_brief_description($pod, $class)
Truth table0%0/3
A ||= B
A= $found{$basename}B= $self->extract_brief_description($pod, $class)
ABresult
1X1
011
000
115  
116 # check availability
117 4 ▶ if ( $pmfile->exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pmfile->exists) 0 0 0
118 4 my $loaded = Class::Inspector->loaded($class);
119 4 ▶ Class::Unload->unload($class) if $loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $loaded 0 0 0
120 4 my $error;
121 4 ▶ try { eval "require $class" or die }
Subroutine __ANON__: called CC=2 SCAR=11.6
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or die 0 0 0
122 4 ▶ catch { $error = $_ || 'Obscure error' };
Subroutine __ANON__: called CC=2 SCAR=9.2
Condition0%0/2
$_ || 'Obscure error'
l!l
00
MC/DC0%0/1
$_ || 'Obscure error'
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
123 4 ▶ if ($error) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error) { } 0 0 0
elsif ($class->can('check')) { } 0 0 0
124 4 ▶ if ($error =~ /^Can't locate /) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error =~ /^Can't locate /) { } 0 0 0
125 # most probably this is a subcommand of some command
126 # (ie. in a wrong namespace)
127 4 delete $found{$basename};
128 }
129 else {
130 4 $found{$basename} .= " [disabled: compile error]";
131 }
132 }
133 elsif ( $class->can('check') ) {
134 4 ▶ try { $class->check }
Subroutine __ANON__: called CC=1 SCAR=0.0
135 catch {
136 4 ▶ $error = $_ || 'Obscure error';
Subroutine __ANON__: called CC=2 SCAR=8.1
Condition0%0/2
$_ || 'Obscure error'
l!l
00
MC/DC0%0/1
$_ || 'Obscure error'
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
137 4 $error =~ s/\s+at .+? line \d+\.?\s*$//;
138 4 $found{$basename} .= " [disabled: $error]";
139 4 };
140 }
141 4 ▶ Class::Unload->unload($class) unless $loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded 0 0 0
142 }
143  
144 4 my $len = length $basename;
145 4 ▶ $maxlength = $len if $maxlength < $len;
Branch: 1 branch
BranchTrueFalseTotal
if $maxlength < $len 0 0 0
146 }
147 }
148 }
149  
150 4 my $text = '';
151 4 my $format = "%-${maxlength}s - %s\n";
152 4 foreach my $key ( sort keys %found ) {
153 4 $text .= sprintf($format, $key, $found{$key});
154 }
155 4 return $text;
156 }
157  
158 sub convert_command {
159 4 ▶ my ($self, $command) = @_;
Subroutine convert_command: called CC=1 SCAR=0.0
Pod documented
160 4 String::CamelCase::decamelize( $command );
161 }
162  
163 sub extract_brief_description {
164 4 ▶ my ($self, $pod, $class) = @_;
Subroutine extract_brief_description: called CC=2 SCAR=8.1
Pod documented
165  
166 # "NAME" header may be localized
167 4 my ($brief_desc) = $pod =~ /^\S+\s+$class\s+\-\s+(.+?)\n/s;
168  
169 4 ▶ return $brief_desc || '';
Condition0%0/2
$brief_desc || ''
l!l
00
MC/DC0%0/1
$brief_desc || ''
$brief_desc
Truth table0%0/2
A
A= $brief_desc
Aresult
11
00
170 }
171  
172 sub _parse_pod {
173 8 ▶ my ($self, $file) = @_;
Subroutine _parse_pod: called CC=1 SCAR=0.0
174  
175 8 my $parser = Pod::Simple::Text->new;
176 8 $parser->output_string( \my $pod );
177 8 $parser->parse_string_document("$file");
178  
179 8 return $pod;
180 }
181  
182 sub _namespaces {
183 7 ▶ my $self = shift;
Subroutine _namespaces: called CC=2 SCAR=7.0
184  
185 7 my %seen;
186 10 return grep { !$seen{$_}++ } (
187 $self->extra_namespaces,
188 7 ▶ @{ $self->option('_namespaces') || [] },
Branch: 1 branch
BranchTrueFalseTotal
$self->option("_namespaces") or [] 0 3 3
189 'CLI::Dispatch'
190 );
191 }
192  
193 sub _lookup {
194 8 ▶ my ($self, $command) = @_;
Subroutine _lookup: called CC=11 SCAR=24.1
195  
196 8 my @paths;
197 8 ▶ if ($command =~ s/^\+//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ s/^\+//) { } 1 3 4
198 5 $command =~ s{::}{/}g;
199 5 @paths = $command;
200 }
201 else {
202 7 @paths = map { s{::}{/}g; "$_/$command" } $self->_namespaces;
203 }
204  
205 8 foreach my $inc ( @INC ) {
206 15 foreach my $path ( @paths ) {
207 15 foreach my $ext (qw( pod pm )) {
208 22 my $file = path( $inc, "$path.$ext" );
209 22 ▶ return $file->slurp if $file->exists;
Branch: 1 branch
BranchTrueFalseTotal
if $file->exists 3 19 22
210 }
211 }
212 }
213  
214 # probably it's embedded in the caller...
215 1 my $ct = 0;
216 1 my %seen;
217 1 ▶ while (my @caller = caller($ct++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $ct++) 5 0 5
218 5 ▶ next if $caller[0] =~ /^CLI::Dispatch(::.+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] =~ /^CLI::Dispatch(::.+)?$/ 4 1 5
219 1 ▶ next if $seen{$caller[0]}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$caller[0]}++ 0 1 1
220 1 my $content = path($caller[1])->slurp;
221 1 for my $path ( @paths ) {
222 1 (my $package = $path) =~ s{/}{::}g;
223 1 ▶ if ($content =~ /=head1\s+\S+\s+$package/s) { # hopefully NAME
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /=head1\s+\S+\s+$package/s) 1 0 1
224 1 return $content;
225 }
226 }
227 }
228  
229 0 return;
230 }
231  
232 9 1;
233  
234 __END__
« blib/lib/CLI/Dispatch/Command.pm ↑ Summary lib/CLI/Dispatch/Command.pm »