CLI/Command/Help.pm

filter: Statementstmt 100% 57 / 57 Branchbran 87.5% 14 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 95.2% 80 / 84 CC 15 SCAR 17.7
CC
15
Cov
97%
_find_topic
8.0
run
8.0
BEGIN
1.0
CRAP 5.9
↑ Summary CLI/Command.pm »
1 package App::CLI::Command::Help;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw/App::CLI::Command/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Find qw(find);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Locale::Maketext::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Pod::Simple::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 App::CLI::Command::Help
12  
13 =head1 SYNOPSIS
14  
15 package MyApp::Help;
16 use base qw(App::CLI::Command::Help);
17  
18 sub run {
19 my $self = shift;
20 # preprocess
21 $self->SUPER::run(@_); # App::CLI::Command::Help would output POD of each command
22 }
23  
24 =head1 DESCRIPTION
25  
26 Your command class should be capitalized.
27  
28 To add a help message, just add POD in the command class:
29  
30 package YourApp::Command::Foo;
31  
32  
33 =head1 NAME
34  
35 YourApp::Command::Foo - execute foo
36  
37 =head1 DESCRIPTION
38  
39 blah blah
40  
41 =head1 USAGE
42  
43 ....
44  
45 =cut
46  
47 =cut
48  
49 7 ▶sub run {
Subroutine run: called CC=8 SCAR=20.8
Pod undocumented
50 7 my $self = shift;
51 7 my @topics = @_;
52  
53 7 ▶ push @topics, 'commands' unless (@topics);
Branch: 1 branch
BranchTrueFalseTotal
unless @topics 3 4 7
54  
55 7 foreach my $topic (@topics) {
56 7 ▶ if ( $topic eq 'commands' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($topic eq 'commands') { } 4 3 7
elsif (my $cmd = eval { do { $self->app->get_cmd($topic) } }) { } 1 2 3
elsif (my $file = $self->_find_topic($topic)) { } 1 1 2
57 4 $self->brief_usage($_) for $self->app->files;
58 }
59 3 elsif ( my $cmd = eval { $self->app->get_cmd($topic) } ) {
60 1 $cmd->usage(1);
61 }
62 elsif ( my $file = $self->_find_topic($topic) ) {
63 1 ▶ open my $fh, '<:encoding(UTF-8)', $file or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or die $! 1 0 1
64 1 require Pod::Simple::Text;
65 1 my $parser = Pod::Simple::Text->new;
66 1 my $buf;
67 1 $parser->output_string( \$buf );
68 1 $parser->parse_file($fh);
69  
70 1 $buf =~ s/^NAME\s+(.*?)::Help::\S+ - (.+)\s+DESCRIPTION/ $2:/;
71 1 print $self->loc_text($buf);
72 }
73 else {
74 1 die loc( "Cannot find help topic '%1'.\n", $topic );
75 }
76 }
77 6 return;
78 }
79  
80 1 ▶sub help_base {
Subroutine help_base: called CC=1 SCAR=0.0
Pod undocumented
81 1 my $self = shift;
82 1 return ref( $self->app ) . "::Help";
83 }
84  
85 1 my ( $inc, @prefix );
86  
87 2 ▶sub _find_topic {
Subroutine _find_topic: called CC=8 SCAR=20.8
88 2 my ( $self, $topic ) = @_;
89  
90 2 ▶ if ( !$inc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($inc) 1 1 2
91 1 my $pkg = __PACKAGE__;
92 1 $pkg =~ s{::}{/};
93 1 $inc = substr( __FILE__, 0, -length("$pkg.pm") );
94  
95 1 my $base = $self->help_base;
96 1 @prefix = ( loc($base) );
97 1 $prefix[0] =~ s{::}{/}g;
98 1 $base =~ s{::}{/}g;
99 1 ▶ push @prefix, $base if $prefix[0] ne $base;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix[0] ne $base 0 1 1
100 }
101  
102 2 foreach my $dir ( $inc, @INC ) {
103 11 foreach my $prefix (@prefix) {
104 11 foreach my $basename ( ucfirst( lc($topic) ), uc($topic) ) {
105 21 foreach my $ext ( 'pod', 'pm' ) {
106 42 my $file = "$dir/$prefix/$basename.$ext";
107 42 ▶ return $file if -f $file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $file 1 41 42
108 }
109 }
110 }
111 }
112  
113 1 return;
114 }
115  
116 1 1;
↑ Summary CLI/Command.pm »