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), <cpanman@monkey-mind.net>
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.061001;
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(
Subroutine BEGIN: called CC=1 SCAR=0.0
27 Maybe
28 Str
29 7 );
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 30 ▶sub get_options_summary {
Subroutine get_options_summary: called CC=15 SCAR=27.1
Pod documented
55 30 my ( $self, @args ) = @_;
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 26 ▶sub _usage_arg_str {
Subroutine _usage_arg_str: called CC=11 SCAR=24.0
120 26 my ( $self, $arg ) = @_;
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 31 ▶sub usage_text {
Subroutine usage_text: called CC=12 SCAR=24.9
Pod documented
164 31 my ( $self, @args ) = @_;
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 { do { $self->root_node->term->term_width } } // 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__
« CLI/ReadLine.pm ↑ Summary CLI/Command.pm »