File Coverage

blib/lib/App/Rad/Help.pm
Criterion Covered Total %
statement 12 32 37.5
branch 0 8 0.0
condition n/a
subroutine 4 10 40.0
pod 5 6 83.3
total 21 56 37.5


line stmt bran cond sub pod time code
1             package App::Rad::Help;
2 1     1   2600 use Attribute::Handlers;
  1         8789  
  1         9  
3 1     1   87 use strict;
  1         2  
  1         31  
4 1     1   6 use warnings;
  1         2  
  1         366  
5              
6             our $VERSION = '0.03';
7              
8             sub load {
9 0     0 1   my ($self, $c) = @_;
10 0           $c->register('help', \&help, 'show syntax and available commands');
11             }
12              
13             # shows specific help commands
14             # TODO: context specific help,
15             # such as "myapp.pl help command"
16             sub help {
17 0     0 1   my $c = shift;
18 0           return usage() . "\n\n" . helpstr($c);
19             }
20              
21             sub usage {
22 0     0 1   return "Usage: $0 command [arguments]";
23             }
24              
25             sub helpstr {
26 0     0 1   my $c = shift;
27            
28 0           my $string = "Available Commands:\n";
29              
30             # get length of largest command name
31 0           my $len = 0;
32 0           foreach ( sort $c->commands() ) {
33 0 0         $len = length($_) if (length($_) > $len);
34             }
35              
36             # format help string
37 0           foreach ( sort $c->commands() ) {
38 0 0         $string .= sprintf " %-*s\t%s\n", $len, $_,
39             defined ($c->{'_commands'}->{$_}->help)
40             ? $c->{'_commands'}->{$_}->help
41             : ''
42             ;
43             ;
44             }
45 0           return $string;
46             }
47            
48              
49             {
50             my %help_attr = ();
51             sub UNIVERSAL::Help :ATTR(CODE) {
52 0     0 0   my ($package, $symbol, undef, undef, $data) = (@_);
53              
54 0 0         if ($package eq 'main') {
55             # If data is a single word, it is received as an array ref. Don't ask.
56 0 0         $data = join(' ', @$data) if ref($data) eq 'ARRAY';
57 0           $help_attr{ *{$symbol}{NAME} } = $data;
  0            
58             }
59 1     1   7 }
  1         1  
  1         17  
60              
61             sub get_help_attr_for {
62 0     0 1   my ($self, $cmd) = (@_);
63 0           return $help_attr{$cmd};
64             }
65             }
66             42;
67             __END__