| 1 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package App::Cmd::Command::help 0.340; | ||||||||||
| 5 | |||||||||||
| 6 | 13 | ▶ | use App::Cmd::Command; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 13 | ▶ | BEGIN { our @ISA = 'App::Cmd::Command'; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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<usage_desc> method | ||||||||||
| 23 | #pod * The C<description> method | ||||||||||
| 24 | #pod * The C<opt_spec> data structure | ||||||||||
| 25 | #pod | ||||||||||
| 26 | #pod The C<usage_desc> method provides the opening usage line, following the | ||||||||||
| 27 | #pod specification described in L<Getopt::Long::Descriptive>. In some cases, | ||||||||||
| 28 | #pod the default C<usage_desc> in L<App::Cmd::Command> 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<opt_spec> data structure is used with L<Getopt::Long::Descriptive> | ||||||||||
| 33 | #pod to generate the description of the options. | ||||||||||
| 34 | #pod | ||||||||||
| 35 | #pod Subcommand classes should override the C<discription> 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 | ▶ | sub usage_desc { '%c help [subcommand]' } | ||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 76 | |||||||||||
| 77 | 34 | ▶ | sub command_names { qw/help --help -h -?/ } | ||||||||
|
Subroutine
command_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 78 | |||||||||||
| 79 | 4 | ▶ | sub execute { | ||||||||
|
Subroutine
execute: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||
| 80 | 4 | my ($self, $opts, $args) = @_; | |||||||||
| 81 | |||||||||||
| 82 | 4 | ▶ | if (!@$args) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | print $self->app->usage->text . "\n"; | |||||||||
| 84 | |||||||||||
| 85 | 0 | print "Available commands:\n\n"; | |||||||||
| 86 | |||||||||||
| 87 | 0 | $self->app->execute_command( $self->app->_prepare_command("commands") ); | |||||||||
| 88 | } else { | ||||||||||
| 89 | 4 | my ($cmd, $opt, $args) = $self->app->prepare_command(@$args); | |||||||||
| 90 | |||||||||||
| 91 | 4 | local $@; | |||||||||
| 92 | 4 | my $desc = $cmd->description; | |||||||||
| 93 | 4 | ▶ | $desc = "\n$desc" if length $desc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | |||||||||||
| 95 | my $ut = join "\n", | ||||||||||
| 96 | 4 | eval { $cmd->usage->leader_text }, | |||||||||
| 97 | $desc, | ||||||||||
| 98 | 4 | eval { $cmd->usage->option_text }; | |||||||||
| 99 | |||||||||||
| 100 | 4 | print "$ut\n"; | |||||||||
| 101 | } | ||||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | 13 | 1; | |||||||||
| 105 | |||||||||||
| 106 | __END__ | ||||||||||