File Coverage

blib/lib/App/Cmd/Command/help.pm
Criterion Covered Total %
statement 22 25 88.0
branch 3 4 75.0
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 35 39 89.7


line stmt bran cond sub pod time code
1 13     13   8632 use strict;
  13         31  
  13         505  
2 13     13   77 use warnings;
  13         25  
  13         521  
3              
4             package App::Cmd::Command::help 0.336;
5              
6 13     13   75 use App::Cmd::Command;
  13         25  
  13         535  
7 13     13   3920 BEGIN { our @ISA = 'App::Cmd::Command'; }
8              
9             # ABSTRACT: display a command's help screen
10              
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This command will either list all of the application commands and their
14             #pod abstracts, or display the usage screen for a subcommand with its
15             #pod description.
16             #pod
17             #pod =head1 USAGE
18             #pod
19             #pod The help text is generated from three sources:
20             #pod
21             #pod =for :list
22             #pod * The C method
23             #pod * The C method
24             #pod * The C data structure
25             #pod
26             #pod The C method provides the opening usage line, following the
27             #pod specification described in L. In some cases,
28             #pod the default C in L may be sufficient and
29             #pod you will only need to override it to provide additional command line
30             #pod usage information.
31             #pod
32             #pod The C data structure is used with L
33             #pod to generate the description of the options.
34             #pod
35             #pod Subcommand classes should override the C method to provide
36             #pod additional information that is prepended before the option descriptions.
37             #pod
38             #pod For example, consider the following subcommand module:
39             #pod
40             #pod package YourApp::Command::initialize;
41             #pod
42             #pod # This is the default from App::Cmd::Command
43             #pod sub usage_desc {
44             #pod my ($self) = @_;
45             #pod my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
46             #pod return "$desc [DIRECTORY]";
47             #pod }
48             #pod
49             #pod sub description {
50             #pod return "The initialize command prepares the application...";
51             #pod }
52             #pod
53             #pod sub opt_spec {
54             #pod return (
55             #pod [ "skip-refs|R", "skip reference checks during init", ],
56             #pod [ "values|v=s@", "starting values", { default => [ 0, 1, 3 ] } ],
57             #pod );
58             #pod }
59             #pod
60             #pod ...
61             #pod
62             #pod That module would generate help output like this:
63             #pod
64             #pod $ yourapp help initialize
65             #pod yourapp initialize [-Rv] [long options...] [DIRECTORY]
66             #pod
67             #pod The initialize command prepares the application...
68             #pod
69             #pod --help This usage screen
70             #pod -R --skip-refs skip reference checks during init
71             #pod -v --values starting values
72             #pod
73             #pod =cut
74              
75 5     5 1 23 sub usage_desc { '%c help [subcommand]' }
76              
77 32     32 1 110 sub command_names { qw/help --help -h -?/ }
78              
79             sub execute {
80 4     4 1 11 my ($self, $opts, $args) = @_;
81              
82 4 50       28 if (!@$args) {
83 0         0 print $self->app->usage->text . "\n";
84              
85 0         0 print "Available commands:\n\n";
86              
87 0         0 $self->app->execute_command( $self->app->_prepare_command("commands") );
88             } else {
89 4         17 my ($cmd, $opt, $args) = $self->app->prepare_command(@$args);
90              
91 4         10 local $@;
92 4         23 my $desc = $cmd->description;
93 4 100       23 $desc = "\n$desc" if length $desc;
94              
95             my $ut = join "\n",
96 4         45 eval { $cmd->usage->leader_text },
97             $desc,
98 4         12 eval { $cmd->usage->option_text };
  4         48  
99              
100 4         228 print "$ut\n";
101             }
102             }
103              
104             1;
105              
106             __END__