CLI.pm

filter: Statementstmt 82.9% 151 / 182 Branchbran 69.5% 32 / 46 Conditioncond 63.1% 12 / 19 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 95.1% 39 / 41 Podpod 100% 7 / 7 totaltotal 79.8% 245 / 307 CC 31 SCAR 21.9
CC
31
Cov
79%
write_pager
59.8
BUILD
6.1
_default_split
5.0
CRAP 8.9
« CLI/Argument/Filename.pm ↑ Summary CLI/Argument/Number.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI
4 #
5 # Description: Class for CLI parsing
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 31/01/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 0.061001;
22  
23 6 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 6 ▶use Text::ParseWords 3.27 qw( parse_line );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Term::CLI::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use FindBin 1.50;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use File::Which 1.09;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 6 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 6 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 # Load all Term::CLI classes so the user doesn't have to.
36  
37 6 ▶use Term::CLI::Argument::Bool;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 6 ▶use Term::CLI::Argument::Enum;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 6 ▶use Term::CLI::Argument::Filename;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 6 ▶use Term::CLI::Argument::Number;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 6 ▶use Term::CLI::Argument::Number::Float;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 6 ▶use Term::CLI::Argument::Number::Int;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use Term::CLI::Argument::String;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 6 ▶use Term::CLI::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use Term::CLI::Command::Help;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 6 ▶use Types::Standard 1.000005 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
49 ArrayRef
50 CodeRef
51 InstanceOf
52 Maybe
53 RegexpRef
54 Str
55 Int
56 6 );
57  
58 6 my @PAGERS = (
59 [ qw(
60 less --no-lessopen --no-init
61 --dumb --quit-at-eof
62 --quit-if-one-screen
63 --RAW-CONTROL-CHARS
64 )
65 ],
66 ['more'],
67 ['pg'],
68 );
69  
70 6 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 6 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 6 extends 'Term::CLI::Base';
74  
75 6 with qw(
76 Term::CLI::Role::CommandSet
77 Term::CLI::Role::State
78 );
79  
80 6 my $DFL_HIST_SIZE = 1000;
81  
82 # Provide a default for 'name'.
83 6 ▶has '+name' => ( default => sub {$FindBin::Script} );
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 has 'pager' => (
86 is => 'rw',
87 isa => ArrayRef [Str],
88 6 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 6 ▶ first { defined which( $_->[0] ) } @PAGERS;
Subroutine __ANON__: called CC=1 SCAR=0.0
90 },
91 6 );
92  
93 6 has cleanup => (
94 is => 'rw',
95 isa => Maybe [CodeRef],
96 predicate => 1
97 );
98  
99 has prompt => (
100 is => 'rw',
101 isa => Str,
102 4 ▶ default => sub {'~> '}
Subroutine __ANON__: called CC=1 SCAR=0.0
103 6 );
104  
105 has split_function => (
106 is => 'rw',
107 isa => CodeRef,
108 7 ▶ default => sub { \&_default_split }
Subroutine __ANON__: called CC=1 SCAR=0.0
109 6 );
110  
111 6 has skip => (
112 is => 'rw',
113 isa => RegexpRef,
114 );
115  
116 6 has history_file => (
117 is => 'rw',
118 isa => Str
119 );
120  
121 has history_lines => (
122 is => 'rw',
123 isa => Int,
124 6 ▶ default => sub {$DFL_HIST_SIZE},
Subroutine __ANON__: called CC=1 SCAR=0.0
125 6 trigger => 1,
126 );
127  
128 7 ▶has word_delimiters => ( is => 'rw', isa => Str, default => sub {" \n\t"} );
Subroutine __ANON__: called CC=1 SCAR=0.0
129 7 ▶has quote_characters => ( is => 'rw', isa => Str, default => sub {q("')} );
Subroutine __ANON__: called CC=1 SCAR=0.0
130  
131 7 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=18.1
Pod documented
132 7 my ( $self, $args ) = @_;
133  
134 7 my @fh_list = ( \*STDIN, \*STDOUT );
135 7 ▶ if (exists $args->{filehandles}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"filehandles"}) 2 5 7
136 2 ▶ @fh_list = @{ $args->{filehandles} // [] };
Condition50%1/2
$args->{"filehandles"} // []
l!l
20
MC/DC0%0/1
$args->{"filehandles"} // []
$args->{"filehandles"}
Truth table50%1/2
A
A= $args->{"filehandles"}
Aresult
11
00
137 }
138  
139 7 my $term = Term::CLI::ReadLine->new( $self->name, @fh_list );
140  
141 7 ▶ if ( my $sig_list = $args->{ignore_keyboard_signals} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sig_list = $args->{"ignore_keyboard_signals"}) 0 7 7
142 0 $term->ignore_keyboard_signals( @{$sig_list} );
143 }
144  
145 0 ▶ $term->Attribs->{char_is_quoted_p} = sub { $self->_is_escaped(@_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
146  
147 7 $self->_set_completion_attribs;
148  
149 7 ▶ if ( !exists $args->{callback} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"callback"}) 5 2 7
150 5 $self->callback( \&_default_callback );
151 }
152  
153 7 ▶ if ( !exists $args->{history_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"history_file"}) 6 1 7
154 6 my $hist_file = $self->name;
155 6 $hist_file =~ s{^/}{}gx;
156 6 $hist_file =~ s{/$}{}gx;
157 6 $hist_file =~ s{/+}{-}gx;
158 6 $self->history_file("$::ENV{HOME}/.${hist_file}_history");
159 }
160  
161 # Ensure that the history_lines trigger is called.
162 # If history_lines is given as a parameter, the trigger
163 # *is* called, but it happens *before* the `$term` is
164 # initialised; and if no history_lines is given, the
165 # trigger is not called for the default.
166 7 $self->history_lines( $self->history_lines );
167 7 return;
168 }
169  
170 7 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.6
Pod documented
171 7 my ($self) = @_;
172 7 ▶ if ( $self->has_cleanup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_cleanup) 0 7 7
173 0 $self->cleanup->($self);
174 }
175 7 return;
176 }
177  
178 9 ▶sub _trigger_history_lines { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _trigger_history_lines: called CC=2 SCAR=6.9
179 9 my ( $self, $arg ) = @_;
180  
181 # Terminal may not be initialiased yet...
182 9 ▶ return if !$self->term;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->term 1 8 9
183  
184 8 $self->term->StifleHistory($arg);
185 8 return;
186 }
187  
188 # %args = $self->_default_callback(%args);
189 #
190 # Default top-level callback if none is given.
191 # Simply check the status and print an error
192 # message if status < 0.
193 2 ▶sub _default_callback {
Subroutine _default_callback: called CC=2 SCAR=6.9
194 2 my ( $self, %args ) = @_;
195  
196 2 ▶ if ( $args{status} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"status"} < 0) 1 1 2
197 1 say STDERR loc("ERROR"), ": ", $args{error};
198 }
199 2 return %args;
200 }
201  
202 # ($error, @words) = $self->_default_split($text);
203 #
204 # Default function to split a string into words.
205 # Similar to "shellwords()", except that we use
206 # "parse_line" to support custom delimiters.
207 #
208 # Unfortunately, there's no way to specify custom
209 # quote characters.
210 #
211 86 ▶sub _default_split {
Subroutine _default_split: called CC=5 SCAR=16.1
212 86 my ( $self, $text ) = @_;
213  
214 86 my $delim = $self->word_delimiters;
215  
216 86 $text =~ s/^ [$delim]+ //gxsm; # Strip leading delimiters.
217  
218 86 ▶ return (q{}) if length $text == 0; # Blank line.
Branch: 1 branch
BranchTrueFalseTotal
if length $text == 0 2 84 86
219  
220 84 my @words = parse_line( qr{ [$delim]+ }xms, 0, $text );
221  
222 84 ▶ return ( loc('unbalanced quotes in input') ) if !@words;
Branch: 1 branch
BranchTrueFalseTotal
unless @words 2 82 84
223  
224 # If the text ends in one or more delimiters, the last word will be
225 # "undef", but it's not an errror, so just eliminate it.
226 82 ▶ if ( @words > 0 && !defined $words[-1] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@words > 0 and not defined $words[-1]) 30 52 82
Condition66%2/3
@words > 0 and not defined $words[-1]
!ll&&!rl&&r
05230
MC/DC50%1/2
@words > 0 and not defined $words[-1]
@words > 0 not defined $words[-1]
Truth table66%2/3
A and not(B)
A= @words > 0B= not defined $words[-1]
ABresult
0X0
100
111
227 30 pop @words;
228 }
229 82 return ( q{}, @words );
230 }
231  
232 43 ▶sub unescape_input {
Subroutine unescape_input: called CC=3 SCAR=11.0
Pod documented
233 43 my ($self, $text, $quote_char) = @_;
234  
235 43 ▶ if ( !$quote_char || $quote_char ne q{'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $quote_char or $quote_char ne "'") 41 2 43
Condition100%3/3
not $quote_char or $quote_char ne "'"
l!l&&r!l&&!r
3832
MC/DC100%2/2
not $quote_char or $quote_char ne "'"
not $quote_char $quote_char ne "'"
Truth table100%3/3
not(A) or B
A= not $quote_charB= $quote_char ne "'"
ABresult
1X1
011
000
236 41 return $text =~ s{ \\ (.) }{$1}xgr;
237 }
238 2 return $text;
239 }
240  
241 68 ▶sub escape_input {
Subroutine escape_input: called CC=3 SCAR=11.0
Pod documented
242 68 my ($self, $text, $quote_char) = @_;
243  
244 68 ▶ if ( !$quote_char ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quote_char) 62 6 68
245 62 my $delim = $self->root_node->word_delimiters;
246 62 return $text =~ s{ ([$delim\'\"\\]) }{\\$1}xgr;
247 }
248 6 ▶ if ( $quote_char ne q{'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_char ne "'") 4 2 6
249 4 return $text =~ s{ ([$quote_char\\]) }{\\$1}xgr;
250 }
251 2 return $text;
252 }
253  
254 # BOOL = CLI->_is_escaped($line, $index);
255 #
256 # The character at $index in $line is a possible word break
257 # character. Check if it is perhaps escaped.
258 #
259 12 ▶sub _is_escaped {
Subroutine _is_escaped: called CC=4 SCAR=13.9
260 12 my ( $self, $line, $index ) = @_;
261 12 ▶ return 0 if not defined $index or $index <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $index or $index <= 0 3 9 12
Condition66%2/3
not defined $index or $index <= 0
l!l&&r!l&&!r
039
MC/DC50%1/2
not defined $index or $index <= 0
not defined $index $index <= 0
Truth table66%2/3
not(A) or B
A= not defined $indexB= $index <= 0
ABresult
1X1
011
000
262 9 ▶ return 0 if substr( $line, $index - 1, 1 ) ne q{\\};
Branch: 1 branch
BranchTrueFalseTotal
if substr($line, $index - 1, 1) ne "\\" 3 6 9
263 6 return !$self->_is_escaped( $line, $index - 1 );
264 }
265  
266 2 ▶sub read_history {
Subroutine read_history: called CC=3 SCAR=11.0
Pod documented
267 2 my ( $self, $hist_file ) = @_;
268  
269 2 ▶ $hist_file //= $self->history_file;
Condition66%2/3
$hist_file //= $self->history_file
l!l&&r!l&&!r
110
MC/DC0%0/2
$hist_file //= $self->history_file
$hist_file $self->history_file
Truth table66%2/3
A //= B
A= $hist_fileB= $self->history_file
ABresult
1X1
011
000
270  
271 2 ▶ $self->term->ReadHistory($hist_file)
Branch: 1 branch
BranchTrueFalseTotal
$self->term->ReadHistory($hist_file) or return $self->set_error("${hist_file}: $!") 1 1 2
272 or return $self->set_error("$hist_file: $!");
273 1 $self->history_file($hist_file);
274 1 $self->clear_error;
275 1 return 1;
276 }
277  
278 2 ▶sub write_history {
Subroutine write_history: called CC=3 SCAR=11.0
Pod documented
279 2 my ( $self, $hist_file ) = @_;
280  
281 2 ▶ $hist_file //= $self->history_file;
Condition66%2/3
$hist_file //= $self->history_file
l!l&&r!l&&!r
110
MC/DC0%0/2
$hist_file //= $self->history_file
$hist_file $self->history_file
Truth table66%2/3
A //= B
A= $hist_fileB= $self->history_file
ABresult
1X1
011
000
282  
283 2 ▶ $self->term->WriteHistory($hist_file)
Branch: 1 branch
BranchTrueFalseTotal
$self->term->WriteHistory($hist_file) or return $self->set_error("${hist_file}: $!") 1 1 2
284 or return $self->set_error("$hist_file: $!");
285 1 $self->history_file($hist_file);
286 1 $self->clear_error;
287 1 return 1;
288 }
289  
290 0 ▶sub write_pager {
Subroutine write_pager: not called CC=8 SCAR=40.9
Pod documented
291 0 my ($self, %args) = @_;
292  
293 0 ▶ my $pager_cmd = $self->pager // [];
Condition0%0/2
$self->pager // []
l!l
00
MC/DC0%0/1
$self->pager // []
$self->pager
Truth table0%0/2
A
A= $self->pager
Aresult
11
00
294  
295 my $text =
296 ref $args{text} eq 'ARRAY'
297 0 ? join( q{}, @{ $args{text} } )
298 0 ▶ : $args{text};
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'text'} eq 'ARRAY' ? : 0 0 0
299  
300 0 ▶ if (@$pager_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$pager_cmd) 0 0 0
301 6 ▶ no warnings 'exec'; ## no critic (ProhibitNoWarnings)
Subroutine BEGIN: called CC=1 SCAR=0.0
302 0 local ( $SIG{PIPE} ) = 'IGNORE'; # Temporarily avoid accidents.
303  
304 0 my $pager_fh;
305 0 ▶ if (! open $pager_fh, '|-', @{$pager_cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $pager_fh, "|-", @{$pager_cmd;}) 0 0 0
306 0 $args{status} = -1;
307 $args{error} =
308 0 loc( "cannot run '[_1]': [_2]", $$pager_cmd[0], $! );
309 0 return %args;
310 }
311  
312 0 print $pager_fh $text;
313 0 $pager_fh->close;
314  
315 0 $args{status} = $?;
316 0 ▶ $args{error} = $! if $args{status} != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"status"} != 0 0 0 0
317 0 return %args;
318 }
319  
320 0 my $pager_fh;
321 0 ▶ if (! open $pager_fh, '>&', \*STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $pager_fh, ">&", \*STDOUT) 0 0 0
322 0 $args{status} = -1;
323 0 $args{error} = "cannot dup STDOUT: $!";
324 0 return %args;
325 }
326 0 print $pager_fh $text;
327 0 ▶ if ( !$pager_fh->close ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pager_fh->close) 0 0 0
328 0 $args{status} = -1;
329 0 $args{error} = $!;
330 }
331 0 return %args;
332 }
333  
334 6 1;
335  
336 __END__
« CLI/Argument/Filename.pm ↑ Summary CLI/Argument/Number.pm »