CLI/Role/HelpText.pm

filter: Statementstmt 99.1% 110 / 111 Branchbran 98.2% 55 / 56 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 96.9% 190 / 196 CC 36 SCAR 24.2
CC
36
Cov
98%
get_options_summary
15.0
usage_text
12.1
_usage_arg_str
11.0
CRAP 11.3
« CLI/ReadLine.pm ↑ Summary CLI/Command.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::HelpText
4 #
5 # Description: Class for sets of (sub-)commands in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
8 # Created: 19/02/18
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::Role::HelpText 0.061000;
22  
23 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 use Types::Standard 1.000005 qw(
27 Maybe
28 Str
29 7 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 7 requires 'options';
35 7 requires 'has_commands';
36 7 requires 'commands';
37  
38 7 has usage => (
39 is => 'rw',
40 isa => Maybe [Str],
41 );
42  
43 7 has description => (
44 is => 'rw',
45 isa => Maybe [Str],
46 );
47  
48 has summary => (
49 is => 'rw',
50 isa => Str,
51 36 ▶ default => sub {''},
Subroutine __ANON__: called CC=1 SCAR=0.0
52 7 );
53  
54 sub get_options_summary {
55 30 ▶ my ( $self, @args ) = @_;
Subroutine get_options_summary: called CC=15 SCAR=27.1
Pod documented
56 30 my %args = ( with_options => 'both', @args );
57  
58 30 my $with_options = 0x00;
59  
60 30 ▶ if ( $args{with_options} =~ /short/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_options"} =~ /short/iu) 1 29 30
61 1 $with_options |= 0x01;
62 }
63 30 ▶ if ( $args{with_options} =~ /long/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_options"} =~ /long/iu) 1 29 30
64 1 $with_options |= 0x02;
65 }
66 30 ▶ if ( $args{with_options} =~ /both/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_options"} =~ /both/iu) 13 17 30
67 13 $with_options |= 0x03;
68 }
69  
70 30 my @options;
71 30 my $short_opts_no_arg = '';
72 30 ▶ if ( my $opt_specs = $self->options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt_specs = $self->options) 11 19 30
73 11 for my $spec (@$opt_specs) {
74 27 my ( $opt_names, $arg_req, $opt_arg ) = $spec =~ m{
75 \A ([^!+=:]+)
76 (?: ([:=]) (.*) )?
77 }xms;
78  
79 27 my $negatable = index($spec, '!') >= 0;
80  
81 27 my $long_arg = my $short_arg = q{};
82 27 ▶ if ( defined $opt_arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_arg) 10 17 27
83 10 $long_arg = "=I<$opt_arg>";
84 10 $short_arg = "I<$opt_arg>";
85 10 ▶ if ( $arg_req eq ':' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_req eq ":") 5 5 10
86 5 $long_arg = "[$long_arg]";
87 5 $short_arg = "[$short_arg]";
88 }
89 }
90  
91 27 for my $optname ( split qr{ \| }x, $opt_names ) {
92 54 ▶ if ( length $optname == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $optname == 1) 27 27 54
93 27 ▶ next unless $with_options & 0x01;
Branch: 1 branch
BranchTrueFalseTotal
unless $with_options & 1 21 6 27
94 6 ▶ if ( length $short_arg == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $short_arg == 0) 2 4 6
95 2 $short_opts_no_arg .= $optname;
96 2 next;
97 }
98 4 push @options, "[B<-$optname>$short_arg]";
99 4 next;
100 }
101 27 ▶ if ( $with_options & 0x02 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($with_options & 2) 6 21 27
102 6 ▶ if ($negatable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negatable) { } 2 4 6
103 #push @options, "[B<-->[B<no>]B<-$optname>]";
104 2 push @options, "[B<--$optname>|B<--no-$optname>]";
105 }
106 else {
107 4 push @options, "[B<--$optname>$long_arg]";
108 }
109 }
110 }
111 }
112 }
113 30 ▶ if ( length $short_opts_no_arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $short_opts_no_arg) 2 28 30
114 2 push @options, "[B<-$short_opts_no_arg>]";
115 }
116 30 return join( ' ', @options );
117 }
118  
119 sub _usage_arg_str {
120 26 ▶ my ( $self, $arg ) = @_;
Subroutine _usage_arg_str: called CC=11 SCAR=24.0
121  
122 26 my $name = $arg->name;
123 26 ▶ my $str = $arg->max_occur > 1 ? "I<${name}1>" : "I<$name>";
Branch: 1 branch
BranchTrueFalseTotal
$arg->max_occur > 1 ? : 14 12 26
124  
125 26 ▶ if ( $arg->min_occur > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->min_occur > 1) 11 15 26
126 11 for my $n ( 2 .. $arg->min_occur ) {
127 12 $str .= " I<${name}$n>";
128 }
129 }
130  
131 SWITCH: {
132 26 ▶ if ( $arg->max_occur <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->max_occur <= 0) 4 22 26
133 4 $str .= ' ...';
134 4 last SWITCH;
135 }
136 22 ▶ if ( $arg->max_occur == $arg->min_occur + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->max_occur == $arg->min_occur + 1) 3 19 22
137 3 ▶ if ( $arg->max_occur > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->max_occur > 1) 2 1 3
138 2 $str .= " [I<${name}" . $arg->max_occur . ">]";
139 }
140 3 last SWITCH;
141 }
142 19 ▶ if ( $arg->max_occur == 2 && $arg->min_occur <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->max_occur == 2 and $arg->min_occur <= 1) 1 18 19
Condition100%3/3
$arg->max_occur == 2 and $arg->min_occur <= 1
!ll&&!rl&&r
1081
MC/DC100%2/2
$arg->max_occur == 2 and $arg->min_occur <= 1
$arg->max_occur == 2 $arg->min_occur <= 1
Truth table100%3/3
A and B
A= $arg->max_occur == 2B= $arg->min_occur <= 1
ABresult
0X0
100
111
143 1 $str .= " [I<${name}" . $arg->max_occur . ">]";
144 1 last SWITCH;
145 }
146 18 ▶ if ( $arg->max_occur > $arg->min_occur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->max_occur > $arg->min_occur) 2 16 18
147 2 $str
148 .= ' ['
149 . "I<$name"
150 . ( $arg->min_occur + 1 ) . ">" . ' ... '
151 . "I<$name"
152 . $arg->max_occur . ">" . ']';
153 2 last SWITCH;
154 }
155 }
156  
157 26 ▶ if ( $arg->min_occur <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->min_occur <= 0) 6 20 26
158 6 $str = "[$str]";
159 }
160 26 return $str;
161 }
162  
163 sub usage_text {
164 31 ▶ my ( $self, @args ) = @_;
Subroutine usage_text: called CC=12 SCAR=24.9
Pod documented
165  
166 31 my %args = (
167 with_options => 'both',
168 with_arguments => 1,
169 with_subcommands => 1,
170 @args,
171 );
172  
173 31 ▶ if ( $self->usage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->usage) 1 30 31
174 1 return $self->usage;
175 }
176  
177 30 my $usage_prefix = 'B<' . $self->name . '>';
178 30 my $usage_suffix = '';
179  
180 30 ▶ if ( $args{with_arguments} and $self->has_arguments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_arguments"} and $self->has_arguments) 23 7 30
Condition100%3/3
$args{"with_arguments"} and $self->has_arguments
!ll&&!rl&&r
1623
MC/DC100%2/2
$args{"with_arguments"} and $self->has_arguments
$args{"with_arguments"} $self->has_arguments
Truth table100%3/3
A and B
A= $args{"with_arguments"}B= $self->has_arguments
ABresult
0X0
100
111
181 23 my @arg_l = map { $self->_usage_arg_str($_) } $self->arguments;
182 23 $usage_suffix = join( ' ', @arg_l );
183 }
184  
185 30 ▶ if ( $args{with_subcommands} and $self->has_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"with_subcommands"} and $self->has_commands) 5 25 30
Condition66%2/3
$args{"with_subcommands"} and $self->has_commands
!ll&&!rl&&r
0255
MC/DC50%1/2
$args{"with_subcommands"} and $self->has_commands
$args{"with_subcommands"} $self->has_commands
Truth table66%2/3
A and B
A= $args{"with_subcommands"}B= $self->has_commands
ABresult
0X0
100
111
186 5 my @sub_commands = $self->commands;
187 5 my $sub_commands_text;
188 5 ▶ if ( @sub_commands == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sub_commands == 1) { } 1 4 5
189 1 $sub_commands_text =
190 $sub_commands[0]->usage_text( %args, with_options => 'none' );
191 }
192 else {
193 $sub_commands_text = '{'
194 4 . join( '|', map { 'B<' . $_->name . '>' } @sub_commands )
195 . '}';
196  
197 4 ▶ my $term_width = eval { $self->root_node->term->term_width } // 80;
Condition50%1/2
eval { // 80
l!l
04
MC/DC0%0/1
eval { do { $self->root_node->term->term_width } } // 80
eval { do { $self->root_node->term->term_width } }
Truth table50%1/2
A
A= eval { do { $self->root_node->term->term_width } }
Aresult
11
00
198 4 my $max_width = int($term_width / 2);
199 4 ▶ if (length $sub_commands_text > $max_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $sub_commands_text > $max_width) 0 4 4
200 0 $sub_commands_text = 'I<sub-command>';
201 }
202 }
203 5 ▶ $usage_suffix .= ' ' if length $usage_suffix;
Branch: 1 branch
BranchTrueFalseTotal
if length $usage_suffix 1 4 5
204 5 $usage_suffix .= $sub_commands_text;
205 }
206  
207 30 ▶ $usage_suffix = " $usage_suffix" if length $usage_suffix;
Branch: 1 branch
BranchTrueFalseTotal
if length $usage_suffix 27 3 30
208  
209 my $opts =
210 30 $self->get_options_summary( with_options => $args{with_options} );
211  
212 30 ▶ if ( length $opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $opts) 3 27 30
213 3 return "$usage_prefix $opts$usage_suffix";
214 }
215 27 return "$usage_prefix$usage_suffix";
216 }
217  
218 7 1;
219  
220 __END__
221  
222 =pod
223  
224 =head1 NAME
225  
226 Term::CLI::Role::HelpText - Role for generating help text in Term::CLI
227  
228 =head1 VERSION
229  
230 version 0.061000
231  
232 =head1 SYNOPSIS
233  
234 package Term::CLI::Command {
235  
236 use Moo;
237  
238 with('Term::CLI::Role::HelpText');
239  
240 ...
241 };
242  
243 my $cmd = Term::CLI::Command->new(
244 name => 'file',
245 options => ['verbose|v'],
246 arguments => [
247 Term::CLI::Argument::Filename->new(name => 'path'),
248 ],
249 commands => [
250 Term::CLI::Command->new(name => 'info'),
251 Term::CLI::Command->new(name => 'delete'),
252 Term::CLI::Command->new(name => 'show'),
253 ],
254 );
255  
256 say $cmd->usage_text();
257 # -> file [--verbose] [-v] path {info|delete|show}
258  
259 say $cmd->usage_text( with_options => 'long' );
260 # -> file [--verbose] path {info|delete|show}
261  
262 say $cmd->usage_text( with_options => 'none');
263 # -> file path {info|delete|show}
264  
265 say $cmd->usage_text( with_arguments => 0);
266 # -> file [--verbose] [-v] {info|delete|show}
267  
268 say $cmd->usage_text( with_subcommands => 0);
269 # -> file [--verbose] [-v] path
270  
271 =head1 DESCRIPTION
272  
273 Role for L<Term::CLI::Command>(3p) elements that need to have
274 help text.
275  
276 This role is consumed by L<Term::CLI::Command>(3p).
277  
278 The functionality of this role is primarily used by
279 L<Term::CLI::Command::Help>(3p).
280  
281 =head1 ATTRIBUTES
282  
283 This role defines three additional attributes:
284  
285 =over
286  
287 =item B<description> =E<gt> I<Str>
288  
289 Fragment of POD text that describes the command in some detail.
290 It is typically shown when help is requested for specifically
291 this command.
292  
293 Default is C<undef>, which typically means that the
294 L<summary|/summary> attribute is used in its place.
295  
296 =item B<summary> =E<gt> I<Str>
297  
298 Short summary of the command (e.g. what you typically find in the B<NAME>
299 section of a manual page), that is typically displayed in a command
300 summary.
301  
302 Default is an empty string.
303  
304 =item B<usage> =E<gt> I<Str>
305  
306 Optional attribute that should contain a single line of POD
307 documentation to describe the syntax of the command.
308  
309 Default is C<undef>, which causes L<usage_text|/usage_text>
310 to automatically generate a usage line.
311  
312 B<NOTE:> if this is specified, the L<usage_text|/usage_text>
313 method will always return this value.
314  
315 =back
316  
317 =head1 ACCESSORS
318  
319 =over
320  
321 =item B<description> ( [ I<Str> ] )
322 X<description>
323  
324 Get or set the description help text.
325  
326 =item B<summary> ( [ I<Str> ] )
327 X<summary>
328  
329 Get or set the summary help text.
330  
331 =item B<usage> ( [ I<Str> ] )
332 X<usage>
333  
334 Get or set the static usage text.
335  
336 =back
337  
338 =head1 METHODS
339  
340 =over
341  
342 =item B<get_options_summary> ( [ B<with_options> =E<gt> I<VAL> )
343 X<get_options_summary>
344  
345 Return a line of POD text for the command line options for this
346 command, depending on the value of the B<with_options> parameter.
347  
348 This function is called by L<usage_text|/usage_text>. You'll
349 probably never need to call it directly.
350  
351 =item B<usage_text> ( I<OPT> =E<gt> I<VAL>, ... )
352 X<usage_text>
353  
354 Return a line of POD text with a usage summary for the command.
355  
356 If the L<usage|/usage> attribute has been set, then this value is always
357 returned. Otherwise, the method will construct a POD fragment from the
358 command's name, options, arguments, and sub-commands.
359  
360 The following parameters are recognised:
361  
362 =over
363  
364 =item B<with_options> =E<gt> {C<long>|C<short>|C<both>|C<none>}
365  
366 Specify which command options to include in the usage text. Options are
367 C<long> to only include long options (e.g. C<< [B<--verbose>] >>),
368 C<short> to only include short options (e.g. C<< [B<-v>] >>),
369 C<both> for both short and long options (e.g. C<< [B<--verbose>] [B<-v>] >>),
370 or C<none> for none.
371  
372 Default is C<both>.
373  
374 =item B<with_arguments> =E<gt> {0|1}
375  
376 Specify whether or not to include placeholders for command line arguments in
377 the usage line.
378  
379 Default is C<1>.
380  
381 =item B<with_subcommands> =E<gt> {0|1}
382  
383 Specify whether or not to include the list of sub-commands in
384 the usage line.
385  
386 Default is C<1>.
387  
388 =back
389  
390 =back
391  
392 =head1 SEE ALSO
393  
394 L<Term::CLI>(3p),
395 L<Term::CLI::Command::Help>(3p),
396 L<Term::CLI::Command>(3p).
397  
398 =head1 AUTHOR
399  
400 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2018.
401  
402 =head1 COPYRIGHT AND LICENSE
403  
404 Copyright (c) 2018 Steven Bakker
405  
406 This module is free software; you can redistribute it and/or modify
407 it under the same terms as Perl itself. See "perldoc perlartistic."
408  
409 This software is distributed in the hope that it will be useful,
410 but WITHOUT ANY WARRANTY; without even the implied warranty of
411 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
412  
413 =begin __PODCOVERAGE
414  
415 =head1 THIS SECTION SHOULD BE HIDDEN
416  
417 This section is meant for methods that should not be considered
418 for coverage. This typically includes things like BUILD and DEMOLISH from
419 Moo/Moose. It is possible to skip these when using the Pod::Coverage class
420 (using C<also_private>), but this is not an option when running C<cover>
421 from the command line.
422  
423 The simplest trick is to add a hidden section with an item list containing
424 these methods.
425  
426 =over
427  
428 =item BUILD
429  
430 =item DEMOLISH
431  
432 =back
433  
434 =end __PODCOVERAGE
435  
436 =cut
« CLI/ReadLine.pm ↑ Summary CLI/Command.pm »