CLI/Command/Help.pm untested

filter: Statementstmt 0.0% 0 / 197 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 273 CC 22 SCAR 38.1
CC
22
Cov
0%
_execute_help
90.0
_get_help
72.0
complete_line
42.0
CRAP 45.2
1 #=============================================================================
2 #
3 # Module: Term::CLI::Command::Help
4 #
5 # Description: Class for Term::CLI 'help' command.
6 #
7 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
8 # Created: 18/02/18
9 #
10 # Copyright (c) 2018 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 use 5.014_001;
22  
23 package Term::CLI::Command::Help 0.051003 {
24  
25 use Modern::Perl 1.20140107;
26 use Pod::Text::Termcap 2.08;
27 use List::Util 1.38 qw( first min );
28 use File::Which 1.09;
29 use Types::Standard 1.000005 qw( ArrayRef Str );
30 use Getopt::Long 2.42 qw( GetOptionsFromArray );
31 use Term::CLI::L10N;
32  
33 my @PAGERS = (
34 [qw(
35 less --no-lessopen --no-init
36 --dumb --quit-at-eof
37 --quit-if-one-screen
38 )],
39 ['more'], ['pg'],
40 );
41  
42 my @PAGER;
43  
44 if (my $pager = first { defined which($_->[0]) } @PAGERS) {
45 @PAGER = @$pager;
46 }
47  
48 use Moo 1.000001;
49 use namespace::clean 0.25;
50  
51 extends 'Term::CLI::Command';
52  
53 has 'pager' => (
54 is => 'rw',
55 isa => ArrayRef[Str],
56 default => sub { [@PAGER] },
57 );
58  
59 has '+name' => (
60 default => sub { 'help' }
61 );
62  
63 has '+callback' => (
64 default => sub {
65 sub {
66 my $self = shift;
67 $self->_execute_help(@_);
68 }
69 }
70 );
71  
72 has '+options' => (
73 default => sub { [ 'pod|p' ] },
74 );
75  
76 has '+description' => (
77 default => sub {
78 loc(qq{Show help for any given command sequence.\n}
79 .qq{The C<--pod> option (or C<-p>) will cause raw POD\n}
80 .qq{to be shown.});
81 }
82 );
83  
84 has '+summary' => (
85 default => sub { loc('show help') },
86 );
87  
88 has '+_arguments' => (
89 default => sub { [
90 Term::CLI::Argument::String->new(
91 name => 'cmd',
92 min_occur => 0,
93 max_occur => 0,
94 )
95 ] },
96 );
97  
98  
99 sub _format_pod {
100 my $self = shift;
101 my $text = shift;
102  
103 my $output;
104 my $parser = Pod::Text::Termcap->new( width => $self->term->term_width - 1 );
105 $parser->output_string(\$output);
106 $parser->parse_string_document($text);
107 return $output;
108 }
109  
110  
111 # ($pod, $text) = $self->_make_command_summary( %args );
112 sub _make_command_summary {
113 my ($self, %args) = @_;
114
115 my $cmd_path = $args{cmd_path};
116 my $commands = $args{commands};
117 my $pod_prefix = $args{pod_prefix};
118  
119 my $text = '';
120 my $full_pod = '';
121  
122 my $item_length = 0;
123 for my $cmd_ref (@$commands) {
124 for my $usage ($cmd_ref->usage_text(with_options => 'none')) {
125 my $item_text = join(' ',
126 (map { "B<$_>" } @$cmd_path),
127 $usage
128 );
129 $full_pod .= "=item $item_text\n\n";
130 my $l = length($item_text =~ s/[BCEIL]<([^>]*)>/$1/gr);
131 $item_length = $l if $l > $item_length;
132 }
133 $full_pod .= $cmd_ref->summary;
134 $full_pod =~ s/\n*$/\n\n/s;
135 }
136  
137 my $max_over_width = int(($self->term->term_width - 4) / 2);
138 my $over_width = min($item_length+4, $max_over_width);
139  
140 $full_pod = $pod_prefix."=over $over_width\n\n$full_pod";
141 $full_pod .= "=back\n\n";
142 $text = $self->_format_pod($full_pod);
143 $text =~ s/\n\n+/\n/gs;
144 $text =~ s/^\n+//;
145  
146 return ($full_pod, $text);
147 }
148  
149 sub _get_help {
150 my ($self, %args) = @_;
151  
152 my $text = '';
153  
154 # Top-level help, i.e. "help" without arguments.
155 # Produce a simple command summary.
156 if (@{$args{arguments}} == 0) {
157 my ($pod, $text)
158 = $self->_make_command_summary(
159 cmd_path => [],
160 pod_prefix => "=head2 ".loc("Commands").":\n\n",
161 commands => [$self->root_node->commands]
162 );
163 return (%args, pod => $pod, text => $text);
164 }
165  
166 my @cmd_path;
167  
168 my $cur_cmd_ref = $self->root_node;
169 my $cmd_list = $args{arguments};
170 my @cmd_ref_path;
171  
172 while (@$cmd_list) {
173 my $new_cmd_ref = $cur_cmd_ref->find_command($cmd_list->[0]);
174 if (!$new_cmd_ref) {
175 return (%args, status => -1,
176 error => @cmd_path > 0
177 ? "@cmd_path: ".$cur_cmd_ref->error
178 : $cur_cmd_ref->error
179 );
180 }
181 push @cmd_path, shift @{$cmd_list};
182 push @cmd_ref_path, $new_cmd_ref;
183 $cur_cmd_ref = $new_cmd_ref;
184 }
185  
186 my $last_cmd = pop @cmd_ref_path;
187 my $usage_prefix = join(' ',
188 map { $_->usage_text(with_options => 'none', with_subcommands => 0) }
189 @cmd_ref_path
190 );
191 $usage_prefix .= ' ' if length $usage_prefix;
192 my $pod .= "=head2 ".loc("Usage").":\n\n";
193 for my $usage ($last_cmd->usage_text(with_options => 'both')) {
194 $pod .= "$usage_prefix$usage\n\n";
195 }
196 $pod =~ s/\n*$//s;
197 $pod .= "\n\n";
198  
199 if (my $description = $cur_cmd_ref->description) {
200 $pod .= "=head2 ".loc("Description").":\n\n";
201 $pod .= $cur_cmd_ref->description;
202 }
203 elsif (my $summary = $cur_cmd_ref->summary) {
204 $pod .= "=head2 Description:\n\n";
205 $pod .= $cur_cmd_ref->summary;
206 }
207  
208 $pod =~ s/\n*$/\n\n/s;
209  
210 my $pod2txt = $self->_format_pod($pod);
211  
212 # Only list sub-commands if there are more than one.
213 if (scalar($cur_cmd_ref->commands) > 1) {
214 my ($cmd_pod, $cmd_text) =
215 $self->_make_command_summary(
216 cmd_path => \@cmd_path,
217 pod_prefix => "=head2 ".loc("Sub-Commands").":\n\n",
218 commands => [$cur_cmd_ref->commands],
219 );
220 $pod .= $cmd_pod;
221 $pod2txt .= $cmd_text;
222 }
223  
224 # Play fast and loose with the POD formatter output.
225 # Remove leading and trailing newlines, reduce line indent.
226 $pod2txt =~ s/^\n+//s;
227 $pod2txt =~ s/\n+$//s;
228 #$pod2txt =~ s/^ //gm;
229 $text .= "$pod2txt\n";
230
231 return (%args, pod => $pod, text => $text);
232 }
233  
234  
235 sub complete_line {
236 my ($self, @words) = @_;
237  
238 my $partial = $words[$#words] // '';
239  
240 # uncoverable branch false
241 if ($self->has_options) {
242  
243 Getopt::Long::Configure(qw(bundling require_order pass_through));
244  
245 my $opt_specs = $self->options;
246  
247 my %parsed_opts;
248  
249 my $has_terminator = first { $_ eq '--' } @words[0..$#words-1];
250  
251 eval { GetOptionsFromArray(\@words, \%parsed_opts, @$opt_specs) };
252  
253 if (!$has_terminator && @words <= 1 && $partial =~ /^-/) {
254 # We have to complete a command-line option.
255 return grep { rindex($_, $partial, 0) == 0 } $self->option_names;
256 }
257 }
258  
259 my $cur_cmd_ref = $self->root_node;
260 while (@words) {
261 my $new_cmd_ref = $cur_cmd_ref->find_command($words[0]);
262 if (!$new_cmd_ref) {
263 last;
264 }
265 shift @words;
266 $cur_cmd_ref = $new_cmd_ref;
267 }
268  
269 if (@words == 0) {
270 return $cur_cmd_ref->name;
271 }
272 elsif ($cur_cmd_ref->has_commands && @words == 1) {
273 return grep { rindex($_, $partial, 0) == 0 } $cur_cmd_ref->command_names;
274 }
275 return ();
276 }
277  
278 # %args = HELP->_execute_help(%args);
279 #
280 # Callback for the builtin "help" command.
281 #
282 sub _execute_help {
283 my ($self, %args) = @_;
284  
285 return %args if $args{status} < 0;
286  
287 %args = $self->_get_help(%args);
288  
289 return %args if $args{status} < 0;
290  
291 if ($args{options}->{pod}) {
292 print "\n$args{pod}";
293 return %args;
294 }
295  
296 my $pager_fh;
297 my $pager_cmd = $self->pager;
298  
299 if (@$pager_cmd) {
300 no warnings 'exec';
301 if (!open $pager_fh, "|-", @{$pager_cmd}) {
302 $args{status} = -1;
303 $args{error} = loc("cannot run '[_1]': [_2]", $$pager_cmd[0], $!);
304 return %args;
305 }
306  
307 local( $SIG{PIPE} ) = 'IGNORE'; # Temporarily avoid accidents.
308 print $pager_fh $args{text};
309  
310 $pager_fh->close;
311 $args{status} = $?;
312 $args{error} = $! if $args{status} != 0;
313 }
314 else {
315 if (!open $pager_fh, '>&', \*STDOUT) {
316 $args{status} = -1;
317 $args{error} = "dup(STDOUT): $!";
318 return %args;
319 }
320 print $pager_fh $args{text};
321 if (!$pager_fh->close) {
322 $args{status} = -1;
323 $args{error} = $!;
324 }
325 }
326 return %args;
327 }
328  
329 }
330  
331 1;
332  
333 __END__