CLI/Role/CommandSet.pm

filter: Statementstmt 98.2% 164 / 167 Branchbran 82.1% 46 / 56 Conditioncond 69.5% 16 / 23 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 96.4% 27 / 28 Podpod 100% 12 / 12 totaltotal 90.3% 272 / 301 CC 40 SCAR 14.8
CC
40
Cov
92%
_get_command_list
12.1
readline
6.0
_trigger__command_list
5.1
CRAP 4.4
« CLI/L10N/nl.pm ↑ Summary CLI/Argument/Tree.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::CommandSet
4 #
5 # Description: Class for sets of (sub-)commands in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 05/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::CommandSet 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 Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
29 7 ▶use Term::CLI::Util qw( find_obj_name_matches );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 7 ▶use Types::Standard 1.000005 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
32 ArrayRef
33 Bool
34 CodeRef
35 InstanceOf
36 ConsumerOf
37 Maybe
38 7 );
39  
40 7 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 7 my $ERROR_STATUS = -1;
44  
45 7 requires qw( parent root_node );
46  
47 7 has '+parent' => (
48 is => 'rwp',
49 weak_ref => 1,
50 isa => Maybe[ ConsumerOf ['Term::CLI::Role::CommandSet'] ],
51 );
52  
53 has _command_list => (
54 is => 'rw',
55 isa => Maybe [ArrayRef|CodeRef],
56 init_arg => 'commands',
57 writer => '_set_command_list',
58 trigger => 1,
59 14 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
60 14 my ($arg) = @_;
61 14 ▶ if (ref $arg && reftype $arg eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg and builtin::reftype($arg) eq "ARRAY") 13 1 14
Condition66%2/3
ref $arg and builtin::reftype($arg) eq "ARRAY"
!ll&&!rl&&r
0113
MC/DC50%1/2
ref $arg and builtin::reftype($arg) eq "ARRAY"
ref $arg builtin::reftype($arg) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $argB= builtin::reftype($arg) eq "ARRAY"
ABresult
0X0
100
111
62 # clone and sort array.
63 13 return [ sort { $a->name cmp $b->name } @{$arg} ];
64 }
65 1 return $arg;
66 }
67 7 );
68  
69 7 has callback => (
70 is => 'rw',
71 isa => Maybe [CodeRef],
72 predicate => 1
73 );
74  
75 has require_sub_command => (
76 is => 'ro',
77 isa => Bool,
78 54 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 7 );
80  
81 # $self->_set_command_list($ref) => $self->_trigger__command_list($ref);
82 #
83 # Trigger to run whenever the object's _commands array ref is set.
84 #
85 14 ▶sub _trigger__command_list { ## no critic (ProhibitUnusedPrivateSubroutines)
Subroutine _trigger__command_list: called CC=5 SCAR=16.2
86 14 my ( $self, $cmd_list ) = @_;
87  
88 14 ▶ return if !$cmd_list;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd_list 0 14 14
89  
90 # Set the parent for each command object.
91 14 ▶ if (ref $cmd_list && reftype $cmd_list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd_list and builtin::reftype($cmd_list) eq "ARRAY") 13 1 14
Condition66%2/3
ref $cmd_list and builtin::reftype($cmd_list) eq "ARRAY"
!ll&&!rl&&r
0113
MC/DC50%1/2
ref $cmd_list and builtin::reftype($cmd_list) eq "ARRAY"
ref $cmd_list builtin::reftype($cmd_list) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $cmd_listB= builtin::reftype($cmd_list) eq "ARRAY"
ABresult
0X0
100
111
92 13 for my $cmd_obj ( @{$cmd_list} ) {
93 27 $cmd_obj->_set_parent($self);
94 }
95 }
96 14 return;
97 }
98  
99 # Get the command list reference, expand a CODE ref if necessary.
100 255 ▶sub _get_command_list {
Subroutine _get_command_list: called CC=8 SCAR=24.9
101 255 my ($self) = @_;
102  
103 255 my $command_list = $self->_command_list;
104  
105 255 ▶ return undef if !ref $command_list;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $command_list 61 194 255
106  
107 194 ▶ return $command_list if reftype $command_list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::reftype($command_list) eq "ARRAY" 193 1 194
108  
109 1 ▶ if (reftype $command_list ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($command_list) ne "CODE") 0 1 1
110 0 croak "internal error: 'command' ($command_list) is ",
111 "neither a CodeRef nor an ArrayRef!";
112 }
113  
114 1 $command_list = $command_list->($self);
115  
116 1 ▶ if (!ref $command_list || reftype $command_list ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $command_list or builtin::reftype($command_list) ne "ARRAY") 0 1 1
Condition33%1/3
not ref $command_list or builtin::reftype($command_list) ne "ARRAY"
l!l&&r!l&&!r
001
MC/DC0%0/2
not ref $command_list or builtin::reftype($command_list) ne "ARRAY"
not ref $command_list builtin::reftype($command_list) ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $command_listB= builtin::reftype($command_list) ne "ARRAY"
ABresult
1X1
011
000
117 0 ▶ croak "'command' CodeRef should return an ARRAY ref, not ",
Branch: 2 branches
BranchTrueFalseTotal
ref $command_list ? : 0 0 0
defined $command_list ? : 0 0 0
118 ref $command_list ? reftype( $command_list ) :
119 defined $command_list ? "'$command_list'" :
120 '(undef)';
121 }
122  
123 1 return $self->_set_command_list($command_list);
124 }
125  
126 14 ▶sub commands {
Subroutine commands: called CC=2 SCAR=7.0
Pod documented
127 14 my ($self) = @_;
128 14 ▶ my $command_list = $self->_get_command_list // [];
Condition50%1/2
$self->_get_command_list // []
l!l
140
MC/DC0%0/1
$self->_get_command_list // []
$self->_get_command_list
Truth table50%1/2
A
A= $self->_get_command_list
Aresult
11
00
129 14 return @{ $command_list };
130 }
131  
132 24 ▶sub command_names {
Subroutine command_names: called CC=2 SCAR=6.9
Pod documented
133 24 my ($self) = @_;
134 24 ▶ my $command_list = $self->_get_command_list or return;
Branch: 1 branch
BranchTrueFalseTotal
my $command_list = $self->_get_command_list or return 23 1 24
135 23 my @l = map { $_->name } @{$command_list};
136 23 return @l;
137 }
138  
139 104 ▶sub has_commands {
Subroutine has_commands: called CC=2 SCAR=6.9
Pod documented
140 104 my ($self) = @_;
141 104 ▶ my $command_list = $self->_get_command_list or return !1;
Branch: 1 branch
BranchTrueFalseTotal
my $command_list = $self->_get_command_list or 47 57 104
142 47 return scalar( @{$command_list} ) > 0;
143 }
144  
145 3 ▶sub add_command {
Subroutine add_command: called CC=3 SCAR=11.0
Pod documented
146 3 my ( $self, @commands ) = @_;
147  
148 3 my $cmd_list = $self->_get_command_list;
149  
150 3 ▶ if (!$cmd_list) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd_list) 1 2 3
151 1 $cmd_list = \@commands;
152 1 $self->_set_command_list($cmd_list);
153 1 return $self;
154 }
155  
156 2 for my $cmd_obj ( @commands ) {
157 15 $cmd_obj->_set_parent( $self );
158 }
159  
160 2 push @{$cmd_list}, @commands;
161 2 @{$cmd_list} = sort { $a->name cmp $b->name } @{$cmd_list};
162  
163 2 return $self;
164 }
165  
166 1 ▶sub delete_command {
Subroutine delete_command: called CC=5 SCAR=16.1
Pod documented
167 1 my ( $self, @commands ) = @_;
168  
169 1 ▶ my $cmd_list = $self->_get_command_list or return;
Branch: 1 branch
BranchTrueFalseTotal
my $cmd_list = $self->_get_command_list or return 1 0 1
170  
171 1 ▶ my %to_delete = map { (ref $_ ? $_->name : $_ ) => 1 } @commands;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 1 1
172  
173 1 my @deleted;
174  
175 1 for my $index (reverse 0..$#{$cmd_list}) {
176 15 my $cmd_obj = $cmd_list->[$index];
177 15 ▶ if ( $to_delete{$cmd_obj->name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_delete{$cmd_obj->name}) 1 14 15
178 1 my $cmd_obj = splice @{$cmd_list}, $index, 1;
179 1 $cmd_obj->_set_parent(undef);
180 1 push @deleted, $cmd_obj;
181 }
182 }
183 1 return @deleted;
184 }
185  
186 2 ▶sub find_matches {
Subroutine find_matches: called CC=1 SCAR=0.0
Pod documented
187 2 my ( $self, $text ) = @_;
188 2 return find_obj_name_matches($text, $self->_get_command_list);
189 }
190  
191 107 ▶sub find_command {
Subroutine find_command: called CC=4 SCAR=13.9
Pod documented
192 107 my ( $self, $text ) = @_;
193  
194 107 my @matches = find_obj_name_matches(
195 $text, $self->_get_command_list, exact => 1 );
196  
197 107 ▶ return $self->set_error( loc( "unknown command '[_1]'", $text ) )
Branch: 1 branch
BranchTrueFalseTotal
if @matches == 0 8 99 107
198 if @matches == 0;
199  
200 99 ▶ return $matches[0] if @matches == 1 || $matches[0]->name eq $text;
Branch: 1 branch
BranchTrueFalseTotal
if @matches == 1 or $matches[0]->name eq $text 98 1 99
Condition66%2/3
@matches == 1 or $matches[0]->name eq $text
l!l&&r!l&&!r
9801
MC/DC50%1/2
@matches == 1 or $matches[0]->name eq $text
@matches == 1 $matches[0]->name eq $text
Truth table66%2/3
A or B
A= @matches == 1B= $matches[0]->name eq $text
ABresult
1X1
011
000
201  
202 return $self->set_error(
203 loc("ambiguous command '[_1]' (matches: [_2])",
204 $text,
205 1 join( ', ', map { $_->name } @matches )
206 )
207 );
208 }
209  
210 94 ▶sub try_callback {
Subroutine try_callback: called CC=3 SCAR=11.0
Pod documented
211 94 my ( $self, %args ) = @_;
212  
213 94 ▶ if ( $self->has_callback && defined $self->callback ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_callback and defined $self->callback) 29 65 94
Condition100%3/3
$self->has_callback and defined $self->callback
!ll&&!rl&&r
313429
MC/DC100%2/2
$self->has_callback and defined $self->callback
$self->has_callback defined $self->callback
Truth table100%3/3
A and B
A= $self->has_callbackB= defined $self->callback
ABresult
0X0
100
111
214 29 return $self->callback->( $self, %args );
215 }
216 65 return %args;
217 }
218  
219 # CLI->_set_completion_attribs();
220 #
221 # Set some attributes in the Term::ReadLine object related to
222 # custom completion.
223 #
224 47 ▶sub _set_completion_attribs {
Subroutine _set_completion_attribs: called CC=1 SCAR=0.0
225 47 my ($self) = @_;
226 47 my $root = $self->root_node;
227 47 my $term = $root->term;
228  
229  
230 # set Completion for current object
231 0 ▶ $term->Attribs->{completion_function} = sub { $self->complete_line( @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
232  
233 # Default: '"
234 47 $term->Attribs->{completer_quote_characters} = $root->quote_characters;
235  
236 # Default: \n\t\\"'`@$><=;|&{( and <space>
237 47 $term->Attribs->{completer_word_break_characters} = $root->word_delimiters;
238  
239 # Default: <space>
240 $term->Attribs->{completion_append_character} =
241 47 substr( $root->word_delimiters, 0, 1 );
242  
243 47 return;
244 }
245  
246 # See POD X<complete_line>
247 37 ▶sub complete_line {
Subroutine complete_line: called CC=5 SCAR=16.1
Pod documented
248 37 my ( $self, $text, $line, $start ) = @_;
249  
250 37 my $root = $self->root_node;
251  
252 37 $self->_set_completion_attribs;
253  
254 37 my $quote_char = $self->term->completion_quote_character;
255  
256 37 my @words;
257  
258 37 ▶ if ( $start > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > 0) 35 2 37
259 35 ▶ if ( length $quote_char ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $quote_char) 1 34 35
260  
261 # ReadLine thinks the $text to be completed is quoted.
262 # The quote character will precede the $start of $text.
263 # Make sure we do not include it in the text to break
264 # into words...
265 1 $start--;
266 }
267 35 ( my $err, @words ) =
268 $root->_split_line( substr( $line, 0, $start ) );
269 }
270  
271 37 my @list;
272  
273 37 $text = $root->unescape_input($text, $quote_char);
274  
275 37 ▶ if ( @words == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@words == 0) { } 2 35 37
elsif (my $cmd = $self->find_command($words[0])) { } 34 1 35
276 2 @list = map { $_->name } $self->find_matches( $text );
277 }
278 elsif ( my $cmd = $self->find_command( $words[0] ) ) {
279 34 shift @words;
280 34 @list = $cmd->complete(
281 $text => {
282 processed => [{
283 element => $cmd,
284 value => $cmd->name,
285 }],
286 unprocessed => \@words,
287 options => {},
288 }
289 );
290 }
291  
292 # Escape spaces/delimiters in reply again if necessary.
293 37 return map { $self->escape_input($_, $quote_char) } @list;
294 }
295  
296 3 ▶sub readline { ## no critic (ProhibitBuiltinHomonyms)
Subroutine readline: called CC=6 SCAR=18.0
Pod documented
297 3 my ( $self, %args ) = @_;
298  
299 3 my $root = $self->root_node;
300  
301 3 ▶ my $prompt = $args{prompt} // $self->prompt;
Condition66%2/3
$args{"prompt"} // $self->prompt
l!l&&r!l&&!r
210
MC/DC0%0/2
$args{"prompt"} // $self->prompt
$args{"prompt"} $self->prompt
Truth table66%2/3
A // B
A= $args{"prompt"}B= $self->prompt
ABresult
1X1
011
000
302 3 ▶ my $skip = exists $args{skip} ? $args{skip} : $root->skip;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'skip'} ? : 1 2 3
303  
304 3 $self->_set_completion_attribs;
305  
306 3 my $input;
307 3 ▶ while ( defined( $input = $root->term->readline($prompt) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($input = $root->term->readline($prompt))) 6 0 6
308 6 ▶ next if defined $skip && $input =~ $skip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $skip and $input =~ /$skip/u 3 3 6
Condition100%3/3
defined $skip and $input =~ /$skip/u
!ll&&!rl&&r
123
MC/DC100%2/2
defined $skip and $input =~ /$skip/u
defined $skip $input =~ /$skip/u
Truth table100%3/3
A and B
A= defined $skipB= $input =~ /$skip/u
ABresult
0X0
100
111
309 3 last;
310 }
311 3 return $input;
312 }
313  
314 # OBJ->_split_line( $text );
315 #
316 # Attempt to split $text into words. Use a custom split function if
317 # necessary.
318 #
319 86 ▶sub _split_line {
Subroutine _split_line: called CC=1 SCAR=0.0
320 86 my ( $self, $text ) = @_;
321 86 my $root_node = $self->root_node;
322 86 return $root_node->split_function->( $root_node, $text );
323 }
324  
325 39 ▶sub execute { return shift->execute_line(@_) } ## DEPRECATED
Subroutine execute: called CC=1 SCAR=0.0
Pod documented
326  
327 48 ▶sub execute_line {
Subroutine execute_line: called CC=4 SCAR=13.9
Pod documented
328 48 my ( $self, $cmd ) = @_;
329  
330 48 my ( $error, @cmd ) = $self->_split_line($cmd);
331  
332 48 my %args = (
333 status => 0,
334 error => q{},
335 command_line => $cmd,
336 command_path => [$self],
337 unprocessed => \@cmd,
338 processed => [],
339 options => {},
340 arguments => [],
341 );
342  
343 48 $args{unparsed} = $args{unprocessed};
344  
345 48 ▶ return $self->try_callback( %args, status => $ERROR_STATUS,
Branch: 1 branch
BranchTrueFalseTotal
if length $error 1 47 48
346 error => $error )
347 if length $error;
348  
349 47 ▶ if ( @cmd == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@cmd == 0) { } 1 46 47
elsif (my $cmd_ref = $self->find_command($cmd[0])) { } 45 1 46
350 1 $args{error} = loc("missing command");
351 1 $args{status} = $ERROR_STATUS;
352 }
353 elsif ( my $cmd_ref = $self->find_command( $cmd[0] ) ) {
354 45 my $cmd = shift @{$args{unprocessed}};
355 45 %args = $cmd_ref->execute_command(
356 %args,
357 processed => [ { element => $cmd_ref, value => $cmd } ],
358 );
359 }
360 else {
361 1 $args{error} = $self->error;
362 1 $args{status} = $ERROR_STATUS;
363 }
364  
365 44 return $self->try_callback(%args);
366 }
367  
368  
369 7 1;
370  
371 __END__
« CLI/L10N/nl.pm ↑ Summary CLI/Argument/Tree.pm »