CLI/Command.pm

filter: Statementstmt 99.4% 171 / 172 Branchbran 96.7% 60 / 62 Conditioncond 94.1% 32 / 34 MC/DCmcdc 90.0% 18 / 20 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 97.7% 298 / 305 CC 49 SCAR 23.3
CC
49
Cov
98%
complete
19.0
_check_arguments
10.0
execute_command
9.0
CRAP 10.3
« CLI/Role/HelpText.pm ↑ Summary CLI/Argument/Filename.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Command
4 #
5 # Description: Class for (sub-)commands in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <cpanman@monkey-mind.net>
8 # Created: 30/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::Command 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 List::Util 1.23 qw( first min );
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use Types::Standard 1.000005 qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
28 ArrayRef
29 CodeRef
30 InstanceOf
31 Maybe
32 Str
33 7 );
34  
35 7 ▶use Term::CLI::L10N qw( loc );
Subroutine BEGIN: called CC=1 SCAR=0.0
36 7 ▶use Term::CLI::Util qw( is_prefix_str get_options_from_array );
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 7 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 7 extends 'Term::CLI::Element';
42  
43 7 has options => (
44 is => 'rw',
45 isa => Maybe [ ArrayRef [Str] ],
46 predicate => 1
47 );
48  
49 7 with qw(
50 Term::CLI::Role::CommandSet
51 Term::CLI::Role::ArgumentSet
52 Term::CLI::Role::HelpText
53 Term::CLI::Role::State
54 );
55  
56 10 ▶sub option_names {
Subroutine option_names: called CC=6 SCAR=17.9
Pod documented
57 10 my ($self) = @_;
58 10 ▶ my $opt_specs = $self->options or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $opt_specs = $self->options or return () 9 1 10
59 9 my @names;
60 9 for my $spec ( @{$opt_specs} ) {
61 24 my ($name_spec, $opts) = $spec =~ m{^ ([^!+=:]+) (.*) $}xms;
62 24 my $negatable = index($opts, '!') >= 0;
63 24 for my $opt_name ( split( qr{\|}x, $name_spec) ) {
64 48 ▶ if (length $opt_name > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $opt_name > 1) { } 24 24 48
65 24 push @names, "--$opt_name";
66 24 ▶ push @names, "--no-$opt_name" if $negatable;
Branch: 1 branch
BranchTrueFalseTotal
if $negatable 0 24 24
67 }
68 else {
69 24 push @names, "-$opt_name";
70 }
71 }
72 }
73 9 return @names;
74 }
75  
76 51 ▶sub complete {
Subroutine complete: called CC=19 SCAR=29.4
Pod documented
77 51 my ( $self, $text, $state ) = @_;
78  
79 51 ▶ $text //= q{};
Condition100%2/2
$text //= ""
l!l
501
MC/DC100%1/1
$text //= ""
$text
Truth table100%2/2
A
A= $text
Aresult
11
00
80 51 ▶ $state //= {};
Condition100%2/2
$state //= {}
l!l
483
MC/DC100%1/1
$state //= {}
$state
Truth table100%2/2
A
A= $state
Aresult
11
00
81  
82 51 ▶ my $processed = $state->{processed} //= [];
Condition100%2/2
$state->{"processed"} //= []
l!l
3516
MC/DC100%1/1
$state->{"processed"} //= []
$state->{"processed"}
Truth table100%2/2
A
A= $state->{"processed"}
Aresult
11
00
83 51 ▶ my $unprocessed = $state->{unprocessed} //= [];
Condition100%2/2
$state->{"unprocessed"} //= []
l!l
483
MC/DC100%1/1
$state->{"unprocessed"} //= []
$state->{"unprocessed"}
Truth table100%2/2
A
A= $state->{"unprocessed"}
Aresult
11
00
84 51 ▶ my $parsed_options = $state->{options} //= {};
Condition100%2/2
$state->{"options"} //= {}
l!l
3516
MC/DC100%1/1
$state->{"options"} //= {}
$state->{"options"}
Truth table100%2/2
A
A= $state->{"options"}
Aresult
11
00
85  
86 51 ▶ if ( $self->has_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_options) 24 27 51
87  
88 24 my %opt_result = get_options_from_array(
89 args => $unprocessed,
90 spec => $self->options,
91 result => $parsed_options,
92 pass_through => 1,
93 );
94  
95 24 my $double_dash = $opt_result{double_dash};
96  
97 # Check if we have to complete a command-line option.
98 24 ▶ if ( !$double_dash && @{$unprocessed} == 0 && $text =~ /^-/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux) 5 19 24
Condition100%6/6
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
!ll&&!rl&&r
1095
not $double_dash and @{$unprocessed;} == 0
!ll&&!rl&&r
2814
MC/DC100%3/3
not $double_dash and @{$unprocessed;} == 0 and $text =~ /^-/ux
not $double_dash @{$unprocessed;} == 0 $text =~ /^-/ux
Truth table100%4/4
not(A) and B and C
A= not $double_dashB= @{$unprocessed;} == 0C= $text =~ /^-/ux
ABCresult
0XX0
10X0
1100
1111
99 5 return grep { is_prefix_str( $text, $_ ) } $self->option_names;
100 }
101 }
102  
103 # If the command has arguments, try to skip over them.
104 46 ▶ if ( $self->has_arguments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_arguments) 26 20 46
105 26 my @args = $self->arguments;
106 26 my $arg_repeat = 0;
107 26 ▶ while ( @{$unprocessed} && @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$unprocessed;} and @args) 43 26 69
Condition100%3/3
@{$unprocessed;} and @args
!ll&&!rl&&r
24243
MC/DC100%2/2
@{$unprocessed;} and @args
@{$unprocessed;} @args
Truth table100%3/3
A and B
A= @{$unprocessed;}B= @args
ABresult
0X0
100
111
108 43 push @{$processed}, {
109 element => $args[0],
110 43 value => shift @{$unprocessed},
111 };
112 43 $arg_repeat++;
113 43 ▶ if ( $args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur) 22 21 43
Condition100%3/3
$args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur
!ll&&!rl&&r
18322
MC/DC100%2/2
$args[0]->max_occur > 0 and $arg_repeat >= $args[0]->max_occur
$args[0]->max_occur > 0 $arg_repeat >= $args[0]->max_occur
Truth table100%3/3
A and B
A= $args[0]->max_occur > 0B= $arg_repeat >= $args[0]->max_occur
ABresult
0X0
100
111
114 22 shift @args;
115 22 $arg_repeat = 0;
116 }
117 }
118  
119 26 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 22 4 26
120 22 return $args[0]->complete( $text, $state );
121 }
122 }
123  
124 24 ▶ if ( $self->has_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_commands) 22 2 24
125 22 ▶ if ( @{$unprocessed} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$unprocessed;} == 0) 13 9 22
126 13 return grep { is_prefix_str( $text, $_ ) } $self->command_names;
127 }
128 9 ▶ if ( my $cmd = $self->find_command( $unprocessed->[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cmd = $self->find_command($unprocessed->[0])) 8 1 9
129 8 push @{$processed}, {
130 element => $cmd,
131 value => $cmd->name,
132 };
133 8 shift @{$unprocessed};
134 8 return $cmd->complete( $text, $state );
135 }
136 }
137  
138 3 return ();
139 }
140  
141 49 ▶sub execute_command {
Subroutine execute_command: called CC=9 SCAR=22.0
Pod documented
142 49 my ( $self, %args ) = @_;
143  
144 49 $args{status} = 0;
145 49 $args{error} = q{};
146  
147 # Dereference and copy arguments/unparsed/options to prevent
148 # unwanted side-effects.
149 49 $args{arguments} = [ @{ $args{arguments} } ];
150 49 $args{unprocessed} = [ @{ $args{unprocessed} } ];
151 49 $args{unparsed} = $args{unprocessed};
152 49 $args{processed} = [ @{ $args{processed} } ];
153 49 $args{options} = { %{ $args{options} } };
154 49 $args{command_path} = [ @{ $args{command_path} } ];
155  
156 49 push @{ $args{command_path} }, $self;
157  
158 49 ▶ if ( $self->has_options ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_options) 17 32 49
159 my %opt_result = get_options_from_array(
160 args => $args{unprocessed},
161 spec => $self->options,
162 result => $args{options},
163 17 pass_through => 0,
164 );
165  
166 17 ▶ if ( !$opt_result{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_result{"success"}) 2 15 17
167 2 $args{status} = -1;
168 2 $args{error} = $opt_result{error_msg};
169 2 return $self->try_callback(%args);
170 }
171 }
172  
173 47 ▶ if ( $self->has_arguments || !$self->has_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_arguments or not $self->has_commands) 42 5 47
Condition100%3/3
$self->has_arguments or not $self->has_commands
l!l&&r!l&&!r
4025
MC/DC100%2/2
$self->has_arguments or not $self->has_commands
$self->has_arguments not $self->has_commands
Truth table100%3/3
A or not(B)
A= $self->has_argumentsB= not $self->has_commands
ABresult
1X1
011
000
174 42 %args = $self->_check_arguments(%args);
175 42 ▶ return $self->try_callback(%args) if $args{status} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"status"} < 0 16 26 42
176 }
177  
178 31 ▶ return $self->try_callback(%args) if !$self->has_commands;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_commands 23 8 31
179  
180 0 ▶ if ( $self->require_sub_command || @{ $args{unprocessed} } > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->require_sub_command or @{$args{"unprocessed"};} > 0) 8 0 8
Condition33%1/3
$self->require_sub_command or @{$args{"unprocessed"};} > 0
l!l&&r!l&&!r
800
MC/DC0%0/2
$self->require_sub_command or @{$args{"unprocessed"};} > 0
$self->require_sub_command @{$args{"unprocessed"};} > 0
Truth table33%1/3
A or B
A= $self->require_sub_commandB= @{$args{"unprocessed"};} > 0
ABresult
1X1
011
000
181 8 %args = $self->_execute_sub_command(%args);
182 }
183 8 return $self->try_callback(%args);
184 }
185  
186 7 ▶sub _too_few_args_error {
Subroutine _too_few_args_error: called CC=5 SCAR=16.1
187 7 my ( $self, $arg_spec ) = @_;
188  
189 7 ▶ if ( $arg_spec->max_occur == $arg_spec->min_occur ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->max_occur == $arg_spec->min_occur) 2 5 7
190 2 ▶ if ( $arg_spec->min_occur == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->min_occur == 1) 1 1 2
191 1 return loc( "missing '[_1]' argument", $arg_spec->name );
192 }
193 1 return loc( "need [_1] '[_2]' [numerate,_1,argument]",
194 $arg_spec->min_occur, $arg_spec->name, );
195 }
196 5 ▶ if ( $arg_spec->max_occur - $arg_spec->min_occur == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->max_occur - $arg_spec->min_occur == 1) 1 4 5
197 1 return loc( "need [_1] or [_2] '[_3]' arguments",
198 $arg_spec->min_occur, $arg_spec->max_occur, $arg_spec->name, );
199 }
200 4 ▶ if ( $arg_spec->max_occur > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_spec->max_occur > 1) 1 3 4
201 1 return loc( "need between [_1] and [_2] '[_3]' arguments",
202 $arg_spec->min_occur, $arg_spec->max_occur, $arg_spec->name, );
203 }
204 3 return loc( "need at least [_1] '[_2]' [numerate,_1,argument]",
205 $arg_spec->min_occur, $arg_spec->name, );
206 }
207  
208 42 ▶sub _check_arguments {
Subroutine _check_arguments: called CC=10 SCAR=23.0
209 42 my ( $self, %args ) = @_;
210  
211 42 my $unprocessed = $args{unprocessed};
212 42 my $processed = $args{processed};
213  
214 42 my @arg_spec = $self->arguments;
215  
216 42 ▶ if ( @arg_spec == 0 and @{$unprocessed} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arg_spec == 0 and @{$unprocessed;} > 0) 1 41 42
Condition100%3/3
@arg_spec == 0 and @{$unprocessed;} > 0
!ll&&!rl&&r
4011
MC/DC100%2/2
@arg_spec == 0 and @{$unprocessed;} > 0
@arg_spec == 0 @{$unprocessed;} > 0
Truth table100%3/3
A and B
A= @arg_spec == 0B= @{$unprocessed;} > 0
ABresult
0X0
100
111
217 return (
218 1 %args,
219 status => -1,
220 error => loc('no arguments allowed'),
221 );
222 }
223  
224 41 my $argno = 0;
225 41 for my $arg_spec (@arg_spec) {
226 54 ▶ if ( @{$unprocessed} < $arg_spec->min_occur ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$unprocessed;} < $arg_spec->min_occur) 7 47 54
227 return (
228 7 %args,
229 status => -1,
230 error => $self->_too_few_args_error($arg_spec),
231 );
232 }
233  
234 my $args_to_check =
235 $arg_spec->max_occur > 0
236 29 ? min( $arg_spec->max_occur, scalar @{$unprocessed} )
237 47 ▶ : scalar @{$unprocessed};
Branch: 1 branch
BranchTrueFalseTotal
$arg_spec->max_occur > 0 ? : 29 18 47
238  
239 47 for my $i ( 1 .. $args_to_check ) {
240 63 my $arg = $unprocessed->[0];
241 63 $argno++;
242 63 my $arg_value = $arg_spec->validate($arg, \%args);
243 63 ▶ if ( !defined $arg_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_value) 4 59 63
244 return (
245 4 %args,
246 status => -1,
247 error => qq{arg#$argno (} . $arg_spec->name . qq{), "$arg": }
248 . $arg_spec->error
249 );
250 }
251 59 push @{ $args{arguments} }, $arg_value;
252 59 push @{ $processed }, { element => $arg_spec, value => $arg_value };
253 59 shift @{ $unprocessed };
254 }
255 }
256  
257 # At this point, we have processed all our arg_spec. The only way there
258 # are any elements left in @arguments is for the last arg_spec to have
259 # a max_occur that is exceeded. If the command has no sub-commands that
260 # is surely an error. If it does have sub-commands, we'll leave it to
261 # be parsed further.
262 30 ▶ if ( @{$unprocessed} > 0 && !$self->has_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$unprocessed;} > 0 and not $self->has_commands) 4 26 30
Condition100%3/3
@{$unprocessed;} > 0 and not $self->has_commands
!ll&&!rl&&r
2424
MC/DC100%2/2
@{$unprocessed;} > 0 and not $self->has_commands
@{$unprocessed;} > 0 not $self->has_commands
Truth table100%3/3
A and not(B)
A= @{$unprocessed;} > 0B= not $self->has_commands
ABresult
0X0
100
111
263 4 my $last_spec = $arg_spec[-1];
264 return (
265 4 %args,
266 status => -1,
267 error => loc(
268 "too many '[_1]' arguments (max. [_2])", $last_spec->name,
269 $last_spec->max_occur,
270 ),
271 );
272 }
273 26 return %args;
274 }
275  
276 8 ▶sub _execute_sub_command {
Subroutine _execute_sub_command: called CC=5 SCAR=16.1
277 8 my ( $self, %args ) = @_;
278  
279 8 my $unprocessed = $args{unprocessed};
280 8 my $processed = $args{processed};
281  
282 8 ▶ if ( @{$unprocessed} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$unprocessed;} == 0) 2 6 8
283 2 ▶ if ( scalar $self->commands == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->commands == 1) 1 1 2
284 1 my ($cmd) = $self->commands;
285 return (
286 1 %args,
287 status => -1,
288 error => loc( "incomplete command: missing '[_1]'", $cmd->name )
289 );
290 }
291 1 return ( %args, status => -1, error => loc("missing sub-command") );
292 }
293  
294 6 my $cmd_name = $unprocessed->[0];
295  
296 6 my $cmd = $self->find_command($cmd_name);
297  
298 6 ▶ if ( !$cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 2 4 6
299 2 ▶ if ( scalar $self->commands == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->commands == 1) 1 1 2
300 1 ($cmd) = $self->commands;
301 return (
302 1 %args,
303 status => -1,
304 error => loc(
305 "expected '[_1]' instead of '[_2]'", $cmd->name,
306 $cmd_name
307 ),
308 );
309 }
310 return (
311 1 %args,
312 status => -1,
313 error => loc( "unknown sub-command '[_1]'", $cmd_name )
314 );
315 }
316  
317 4 shift @{ $unprocessed };
318 4 push @{ $processed }, { element => $cmd, value => $cmd->name };
319 4 return $cmd->execute_command(%args);
320 }
321  
322 7 1;
323  
324 __END__
« CLI/Role/HelpText.pm ↑ Summary CLI/Argument/Filename.pm »