CmdDispatch/Help.pm

filter: Statementstmt 100% 119 / 119 Branchbran 93.5% 58 / 62 Conditioncond 78.5% 11 / 14 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 26 / 26 Podpod 100% 5 / 5 totaltotal 95.3% 224 / 235 CC 42 SCAR 14.9
CC
42
Cov
96%
help
9.0
hint
8.0
new
6.0
CRAP 4.5
« CmdDispatch.pm ↑ Summary CmdDispatch/Table.pm »
1 package App::CmdDispatch::Help;
2  
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 our $VERSION = '0.44';
7  
8 sub new
9 39 ▶{
Subroutine new: called CC=6 SCAR=17.9
Pod documented
10 39 my ( $class, $owner, $commands, $config ) = @_;
11 39 ▶ $config ||= {};
Condition100%2/2
$config ||= {}
l!l
2910
MC/DC100%1/1
$config ||= {}
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
12 39 ▶ die "Command definition is not a hashref.\n" unless ref $commands eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $commands eq ref {} 2 37 39
13 37 ▶ die "No commands specified.\n" unless keys %{$commands};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$commands;} 1 36 37
14 36 ▶ die "Config parameter is not a hashref.\n" unless ref $config eq ref {};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq ref {} 1 35 36
15 35 ▶ die "Invalid owner object.\n" unless eval { $owner->isa( 'App::CmdDispatch' ); };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $owner->isa("App::CmdDispatch") } } 1 34 35
16 34 _extend_table_with_help( $commands );
17 34 my %conf = (
18 indent_hint => ' ',
19 indent_help => ' ',
20 _extract_config_parm( $config, 'indent_hint' ),
21 _extract_config_parm( $config, 'pre_hint' ),
22 _extract_config_parm( $config, 'post_hint' ),
23 _extract_config_parm( $config, 'indent_help' ),
24 _extract_config_parm( $config, 'pre_help' ),
25 _extract_config_parm( $config, 'post_help' ),
26 alias_len => 1,
27 );
28 34 return bless { owner => $owner, %conf }, $class;
29 }
30  
31 sub _extract_config_parm
32 204 ▶{
Subroutine _extract_config_parm: called CC=2 SCAR=6.9
33 204 my ( $config, $parm ) = @_;
34 204 ▶ return unless defined $config->{"help:$parm"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$config{"help:$parm"} 198 6 204
35 6 return ( $parm => $config->{"help:$parm"} );
36 }
37  
38 sub _extend_table_with_help
39 34 ▶{
Subroutine _extend_table_with_help: called CC=1 SCAR=0.0
40 34 my ( $commands ) = @_;
41 $commands->{help} = {
42 34 code => \&_dispatch_help,
43 clue => "help [command|alias]",
44 abstract => 'Display complete help',
45 help => "Display help about commands and/or aliases. Limit display with the\nargument.",
46 };
47 $commands->{hint} = {
48 34 code => \&_dispatch_hint,
49 clue => "hint [command|alias]",
50 abstract => 'Display command hints',
51 help => 'A list of commands and/or aliases. Limit display with the argument.',
52 };
53 34 return;
54 }
55  
56 sub _dispatch_help
57 22 ▶{
Subroutine _dispatch_help: called CC=1 SCAR=0.0
58 22 my $owner = shift;
59 22 return $owner->{_helper}->help( @_ );
60 }
61  
62 sub _dispatch_hint
63 24 ▶{
Subroutine _dispatch_hint: called CC=1 SCAR=0.0
64 24 my $owner = shift;
65 24 return $owner->{_helper}->hint( @_ );
66 }
67  
68 sub _hint_string
69 80 ▶{
Subroutine _hint_string: called CC=4 SCAR=13.9
70 80 my ( $self, $cmd, $maxlen ) = @_;
71 80 my $desc = $self->_table->get_command( $cmd );
72 80 ▶ return '' unless $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 80 80
73 80 ▶ my $indent = ( $maxlen ? ' ' x ( 3 + $maxlen - length $desc->{clue} ) : ' ' );
Branch: 1 branch
BranchTrueFalseTotal
$maxlen ? : 76 4 80
74 80 ▶ return $desc->{clue} . ( $desc->{abstract} ? $indent . $desc->{abstract} : '' );
Branch: 1 branch
BranchTrueFalseTotal
$desc->{'abstract'} ? : 57 23 80
75 }
76  
77 sub _clue_string
78 55 ▶{
Subroutine _clue_string: called CC=2 SCAR=7.0
79 55 my ( $self, $cmd ) = @_;
80 55 my $desc = $self->_table->get_command( $cmd );
81 55 ▶ return '' unless $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 0 55 55
82 55 return $desc->{clue};
83 }
84  
85 sub _alias_hint
86 16 ▶{
Subroutine _alias_hint: called CC=1 SCAR=0.0
87 16 my ($self, $alias) = @_;
88 16 return sprintf "%-$self->{alias_len}s : %s", $alias, $self->_table->get_alias( $alias );
89 }
90  
91 sub _help_string
92 55 ▶{
Subroutine _help_string: called CC=2 SCAR=6.9
93 55 my ( $self, $cmd ) = @_;
94 55 my $desc = $self->_table->get_command( $cmd );
95  
96 55 ▶ return '' unless defined $desc->{help};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc->{"help"} 1 54 55
97 54 return join( "\n", map { $self->{indent_help} . $_ } split /\n/, $desc->{help} );
98 }
99  
100 sub _list_command
101 39 ▶{
Subroutine _list_command: called CC=4 SCAR=14.6
102 39 my ( $self, $code ) = @_;
103 39 $self->_print( "\nCommands:\n" );
104 39 foreach my $c ( $self->{owner}->command_list() )
105 {
106 # The following should not be possible. But I'll keep this until
107 # I'm absolutely certain.
108 125 ▶ next if $c eq '' or !$self->_table->get_command( $c );
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "" or not $self->_table->get_command($c) 0 125 125
Condition33%1/3
$c eq "" or not $self->_table->get_command($c)
l!l&&r!l&&!r
00125
MC/DC0%0/2
$c eq "" or not $self->_table->get_command($c)
$c eq "" not $self->_table->get_command($c)
Truth table33%1/3
A or not(B)
A= $c eq ""B= not $self->_table->get_command($c)
ABresult
1X1
011
000
109 125 $self->_print( $code->( $c ) );
110 }
111 39 return;
112 }
113  
114 sub _find_longest_alias
115 11 ▶{
Subroutine _find_longest_alias: called CC=3 SCAR=11.0
116 11 my ( $self ) = @_;
117 11 my $len;
118 11 foreach my $c ( $self->{owner}->alias_list() )
119 {
120 14 $len = length $c;
121 14 ▶ $self->{alias_len} = $len if $len > $self->{alias_len};
Branch: 1 branch
BranchTrueFalseTotal
if $len > $self->{"alias_len"} 5 9 14
122 }
123 11 return;
124 }
125  
126 sub _list_aliases
127 39 ▶{
Subroutine _list_aliases: called CC=3 SCAR=11.0
128 39 my ( $self ) = @_;
129 39 ▶ return unless $self->_table->has_aliases;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_table->has_aliases 28 11 39
130  
131 11 $self->_find_longest_alias();
132 11 $self->_print( "\nAliases:\n" );
133 11 foreach my $c ( $self->{owner}->alias_list() )
134 {
135 14 $self->_print( $self->{indent_hint} . $self->_alias_hint( $c ) . "\n" );
136 }
137 11 return;
138 }
139  
140 61 ▶sub _is_missing { return !defined $_[0] || $_[0] eq ''; }
Subroutine _is_missing: called CC=2 SCAR=6.9
Condition100%3/3
!defined($_[0]) || $_[0] eq ''
l!l&&r!l&&!r
29428
MC/DC100%2/2
!defined($_[0]) || $_[0] eq ''
!defined($_[0]) $_[0] eq ''
Truth table100%3/3
not(A) || B
A= !defined($_[0])B= $_[0] eq ''
ABresult
1X1
011
000
141  
142 sub _get_abstract_offset
143 23 ▶{
Subroutine _get_abstract_offset: called CC=3 SCAR=11.0
144 23 my ( $self ) = @_;
145  
146 23 my $maxlen = 0;
147 23 my $len;
148 23 foreach my $cmd ( $self->_table->command_list() )
149 {
150 76 $len = length $self->_table->get_command( $cmd )->{clue};
151 76 ▶ $maxlen = $len if $len > $maxlen;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $maxlen 24 52 76
152 }
153 23 return $maxlen;
154 }
155  
156 sub hint
157 33 ▶{
Subroutine hint: called CC=8 SCAR=20.8
Pod documented
158 33 my ( $self, $arg ) = @_;
159  
160 33 ▶ if( _is_missing( $arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_missing($arg)) 20 13 33
161 {
162 20 my $maxlen = $self->_get_abstract_offset();
163 20 ▶ $self->_print( "\n$self->{pre_hint}\n" ) if $self->{pre_hint};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_hint"} 1 19 20
164 $self->_list_command(
165 20 ▶ sub { $self->{indent_hint}, $self->_hint_string( $_[0], $maxlen ), "\n"; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
166 20 $self->_list_aliases();
167 20 ▶ $self->_print( "\n$self->{post_hint}\n" ) if $self->{post_hint};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"post_hint"} 1 19 20
168 20 return;
169 }
170  
171 13 ▶ if( $self->_table->get_command( $arg ) )
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_table->get_command($arg)) { } 4 9 13
elsif ($self->_table->get_alias($arg)) { } 1 8 9
elsif ($arg eq 'commands') { } 3 5 8
elsif ($arg eq 'aliases') { } 3 2 5
172 {
173 4 $self->_print( "\n", $self->_hint_string( $arg ), "\n" );
174 }
175 elsif( $self->_table->get_alias( $arg ) )
176 {
177 1 $self->_print( "\n", $self->_alias_hint( $arg ), "\n" );
178 }
179 elsif( $arg eq 'commands' )
180 {
181 3 my $maxlen = $self->_get_abstract_offset();
182 3 ▶ $self->_list_command( sub { $self->{indent_hint}, $self->_hint_string( $_[0], $maxlen ), "\n"; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
183 }
184 elsif( $arg eq 'aliases' )
185 {
186 3 $self->_list_aliases();
187 }
188 else
189 {
190 2 $self->_print( "Unrecognized command '$arg'\n" );
191 }
192  
193 13 return;
194 }
195  
196 sub help
197 28 ▶{
Subroutine help: called CC=9 SCAR=22.0
Pod documented
198 28 my ( $self, $arg ) = @_;
199  
200 28 ▶ if( _is_missing( $arg ) )
Branch: 1 branch
BranchTrueFalseTotal
if (_is_missing($arg)) 13 15 28
201 {
202 13 ▶ $self->_print( "\n$self->{pre_help}\n" ) if $self->{pre_help};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_help"} 1 12 13
203 $self->_list_command(
204 37 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
205 37 $self->{indent_hint}, $self->_clue_string( $_[0] ), "\n",
206 $self->_help_string( $_[0] ), "\n";
207 }
208 13 );
209 13 $self->_list_aliases();
210 13 ▶ $self->_print( "\n$self->{post_help}\n" ) if $self->{post_help};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"post_help"} 1 12 13
211 13 return;
212 }
213  
214 15 ▶ if( $self->_table->get_command( $arg ) )
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_table->get_command($arg)) { } 6 9 15
elsif ($self->_table->get_alias($arg)) { } 1 8 9
elsif ($arg eq 'commands') { } 3 5 8
elsif ($arg eq 'aliases') { } 3 2 5
215 {
216 $self->_print( "\n", $self->_clue_string( $arg ),
217 6 ▶ "\n", ( $self->_help_string( $arg ) || $self->{indent_help} . "No help for '$arg'" ),
Condition66%2/3
$self->_help_string($arg) || $self->{'indent_help'} . "No help for '${arg}'"
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->_help_string($arg) || $self->{'indent_help'} . "No help for '${arg}'"
$self->_help_string($arg) $self->{'indent_help'} . "No help for '${arg}'"
Truth table66%2/3
A || B
A= $self->_help_string($arg)B= $self->{'indent_help'} . "No help for '${arg}'"
ABresult
1X1
011
000
218 "\n" );
219 }
220 elsif( $self->_table->get_alias( $arg ) )
221 {
222 1 $self->_print( "\n", $self->_alias_hint( $arg ), "\n" );
223 }
224 elsif( $arg eq 'commands' )
225 {
226 $self->_list_command(
227 12 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 12 $self->{indent_hint}, $self->_clue_string( $_[0] ), "\n",
229 $self->_help_string( $_[0] ), "\n";
230 }
231 3 );
232 }
233 elsif( $arg eq 'aliases' )
234 {
235 3 $self->_list_aliases();
236 }
237 else
238 {
239 2 $self->_print( "Unrecognized command '$arg'\n" );
240 }
241  
242 15 return;
243 }
244  
245 sub normalize_command_help
246 22 ▶{
Subroutine normalize_command_help: called CC=5 SCAR=16.1
Pod documented
247 22 my ( $self, $table ) = @_;
248 22 foreach my $cmd ( $table->command_list )
249 {
250 87 my $desc = $table->get_command( $cmd );
251 87 ▶ $desc->{clue} = $cmd unless defined $desc->{clue};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc->{"clue"} 16 71 87
252 87 ▶ $desc->{hint} = '' unless defined $desc->{hint};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc->{"hint"} 87 0 87
253 87 ▶ $desc->{help} = '' unless defined $desc->{help};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc->{"help"} 18 69 87
254 }
255 22 return;
256 }
257  
258 sub sort_commands
259 31 ▶{
Subroutine sort_commands: called CC=2 SCAR=6.9
Pod documented
260 31 ▶ return ( sort grep { $_ ne 'hint' && $_ ne 'help' } @_ ), 'hint', 'help';
Condition100%3/3
$_ ne 'hint' && $_ ne 'help'
!ll&&!rl&&r
313161
MC/DC100%2/2
$_ ne 'hint' && $_ ne 'help'
$_ ne 'hint' $_ ne 'help'
Truth table100%3/3
A && B
A= $_ ne 'hint'B= $_ ne 'help'
ABresult
0X0
100
111
261 }
262  
263 sub _print
264 209 ▶{
Subroutine _print: called CC=1 SCAR=0.0
265 209 my ( $self ) = shift;
266 209 return $self->{owner}->_print( @_ );
267 }
268  
269 515 ▶sub _table { return $_[0]->{owner}->{table}; }
Subroutine _table: called CC=1 SCAR=0.0
270  
271 9 1;
272 __END__
« CmdDispatch.pm ↑ Summary CmdDispatch/Table.pm »