File Coverage

blib/lib/App/Cmd/Command/commands.pm
Criterion Covered Total %
statement 39 43 90.7
branch 8 12 66.6
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 57 65 87.6


line stmt bran cond sub pod time code
1 9     9   5949 use strict;
  9         20  
  9         275  
2 9     9   48 use warnings;
  9         20  
  9         359  
3              
4             package App::Cmd::Command::commands 0.335;
5              
6 9     9   52 use App::Cmd::Command;
  9         22  
  9         379  
7 9     9   4821 BEGIN { our @ISA = 'App::Cmd::Command' };
8              
9             # ABSTRACT: list the application's commands
10              
11             #pod =head1 DESCRIPTION
12             #pod
13             #pod This command will list all of the application commands available and their
14             #pod abstracts.
15             #pod
16             #pod =method execute
17             #pod
18             #pod This is the command's primary method and raison d'etre. It prints the
19             #pod application's usage text (if any) followed by a sorted listing of the
20             #pod application's commands and their abstracts.
21             #pod
22             #pod The commands are printed in sorted groups (created by C); each
23             #pod group is set off by blank lines.
24             #pod
25             #pod =cut
26              
27             sub execute {
28 4     4 1 13 my ($self, $opt, $args) = @_;
29              
30 4 100       14 my $target = $opt->stderr ? *STDERR : *STDOUT;
31            
32 4         51 my @cmd_groups = $self->app->command_groups;
33 4 0       12 my @primary_commands = map { @$_ if ref $_ } @cmd_groups;
  0         0  
34              
35 4 50       15 if (!@cmd_groups) {
36             @primary_commands =
37 32 100       102 grep { $_ ne 'version' or $self->app->{show_version} }
38 4         27 map { ($_->command_names)[0] }
  32         152  
39             $self->app->command_plugins;
40              
41 4         26 @cmd_groups = $self->sort_commands(@primary_commands);
42             }
43              
44 4         11 my $fmt_width = 0;
45 4 100       10 for (@primary_commands) { $fmt_width = length if length > $fmt_width }
  29         60  
46 4         6 $fmt_width += 2; # pretty
47              
48 4         9 foreach my $cmd_set (@cmd_groups) {
49 8 50       68 if (!ref $cmd_set) {
50 0         0 print { $target } "$cmd_set:\n";
  0         0  
51 0         0 next;
52             }
53 8         20 for my $command (@$cmd_set) {
54 29         578 my $abstract = $self->app->plugin_for($command)->abstract;
55 29         85 printf { $target } "%${fmt_width}s: %s\n", $command, $abstract;
  29         183  
56             }
57 8         185 print { $target } "\n";
  8         28  
58             }
59             }
60              
61             #pod =method C
62             #pod
63             #pod my @sorted = $cmd->sort_commands(@unsorted);
64             #pod
65             #pod This method orders the list of commands into groups which it returns as a list of
66             #pod arrayrefs, and optional group header strings.
67             #pod
68             #pod By default, the first group is for the "help" and "commands" commands, and all
69             #pod other commands are in the second group.
70             #pod
71             #pod This method can be overridden by implementing the C method in
72             #pod your application base clase.
73             #pod
74             #pod =cut
75              
76             sub sort_commands {
77 4     4 1 16 my ($self, @commands) = @_;
78              
79 4         20 my $float = qr/^(?:help|commands)$/;
80              
81 4         11 my @head = sort grep { $_ =~ $float } @commands;
  29         113  
82 4         10 my @tail = sort grep { $_ !~ $float } @commands;
  29         101  
83              
84 4         18 return (\@head, \@tail);
85             }
86              
87             sub opt_spec {
88             return (
89 6     6 1 36 [ 'stderr' => 'hidden' ],
90             );
91             }
92              
93             1;
94              
95             __END__