CLI/Command/Help.pm

filter: Statementstmt 58.2% 109 / 187 Branchbran 47.9% 22 / 48 (1 uncoverable) Conditioncond 40.9% 9 / 22 MC/DCmcdc 25.0% 3 / 12 Subroutinesub 84.6% 22 / 26 Podpod 100% 1 / 1 totaltotal 56.4% 166 / 296 (1 uncoverable) CC 38 SCAR 30.9
CC
38
Cov
55%
_get_help_cmd
90.0
_get_help_all_commands
30.0
_execute_help
26.9
CRAP 21.9
↑ Summary CLI/Util.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Command::Help
4 #
5 # Description: Class for Term::CLI 'help' command.
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 18/Feb/2018
9 #
10 # Copyright (c) 2018-2022 Steven Bakker
11 #
12 # This module is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl itself. See "perldoc perlartistic."
14 #
15 # This software is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 #
19 #=============================================================================
20  
21 package Term::CLI::Command::Help 0.061001;
22  
23 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 ▶use List::Util 1.23 qw( first min );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Types::Standard 1.000005 qw( ArrayRef Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Term::CLI::Util qw( is_prefix_str get_options_from_array );
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 6 ▶use Pod::Text::Termcap 2.06;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use Pod::Text::Overstrike 2.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 6 ▶my $POD_PARSER_CLASS = $Pod::Text::Termcap::VERSION >= 4.11
Branch: 1 branch
BranchTrueFalseTotal
$Pod::Text::Termcap::VERSION >= 4.11 ? : 6 0 6
36 ? 'Pod::Text::Termcap'
37 : 'Pod::Text::Overstrike';
38  
39 6 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 6 extends 'Term::CLI::Command';
43  
44 1 ▶has '+name' => ( default => sub {'help'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 has '+callback' => (
47 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
48 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
49 6 my $self = shift;
50 6 $self->_execute_help(@_);
51 }
52 1 }
53 6 );
54  
55 1 ▶has '+options' => ( default => sub { [ 'pod|p', 'all|a' ] }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 has '+description' => (
58 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 loc( qq{Show help for any given command sequence (or a command\n}
60 . qq{overview if no argument is given).\n\n}
61 . qq{The C<--pod> (C<-p>) option will cause raw POD\n}
62 . qq{to be shown.\n\n}
63 . qq{The C<--all> (C<-a>) option will list help text for all commands.}
64 );
65 }
66 6 );
67  
68 1 ▶has '+summary' => ( default => sub { loc('show help') }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 has '+_arguments' => (
71 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 [ Term::CLI::Argument::String->new(
73 name => 'cmd',
74 min_occur => 0,
75 max_occur => 0,
76 )
77 ]
78 },
79 6 );
80  
81 0 ▶sub _format_pod {
Subroutine _format_pod: not called CC=1 SCAR=6.9
82 0 my ($self, $text) = @_;
83  
84 0 my $output;
85  
86 0 my $parser =
87 $POD_PARSER_CLASS->new( width => $self->term->term_width - 1 );
88  
89 0 $parser->output_string( \$output );
90 0 $parser->parse_string_document($text);
91 0 return $output;
92 }
93  
94 # ($pod, $text) = $self->_make_command_summary( %args );
95 3 ▶sub _make_command_summary {
Subroutine _make_command_summary: called CC=4 SCAR=15.1
96 3 my ( $self, %args ) = @_;
97  
98 0 my @cmd_path = map { $_->name } @{ $args{cmd_path} };
99 3 my $commands = $args{commands};
100 3 my $pod_prefix = $args{pod_prefix};
101  
102 3 my $text = '';
103 3 my $full_pod = '';
104  
105 3 my $item_length = 0;
106 3 for my $cmd_ref (@$commands) {
107 12 for my $usage ( $cmd_ref->usage_text( with_options => 'none' ) ) {
108 0 my $item_text = join( ' ', ( map {"B<$_>"} @cmd_path ), $usage );
109 12 $full_pod .= "=item $item_text\n\n";
110 12 my $l = length( $item_text =~ s/[BCEIL] < ([^>]*) >/$1/grx );
111 12 ▶ $item_length = $l if $l > $item_length;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $item_length 9 3 12
112 }
113 12 $full_pod .= $cmd_ref->summary;
114 12 $full_pod =~ s/\n*$/\n\n/sx;
115 }
116  
117 3 my $max_over_width = int( ( $self->term->term_width - 4 ) / 2 );
118 0 my $over_width = min( $item_length + 4, $max_over_width );
119  
120 0 $full_pod = $pod_prefix . "=over $over_width\n\n$full_pod";
121 0 $full_pod .= "=back\n\n";
122  
123 # Format POD to text, remove extraneous empty lines.
124 0 $text = $self->_format_pod($full_pod);
125 0 $text =~ s/\n\n+/\n/gxs;
126 0 $text =~ s/^\n+//x;
127  
128 0 return ( $full_pod, $text );
129 }
130  
131 5 ▶sub _get_help {
Subroutine _get_help: called CC=7 SCAR=23.0
132 5 my ( $self, %args ) = @_;
133  
134 # Handle "--all" in a separate routine.
135 5 ▶ if ( $args{options}->{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"options"}{"all"}) 0 5 5
136 0 return $self->_get_all_help(%args);
137 }
138  
139 # Top-level help, i.e. "help" without arguments.
140 # Produce a simple command summary.
141 5 ▶ if ( @{ $args{arguments} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args{"arguments"};} == 0) 3 2 5
142 3 my ( $pod, $text ) = $self->_make_command_summary(
143 cmd_path => [],
144 pod_prefix => "=head2 " . loc("Commands") . ":\n\n",
145 commands => [ $self->parent->commands ]
146 );
147 0 return ( %args, pod => $pod, text => $text );
148 }
149  
150 # We've been given arguments to "help". Find the
151 # appropriate command object, and work from there.
152  
153 2 my $cur_cmd_ref = $self->parent;
154 2 my @cmd_ref_path;
155  
156 2 for my $cmd_name ( @{ $args{arguments} } ) {
157 3 my $new_cmd_ref = $cur_cmd_ref->find_command($cmd_name);
158 3 ▶ if ( !$new_cmd_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_cmd_ref) 2 1 3
159 2 my @cmd_path = map { $_->name } @cmd_ref_path;
160 return (
161 2 ▶ %args,
Branch: 1 branch
BranchTrueFalseTotal
@cmd_path > 0 ? : 1 1 2
162 status => -1,
163 error => @cmd_path > 0
164 ? "@cmd_path: " . $cur_cmd_ref->error
165 : $cur_cmd_ref->error
166 );
167 }
168 1 push @cmd_ref_path, $new_cmd_ref;
169 1 $cur_cmd_ref = $new_cmd_ref;
170 }
171  
172 0 my $pod = $self->_get_help_cmd(
173 cmd_path => \@cmd_ref_path,
174 style => 'head1',
175 );
176  
177 0 $pod =~ s/\n*$/\n\n/sx;
178  
179 0 my $pod2txt = $self->_format_pod($pod);
180  
181 # Only list sub-commands if there are more than one.
182 0 ▶ if ( scalar( $cur_cmd_ref->commands ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $cur_cmd_ref->commands > 1) 0 0 0
183 0 my ( $cmd_pod, $cmd_text ) = $self->_make_command_summary(
184 cmd_path => \@cmd_ref_path,
185 pod_prefix => "=head2 " . loc("Sub-Commands") . ":\n\n",
186 commands => [ $cur_cmd_ref->commands ],
187 );
188 0 $pod .= $cmd_pod;
189 0 $pod2txt .= $cmd_text;
190 }
191  
192 # Play fast and loose with the POD formatter output.
193 # Remove leading and trailing newlines.
194 0 $pod2txt =~ s/^\n+//sx;
195 0 $pod2txt =~ s/\n+$//sx;
196  
197 0 return ( %args, pod => $pod, text => $pod2txt . "\n" );
198 }
199  
200 0 ▶sub _get_help_cmd {
Subroutine _get_help_cmd: not called CC=9 SCAR=45.0
201 0 my ( $self, %args ) = @_;
202  
203 0 ▶ my $style = $args{style} // 'item';
Condition0%0/2
$args{"style"} // "item"
l!l
00
MC/DC0%0/1
$args{"style"} // "item"
$args{"style"}
Truth table0%0/2
A
A= $args{"style"}
Aresult
11
00
204 0 my @cmd_path = @{ $args{cmd_path} };
205  
206 0 my $cmd = $cmd_path[-1];
207  
208 my $usage_prefix = join(
209 ' ',
210 map {
211 0 $_->usage_text( with_options => 'none', with_subcommands => 0 )
212 } @cmd_path[ 0 .. $#cmd_path - 1 ]
213 );
214  
215 0 ▶ $usage_prefix .= ' ' if length $usage_prefix;
Branch: 1 branch
BranchTrueFalseTotal
if length $usage_prefix 0 0 0
216  
217 0 my $pod;
218  
219 0 ▶ if ( $style =~ /head/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style =~ /head/u) 0 0 0
220 0 $pod .= "=$style " . loc("Usage") . ":\n\n";
221 }
222  
223 0 for my $usage ( $cmd->usage_text( with_options => 'both' ) ) {
224 0 ▶ $pod .= "=item " if $style eq 'item';
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "item" 0 0 0
225 0 $pod .= "$usage_prefix$usage\n\n";
226 }
227  
228 0 $pod =~ s/\n*$//sx;
229 0 $pod .= "\n\n";
230  
231 0 ▶ my $description = ( $cmd->description || $cmd->summary );
Condition0%0/3
$cmd->description || $cmd->summary
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd->description || $cmd->summary
$cmd->description $cmd->summary
Truth table0%0/3
A || B
A= $cmd->descriptionB= $cmd->summary
ABresult
1X1
011
000
232 0 ▶ if ($description) {
Branch: 1 branch
BranchTrueFalseTotal
if ($description) 0 0 0
233 0 ▶ $pod .= "=$style " . loc("Description") . ":\n\n" if $style =~ /head/;
Branch: 1 branch
BranchTrueFalseTotal
if $style =~ /head/u 0 0 0
234 0 $pod .= $description;
235 }
236  
237 0 $pod =~ s/\n*$/\n\n/sx;
238  
239 0 return $pod;
240 }
241  
242 0 ▶sub _get_help_all_commands {
Subroutine _get_help_all_commands: not called CC=5 SCAR=34.0
243 0 my ( $self, %args ) = @_;
244  
245 0 ▶ my @cmd_path = @{ $args{cmd_path} // [] };
Condition0%0/2
$args{"cmd_path"} // []
l!l
00
MC/DC0%0/1
$args{"cmd_path"} // []
$args{"cmd_path"}
Truth table0%0/2
A
A= $args{"cmd_path"}
Aresult
11
00
246 0 ▶ my $cmd = $cmd_path[-1] // $self->parent;
Condition0%0/3
$cmd_path[-1] // $self->parent
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd_path[-1] // $self->parent
$cmd_path[-1] $self->parent
Truth table0%0/3
A // B
A= $cmd_path[-1]B= $self->parent
ABresult
1X1
011
000
247  
248 0 my $pod = '';
249  
250 0 ▶ if ( $cmd->has_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->has_commands) 0 0 0
251 0 for my $command ( $cmd->commands ) {
252 0 $pod .= $self->_get_help_all_commands(
253 cmd_path => [ @cmd_path, $command ] );
254 }
255 0 return $pod;
256 }
257  
258 0 return $self->_get_help_cmd( cmd_path => \@cmd_path );
259 }
260  
261 0 ▶sub _get_all_help {
Subroutine _get_all_help: not called CC=1 SCAR=6.9
262 0 my ( $self, %args ) = @_;
263  
264 0 my ( $pod1, $txt1 ) = $self->_make_command_summary(
265 cmd_path => [],
266 pod_prefix => "=head1 " . loc("COMMAND SUMMARY") . "\n\n",
267 commands => [ $self->parent->commands ]
268 );
269  
270 0 my $pod2 =
271 "\n=head1 "
272 . loc("COMMANDS") . "\n\n"
273 . "=over\n\n"
274 . $self->_get_help_all_commands()
275 . "=back\n";
276  
277 0 my $txt2 = $self->_format_pod($pod2);
278  
279 # Play fast and loose with the POD formatter output.
280 # Remove leading and trailing newlines.
281 0 $txt2 =~ s/^\n+//sx;
282 0 $txt2 =~ s/\n+$//sx;
283 0 $txt2 .= "\n";
284  
285 0 return ( %args, pod => $pod1 . $pod2, text => "$txt1\n$txt2" );
286 }
287  
288 7 ▶sub complete {
Subroutine complete: called CC=11 SCAR=24.0
Pod documented
289 7 my ( $self, $text, $state ) = @_;
290  
291 7 ▶ my $processed = $state->{processed} //= [];
Condition50%1/2
$state->{"processed"} //= []
l!l
70
MC/DC0%0/1
$state->{"processed"} //= []
$state->{"processed"}
Truth table50%1/2
A
A= $state->{"processed"}
Aresult
11
00
292 7 ▶ my $unprocessed = $state->{unprocessed} //= [];
Condition50%1/2
$state->{"unprocessed"} //= []
l!l
70
MC/DC0%0/1
$state->{"unprocessed"} //= []
$state->{"unprocessed"}
Truth table50%1/2
A
A= $state->{"unprocessed"}
Aresult
11
00
293 7 ▶ my $parsed_options = $state->{options} //= {};
Condition50%1/2
$state->{"options"} //= {}
l!l
70
MC/DC0%0/1
$state->{"options"} //= {}
$state->{"options"}
Truth table50%1/2
A
A= $state->{"options"}
Aresult
11
00
294  
295 # uncoverable branch false
296 7 ▶ if ( $self->has_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_options) 7 0 7
297  
298 7 Getopt::Long::Configure(qw(bundling require_order pass_through));
299  
300 7 my %opt_result = get_options_from_array(
301 args => $unprocessed,
302 spec => $self->options,
303 result => $parsed_options,
304 pass_through => 1,
305 );
306  
307 7 my $double_dash = $opt_result{double_dash};
308  
309 7 ▶ if ( !$double_dash && @{$unprocessed} == 0 && $text =~ /^-/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux) 1 6 7
Condition100%6/6
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
!ll&&!rl&&r
421
not $double_dash and @{$unprocessed;} == 0
!ll&&!rl&&r
133
MC/DC100%3/3
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
not $double_dash @{$unprocessed;} == 0 $text =~ /^-/ux
Truth table100%4/4
not(A) and B and C
A= not $double_dashB= @{$unprocessed;} == 0C= $text =~ /^-/ux
ABCresult
0XX0
10X0
1100
1111
310  
311 # We have to complete a command-line option.
312 1 return grep { is_prefix_str( $text, $_ ) } $self->option_names;
313 }
314 }
315  
316 6 my $cur_cmd_ref = $self->parent;
317 6 ▶ while (@$unprocessed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$unprocessed) 4 5 9
318 4 my $new_cmd_ref = $cur_cmd_ref->find_command( $unprocessed->[0] );
319  
320 4 ▶ return () if !$new_cmd_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_cmd_ref 1 3 4
321  
322 3 push @{$processed}, {
323 element => $new_cmd_ref,
324 3 value => shift @{$unprocessed},
325 };
326 3 $cur_cmd_ref = $new_cmd_ref;
327 }
328  
329 5 ▶ return grep { is_prefix_str( $text, $_ ) } $cur_cmd_ref->command_names
Branch: 1 branch
BranchTrueFalseTotal
if $cur_cmd_ref->has_commands 4 1 5
330 if $cur_cmd_ref->has_commands;
331  
332 1 return ();
333 }
334  
335 # %args = HELP->_execute_help(%args);
336 #
337 # Callback for the builtin "help" command.
338 #
339 6 ▶sub _execute_help {
Subroutine _execute_help: called CC=7 SCAR=32.9
340 6 my ( $self, %args ) = @_;
341  
342 6 ▶ return %args if $args{status} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"status"} < 0 1 5 6
343  
344 5 %args = $self->_get_help(%args);
345  
346 2 ▶ return %args if $args{status} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"status"} < 0 2 0 2
347  
348 0 ▶ if ( $args{options}->{pod} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"options"}{"pod"}) 0 0 0
349 0 print "\n$args{pod}";
350 0 return %args;
351 }
352  
353 0 my $cli = $self->root_node;
354 0 ▶ if ( $cli->can('write_pager') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli->can("write_pager")) 0 0 0
355 0 return $cli->write_pager(%args);
356 }
357  
358 0 ▶ my $ok = print $args{text}, $args{text} =~ /\n$/xms ? '' : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$args{'text'} =~ /\n$/msux ? : 0 0 0
359  
360 0 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
361 0 $args{status} = -1;
362 0 $args{error} = $!;
363 }
364 0 return %args;
365 }
366  
367 6 1;
368  
369 __END__
↑ Summary CLI/Util.pm »