line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Rad::Help; |
2
|
1
|
|
|
1
|
|
4234
|
use Attribute::Handlers; |
|
1
|
|
|
|
|
5365
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
56
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
894
|
|
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($c) . "\n\n" . helpstr($c); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub usage { |
22
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
23
|
0
|
|
|
|
|
|
my $cmd; |
24
|
0
|
0
|
|
|
|
|
$cmd = $c->argv->[0] if $c->is_command($c->argv->[0]); |
25
|
0
|
0
|
|
|
|
|
return "Usage: $0 command [arguments]" unless $cmd; |
26
|
0
|
|
|
|
|
|
my %options = %{ $c->{'_commands'}->{$cmd}->{opts} }; |
|
0
|
|
|
|
|
|
|
27
|
0
|
|
|
|
|
|
my @opts; |
28
|
|
|
|
|
|
|
@opts = map {"-" . ("-" x (length != 1)) . "$_=" |
29
|
|
|
|
|
|
|
. (join " ", ((uc($options{$_}->{type})) |
30
|
0
|
0
|
|
|
|
|
x (exists $options{$_}->{arguments} ? $options{$_}->{arguments} : 1))) } |
31
|
0
|
|
|
|
|
|
sort grep {$options{$_}->{required}} keys %options; |
|
0
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
push @opts, map {"[-" . ("-" x (length != 1)) . "$_=" |
33
|
|
|
|
|
|
|
. join(" ", (uc($options{$_}->{type})) |
34
|
0
|
0
|
|
|
|
|
x (exists $options{$_}->{arguments} ? $options{$_}->{arguments} : 1)) . "]"} |
35
|
0
|
|
|
|
|
|
sort grep {not $options{$_}->{required}} keys %options; |
|
0
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#print $c->{'_commands'}->{$cmd}, $/; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
|
|
|
return "Usage: $0 $cmd @opts"; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub helpstr { |
42
|
0
|
|
|
0
|
1
|
|
my $c = shift; |
43
|
0
|
|
|
|
|
|
my $cmd; |
44
|
0
|
0
|
|
|
|
|
$cmd = $c->argv->[0] if $c->is_command($c->argv->[0]); |
45
|
0
|
0
|
|
|
|
|
unless($cmd) { |
46
|
0
|
|
|
|
|
|
my $string = "Available Commands:\n"; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# get length of largest command name |
50
|
0
|
|
|
|
|
|
my $len = 0; |
51
|
0
|
|
|
|
|
|
foreach ( sort $c->commands() ) { |
52
|
0
|
0
|
|
|
|
|
$len = length($_) if (length($_) > $len); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# format help string |
56
|
0
|
|
|
|
|
|
foreach ( sort $c->commands() ) { |
57
|
|
|
|
|
|
|
$string .= sprintf " %-*s\t%s\n", $len, $_, |
58
|
|
|
|
|
|
|
defined ($c->{'_commands'}->{$_}->help) |
59
|
0
|
0
|
|
|
|
|
? $c->{'_commands'}->{$_}->help |
60
|
|
|
|
|
|
|
: '' |
61
|
|
|
|
|
|
|
; |
62
|
|
|
|
|
|
|
; |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
return $string; |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
|
my %options = %{ $c->{'_commands'}->{$cmd}->{opts} }; |
|
0
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
my $string = "Available Options:\n"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# get length of largest command name |
71
|
0
|
|
|
|
|
|
my $len = 0; |
72
|
0
|
|
|
|
|
|
foreach ( sort keys %options ) { |
73
|
0
|
0
|
|
|
|
|
$len = length($_) if (length($_) > $len); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# format help string |
77
|
0
|
|
|
|
|
|
foreach ( sort keys %options ) { |
78
|
|
|
|
|
|
|
$string .= sprintf " %-*s\t%s\n", $len, $_, |
79
|
|
|
|
|
|
|
defined ($options{$_}->{help}) |
80
|
|
|
|
|
|
|
? $options{$_}->{help} |
81
|
0
|
0
|
|
|
|
|
: '' |
82
|
|
|
|
|
|
|
; |
83
|
|
|
|
|
|
|
; |
84
|
|
|
|
|
|
|
} |
85
|
0
|
|
|
|
|
|
return $string; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
{ |
91
|
|
|
|
|
|
|
my %help_attr = (); |
92
|
|
|
|
|
|
|
sub UNIVERSAL::Help :ATTR(CODE) { |
93
|
0
|
|
|
0
|
0
|
|
my ($package, $symbol, undef, undef, $data) = (@_); |
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if ($package eq 'main') { |
96
|
|
|
|
|
|
|
# If data is a single word, it is received as an array ref. Don't ask. |
97
|
0
|
0
|
|
|
|
|
$data = join(' ', @$data) if ref($data) eq 'ARRAY'; |
98
|
0
|
|
|
|
|
|
$help_attr{ *{$symbol}{NAME} } = $data; |
|
0
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
1
|
|
|
1
|
|
8
|
} |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub get_help_attr_for { |
103
|
0
|
|
|
0
|
1
|
|
my ($self, $cmd) = (@_); |
104
|
0
|
|
|
|
|
|
return $help_attr{$cmd}; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
42; |
108
|
|
|
|
|
|
|
__END__ |