File Coverage

blib/lib/App/CLI/Command/Help.pm
Criterion Covered Total %
statement 58 58 100.0
branch 14 16 87.5
condition n/a
subroutine 10 10 100.0
pod 0 2 0.0
total 82 86 95.3


line stmt bran cond sub pod time code
1             package App::CLI::Command::Help;
2 1     1   1076 use strict;
  1         2  
  1         31  
3 1     1   5 use warnings;
  1         2  
  1         27  
4 1     1   4 use base qw/App::CLI::Command/;
  1         2  
  1         123  
5 1     1   8 use File::Find qw(find);
  1         2  
  1         74  
6 1     1   8 use Locale::Maketext::Simple;
  1         2  
  1         8  
7 1     1   818 use Pod::Simple::Text;
  1         36235  
  1         574  
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             sub run {
50 7     7 0 38 my $self = shift;
51 7         15 my @topics = @_;
52              
53 7 100       26 push @topics, 'commands' unless (@topics);
54              
55 7         16 foreach my $topic (@topics) {
56 7 100       17 if ( $topic eq 'commands' ) {
    100          
    100          
57 4         9 $self->brief_usage($_) for $self->app->files;
58             }
59 3         9 elsif ( my $cmd = eval { $self->app->get_cmd($topic) } ) {
60 1         10 $cmd->usage(1);
61             }
62             elsif ( my $file = $self->_find_topic($topic) ) {
63 1 50   1   8 open my $fh, '<:encoding(UTF-8)', $file or die $!;
  1         2  
  1         7  
  1         41  
64 1         12706 require Pod::Simple::Text;
65 1         11 my $parser = Pod::Simple::Text->new;
66 1         86 my $buf;
67 1         8 $parser->output_string( \$buf );
68 1         61 $parser->parse_file($fh);
69              
70 1         1654 $buf =~ s/^NAME\s+(.*?)::Help::\S+ - (.+)\s+DESCRIPTION/ $2:/;
71 1         11 print $self->loc_text($buf);
72             }
73             else {
74 1         14 die loc( "Cannot find help topic '%1'.\n", $topic );
75             }
76             }
77 6         60 return;
78             }
79              
80             sub help_base {
81 1     1 0 3 my $self = shift;
82 1         4 return ref( $self->app ) . "::Help";
83             }
84              
85             my ( $inc, @prefix );
86              
87             sub _find_topic {
88 2     2   6 my ( $self, $topic ) = @_;
89              
90 2 100       6 if ( !$inc ) {
91 1         2 my $pkg = __PACKAGE__;
92 1         5 $pkg =~ s{::}{/};
93 1         5 $inc = substr( __FILE__, 0, -length("$pkg.pm") );
94              
95 1         6 my $base = $self->help_base;
96 1         6 @prefix = ( loc($base) );
97 1         18 $prefix[0] =~ s{::}{/}g;
98 1         3 $base =~ s{::}{/}g;
99 1 50       4 push @prefix, $base if $prefix[0] ne $base;
100             }
101              
102 2         7 foreach my $dir ( $inc, @INC ) {
103 13         30 foreach my $prefix (@prefix) {
104 13         34 foreach my $basename ( ucfirst( lc($topic) ), uc($topic) ) {
105 25         46 foreach my $ext ( 'pod', 'pm' ) {
106 50         139 my $file = "$dir/$prefix/$basename.$ext";
107 50 100       747 return $file if -f $file;
108             }
109             }
110             }
111             }
112              
113 1         11 return;
114             }
115              
116             1;