CLI/Util.pm

filter: Statementstmt 96.0% 123 / 128 Branchbran 94.4% 51 / 54 Conditioncond 89.1% 33 / 37 MC/DCmcdc 77.2% 17 / 22 Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 93.0% 240 / 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), <cpanman@monkey-mind.net>
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.061001;
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 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 13 our @EXPORT_OK = qw(
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 202 ▶sub is_prefix_str {
Subroutine is_prefix_str: called CC=1 SCAR=0.0
Pod documented
47 202 my ($substr, $str) = @_;
48 202 return rindex( $str, $substr, 0 ) == 0;
49 }
50  
51 48 ▶sub get_options_from_array {
Subroutine get_options_from_array: called CC=11 SCAR=24.6
Pod documented
52 48 my %args = @_;
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 119 ▶sub find_obj_name_matches {
Subroutine find_obj_name_matches: called CC=18 SCAR=28.9
Pod documented
91 119 my ( $text, $list, %opt ) = @_;
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
65246
not $exact and @$list <= 45
!ll&&!rl&&r
4512
MC/DC100%3/3
@$list <= 10 or not $exact and @$list <= 45
@$list <= 10 not $exact @$list <= 45
Truth table100%4/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 42 ▶sub find_text_matches {
Subroutine find_text_matches: called CC=18 SCAR=28.9
Pod documented
148 42 my ( $text, $list, %opt ) = @_;
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
3413
not $exact and @$list <= 215
!ll&&!rl&&r
211
MC/DC100%3/3
@$list <= 45 or not $exact and @$list <= 215
@$list <= 45 not $exact @$list <= 215
Truth table100%4/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__
« CLI/Command/Help.pm ↑ Summary CLI/ReadLine.pm »