CLI/Util.pm

filter: Statementstmt 96.0% 123 / 128 Branchbran 94.4% 51 / 54 Conditioncond 89.1% 33 / 37 MC/DCmcdc 68.1% 15 / 22 Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 92.2% 238 / 258 CC 45 SCAR 26.3
CC
45
Cov
95%
find_text_matches
18.1
find_obj_name_matches
18.0
get_options_from_array
11.7
CRAP 13.8
« CLI/Command/Help.pm ↑ Summary CLI/ReadLine.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Util
4 #
5 # Description: Utility functions for Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
8 # Created: 21/01/2022
9 #
10 # Copyright (c) 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::Util 0.061000;
22  
23 13 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶use parent qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 13 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 13 ▶use Getopt::Long 2.38 qw( GetOptionsFromArray );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # When "pass_through" is enabled, Getopt::Long >= 2.51 will leave a
32 # "--" in the argument list, but older versions won't.
33 13 my $HAVE_OLD_GETOPT = $Getopt::Long::VERSION < 2.51;
34  
35 BEGIN {
36 13 ▶ our @EXPORT_OK = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
37 get_options_from_array
38 is_prefix_str
39 find_obj_name_matches
40 find_text_matches
41 );
42 13 our @EXPORT = ();
43 13 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
44 }
45  
46 sub is_prefix_str {
47 202 ▶ my ($substr, $str) = @_;
Subroutine is_prefix_str: called CC=1 SCAR=0.0
Pod documented
48 202 return rindex( $str, $substr, 0 ) == 0;
49 }
50  
51 sub get_options_from_array {
52 48 ▶ my %args = @_;
Subroutine get_options_from_array: called CC=11 SCAR=24.6
Pod documented
53 my ($arguments, $result, $opt_specs, $pass_through) =
54 48 @args{qw(args result spec pass_through)};
55  
56 48 ▶ $result //= {};
Condition50%1/2
$result //= {}
l!l
480
MC/DC0%0/1
$result //= {}
$result
Truth table50%1/2
A
A= $result
Aresult
11
00
57  
58 48 my $double_dash;
59  
60 48 ▶ Getopt::Long::Configure(
Branch: 1 branch
BranchTrueFalseTotal
$pass_through ? : 31 17 48
61 qw(bundling require_order),
62 ($pass_through ? '' : 'no_') . 'pass_through'
63 );
64  
65 48 ▶ if ( $pass_through && $HAVE_OLD_GETOPT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_through and $HAVE_OLD_GETOPT) 0 48 48
Condition66%2/3
$pass_through and $HAVE_OLD_GETOPT
!ll&&!rl&&r
17310
MC/DC0%0/2
$pass_through and $HAVE_OLD_GETOPT
$pass_through $HAVE_OLD_GETOPT
Truth table66%2/3
A and B
A= $pass_throughB= $HAVE_OLD_GETOPT
ABresult
0X0
100
111
66 0 ▶ $double_dash = first { $_ eq '--' } @{$arguments};
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 }
68  
69 48 ▶ $double_dash //= q{};
Condition50%1/2
$double_dash //= ""
l!l
048
MC/DC0%0/1
$double_dash //= ""
$double_dash
Truth table50%1/2
A
A= $double_dash
Aresult
11
00
70  
71 48 my $error = q{};
72 48 my $ok = do {
73 48 ▶ local ( $SIG{__WARN__} ) = sub { chomp( $error = join( q{}, @_ ) ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 48 GetOptionsFromArray( $arguments, $result, @{$opt_specs} );
75 };
76  
77 48 ▶ if ( $pass_through && !$HAVE_OLD_GETOPT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_through and not $HAVE_OLD_GETOPT) 31 17 48
Condition66%2/3
$pass_through and not $HAVE_OLD_GETOPT
!ll&&!rl&&r
17031
MC/DC50%1/2
$pass_through and not $HAVE_OLD_GETOPT
$pass_through not $HAVE_OLD_GETOPT
Truth table66%2/3
A and not(B)
A= $pass_throughB= not $HAVE_OLD_GETOPT
ABresult
0X0
100
111
78 31 ▶ if ( @{$arguments} && $arguments->[0] eq '--' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$arguments;} and $arguments->[0] eq "--") 3 28 31
Condition100%3/3
@{$arguments;} and $arguments->[0] eq "--"
!ll&&!rl&&r
17113
MC/DC100%2/2
@{$arguments;} and $arguments->[0] eq "--"
@{$arguments;} $arguments->[0] eq "--"
Truth table100%3/3
A and B
A= @{$arguments;}B= $arguments->[0] eq "--"
ABresult
0X0
100
111
79 3 $double_dash = shift @{$arguments};
80 }
81 }
82  
83 return (
84 48 ▶ success => $ok,
Branch: 1 branch
BranchTrueFalseTotal
$pass_through ? : 31 17 48
85 error_msg => $error,
86 $pass_through ? ( double_dash => $double_dash ne q{} ) : (),
87 );
88 }
89  
90 sub find_obj_name_matches {
91 119 ▶ my ( $text, $list, %opt ) = @_;
Subroutine find_obj_name_matches: called CC=18 SCAR=28.9
Pod documented
92  
93 119 ▶ return if !$list || @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $list or @$list == 0 6 113 119
Condition100%3/3
not $list or @$list == 0
l!l&&r!l&&!r
42113
MC/DC100%2/2
not $list or @$list == 0
not $list @$list == 0
Truth table100%3/3
not(A) or B
A= not $listB= @$list == 0
ABresult
1X1
011
000
94  
95 113 my $exact = $opt{exact};
96  
97 113 ▶ if ( @$list <= 10 || ( !$exact && @$list <= 45 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list <= 10 or not $exact and @$list <= 45) 67 46 113
Condition100%6/6
@$list <= 10 or not $exact and @$list <= 45
l!l&&r!l&&!r
65345
not $exact and @$list <= 45
!ll&&!rl&&r
4512
MC/DC66%2/3
@$list <= 10 or not $exact and @$list <= 45
@$list <= 10 not $exact @$list <= 45
Truth table75%3/4
A or not(B) and C
A= @$list <= 10B= not $exactC= @$list <= 45
ABCresult
1XX1
0111
00X0
0100
98 67 my @found;
99 67 foreach (@{$list}) {
100 160 my $n = $_->name;
101 160 ▶ next if $n lt $text;
Branch: 1 branch
BranchTrueFalseTotal
if $n lt $text 68 92 160
102 92 ▶ if (rindex( $n, $text, 0 ) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($n, $text, 0) == 0) 83 9 92
103 83 push @found, $_;
104 83 ▶ return @found if $exact && $n eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $exact and $n eq $text 53 30 83
Condition100%3/3
$exact and $n eq $text
!ll&&!rl&&r
23753
MC/DC100%2/2
$exact and $n eq $text
$exact $n eq $text
Truth table100%3/3
A and B
A= $exactB= $n eq $text
ABresult
0X0
100
111
105 30 next;
106 }
107 9 last;
108 }
109 14 return @found;
110 }
111  
112 46 my ( $lo, $hi ) = ( 0, $#{$list} );
113  
114 46 ▶ while ($lo < $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo < $hi) 173 1 174
115 173 my $mid = int( ($lo + $hi) / 2 );
116 173 my $cmp = $text cmp $list->[$mid]->name;
117 173 ▶ if ($cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0) 44 129 173
118 44 $hi = $mid;
119 44 next;
120 }
121 129 ▶ if ($cmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp > 0) 92 37 129
122 92 ▶ if ($lo == $hi-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo == $hi - 1) 8 84 92
123 8 $lo++;
124 8 last;
125 }
126 84 $lo = $mid;
127 84 next;
128 }
129 37 ▶ return $list->[$mid] if $exact;
Branch: 1 branch
BranchTrueFalseTotal
if $exact 36 1 37
130 1 $lo = $hi = $mid;
131 1 last;
132 }
133  
134 10 my @found;
135 10 foreach (@{$list}[$lo..$#{$list}]) {
136 13 my $n = $_->name;
137 13 ▶ if (rindex( $n, $text, 0 ) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($n, $text, 0) == 0) 10 3 13
138 10 push @found, $_;
139 10 next;
140 }
141 3 last;
142 }
143 10 return @found;
144 }
145  
146  
147 sub find_text_matches {
148 42 ▶ my ( $text, $list, %opt ) = @_;
Subroutine find_text_matches: called CC=18 SCAR=28.9
Pod documented
149  
150 42 ▶ return if !$list || @$list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $list or @$list == 0 4 38 42
Condition100%3/3
not $list or @$list == 0
l!l&&r!l&&!r
2238
MC/DC100%2/2
not $list or @$list == 0
not $list @$list == 0
Truth table100%3/3
not(A) or B
A= not $listB= @$list == 0
ABresult
1X1
011
000
151  
152 38 my $exact = $opt{exact};
153  
154 38 ▶ if ( @$list <= 45 || ( !$exact && @$list <= 215 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$list <= 45 or not $exact and @$list <= 215) 35 3 38
Condition100%6/6
@$list <= 45 or not $exact and @$list <= 215
l!l&&r!l&&!r
3422
not $exact and @$list <= 215
!ll&&!rl&&r
211
MC/DC66%2/3
@$list <= 45 or not $exact and @$list <= 215
@$list <= 45 not $exact @$list <= 215
Truth table75%3/4
A or not(B) and C
A= @$list <= 45B= not $exactC= @$list <= 215
ABCresult
1XX1
0111
00X0
0100
155 35 my @found;
156 35 foreach (@{$list}) {
157 237 ▶ next if $_ lt $text;
Branch: 1 branch
BranchTrueFalseTotal
if $_ lt $text 181 56 237
158 56 ▶ if (rindex( $_, $text, 0 ) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($_, $text, 0) == 0) 45 11 56
159 45 push @found, $_;
160 45 ▶ return @found if $exact && $_ eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if $exact and $_ eq $text 6 39 45
Condition100%3/3
$exact and $_ eq $text
!ll&&!rl&&r
3546
MC/DC100%2/2
$exact and $_ eq $text
$exact $_ eq $text
Truth table100%3/3
A and B
A= $exactB= $_ eq $text
ABresult
0X0
100
111
161 39 next;
162 }
163 11 last;
164 }
165 29 return @found;
166 }
167  
168 3 my ( $lo, $hi ) = ( 0, $#{$list} );
169  
170 3 ▶ while ($lo < $hi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo < $hi) 26 0 26
171 26 my $mid = int( ($lo + $hi) / 2 );
172 26 my $cmp = $text cmp $list->[$mid];
173 26 ▶ if ($cmp < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0) 6 20 26
174 6 $hi = $mid;
175 6 next;
176 }
177 20 ▶ if ($cmp > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp > 0) 17 3 20
178 17 ▶ if ($lo == $hi-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lo == $hi - 1) 0 17 17
179 0 $lo++;
180 0 last;
181 }
182 17 $lo = $mid;
183 17 next;
184 }
185 3 ▶ return $list->[$mid] if $exact;
Branch: 1 branch
BranchTrueFalseTotal
if $exact 2 1 3
186 1 $lo = $hi = $mid;
187 1 last;
188 }
189  
190 1 my @found;
191 1 foreach (@{$list}[$lo..$#{$list}]) {
192 4 ▶ if (rindex( $_, $text, 0 ) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($_, $text, 0) == 0) 3 1 4
193 3 push @found, $_;
194 3 next;
195 }
196 1 last;
197 }
198 1 return @found;
199  
200 }
201  
202 13 1;
203  
204 __END__
205  
206 =pod
207  
208 =head1 NAME
209  
210 Term::CLI::Util - utility functions for Term::CLI(3p)
211  
212 =head1 VERSION
213  
214 version 0.061000
215  
216 =head1 SYNOPSIS
217  
218 use Term::CLI::Util qw( :all );
219  
220 use Data::Dumper;
221  
222 my %result = get_options_from_array(
223 args => \@ARGV,
224 spec => [ 'verbose|v', 'debug|d' ]
225 result => \(my %options),
226 pass_through => 1,
227 );
228  
229 say "Result: ", Data::Dumper->Dump(
230 [ \%result, \%options, \@ARGV ],
231 [ 'result', 'options', 'ARGV' ]
232 );
233  
234 is_prefix_str( 'foo', 'foobar' ); # returns true.
235 is_prefix_str( 'bar', 'foobar' ); # returns false.
236  
237 =head1 DESCRIPTION
238  
239 Provide utility functions for various modules in the
240 L<Term::CLI|Term::CLI>(3p) collection.
241  
242 =head1 EXPORTED FUNCTIONS
243  
244 No functions are exported by default. Functions need be imported explicitly
245 by name, or by specifying the C<:all> tag, which will import all functions.
246  
247 =head1 FUNCTIONS
248  
249 =over
250  
251 =item B<find_obj_name_matches>
252 X<find_obj_matches>
253  
254 LIST = find_obj_name_matches( TEXT, OBJ_LIST_REF );
255 LIST = find_obj_name_matches( TEXT, OBJ_LIST_REF, exact => 1 );
256  
257 Find objects in I<OBJ_LIST_REF> (an C<ArrayRef>) where I<TEXT> is a
258 prefix for the object's C<name> attribute. The list I<must> be sorted
259 on C<name> field.
260  
261 If the C<exact> options is specified to be true, an exact name match
262 will result in exactly one item to be returned.
263  
264 =item B<find_text_matches>
265 X<find_text_matches>
266  
267 LIST = find_string_matches( TEXT, STRING_LIST_REF );
268 LIST = find_string_matches( TEXT, STRING_LIST_REF, exact => 1 );
269  
270 Same as L<find_obj_name_matches> above, except that the second argument
271 refers to a I<sorted> list of scalars (strings), rather than objects.
272  
273 =item B<get_options_from_array>
274 X<get_options_from_array>
275  
276 HASH = get_options_from_array(
277 args => ArrayRef,
278 spec => ArrayRef,
279 [ result => HashRef, ]
280 [ pass_through => Bool, ]
281 );
282  
283 Parse the command line options in the C<args> array according to the options
284 specified in the C<spec> array. Recognised options are stored in the C<result>
285 hash.
286  
287 This is a fancy wrapper around
288 L<GetOptionsFromArray|Getopt::Long/GetOptionsFromArray>
289 from
290 L<Getopt::Long|Getopt::Long>(3p).
291  
292 This function, however, sets the following configuration flags for
293 C<Getopt::Long>: C<bundling>, C<require_order> and either C<pass_through>
294 or C<no_pass_through>, depending on the C<pass_through> parameter it was
295 given.
296  
297 Unlike C<Getopt::Long>, this function will remove the option terminating
298 "double dash" (C<-->) argument from the argument list, even if
299 C<pass_through> is true.
300  
301 The return value is a hash with the following keys:
302  
303 =over
304  
305 =item C<success>
306  
307 A boolean indicating whether the option parsing was successful or not. If
308 false, the C<error_msg> key will hold a diagnostic message.
309  
310 =item C<error_msg>
311  
312 A string holding the error message from C<Getopt::Long>. If C<success> is
313 true, this will be an empty string.
314  
315 =item C<double_dash>
316  
317 A boolean indicating whether option parsing was stopped due to encountering
318 a C<--> word in the argument list.
319  
320 This is only present if C<pass_through> was specified with a true value.
321  
322 =back
323  
324 =item B<is_prefix_str>
325 X<is_prefix_str>
326  
327 BOOL = is_prefix_str( SUBSTR, STRING );
328  
329 Returns whether I<SUBSTR> is a prefix of I<STRING>.
330  
331 =back
332  
333 =head1 EXAMPLES
334  
335 Consider the following script, called C<getopt_test.pl>:
336  
337 use 5.014;
338 use warnings;
339 use Data::Dumper;
340 use Term::CLI::Util qw( get_options_from_array );
341  
342 my ($pass_through, @args) = @ARGV;
343  
344 my %options;
345  
346 my %result = get_options_from_array(
347 args => \@args,
348 spec => [ 'verbose|v+', 'debug|d' ],
349 result => \%options,
350 pass_through => $pass_through,
351 );
352  
353 print Data::Dumper->Dump(
354 [ \%result, \%options, \@args ],
355 [ '*result', '*options', '*args' ]
356 );
357  
358 =head2 With correct input
359  
360 With C<pass_through>:
361  
362 $ perl getopt_test.pl 1 -vv --debug -- foo bar
363 %result = (
364 'success' => 1,
365 'error_msg' => '',
366 'double_dash' => 1
367 );
368 %options = (
369 'debug' => 1,
370 'verbose' => 2
371 );
372 @args = (
373 'foo',
374 'bar'
375 );
376  
377 Without C<pass_through>:
378  
379 $ perl getopt_test.pl 0 -vv --debug -- foo bar
380 %result = (
381 'success' => 1,
382 'error_msg' => '',
383 );
384 %options = (
385 'debug' => 1,
386 'verbose' => 2
387 );
388 @args = (
389 'foo',
390 'bar'
391 );
392  
393 Note the difference in output: in the case where C<pass_through> is enabled,
394 there is an entry in the result for C<double_dash> (which in this case is
395 true).
396  
397 =head2 With incorrect input
398  
399 With C<pass_through>:
400  
401 $ perl getopt_test.pl 1 -vv --debug --bad -- foo bar
402 %result = (
403 'success' => 1,
404 'error_msg' => '',
405 'double_dash' => ''
406 );
407 %options = (
408 'verbose' => 2
409 );
410 @args = (
411 '--bad',
412 '--debug',
413 '--',
414 'foo',
415 'bar'
416 );
417  
418 Without C<pass_through>:
419  
420 $ perl getopt_test.pl 0 -vv --debug -- foo bar
421 %result = (
422 'success' => '',
423 'error_msg' => 'Unknown option: bad'
424 );
425 %options = (
426 'debug' => 1,
427 'verbose' => 2
428 );
429 @args = (
430 'foo',
431 'bar'
432 );
433  
434 =head1 SEE ALSO
435  
436 L<Term::CLI|Term::CLI>(3p),
437 L<Getopt::Long|Getopt::Long>(3p).
438  
439 =head1 CAVEATS
440  
441 When C<pass_through> is enabled, L<Getopt::Long> should leave a
442 C<--> in the argument list when it encounters it; however, versions
443 before 2.51 do not do so.
444  
445 This module tries to compensate for that when it detects an older
446 L<Getopt::Long|Getopt::Long> version by searching for C<--> in
447 the argument list beforehand. There are still instances where this
448 can go wrong, though:
449  
450 =over
451  
452 =item *
453  
454 Input is C<--foo -- --verbose> and option C<foo> happens to take an
455 argument.
456  
457 =item *
458  
459 Input is I<--opt1 arg1 arg2> and I<arg2> happens to be C<-->.
460  
461 =back
462  
463 However, since the C<double_dash> flag is really only used in command
464 completion, this is not a very major issue.
465  
466 =head1 AUTHOR
467  
468 Steven Bakker E<lt>Steven.Bakker@ams-ix.netE<gt>, AMS-IX B.V.; 2022.
469  
470 =head1 COPYRIGHT AND LICENSE
471  
472 Copyright (c) 2022 AMS-IX B.V.; All rights reserved.
473  
474 This module is free software; you can redistribute it and/or modify
475 it under the same terms as Perl itself. See "perldoc perlartistic."
476  
477 This software is distributed in the hope that it will be useful,
478 but WITHOUT ANY WARRANTY; without even the implied warranty of
479 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
480  
481 =cut
« CLI/Command/Help.pm ↑ Summary CLI/ReadLine.pm »