blib/lib/Perinci/CmdLine/Base.pm

filter: Statementstmt 61.1% 558 / 912 Branchbran 44.4% 200 / 450 Conditioncond 51.2% 144 / 281 MC/DCmcdc 31.3% 47 / 150 Subroutinesub 70.3% 57 / 81 Podpod 50.0% 9 / 18 totaltotal 53.6% 1015 / 1892 CC 354 SCAR 47.7
CC
354
Cov
55%
select_output_handle
547.9
_plugin_run_event
302.9
do_completion
182.0
CRAP 117.5
« blib/lib/Perinci/CmdLine/Plugin/Run/Normal.pm ↑ Summary blib/lib/Perinci/CmdLine/Lite.pm »
1 ## no critic: ControlStructures::ProhibitUnreachableCode
2 package Perinci::CmdLine::Base;
3  
4 # put pragmas + Log::ger here
5 87 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # put other modules alphabetically here
11 87 ▶use IO::Interactive qw(is_interactive);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # put global variables alphabetically here
14 87 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 87 our $DATE = '2023-11-23'; # DATE
16 87 our $DIST = 'Perinci-CmdLine-Lite'; # DIST
17 87 our $VERSION = '1.929'; # VERSION
18  
19 # TODO: this class can actually be a role instead of base class for pericmd &
20 # pericmd-lite.
21  
22 87 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # BEGIN taken from Array::Iter
25 0 ▶sub __array_iter {
Subroutine __array_iter: not called CC=1 SCAR=6.9
26 0 my $ary = shift;
27 0 my $i = 0;
28 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
29 0 ▶ if ($i < @$ary) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$ary) { } 0 0 0
30 0 return $ary->[$i++];
31 } else {
32 0 return undef;
33 }
34 0 };
35 }
36  
37 0 ▶sub __list_iter {
Subroutine __list_iter: not called CC=1 SCAR=6.9
38 0 __array_iter([@_]);
39 }
40 # END from Array::Iter
41  
42 87 has actions => (is=>'rw');
43 87 has common_opts => (is=>'rw');
44 87 has completion => (is=>'rw');
45 87 has default_subcommand => (is=>'rw');
46 87 has get_subcommand_from_arg => (is=>'rw', default=>1);
47 87 has auto_abbrev_subcommand => (is=>'rw', default=>1);
48 87 has description => (is=>'rw');
49 87 has exit => (is=>'rw', default=>1);
50 87 has formats => (is=>'rw');
51 87 has default_format => (is=>'rw');
52 87 has allow_unknown_opts => (is=>'rw', default=>0);
53 87 has pass_cmdline_object => (is=>'rw', default=>0);
54 87 has per_arg_json => (is=>'rw');
55 87 has per_arg_yaml => (is=>'rw');
56 has program_name => (
57 is=>'rw',
58 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 my $pn = $ENV{PERINCI_CMDLINE_PROGRAM_NAME};
60 0 ▶ if (!defined($pn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pn) 0 0 0
61 0 $pn = $0; $pn =~ s!.+/!!;
62 }
63 0 $pn;
64 87 });
65 87 has riap_version => (is=>'rw', default=>1.1);
66 87 has riap_client => (is=>'rw');
67 87 has riap_client_args => (is=>'rw');
68 87 has subcommands => (is=>'rw');
69 87 has summary => (is=>'rw');
70 87 has tags => (is=>'rw');
71 87 has url => (is=>'rw');
72 87 has log => (is=>'rw', default => 0);
73 87 has log_level => (is=>'rw');
74  
75 87 has read_env => (is=>'rw', default=>1);
76 has env_name => (
77 is => 'rw',
78 lazy => 1,
79 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 2 my $self = shift;
81 2 __default_env_name($self->program_name);
82 },
83 87 );
84  
85 87 has read_config => (is=>'rw', default=>1);
86 87 has config_filename => (is=>'rw');
87 has config_dirs => (
88 is=>'rw',
89 73 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 73 require Perinci::CmdLine::Util::Config;
91 73 Perinci::CmdLine::Util::Config::get_default_config_dirs();
92 },
93 87 );
94  
95 has cleanser => (
96 is => 'rw',
97 lazy => 1,
98 2 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 2 require Data::Clean::ForJSON;
100 2 Data::Clean::ForJSON->get_cleanser;
101 },
102 87 );
103 87 has use_cleanser => (is=>'rw', default=>1);
104  
105 87 has extra_urls_for_version => (is=>'rw');
106  
107 87 has skip_format => (is=>'rw');
108  
109 has use_utf8 => (
110 is=>'rw',
111 85 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
112 85 ▶ $ENV{UTF8} // 0;
Condition50%1/2
$ENV{"UTF8"} // 0
l!l
085
MC/DC0%0/1
$ENV{"UTF8"} // 0
$ENV{"UTF8"}
Truth table50%1/2
A
A= $ENV{"UTF8"}
Aresult
11
00
113 },
114 87 );
115  
116 87 has use_locale => (
117 is=>'rw',
118 default => 0,
119 );
120  
121 87 has default_dry_run => (
122 is=>'rw',
123 default => 0,
124 );
125  
126 87 has plugins => (
127 is => 'rw',
128 );
129  
130 # role: requires 'default_prompt_template'
131  
132 # role: requires 'hook_before_run'
133 # role: requires 'hook_before_parse_argv'
134 # role: requires 'hook_before_read_config_file'
135 # role: requires 'hook_config_file_section'
136 # role: requires 'hook_after_read_config_file'
137 # role: requires 'hook_after_get_meta'
138 # role: requires 'hook_after_parse_argv'
139 # role: requires 'hook_before_action'
140 # role: requires 'hook_format_row' (for action=call)
141 # role: requires 'hook_after_action'
142 # role: requires 'hook_format_result'
143 # role: requires 'hook_display_result'
144  
145 # we put common stuffs here, but PC::Classic's final version will differ from
146 # PC::Lite's in several aspects: translation, supported output formats,
147 # PC::Classic currently adds some extra keys, some options are not added by
148 # PC::Lite (e.g. history/undo stuffs).
149 our %copts = (
150  
151 version => {
152 getopt => "version|v",
153 summary => "Display program's version and exit",
154 # XXX when option is changed, we need to update this. we should generate
155 # usage automatically instead.
156 usage => "--version (or -v)",
157 'usage.alt.fmt.pod' => qq{B<L<--version|/"--version, -v">> (or B<L<-v|/"--version, -v">>)},
158 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 1 my ($go, $val, $r) = @_;
160 1 $r->{action} = 'version';
161 1 $r->{skip_parse_subcommand_argv} = 1;
162 },
163 key => 'action',
164 },
165  
166 help => {
167 getopt => 'help|h|?',
168 summary => 'Display help message and exit',
169 # XXX when option is changed, we need to update this. we should generate
170 # usage automatically instead.
171 usage => "--help (or -h, -?)",
172 'usage.alt.fmt.pod' => qq{B<L<--help|/"--help, -h, -?">> (or B<L<-h|/"--help, -h, -?">>, B<L<-?|/"--help, -h, -?">>)},
173 4 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
174 4 my ($go, $val, $r) = @_;
175 4 $r->{action} = 'help';
176 4 $r->{skip_parse_subcommand_argv} = 1;
177 },
178 order => 0, # high
179 key => 'action',
180 },
181  
182 format => {
183 getopt => 'format=s',
184 summary => 'Choose output format, e.g. json, text',
185 description => <<'_',
186  
187 Output can be displayed in multiple formats, and a suitable default format is
188 chosen depending on the application and/or whether output destination is
189 interactive terminal (i.e. whether output is piped). This option specifically
190 chooses an output format.
191  
192 _
193 value_label => 'name',
194 3 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 3 my ($go, $val, $r) = @_;
196 3 $r->{format} = $val;
197 },
198 default => undef,
199 tags => ['category:output'],
200 is_settable_via_config => 1,
201 key => 'format',
202 },
203 json => {
204 getopt => 'json',
205 summary => 'Set output format to json',
206 10 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
207 10 my ($go, $val, $r) = @_;
208 10 ▶ $r->{format} = is_interactive(*STDOUT) ? 'json-pretty' : 'json';
Branch: 1 branch
BranchTrueFalseTotal
is_interactive(*STDOUT) ? : 0 10 10
209 },
210 tags => ['category:output'],
211 key => 'format',
212 },
213  
214 page_result => {
215 getopt => "page-result:s",
216 summary => "Filter output through a pager",
217 description => <<'_',
218  
219 This option will pipe the output to a specified pager program. If pager program
220 is not specified, a suitable default e.g. `less` is chosen.
221  
222 _
223 value_label => 'program',
224 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
225 0 my ($go, $val, $r) = @_;
226 0 $r->{page_result} = 1;
227 0 ▶ $r->{pager} = $val if length $val;
Branch: 1 branch
BranchTrueFalseTotal
if length $val 0 0 0
228 },
229 tags => ['category:output'],
230 key => 'send_output',
231 },
232  
233 view_result => {
234 getopt => "view-result:s",
235 summary => "View output using a viewer",
236 description => <<'_',
237  
238 This option will first save the output to a temporary file, then open a viewer
239 program to view the temporary file. If a viewer program is not chosen, a
240 suitable default, e.g. the browser, is chosen.
241  
242 _
243 value_label => 'program',
244 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
245 0 my ($go, $val, $r) = @_;
246 0 $r->{view_result} = 1;
247 0 ▶ $r->{viewer} = $val if length $val;
Branch: 1 branch
BranchTrueFalseTotal
if length $val 0 0 0
248 },
249 tags => ['category:output'],
250 key => 'send_output',
251 },
252  
253 naked_res => {
254 getopt => 'naked-res!',
255 summary => 'When outputing as JSON, strip result envelope',
256 'summary.alt.bool.not' => 'When outputing as JSON, add result envelope',
257 description => <<'_',
258  
259 By default, when outputing as JSON, the full enveloped result is returned, e.g.:
260  
261 [200,"OK",[1,2,3],{"func.extra"=>4}]
262  
263 The reason is so you can get the status (1st element), status message (2nd
264 element) as well as result metadata/extra result (4th element) instead of just
265 the result (3rd element). However, sometimes you want just the result, e.g. when
266 you want to pipe the result for more post-processing. In this case you can use
267 `--naked-res` so you just get:
268  
269 [1,2,3]
270  
271 _
272 3 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
273 3 my ($go, $val, $r) = @_;
274 3 ▶ $r->{naked_res} = $val ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 2 1 3
275 },
276 default => 0,
277 tags => ['category:output'],
278 is_settable_via_config => 1,
279 },
280  
281 subcommands => {
282 getopt => 'subcommands',
283 summary => 'List available subcommands',
284 # XXX when option is changed, we need to update this. we should generate
285 # usage automatically instead.
286 usage => "--subcommands",
287 'usage.alt.fmt.pod' => qq{B<L<--subcommands|/"--subcommands">>},
288 0 ▶ show_in_usage => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 my ($self, $r) = @_;
290 0 !$r->{subcommand_name};
291 },
292 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
293 1 my ($go, $val, $r) = @_;
294 1 $r->{action} = 'subcommands';
295 1 $r->{skip_parse_subcommand_argv} = 1;
296 },
297 key => 'action',
298 },
299  
300 # 'cmd=SUBCOMMAND_NAME' can be used to select other subcommands when
301 # default_subcommand is in effect.
302 cmd => {
303 getopt => "cmd=s",
304 summary => 'Select subcommand',
305 description => <<'_',
306  
307 This is useful if a default subcommand exists, where that subcommand will be
308 chosen if user does not specify another explicitly.
309  
310 _
311 value_label => 'subcommand_name',
312 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
313 1 my ($go, $val, $r) = @_;
314 1 $r->{subcommand_name} = $val;
315 1 $r->{subcommand_name_from} = '--cmd';
316 },
317 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
318 0 require Complete::Util;
319 0 my %args = @_;
320 0 my $cmdline = $args{cmdline};
321 Complete::Util::complete_array_elem(
322 0 array => [keys %{ $cmdline->list_subcommands }],
323 word => $args{word},
324 0 ci => 1,
325 );
326 },
327 },
328  
329 config_path => {
330 getopt => 'config-path=s@',
331 schema => ['array*', of => 'filename*'],
332 summary => 'Set path to configuration file',
333 description => <<'_',
334  
335 Can actually be specified multiple times to instruct application to read from
336 multiple configuration files (and merge them).
337  
338 _
339 value_label=>'path',
340 1 ▶ handler => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
341 1 my ($go, $val, $r) = @_;
342 1 ▶ $r->{config_paths} //= [];
Condition50%1/2
$r->{"config_paths"} //= []
l!l
01
MC/DC0%0/1
$r->{"config_paths"} //= []
$r->{"config_paths"}
Truth table50%1/2
A
A= $r->{"config_paths"}
Aresult
11
00
343 1 push @{ $r->{config_paths} }, $val;
344 },
345 tags => ['category:configuration'],
346 key => 'config_path',
347 },
348 no_config => {
349 getopt => 'no-config',
350 summary => 'Do not use any configuration file',
351 description => <<'_',
352  
353 If you specify `--no-config`, the application will not read any configuration
354 file.
355  
356 _
357 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
358 1 my ($go, $val, $r) = @_;
359 1 $r->{read_config} = 0;
360 },
361 tags => ['category:configuration'],
362 key => 'config_path',
363 },
364 no_env => {
365 getopt => 'no-env',
366 summary => 'Do not read environment for default options',
367 description => <<'_',
368  
369 If you specify `--no-env`, the application wil not read any environment
370 variable.
371  
372 _
373 1 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
374 1 my ($go, $val, $r) = @_;
375 1 $r->{read_env} = 0;
376 },
377 tags => ['category:environment'],
378 },
379 config_profile => {
380 getopt => 'config-profile=s',
381 summary => 'Set configuration profile to use',
382 description => <<'_',
383  
384 A single configuration file can contain profiles, i.e. alternative sets of
385 values that can be selected. For example:
386  
387 [profile=dev]
388 username=foo
389 pass=beaver
390  
391 [profile=production]
392 username=bar
393 pass=honey
394  
395 When you specify `--config-profile=dev`, `username` will be set to `foo` and
396 `password` to `beaver`. When you specify `--config-profile=production`,
397 `username` will be set to `bar` and `password` to `honey`.
398  
399 _
400 value_label=>'profile',
401 5 ▶ handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
402 5 my ($go, $val, $r) = @_;
403 5 $r->{config_profile} = $val;
404 },
405 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
406 # return list of profiles in read config file
407  
408 0 my %args = @_;
409 0 ▶ my $word = $args{word} // '';
Condition0%0/2
$args{"word"} // ""
l!l
00
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table0%0/2
A
A= $args{"word"}
Aresult
11
00
410 0 my $cmdline = $args{cmdline};
411 0 my $r = $args{r};
412  
413 # we are not called from cmdline, bail (actually we might want to
414 # return list of programs anyway, but we want to read the value of
415 # bash_global_dir et al)
416 0 ▶ return {message=>'No completion (not called from cmdline)'}
Branch: 1 branch
BranchTrueFalseTotal
unless $cmdline 0 0 0
417 unless $cmdline;
418  
419 # since this is common option, at this point we haven't parsed
420 # argument or even read config file. let's parse argv first (argv
421 # might set --config-path). then read the config files.
422 {
423 # this is not activated yet
424 0 $r->{read_config} = 1;
425  
426 0 my $res = $cmdline->parse_argv($r);
427 #return undef unless $res->[0] == 200;
428  
429 # parse_argv() might decide that it doesn't need to read config
430 # files (e.g. in the case of program having a subcommand and
431 # user does not specify any subcommand name, then it will
432 # shortcut to --help and set skip_parse_subcommand_argv=1). we
433 # don't want that here, we want to force reading config files:
434 0 ▶ $cmdline->_read_config($r) unless $r->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"} 0 0 0
435 }
436  
437 # we are not reading any config file, return empty list
438 return {message=>'No completion (not reading any config file)'}
439 0 ▶ unless $r->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"} 0 0 0
440  
441 0 my @profiles;
442 0 for my $section (keys %{$r->{config}}) {
443 0 my %keyvals;
444 0 ▶ for my $word (split /\s+/, ($section eq 'GLOBAL' ? '' : $section)) {
Branch: 1 branch
BranchTrueFalseTotal
$section eq 'GLOBAL' ? : 0 0 0
445 0 ▶ if ($word =~ /(.+)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /(.+)=(.*)/) { } 0 0 0
446 0 $keyvals{$1} = $2;
447 } else {
448 # old syntax, will be removed sometime in the future
449 0 $keyvals{subcommand} = $word;
450 }
451 }
452 0 ▶ if (defined(my $p = $keyvals{profile})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $p = $keyvals{"profile"})) 0 0 0
453 0 ▶ push @profiles, $p unless grep {$_ eq $p} @profiles;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $p;} @profiles 0 0 0
454 }
455 }
456  
457 0 require Complete::Util;
458 0 Complete::Util::complete_array_elem(
459 array=>\@profiles, word=>$word, ci=>1);
460 },
461 tags => ['category:configuration'],
462 },
463  
464 # since the cmdline opts is consumed, Log::Any::App doesn't see this. we
465 # currently work around this via setting env.
466 log_level => {
467 getopt => 'log-level=s',
468 summary => 'Set log level',
469 description => <<'_',
470  
471 By default, these log levels are available (in order of increasing level of
472 importance, from least important to most): `trace`, `debug`, `info`,
473 `warn`/`warning`, `error`, `fatal`. By default, the level is usually set to
474 `warn`, which means that log statements with level `info` and less important
475 levels will not be shown. To increase verbosity, choose `info`, `debug`, or
476 `trace`.
477  
478 For more details on log level and logging, as well as how new logging levels can
479 be defined or existing ones modified, see <pm:Log::ger>.
480  
481 _
482 schema => ['str*' => in => [
483 qw/trace debug info warn warning error fatal/]],
484 value_label=>'level',
485 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
486 0 my ($go, $val, $r) = @_;
487 0 $r->{log_level} = $val;
488 },
489 is_settable_via_config => 1,
490 tags => ['category:logging'],
491 key => 'log_level',
492 },
493 trace => {
494 getopt => "trace",
495 summary => "Shortcut for --log-level=trace",
496 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
497 0 my ($go, $val, $r) = @_;
498 0 $r->{log_level} = 'trace';
499 },
500 tags => ['category:logging'],
501 key => 'log_level',
502 },
503 debug => {
504 getopt => "debug",
505 summary => "Shortcut for --log-level=debug",
506 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
507 0 my ($go, $val, $r) = @_;
508 0 $r->{log_level} = 'debug';
509 },
510 tags => ['category:logging'],
511 key => 'log_level',
512 },
513 verbose => {
514 getopt => "verbose",
515 summary => "Shortcut for --log-level=info",
516 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
517 0 my ($go, $val, $r) = @_;
518 0 $r->{log_level} = 'info';
519 0 $r->{_help_verbose} = 1;
520 },
521 tags => ['category:logging'],
522 key => 'log_level',
523 },
524 quiet => {
525 getopt => "quiet",
526 summary => "Shortcut for --log-level=error",
527 0 ▶ handler => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
528 0 my ($go, $val, $r) = @_;
529 0 $r->{log_level} = 'error';
530 },
531 87 tags => ['category:logging'],
532 key => 'log_level',
533 },
534  
535 );
536  
537 87 our @CmdLine_Instances;
538  
539 85 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.5
Pod undocumented
540 85 my ($self, $args) = @_;
541  
542 85 push @CmdLine_Instances, $self;
543 85 ▶ if (@CmdLine_Instances > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Perinci::CmdLine::Base::CmdLine_Instances > 1) 0 85 85
544 0 warn "Multiple cmdline instances in a process can be problematic ".
545 "because plugins installed as handlers retain the cmdline object ".
546 "with which they were instantiated with. Please make sure you ".
547 "know what you are doing with multiple cmdline objects.";
548 }
549  
550 85 ▶ $self->{plugins} //= [];
Condition50%1/2
$self->{"plugins"} //= []
l!l
085
MC/DC0%0/1
$self->{"plugins"} //= []
$self->{"plugins"}
Truth table50%1/2
A
A= $self->{"plugins"}
Aresult
11
00
551 # always add these plugins
552 85 push @{ $self->{plugins} }, (
553 "Run::Normal",
554 "Run::Completion",
555 "Run::DumpObject",
556 );
557  
558 85 $self->_plugin_activate_plugins_in_env();
559 85 $self->_plugin_activate_plugins(@{ $self->{plugins} })
560 85 ▶ if $self->{plugins};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"plugins"} 85 0 85
561 }
562  
563 # plugin stuffs
564 87 our @Plugin_Instances;
565 87 our %Handlers; # key=event name, val=[ [$label, $prio, $handler, $epoch], ... ]
566  
567 87 our $tempfile_opt_suffix = '';
568  
569 87 my $r;
570  
571 468 ▶sub _plugin_run_event {
Subroutine _plugin_run_event: called CC=46 SCAR=57.1
572 468 my ($self, %args) = @_;
573  
574 468 my $name = $args{name};
575 468 ▶ if (log_is_trace()) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_is_trace()) 0 468 468
576 0 local $args{code} = '...';
577 0 local $args{r} = '...';
578 0 local $args{on_success} = '...';
579 0 local $args{on_failure} = '...';
580 0 log_trace "[pericmd] -> run_event(%s)", \%args;
581 }
582 468 ▶ defined $name or die "Please supply 'name'";
Branch: 1 branch
BranchTrueFalseTotal
defined $name or die "Please supply 'name'" 468 0 468
583 468 ▶ $Handlers{$name} ||= [];
Condition100%2/2
$Perinci::CmdLine::Base::Handlers{$name} ||= []
l!l
255213
MC/DC100%1/1
$Perinci::CmdLine::Base::Handlers{$name} ||= []
$Perinci::CmdLine::Base::Handlers{$name}
Truth table100%2/2
A
A= $Perinci::CmdLine::Base::Handlers{$name}
Aresult
11
00
584  
585 468 my $before_name = "before_$name";
586 468 ▶ $Handlers{$before_name} ||= [];
Condition100%2/2
$Perinci::CmdLine::Base::Handlers{$before_name} ||= []
l!l
170298
MC/DC100%1/1
$Perinci::CmdLine::Base::Handlers{$before_name} ||= []
$Perinci::CmdLine::Base::Handlers{$before_name}
Truth table100%2/2
A
A= $Perinci::CmdLine::Base::Handlers{$before_name}
Aresult
11
00
587  
588 468 my $after_name = "after_$name";
589 468 ▶ $Handlers{$after_name} ||= [];
Condition100%2/2
$Perinci::CmdLine::Base::Handlers{$after_name} ||= []
l!l
170298
MC/DC100%1/1
$Perinci::CmdLine::Base::Handlers{$after_name} ||= []
$Perinci::CmdLine::Base::Handlers{$after_name}
Truth table100%2/2
A
A= $Perinci::CmdLine::Base::Handlers{$after_name}
Aresult
11
00
590  
591 468 ▶ my $req_handler = $args{req_handler}; $req_handler = 0 unless defined $req_handler;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req_handler 468 0 468
592 468 ▶ my $run_all_handlers = $args{run_all_handlers}; $run_all_handlers = 1 unless defined $run_all_handlers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $run_all_handlers 468 0 468
593 468 ▶ my $allow_before_handler_to_cancel_event = $args{allow_before_handler_to_cancel_event}; $allow_before_handler_to_cancel_event = 1 unless defined $allow_before_handler_to_cancel_event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_before_handler_to_cancel_event 468 0 468
594 468 ▶ my $allow_before_handler_to_skip_rest = $args{allow_before_handler_to_skip_rest}; $allow_before_handler_to_skip_rest = 1 unless defined $allow_before_handler_to_skip_rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_before_handler_to_skip_rest 468 0 468
595 468 ▶ my $allow_handler_to_skip_rest = $args{allow_handler_to_skip_rest}; $allow_handler_to_skip_rest = 1 unless defined $allow_handler_to_skip_rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_handler_to_skip_rest 468 0 468
596 468 ▶ my $allow_handler_to_repeat_event = $args{allow_handler_to_repeat_event}; $allow_handler_to_repeat_event = 1 unless defined $allow_handler_to_repeat_event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_handler_to_repeat_event 468 0 468
597 468 ▶ my $allow_after_handler_to_repeat_event = $args{allow_after_handler_to_repeat_event}; $allow_after_handler_to_repeat_event = 1 unless defined $allow_after_handler_to_repeat_event;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_after_handler_to_repeat_event 468 0 468
598 468 ▶ my $allow_after_handler_to_skip_rest = $args{allow_after_handler_to_skip_rest}; $allow_after_handler_to_skip_rest = 1 unless defined $allow_after_handler_to_skip_rest;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allow_after_handler_to_skip_rest 468 0 468
599 468 ▶ my $stop_after_first_handler_failure = $args{stop_after_first_handler_failure}; $stop_after_first_handler_failure = 1 unless defined $stop_after_first_handler_failure;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stop_after_first_handler_failure 468 0 468
600  
601 468 my ($res, $is_success);
602  
603 RUN_BEFORE_EVENT_HANDLERS:
604 {
605 468 ▶ last if $name =~ /\A(after|before)_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A(after|before)_/ 0 468 468
606 468 local $r->{event} = $before_name;
607 468 my $i = 0;
608 468 for my $rec (@{ $Handlers{$before_name} }) {
609 0 $i++;
610 0 my ($label, $prio, $handler) = @$rec;
611 log_trace "[pericmd] [event %s] [%d/%d] -> handler %s ...",
612 0 $before_name, $i, scalar(@{ $Handlers{$before_name} }), $label;
613 0 $res = $handler->($r);
614 0 $is_success = $res->[0] =~ /\A[123]/;
615 log_trace "[pericmd] [event %s] [%d/%d] <- handler %s: %s (%s)",
616 0 ▶ $before_name, $i, scalar(@{ $Handlers{$before_name} }), $label,
Branch: 1 branch
BranchTrueFalseTotal
$is_success ? : 0 0 0
617 $res, $is_success ? "success" : "fail";
618 0 ▶ if ($res->[0] == 601) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 601) 0 0 0
619 0 ▶ if ($allow_before_handler_to_cancel_event) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_before_handler_to_cancel_event) { } 0 0 0
620 0 log_trace "[pericmd] Cancelling event $name (status 601)";
621 0 goto RETURN;
622 } else {
623 0 die "$before_name handler returns 601 when allow_before_handler_to_cancel_event is set to false";
624 }
625 }
626 0 ▶ if ($res->[0] == 201) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 201) 0 0 0
627 0 ▶ if ($allow_before_handler_to_skip_rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_before_handler_to_skip_rest) { } 0 0 0
628 0 log_trace "[pericmd] Skipping the rest of the $before_name handlers (status 201)";
629 0 last RUN_BEFORE_EVENT_HANDLERS;
630 } else {
631 0 log_trace "[pericmd] $before_name handler returns 201, but we ignore it because allow_before_handler_to_skip_rest is set to false";
632 }
633 }
634 }
635 }
636  
637 RUN_EVENT_HANDLERS:
638 {
639 468 local $r->{event} = $name;
640 468 my $i = 0;
641 468 $res = [304, "There is no handler for event $name"];
642 468 $is_success = 1;
643 468 ▶ if ($req_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_handler) 0 468 468
644 die "There is no handler for event $name"
645 0 ▶ unless @{ $Handlers{$name} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$Handlers{$name};} 0 0 0
646 }
647  
648 468 for my $rec (@{ $Handlers{$name} }) {
649 249 $i++;
650 249 my ($label, $prio, $handler) = @$rec;
651 log_trace "[pericmd] [event %s] [%d/%d] -> handler %s ...",
652 249 $name, $i, scalar(@{ $Handlers{$name} }), $label;
653 249 $res = $handler->($r);
654 167 $is_success = $res->[0] =~ /\A[123]/;
655 log_trace "[pericmd] [event %s] [%d/%d] <- handler %s: %s (%s)",
656 167 ▶ $name, $i, scalar(@{ $Handlers{$name} }), $label,
Branch: 1 branch
BranchTrueFalseTotal
$is_success ? : 167 0 167
657 $res, $is_success ? "success" : "fail";
658 167 ▶ last RUN_EVENT_HANDLERS if $is_success && !$run_all_handlers;
Branch: 1 branch
BranchTrueFalseTotal
if $is_success and not $run_all_handlers 0 167 167
Condition33%1/3
$is_success and not $run_all_handlers
!ll&&!rl&&r
01670
MC/DC0%0/2
$is_success and not $run_all_handlers
$is_success not $run_all_handlers
Truth table33%1/3
A and not(B)
A= $is_successB= not $run_all_handlers
ABresult
0X0
100
111
659 167 ▶ if ($res->[0] == 601) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 601) 0 167 167
660 0 die "$name handler is not allowed to return 601";
661 }
662 167 ▶ if ($res->[0] == 602) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 602) 0 167 167
663 0 ▶ if ($allow_handler_to_repeat_event) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_handler_to_repeat_event) { } 0 0 0
664 0 log_trace "[pericmd] Repeating event $name (handler returns 602)";
665 0 goto RUN_EVENT_HANDLERS;
666 } else {
667 0 die "$name handler returns 602 when allow_handler_to_repeat_event is set to false";
668 }
669 }
670 167 ▶ if ($res->[0] == 201) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 201) 3 164 167
671 3 ▶ if ($allow_handler_to_skip_rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_handler_to_skip_rest) { } 3 0 3
672 3 log_trace "[pericmd] Skipping the rest of the $name handlers (status 201)";
673 3 last RUN_EVENT_HANDLERS;
674 } else {
675 0 log_trace "[pericmd] $name handler returns 201, but we ignore it because allow_handler_to_skip_rest is set to false";
676 }
677 }
678 164 ▶ last RUN_EVENT_HANDLERS if !$is_success && $stop_after_first_handler_failure;
Branch: 1 branch
BranchTrueFalseTotal
if not $is_success and $stop_after_first_handler_failure 0 164 164
Condition33%1/3
not $is_success and $stop_after_first_handler_failure
!ll&&!rl&&r
16400
MC/DC0%0/2
not $is_success and $stop_after_first_handler_failure
not $is_success $stop_after_first_handler_failure
Truth table33%1/3
not(A) and B
A= not $is_successB= $stop_after_first_handler_failure
ABresult
0X0
100
111
679 }
680 }
681  
682 386 ▶ if ($is_success && $args{on_success}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_success and $args{'on_success'}) { } 383 3 386
elsif (not $is_success and $args{'on_failure'}) { } 0 6 6
Condition66%2/3
$is_success and $args{"on_success"}
!ll&&!rl&&r
03383
MC/DC50%1/2
$is_success and $args{"on_success"}
$is_success $args{"on_success"}
Truth table66%2/3
A and B
A= $is_successB= $args{"on_success"}
ABresult
0X0
100
111
683 383 log_trace "[pericmd] Running on_success ...";
684 383 $args{on_success}->($r);
685 ▶ } elsif (!$is_success && $args{on_failure}) {
Condition33%1/3
not $is_success and $args{"on_failure"}
!ll&&!rl&&r
300
MC/DC0%0/2
not $is_success and $args{"on_failure"}
not $is_success $args{"on_failure"}
Truth table33%1/3
not(A) and B
A= not $is_successB= $args{"on_failure"}
ABresult
0X0
100
111
686 0 log_trace "[pericmd] Running on_failure ...";
687 0 $args{on_failure}->($r);
688 }
689  
690 RUN_AFTER_EVENT_HANDLERS:
691 {
692 386 ▶ last if $name =~ /\A(after|before)_/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A(after|before)_/ 0 386 386
693 386 local $r->{event} = $after_name;
694 386 my $i = 0;
695 386 for my $rec (@{ $Handlers{$after_name} }) {
696 0 $i++;
697 0 my ($label, $prio, $handler) = @$rec;
698 log_trace "[pericmd] [event %s] [%d/%d] -> handler %s ...",
699 0 $after_name, $i, scalar(@{ $Handlers{$after_name} }), $label;
700 0 $res = $handler->($r);
701 0 $is_success = $res->[0] =~ /\A[123]/;
702 log_trace "[pericmd] [event %s] [%d/%d] <- handler %s: %s (%s)",
703 0 ▶ $after_name, $i, scalar(@{ $Handlers{$after_name} }), $label,
Branch: 1 branch
BranchTrueFalseTotal
$is_success ? : 0 0 0
704 $res, $is_success ? "success" : "fail";
705 0 ▶ if ($res->[0] == 602) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 602) 0 0 0
706 0 ▶ if ($allow_after_handler_to_repeat_event) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_after_handler_to_repeat_event) { } 0 0 0
707 0 log_trace "[pericmd] Repeating event $name (status 602)";
708 0 goto RUN_EVENT_HANDLERS;
709 } else {
710 0 die "$after_name handler returns 602 when allow_after_handler_to_repeat_event it set to false";
711 }
712 }
713 0 ▶ if ($res->[0] == 201) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->[0] == 201) 0 0 0
714 0 ▶ if ($allow_after_handler_to_skip_rest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_after_handler_to_skip_rest) { } 0 0 0
715 0 log_trace "[pericmd] Skipping the rest of the $after_name handlers (status 201)";
716 0 last RUN_AFTER_EVENT_HANDLERS;
717 } else {
718 0 log_trace "[pericmd] $after_name handler returns 201, but we ignore it because allow_after_handler_to_skip_rest is set to false";
719 }
720 }
721 }
722 }
723  
724 RETURN:
725 386 log_trace "[pericmd] <- run_event(name=%s)", $name;
726 386 undef;
727 }
728  
729 87 my $handler_seq = 0;
730 255 ▶sub _plugin_add_handler {
Subroutine _plugin_add_handler: called CC=3 SCAR=11.0
731 255 my ($self, $event, $label, $prio, $handler) = @_;
732  
733 # XXX check for known events?
734 255 ▶ $Handlers{$event} ||= [];
Condition100%2/2
$Handlers{$event} ||= []
l!l
17085
MC/DC100%1/1
$Handlers{$event} ||= []
$Handlers{$event}
Truth table100%2/2
A
A= $Handlers{$event}
Aresult
11
00
735  
736 # keep sorted
737 255 splice @{ $Handlers{$event} }, 0, scalar(@{ $Handlers{$event} }),
738 255 ▶ (sort { $a->[1] <=> $b->[1] || $a->[3] <=> $b->[3] } @{ $Handlers{$event} },
Condition66%2/3
$Perinci::CmdLine::Base::a->[1] <=> $Perinci::CmdLine::Base::b->[1] || $Perinci::CmdLine::Base::a->[3] <=> $Perinci::CmdLine::Base::b->[3]
l!l&&r!l&&!r
255850
MC/DC0%0/2
$Perinci::CmdLine::Base::a->[1] <=> $Perinci::CmdLine::Base::b->[1] || $Perinci::CmdLine::Base::a->[3] <=> $Perinci::CmdLine::Base::b->[3]
$Perinci::CmdLine::Base::a->[1] <=> $Perinci::CmdLine::Base::b->[1] $Perinci::CmdLine::Base::a->[3] <=> $Perinci::CmdLine::Base::b->[3]
Truth table66%2/3
A || B
A= $Perinci::CmdLine::Base::a->[1] <=> $Perinci::CmdLine::Base::b->[1]B= $Perinci::CmdLine::Base::a->[3] <=> $Perinci::CmdLine::Base::b->[3]
ABresult
1X1
011
000
739 [$label, $prio, $handler, $handler_seq++]);
740 }
741  
742 255 ▶sub _plugin_activate_single {
Subroutine _plugin_activate_single: called CC=2 SCAR=7.0
743 255 my ($self, $plugin_name0, $args) = @_;
744  
745 255 ▶ my ($plugin_name, $wanted_event, $wanted_prio) =
Branch: 1 branch
BranchTrueFalseTotal
() = $plugin_name0 =~ /\A(\w+(?:::\w+)*)(?:\@(\w+)(?:\@(\d+))?)?\z/ or die "Invalid plugin name syntax, please use Foo::Bar or Foo::Bar\@event or Foo::Bar\@event\@prio\n" 255 0 255
746 $plugin_name0 =~ /\A(\w+(?:::\w+)*)(?:\@(\w+)(?:\@(\d+))?)?\z/
747 or die "Invalid plugin name syntax, please use Foo::Bar or ".
748 "Foo::Bar\@event or Foo::Bar\@event\@prio\n";
749  
750 255 local $r->{plugin_name} = $plugin_name;
751 255 local $r->{plugin_args} = $args;
752  
753 $self->_plugin_run_event(
754 name => 'activate_plugin',
755 255 ▶ on_success => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
756 255 my $package = "Perinci::CmdLine::Plugin::$plugin_name";
757 255 (my $package_pm = "$package.pm") =~ s!::!/!g;
758 255 log_trace "[pericmd] Loading module $package ...";
759 255 require $package_pm;
760 255 ▶ my $obj = $package->new(%{ $args || {} }, cmdline => $self);
Condition50%1/2
$args || {}
l!l
2550
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
761 255 $obj->activate($wanted_event, $wanted_prio);
762 },
763 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
764 0 die "Cannot activate plugin $plugin_name";
765 },
766 255 );
767 }
768  
769 0 ▶sub _plugin_unflatten_import {
Subroutine _plugin_unflatten_import: not called CC=11 SCAR=48.8
770 0 my ($self, $env, $what) = @_;
771  
772 0 ▶ $what ||= "import";
Condition0%0/2
$what ||= "import"
l!l
00
MC/DC0%0/1
$what ||= "import"
$what
Truth table0%0/2
A
A= $what
Aresult
11
00
773 0 my @imports;
774 my $plugin_name0;
775 0 my @plugin_args;
776  
777 0 ▶ my @elems = ref $env eq 'ARRAY' ? @$env : split /,/, $env;
Branch: 1 branch
BranchTrueFalseTotal
ref $env eq 'ARRAY' ? : 0 0 0
778 0 ▶ while (@elems) {
Branch: 1 branch
BranchTrueFalseTotal
if (@elems) 0 0 0
779 0 my $el = shift @elems;
780 # dash prefix to disambiguate between plugin name and arguments, e.g.
781 # '-PluginName,argname,argval,argname2,argval2,-Plugin2Name,...'
782 0 ▶ if ($el =~ /\A-(\w+(?:::\w+)*(?:\@.+)?)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el =~ /\A-(\w+(?:::\w+)*(?:\@.+)?)\z/) { } 0 0 0
783 0 ▶ if (defined $plugin_name0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plugin_name0) 0 0 0
784 0 push @imports, $plugin_name0;
785 0 ▶ push @imports, {@plugin_args} if @plugin_args;
Branch: 1 branch
BranchTrueFalseTotal
if @plugin_args 0 0 0
786 }
787 0 $plugin_name0 = $1;
788 0 @plugin_args = ();
789 0 ▶ if (!@elems) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@elems) 0 0 0
790 0 push @imports, $1;
791 }
792 } else {
793 0 ▶ die "Invalid syntax in $what, first element needs to be ".
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plugin_name0 0 0 0
794 "a plugin name (e.g. -Foo), not '$el'"
795 unless defined $plugin_name0;
796 0 push @plugin_args, $el;
797 0 ▶ if (!@elems) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@elems) 0 0 0
798 0 push @imports, $plugin_name0;
799 0 ▶ push @imports, {@plugin_args} if @plugin_args;
Branch: 1 branch
BranchTrueFalseTotal
if @plugin_args 0 0 0
800 }
801 }
802 }
803 0 @imports;
804 }
805  
806 85 ▶sub _plugin_activate_plugins {
Subroutine _plugin_activate_plugins: called CC=4 SCAR=14.0
807 85 my $self = shift;
808  
809 85 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 255 85 340
810 255 my $plugin_name0 = shift;
811 255 ▶ my $plugin_args = @_ && ref($_[0]) eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] eq 'HASH' ? : 0 255 255
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
851700
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
812 255 $self->_plugin_activate_single($plugin_name0, $plugin_args);
813 }
814 }
815  
816 87 my $has_read_plugins_env;
817 85 ▶sub _plugin_activate_plugins_in_env {
Subroutine _plugin_activate_plugins_in_env: called CC=4 SCAR=20.3
818 85 my $self = shift;
819  
820 85 ▶ last if $has_read_plugins_env;
Branch: 1 branch
BranchTrueFalseTotal
if $has_read_plugins_env 0 85 85
821  
822 READ_PERINCI_CMDLINE_PLUGINS:
823 {
824 85 my $env = $ENV{PERINCI_CMDLINE_PLUGINS};
825 85 ▶ last unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 85 0 85
826 0 log_trace "[pericmd] Reading env variable PERINCI_CMDLINE_PLUGINS ...";
827 0 ▶ if ($env =~ /\A\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env =~ /\A\[/) { } 0 0 0
828 0 my $imports = JSON::PP::decode_json($env);
829 0 $self->_plugin_active_plugins(@$imports);
830 } else {
831 0 $self->_plugin_activate_plugins($self->_plugin_unflatten_import($env, "PERINCI_CMDLINE_PLUGINS"));
832 }
833 0 $has_read_plugins_env++;
834 0 return;
835 }
836 }
837  
838 2 ▶sub __default_env_name {
Subroutine __default_env_name: called CC=4 SCAR=13.9
839 2 my ($prog) = @_;
840  
841 2 for ($prog) {
842 2 ▶ $_ //= "PROG"; # shouldn't happen
Condition50%1/2
$_ //= "PROG"
l!l
20
MC/DC0%0/1
$_ //= "PROG"
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
843 2 $_ = uc($_);
844 2 s/[^A-Z0-9]+/_/g;
845 2 ▶ $_ = "_$_" if /\A\d/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A\d/ 1 1 2
846 }
847 2 "${prog}_OPT";
848 }
849  
850 82 ▶sub hook_before_run {}
Subroutine hook_before_run: called CC=1 SCAR=0.0
Pod documented
851  
852 12 ▶sub hook_before_read_config_file {}
Subroutine hook_before_read_config_file: called CC=1 SCAR=0.0
Pod documented
853  
854 12 ▶sub hook_after_read_config_file {}
Subroutine hook_after_read_config_file: called CC=1 SCAR=0.0
Pod documented
855  
856 0 ▶sub hook_before_action {}
Subroutine hook_before_action: not called CC=1 SCAR=0.0
Pod documented
857  
858 67 ▶sub hook_after_action {}
Subroutine hook_after_action: called CC=1 SCAR=0.0
Pod documented
859  
860 98 ▶sub get_meta {
Subroutine get_meta: called CC=2 SCAR=7.0
Pod documented
861 98 my ($self, $r, $url) = @_;
862  
863 98 my $res = $self->riap_client->request(meta => $url);
864 98 ▶ die $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 98 98
865 98 my $meta = $res->[2];
866 98 $r->{meta} = $meta;
867 98 log_trace("[pericmd] Running hook_after_get_meta ...");
868 98 $self->hook_after_get_meta($r);
869 98 $meta;
870 }
871  
872 5 ▶sub get_program_and_subcommand_name {
Subroutine get_program_and_subcommand_name: called CC=3 SCAR=11.4
Pod undocumented
873 5 my ($self, $r) = @_;
874 my $res = ($self->program_name // "") . " " .
875 5 ▶ ($r->{subcommand_name} // "");
Condition50%2/4
$self->program_name // ""
l!l
50
$r->{"subcommand_name"} // ""
l!l
50
MC/DC0%0/1
$self->program_name // ""
$self->program_name
MC/DC0%0/1
$r->{"subcommand_name"} // ""
$r->{"subcommand_name"}
Truth table50%1/2
A
A= $self->program_name
Aresult
11
00
Truth table50%1/2
A
A= $r->{"subcommand_name"}
Aresult
11
00
876 5 $res =~ s/\s+$//;
877 5 $res;
878 }
879  
880 9 ▶sub get_subcommand_data {
Subroutine get_subcommand_data: called CC=3 SCAR=11.8
Pod undocumented
881 9 my ($self, $name) = @_;
882  
883 9 my $scs = $self->subcommands;
884 9 ▶ return undef unless $scs;
Branch: 1 branch
BranchTrueFalseTotal
unless $scs 0 9 9
885  
886 9 ▶ if (ref($scs) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scs eq 'CODE') { } 0 9 9
887 0 return $scs->($self, name=>$name, in_completion=>$r->{in_completion});
888 } else {
889 9 return $scs->{$name};
890 }
891 }
892  
893 4 ▶sub list_subcommands {
Subroutine list_subcommands: called CC=5 SCAR=18.9
Pod undocumented
894 4 my ($self) = @_;
895 4 ▶ return $self->{_cache_subcommands} if $self->{_cache_subcommands};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_cache_subcommands"} 0 4 4
896  
897 4 my $scs = $self->subcommands;
898 4 my $res;
899 4 ▶ if ($scs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scs) { } 4 0 4
900 4 ▶ if (ref($scs) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $scs eq "CODE") 0 4 4
901 0 $scs = $scs->($self, in_completion=>$r->{in_completion});
902 0 ▶ die [500, "BUG: Subcommands code didn't return a hashref"]
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scs eq "HASH" 0 0 0
903 unless ref($scs) eq 'HASH';
904 }
905 4 $res = $scs;
906 } else {
907 0 $res = {};
908 }
909 4 $self->{_cache_subcommands} = $res;
910 4 $res;
911 }
912  
913 81 ▶sub status2exitcode {
Subroutine status2exitcode: called CC=2 SCAR=6.9
Pod undocumented
914 81 my ($self, $status) = @_;
915 81 ▶ return 0 if $status =~ /^2..|304/;
Branch: 1 branch
BranchTrueFalseTotal
if $status =~ /^2..|304/ 66 15 81
916 15 $status - 300;
917 }
918  
919 85 ▶sub _detect_completion {
Subroutine _detect_completion: called CC=5 SCAR=18.7
920 85 my ($self, $r) = @_;
921  
922 85 ▶ if ($ENV{COMP_SHELL}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ENV{'COMP_SHELL'}) { } 0 85 85
elsif ($ENV{'COMP_LINE'}) { } 3 82 85
elsif ($ENV{'COMMAND_LINE'}) { } 0 82 82
923 0 $r->{shell} = $ENV{COMP_SHELL};
924 0 return 1;
925 } elsif ($ENV{COMP_LINE}) {
926 3 $r->{shell} = 'bash';
927 3 return 1;
928 } elsif ($ENV{COMMAND_LINE}) {
929 0 $r->{shell} = 'tcsh';
930 0 return 1;
931 }
932  
933 82 ▶ $r->{shell} //= 'bash';
Condition50%1/2
$r->{"shell"} //= "bash"
l!l
082
MC/DC0%0/1
$r->{"shell"} //= "bash"
$r->{"shell"}
Truth table50%1/2
A
A= $r->{"shell"}
Aresult
11
00
934 82 0;
935 }
936  
937 3 ▶sub _read_env {
Subroutine _read_env: called CC=3 SCAR=11.1
938 3 my ($self, $r) = @_;
939  
940 3 ▶ return [] unless $self->read_env;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->read_env 0 3 3
941 3 my $env_name = $self->env_name;
942 3 my $env = $ENV{$env_name};
943 3 log_trace("[pericmd] Checking env %s: %s", $env_name, $env);
944 3 ▶ return [] unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 0 3 3
945  
946 3 require Text::ParseWords;
947 3 my @words = Text::ParseWords::shellwords($env);
948 3 log_trace("[pericmd] Words from env: %s", \@words);
949 3 \@words;
950 }
951  
952 0 ▶sub do_completion {
Subroutine do_completion: not called CC=13 SCAR=52.0
Pod documented
953 0 my ($self, $r) = @_;
954  
955 0 local $r->{in_completion} = 1;
956  
957 0 my ($words, $cword);
958 0 ▶ if ($r->{shell} eq 'bash') {
Branch: 4 branches
BranchTrueFalseTotal
if ($r->{'shell'} eq 'bash') { } 0 0 0
elsif ($r->{'shell'} eq 'fish') { } 0 0 0
elsif ($r->{'shell'} eq 'tcsh') { } 0 0 0
elsif ($r->{'shell'} eq 'zsh') { } 0 0 0
959 0 require Complete::Bash;
960 0 require Encode;
961 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline(undef, undef, {truncate_current_word=>1}) };
962 0 ($words, $cword) = @{ Complete::Bash::join_wordbreak_words($words, $cword) };
963 0 $words = [map {Encode::decode('UTF-8', $_)} @$words];
964 } elsif ($r->{shell} eq 'fish') {
965 0 require Complete::Bash;
966 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline() };
967 } elsif ($r->{shell} eq 'tcsh') {
968 0 require Complete::Tcsh;
969 0 ($words, $cword) = @{ Complete::Tcsh::parse_cmdline() };
970 } elsif ($r->{shell} eq 'zsh') {
971 0 require Complete::Bash;
972 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline() };
973 } else {
974 0 die "Unsupported shell '$r->{shell}'";
975 }
976  
977 0 shift @$words; $cword--; # strip program name
978  
979 # @ARGV given by bash is messed up / different. during completion, we
980 # get ARGV from parsing COMP_LINE/COMP_POINT.
981 0 @ARGV = @$words;
982  
983 # check whether subcommand is defined. try to search from --cmd, first
984 # command-line argument, or default_subcommand.
985 0 $self->hook_before_parse_argv($r);
986 0 $self->_parse_argv1($r);
987  
988 0 ▶ if ($r->{read_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"read_env"}) 0 0 0
989 0 my $env_words = $self->_read_env($r);
990 0 unshift @ARGV, @$env_words;
991 0 $cword += @$env_words;
992 }
993  
994 #log_trace("ARGV=%s", \@ARGV);
995 #log_trace("words=%s", $words);
996  
997 # force format to text for completion, because user might type 'cmd --format
998 # blah -^'.
999 0 $r->{format} = 'text';
1000  
1001 0 my $scd = $r->{subcommand_data};
1002 0 ▶ my $meta = $self->get_meta($r, $scd->{url} // $self->{url});
Condition0%0/3
$scd->{"url"} // $self->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$scd->{"url"} // $self->{"url"}
$scd->{"url"} $self->{"url"}
Truth table0%0/3
A // B
A= $scd->{"url"}B= $self->{"url"}
ABresult
1X1
011
000
1003  
1004 0 ▶ my $subcommand_name_from = $r->{subcommand_name_from} // '';
Condition0%0/2
$r->{"subcommand_name_from"} // ""
l!l
00
MC/DC0%0/1
$r->{"subcommand_name_from"} // ""
$r->{"subcommand_name_from"}
Truth table0%0/2
A
A= $r->{"subcommand_name_from"}
Aresult
11
00
1005  
1006 0 require Perinci::Sub::Complete;
1007 my $compres = Perinci::Sub::Complete::complete_cli_arg(
1008 meta => $meta, # must be normalized
1009 words => $words,
1010 cword => $cword,
1011 common_opts => $self->common_opts,
1012 riap_server_url => $scd->{url},
1013 riap_uri => undef,
1014 riap_client => $self->riap_client,
1015 extras => {r=>$r, cmdline=>$self},
1016 func_arg_starts_at => ($subcommand_name_from eq 'arg' ? 1:0),
1017 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
1018 0 my %args = @_;
1019 0 my $type = $args{type};
1020  
1021 # user specifies custom completion routine, so use that first
1022 0 ▶ if ($self->completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->completion) 0 0 0
1023 0 my $res = $self->completion(%args);
1024 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
1025 }
1026 # if subcommand name has not been supplied and we're at arg#0,
1027 # complete subcommand name
1028 0 ▶ if ($self->subcommands &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0) 0 0 0
Condition0%0/9
$self->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0
!ll&&!rl&&r
000
$self->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg"
!ll&&!rl&&r
000
$self->subcommands and $subcommand_name_from ne "--cmd"
!ll&&!rl&&r
000
MC/DC0%0/4
$self->subcommands and $subcommand_name_from ne "--cmd" and $type eq "arg" and $args{"argpos"} == 0
$self->subcommands $subcommand_name_from ne "--cmd" $type eq "arg" $args{"argpos"} == 0
Truth table0%0/5
A and B and C and D
A= $self->subcommandsB= $subcommand_name_from ne "--cmd"C= $type eq "arg"D= $args{"argpos"} == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1029 $subcommand_name_from ne '--cmd' &&
1030 $type eq 'arg' && $args{argpos}==0) {
1031 0 require Complete::Util;
1032 0 my $subcommands = $self->list_subcommands;
1033 0 my @subc_names = keys %$subcommands;
1034 0 my @subc_summaries = map { $subcommands->{$_}{summary} }
1035 @subc_names;
1036 0 return Complete::Util::complete_array_elem(
1037 array => \@subc_names,
1038 summaries => \@subc_summaries,
1039 word => $words->[$cword]);
1040 }
1041  
1042 # otherwise let periscomp do its thing
1043 0 return undef;
1044 },
1045 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$subcommand_name_from eq 'arg' ? : 0 0 0
1046  
1047 0 my $formatted;
1048 0 ▶ if ($r->{shell} eq 'bash') {
Branch: 4 branches
BranchTrueFalseTotal
if ($r->{'shell'} eq 'bash') { } 0 0 0
elsif ($r->{'shell'} eq 'fish') { } 0 0 0
elsif ($r->{'shell'} eq 'tcsh') { } 0 0 0
elsif ($r->{'shell'} eq 'zsh') { } 0 0 0
1049 0 require Complete::Bash;
1050 0 $formatted = Complete::Bash::format_completion(
1051 $compres, {word=>$words->[$cword]});
1052 } elsif ($r->{shell} eq 'fish') {
1053 0 require Complete::Fish;
1054 0 $formatted = Complete::Fish::format_completion($compres);
1055 } elsif ($r->{shell} eq 'tcsh') {
1056 0 require Complete::Tcsh;
1057 0 $formatted = Complete::Tcsh::format_completion($compres);
1058 } elsif ($r->{shell} eq 'zsh') {
1059 0 require Complete::Zsh;
1060 0 $formatted = Complete::Zsh::format_completion($compres);
1061 }
1062  
1063 # to properly display unicode filenames
1064 0 $self->use_utf8(1);
1065  
1066 0 [200, "OK", $formatted,
1067 # these extra result are for debugging
1068 {
1069 "func.words" => $words,
1070 "func.cword" => $cword,
1071 "cmdline.skip_format" => 1,
1072 }];
1073 }
1074  
1075 12 ▶sub _read_config {
Subroutine _read_config: called CC=4 SCAR=15.9
1076 12 require Perinci::CmdLine::Util::Config;
1077  
1078 12 my ($self, $r) = @_;
1079  
1080 12 my $hook_section;
1081 12 ▶ if ($self->can("hook_config_file_section")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("hook_config_file_section")) 0 12 12
1082 0 ▶ $hook_section = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1083 0 my ($section_name, $section_content) = @_;
1084 0 $self->hook_config_file_section(
1085 $r, $section_name, $section_content);
1086 0 };
1087 }
1088  
1089 my $res = Perinci::CmdLine::Util::Config::read_config(
1090 config_paths => $r->{config_paths},
1091 12 config_filename => $self->config_filename,
1092 config_dirs => $self->config_dirs,
1093 program_name => $self->program_name,
1094 hook_section => $hook_section,
1095 );
1096 12 ▶ die $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 12 12
1097 12 $r->{config} = $res->[2];
1098 12 $r->{read_config_files} = $res->[3]{'func.read_files'};
1099 12 $r->{_config_section_read_order} = $res->[3]{'func.section_read_order'}; # we currently don't want to publish this request key
1100  
1101 12 ▶ if ($ENV{LOG_DUMP_CONFIG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"LOG_DUMP_CONFIG"}) 0 12 12
1102 0 log_trace "config: %s", $r->{config};
1103 0 log_trace "read_config_files: %s", $r->{read_config_files};
1104 }
1105 }
1106  
1107 34 ▶sub __min(@) {
Subroutine __min: called CC=3 SCAR=11.0
1108 34 my $m = $_[0];
1109 34 for (@_) {
1110 98 ▶ $m = $_ if $_ < $m;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $m 7 91 98
1111 }
1112 34 $m;
1113 }
1114  
1115 # straight copy of Wikipedia's "Levenshtein Distance"
1116 2 ▶sub __editdist {
Subroutine __editdist: called CC=6 SCAR=17.9
1117 2 my @a = split //, shift;
1118 2 my @b = split //, shift;
1119  
1120 # There is an extra row and column in the matrix. This is the distance from
1121 # the empty string to a substring of the target.
1122 2 my @d;
1123 2 $d[$_][0] = $_ for 0 .. @a;
1124 2 $d[0][$_] = $_ for 0 .. @b;
1125  
1126 2 for my $i (1 .. @a) {
1127 12 for my $j (1 .. @b) {
1128 36 ▶ $d[$i][$j] = (
Branch: 1 branch
BranchTrueFalseTotal
$a[$i - 1] eq $b[$j - 1] ? : 6 30 36
1129 $a[$i-1] eq $b[$j-1]
1130 ? $d[$i-1][$j-1]
1131 : 1 + __min(
1132 $d[$i-1][$j],
1133 $d[$i][$j-1],
1134 $d[$i-1][$j-1]
1135 )
1136 );
1137 }
1138 }
1139  
1140 2 $d[@a][@b];
1141 }
1142  
1143 0 ▶sub __uniq {
Subroutine __uniq: not called CC=2 SCAR=17.9
1144 0 my %seen = ();
1145 0 my $k;
1146 my $seen_undef;
1147 0 ▶ grep { defined $_ ? not $seen{ $k = $_ }++ : not $seen_undef++ } @_;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
1148 }
1149  
1150 # $cut is whether we should only compare each element of haystack up to the
1151 # length of needle, so if needle is "foo" and haystack is ["bar", "baroque",
1152 # "fizzle", "food"], it will be as if haystack is ["bar", "bar", "fiz", "foo"].
1153 1 ▶sub __find_similar_strings {
Subroutine __find_similar_strings: called CC=5 SCAR=16.9
1154 1 my ($needle, $haystack, $cut) = @_;
1155  
1156 1 my $factor = 1.5;
1157 1 my $max_dist = 4;
1158  
1159 my @res =
1160 1 map { $_->[0] }
1161 0 sort { $a->[1] <=> $b->[1] }
1162 2 grep { defined }
1163 map {
1164 1 my $el = $_;
1165 2 ▶ if ($cut && length($_) > length($needle)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cut and length $_ > length $needle) 0 2 2
Condition33%1/3
$cut and length $_ > length $needle
!ll&&!rl&&r
200
MC/DC0%0/2
$cut and length $_ > length $needle
$cut length $_ > length $needle
Truth table33%1/3
A and B
A= $cutB= length $_ > length $needle
ABresult
0X0
100
111
1166 0 $el = substr($el, 0, length($needle));
1167 }
1168 2 my $d = __editdist($el, $needle);
1169 2 my $max_distance = __min(
1170 __min(length($el), length($needle))/$factor,
1171 $max_dist,
1172 );
1173 2 ▶ ($d <= $max_distance) ? [$_, $d] : undef
Branch: 1 branch
BranchTrueFalseTotal
$d <= $max_distance ? : 1 1 2
1174 } @$haystack;
1175  
1176 1 ▶ $cut ? __uniq(@res) : @res;
Branch: 1 branch
BranchTrueFalseTotal
$cut ? : 0 1 1
1177 }
1178  
1179 0 ▶sub __find_similar_go_opts {
Subroutine __find_similar_go_opts: not called CC=7 SCAR=40.3
1180 0 my ($opt, $go_spec) = @_;
1181  
1182 0 $opt =~ s/^--?//;
1183  
1184 my @ospecs0 = ref($go_spec) eq 'ARRAY' ?
1185 0 ▶ keys(%{ { @$go_spec } }) : keys(%$go_spec);
Branch: 1 branch
BranchTrueFalseTotal
ref $go_spec eq 'ARRAY' ? : 0 0 0
1186 0 my @ospecs;
1187 0 for my $o (@ospecs0) {
1188 0 $o =~ s/^--?//;
1189 0 my $is_neg = $o =~ /\!$/;
1190 0 $o =~ s/[=:].+|[?+!]$//;
1191 0 for (split /\|/, $o) {
1192 0 ▶ if ($is_neg && length($_) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_neg and length $_ > 1) { } 0 0 0
Condition0%0/3
$is_neg and length $_ > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$is_neg and length $_ > 1
$is_neg length $_ > 1
Truth table0%0/3
A and B
A= $is_negB= length $_ > 1
ABresult
0X0
100
111
1193 0 push @ospecs, $_, "no$_", "no-$_";
1194 } else {
1195 0 push @ospecs, $_;
1196 }
1197 }
1198 }
1199  
1200 0 ▶ map { length($_) > 1 ? "--$_" : "-$_" }
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 0 0 0
1201 __find_similar_strings($opt, \@ospecs, "cut");
1202 }
1203  
1204 85 ▶sub _parse_argv1 {
Subroutine _parse_argv1: called CC=25 SCAR=38.7
1205 85 my ($self, $r) = @_;
1206  
1207 # parse common_opts which potentially sets subcommand
1208 85 my @go_spec;
1209 {
1210 # one small downside for this is that we cannot do autoabbrev here,
1211 # because we're not yet specifying all options here.
1212  
1213 85 require Getopt::Long;
1214 85 my $old_go_conf = Getopt::Long::Configure(
1215 'pass_through', 'no_ignore_case', 'no_auto_abbrev',
1216 'no_getopt_compat', 'gnu_compat', 'bundling');
1217 85 ▶ my $co = $self->common_opts // {};
Condition50%1/2
$self->common_opts // {}
l!l
850
MC/DC0%0/1
$self->common_opts // {}
$self->common_opts
Truth table50%1/2
A
A= $self->common_opts
Aresult
11
00
1218 85 for my $k (keys %$co) {
1219 30 ▶ push @go_spec, $co->{$k}{getopt} => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1220 30 my ($go, $val) = @_;
1221 30 $co->{$k}{handler}->($go, $val, $r);
1222 655 };
1223 }
1224 #log_trace("\@ARGV before parsing common opts: %s", \@ARGV);
1225 85 Getopt::Long::GetOptions(@go_spec);
1226 85 Getopt::Long::Configure($old_go_conf);
1227 #log_trace("\@ARGV after parsing common opts: %s", \@ARGV);
1228 }
1229  
1230 # select subcommand and fill subcommand data
1231 {
1232 85 my $scn = $r->{subcommand_name};
1233 85 my $scn_from = $r->{subcommand_name_from};
1234 85 ▶ if (!defined($scn) && defined($self->{default_subcommand})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $scn and defined $self->{"default_subcommand"}) 2 83 85
Condition100%3/3
not defined $scn and defined $self->{"default_subcommand"}
!ll&&!rl&&r
1822
MC/DC100%2/2
not defined $scn and defined $self->{"default_subcommand"}
not defined $scn defined $self->{"default_subcommand"}
Truth table100%3/3
not(A) and B
A= not defined $scnB= defined $self->{"default_subcommand"}
ABresult
0X0
100
111
1235 # get from default_subcommand
1236 2 ▶ if ($self->get_subcommand_from_arg == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get_subcommand_from_arg == 1) { } 2 0 2
elsif ($self->get_subcommand_from_arg == 2 and not @ARGV) { } 0 0 0
1237 2 $scn = $self->{default_subcommand};
1238 2 $scn_from = 'default_subcommand';
1239 ▶ } elsif ($self->get_subcommand_from_arg == 2 && !@ARGV) {
Condition0%0/3
$self->get_subcommand_from_arg == 2 and not @ARGV
!ll&&!rl&&r
000
MC/DC0%0/2
$self->get_subcommand_from_arg == 2 and not @ARGV
$self->get_subcommand_from_arg == 2 not @ARGV
Truth table0%0/3
A and not(B)
A= $self->get_subcommand_from_arg == 2B= not @ARGV
ABresult
0X0
100
111
1240 0 $scn = $self->{default_subcommand};
1241 0 $scn_from = 'default_subcommand';
1242 }
1243 }
1244 85 ▶ if (!defined($scn) && $self->{subcommands} && @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $scn and $self->{"subcommands"} and @ARGV) 6 79 85
Condition100%6/6
not defined $scn and $self->{"subcommands"} and @ARGV
!ll&&!rl&&r
7726
not defined $scn and $self->{"subcommands"}
!ll&&!rl&&r
3748
MC/DC100%3/3
not defined $scn and $self->{"subcommands"} and @ARGV
not defined $scn $self->{"subcommands"} @ARGV
Truth table100%4/4
not(A) and B and C
A= not defined $scnB= $self->{"subcommands"}C= @ARGV
ABCresult
0XX0
10X0
1100
1111
1245 # get from first command-line arg
1246 6 ▶ if ($ARGV[0] =~ /\A-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[0] =~ /\A-/) { } 0 6 6
1247 0 ▶ if ($r->{in_completion}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'in_completion'}) { } 0 0 0
1248 0 $scn = shift @ARGV;
1249 0 $scn_from = 'arg';
1250 } else {
1251 0 my $suggestion = '';
1252 0 my @similar = __find_similar_go_opts($ARGV[0], \@go_spec);
1253 0 ▶ $suggestion = " (perhaps you meant ".
Branch: 1 branch
BranchTrueFalseTotal
if @similar 0 0 0
1254 join("/", @similar)."?)" if @similar;
1255 0 die [400, "Unknown option: $ARGV[0]".$suggestion];
1256 }
1257 } else {
1258 6 $scn = shift @ARGV;
1259 6 $scn_from = 'arg';
1260 }
1261 }
1262  
1263 85 my $scd;
1264 85 ▶ if (defined $scn) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $scn) { } 9 76 85
elsif (not $r->{'action'} and $self->{'subcommands'}) { } 0 76 76
1265 9 $scd = $self->get_subcommand_data($scn);
1266 9 ▶ unless ($r->{in_completion}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"in_completion"}) 7 2 9
1267 7 ▶ unless ($scd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scd) 1 6 7
1268 1 my $scs = $self->list_subcommands;
1269 1 ▶ if ($self->auto_abbrev_subcommand) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_abbrev_subcommand) 1 0 1
1270 # check that subcommand is an unambiguous abbreviation
1271 # of an existing subcommand
1272 1 my $num_matches = 0;
1273 1 my $complete_scn;
1274 1 for (keys %$scs) {
1275 2 ▶ if (index($_, $scn) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($_, $scn) == 0) 0 2 2
1276 0 $num_matches++;
1277 0 $complete_scn = $_;
1278 0 ▶ last if $num_matches > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $num_matches > 1 0 0 0
1279 }
1280 }
1281 1 ▶ if ($num_matches == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_matches == 1) 0 1 1
1282 0 $scn = $complete_scn;
1283 0 $scd = $self->get_subcommand_data($scn);
1284 0 goto L1;
1285 }
1286 }
1287 # provide suggestion of probably mistyped subcommand to user
1288 1 my @similar =
1289 __find_similar_strings($scn, [keys %$scs]);
1290 1 my $suggestion = '';
1291 1 ▶ $suggestion = " (perhaps you meant ".
Branch: 1 branch
BranchTrueFalseTotal
if @similar 1 0 1
1292 join("/", @similar)."?)" if @similar;
1293 1 die [500, "Unknown subcommand: $scn".$suggestion];
1294 }
1295 }
1296 ▶ } elsif (!$r->{action} && $self->{subcommands}) {
Condition66%2/3
not $r->{"action"} and $self->{"subcommands"}
!ll&&!rl&&r
5710
MC/DC0%0/2
not $r->{"action"} and $self->{"subcommands"}
not $r->{"action"} $self->{"subcommands"}
Truth table66%2/3
not(A) and B
A= not $r->{"action"}B= $self->{"subcommands"}
ABresult
0X0
100
111
1297 # program has subcommands but user doesn't specify any subcommand,
1298 # or specific action. display help instead.
1299 0 $r->{action} = 'help';
1300 0 $r->{skip_parse_subcommand_argv} = 1;
1301 } else {
1302 76 $scn = '';
1303 76 $scd = {
1304 url => $self->url,
1305 summary => $self->summary,
1306 description => $self->description,
1307 pass_cmdline_object => $self->pass_cmdline_object,
1308 tags => $self->tags,
1309 };
1310 }
1311 L1:
1312 84 $r->{subcommand_name} = $scn;
1313 84 $r->{subcommand_name_from} = $scn_from;
1314 84 $r->{subcommand_data} = $scd;
1315 }
1316  
1317 84 $r->{_parse_argv1_done} = 1;
1318 }
1319  
1320 81 ▶sub _parse_argv2 {
Subroutine _parse_argv2: called CC=33 SCAR=37.5
1321 81 require Perinci::CmdLine::Util::Config;
1322  
1323 81 my ($self, $r) = @_;
1324  
1325 81 my %args;
1326  
1327 81 ▶ if ($r->{read_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"read_env"}) 3 78 81
1328 3 my $env_words = $self->_read_env($r);
1329 3 unshift @ARGV, @$env_words;
1330 }
1331  
1332 # parse argv for per-subcommand command-line opts
1333 81 ▶ if ($r->{skip_parse_subcommand_argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'skip_parse_subcommand_argv'}) { } 6 75 81
1334 6 return [200, "OK (subcommand options parsing skipped)"];
1335 } else {
1336 75 my $scd = $r->{subcommand_data};
1337 75 ▶ if ($r->{meta} && !$self->{subcommands}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'meta'} and not $self->{'subcommands'}) { } 70 5 75
Condition66%2/3
$r->{"meta"} and not $self->{"subcommands"}
!ll&&!rl&&r
0570
MC/DC50%1/2
$r->{"meta"} and not $self->{"subcommands"}
$r->{"meta"} not $self->{"subcommands"}
Truth table66%2/3
A and not(B)
A= $r->{"meta"}B= not $self->{"subcommands"}
ABresult
0X0
100
111
1338 # we have retrieved meta, no need to get it again
1339 } else {
1340 5 $self->get_meta($r, $scd->{url});
1341 }
1342  
1343 # first fill in from subcommand specification
1344 75 ▶ if ($scd->{args}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scd->{"args"}) 0 75 75
1345 0 $args{$_} = $scd->{args}{$_} for keys %{ $scd->{args} };
1346 }
1347  
1348 # then read from configuration
1349 75 ▶ if ($r->{read_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"read_config"}) 12 63 75
1350  
1351 12 log_trace("[pericmd] Running hook_before_read_config_file ...");
1352 12 $self->hook_before_read_config_file($r);
1353  
1354 12 ▶ $self->_read_config($r) unless $r->{config};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"config"} 12 0 12
1355  
1356 12 log_trace("[pericmd] Running hook_after_read_config_file ...");
1357 12 $self->hook_after_read_config_file($r);
1358  
1359 12 my @plugins;
1360 my $res = Perinci::CmdLine::Util::Config::get_args_from_config(
1361 r => $r,
1362 config => $r->{config},
1363 args => \%args,
1364 plugins => \@plugins,
1365 program_name => $self->program_name,
1366 subcommand_name => $r->{subcommand_name},
1367 config_profile => $r->{config_profile},
1368 common_opts => $self->common_opts,
1369 meta => $r->{meta},
1370 12 meta_is_normalized => 1,
1371 );
1372 12 ▶ die $res unless $res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 12 12
1373  
1374 # interpret special parameters (/^-foo/). these will not be
1375 # arguments passed to function but instead treated specially
1376 TREAT_SPECIAL_PARAMS: {
1377  
1378 12 PLUGINS_FROM_DASH_PLUGINS: {
1379 12 my $plugins = delete $args{-plugins};
1380 12 ▶ last unless defined $plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plugins 12 0 12
1381 0 ▶ $self->_plugin_activate_plugins(
Branch: 1 branch
BranchTrueFalseTotal
ref $plugins eq 'ARRAY' ? : 0 0 0
1382 ref $plugins eq 'ARRAY' ? @$plugins :
1383 $self->_plugin_unflatten_import($plugins)
1384 );
1385 } # PLUGINS_FROM_DASH_PLUGINS
1386  
1387 PLUGINS_FROM_CONFIG_SECTIONS: {
1388 12 ▶ last unless @plugins;
Branch: 1 branch
BranchTrueFalseTotal
unless @plugins 12 0 12
1389 0 $self->_plugin_activate_plugins(@plugins);
1390 } # PLUGINS_FROM_CONFIG_SECTIONS
1391 } # TREAT_SPECIAL_PARAMS
1392  
1393 12 log_trace("[pericmd] args after reading config files: %s",
1394 \%args);
1395 12 my $found = $res->[3]{'func.found'};
1396 12 ▶ if (defined($r->{config_profile}) && !$found &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"config_profile"} and not $found and defined $r->{"read_config_files"} and @{$r->{"read_config_files"};} and not $r->{"ignore_missing_config_profile_section"}) 1 11 12
Condition83%10/12
defined $r->{"config_profile"} and not $found and defined $r->{"read_config_files"} and @{$r->{"read_config_files"};} and not $r->{"ignore_missing_config_profile_section"}
!ll&&!rl&&r
1101
defined $r->{"config_profile"} and not $found and defined $r->{"read_config_files"} and @{$r->{"read_config_files"};}
!ll&&!rl&&r
1011
defined $r->{"config_profile"} and not $found and defined $r->{"read_config_files"}
!ll&&!rl&&r
1002
defined $r->{"config_profile"} and not $found
!ll&&!rl&&r
732
MC/DC60%3/5
defined $r->{"config_profile"} and not $found and defined $r->{"read_config_files"} and @{$r->{"read_config_files"};} and not $r->{"ignore_missing_config_profile_section"}
defined $r->{"config_profile"} not $found defined $r->{"read_config_files"} @{$r->{"read_config_files"};} not $r->{"ignore_missing_config_profile_section"}
Truth table66%4/6
A and not(B) and C and D and not(E)
A= defined $r->{"config_profile"}B= not $foundC= defined $r->{"read_config_files"}D= @{$r->{"read_config_files"};}E= not $r->{"ignore_missing_config_profile_section"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1397 defined($r->{read_config_files}) &&
1398 2 @{$r->{read_config_files}} &&
1399 !$r->{ignore_missing_config_profile_section}) {
1400 1 return [412, "Profile '$r->{config_profile}' not found ".
1401 "in configuration file"];
1402 }
1403  
1404 }
1405  
1406 # finally get from argv
1407  
1408 # since get_args_from_argv() doesn't pass $r, we need to wrap it
1409 74 my $copts = $self->common_opts;
1410 74 my %old_handlers;
1411 74 for (keys %$copts) {
1412 569 my $h = $copts->{$_}{handler};
1413 0 ▶ $copts->{$_}{handler} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1414 0 my ($go, $val) = @_;
1415 0 $h->($go, $val, $r);
1416 569 };
1417 569 $old_handlers{$_} = $h;
1418 }
1419  
1420 74 my $has_cmdline_src;
1421 74 ▶ for my $ak (keys %{$r->{meta}{args} // {}}) {
Condition100%2/2
$r->{"meta"}{"args"} // {}
l!l
695
MC/DC100%1/1
$r->{"meta"}{"args"} // {}
$r->{"meta"}{"args"}
Truth table100%2/2
A
A= $r->{"meta"}{"args"}
Aresult
11
00
1422 112 my $av = $r->{meta}{args}{$ak};
1423 112 ▶ if ($av->{cmdline_src}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($av->{"cmdline_src"}) 35 77 112
1424 35 $has_cmdline_src = 1;
1425 35 last;
1426 }
1427 }
1428  
1429 74 require Perinci::Sub::GetArgs::Argv;
1430 my $ga_res = Perinci::Sub::GetArgs::Argv::get_args_from_argv(
1431 argv => \@ARGV,
1432 args => \%args,
1433 meta => $r->{meta},
1434 meta_is_normalized => 1,
1435 allow_extra_elems => $has_cmdline_src ? 1:0,
1436 per_arg_json => $self->{per_arg_json},
1437 per_arg_yaml => $self->{per_arg_yaml},
1438 common_opts => $copts,
1439 strict => $r->{in_completion} || $self->{allow_unknown_opts} ? 0:1,
1440 (ggls_res => $r->{_ggls_res}) x defined($r->{_ggls_res}),
1441 10 ▶ on_missing_required_args => sub {
Subroutine __ANON__: called CC=6 SCAR=18.5
1442 10 my %a = @_;
1443  
1444 10 my ($an, $aa, $as) = ($a{arg}, $a{args}, $a{spec});
1445 10 ▶ my $src = $as->{cmdline_src} // '';
Condition100%2/2
$as->{"cmdline_src"} // ""
l!l
82
MC/DC100%1/1
$as->{"cmdline_src"} // ""
$as->{"cmdline_src"}
Truth table100%2/2
A
A= $as->{"cmdline_src"}
Aresult
11
00
1446  
1447 # we only get from stdin if stdin is piped
1448 10 ▶ $src = '' if $src eq 'stdin_or_args' && is_interactive(*STDIN);
Branch: 1 branch
BranchTrueFalseTotal
if $src eq "stdin_or_args" and is_interactive(*STDIN) 0 10 10
Condition33%1/3
$src eq "stdin_or_args" and is_interactive(*STDIN)
!ll&&!rl&&r
1000
MC/DC0%0/2
$src eq "stdin_or_args" and is_interactive(*STDIN)
$src eq "stdin_or_args" is_interactive(*STDIN)
Truth table33%1/3
A and B
A= $src eq "stdin_or_args"B= is_interactive(*STDIN)
ABresult
0X0
100
111
1449  
1450 10 ▶ if ($src && $as->{req}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src and $as->{'req'}) { } 8 2 10
Condition66%2/3
$src and $as->{"req"}
!ll&&!rl&&r
208
MC/DC50%1/2
$src and $as->{"req"}
$src $as->{"req"}
Truth table66%2/3
A and B
A= $srcB= $as->{"req"}
ABresult
0X0
100
111
1451 # don't complain, we will fill argument from other source
1452 8 return 1;
1453 } else {
1454 # we have no other sources, so we complain about missing arg
1455 2 return 0;
1456 }
1457 },
1458 74 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$has_cmdline_src ? : 35 39 74
$r->{'in_completion'} || $self->{'allow_unknown_opts'} ? : 0 74 74
Condition33%1/3
$r->{"in_completion"} or $self->{"allow_unknown_opts"}
l!l&&r!l&&!r
0074
MC/DC0%0/2
$r->{"in_completion"} or $self->{"allow_unknown_opts"}
$r->{"in_completion"} $self->{"allow_unknown_opts"}
Truth table33%1/3
A or B
A= $r->{"in_completion"}B= $self->{"allow_unknown_opts"}
ABresult
1X1
011
000
1459  
1460 74 ▶ return $ga_res unless $ga_res->[0] == 200;
Branch: 1 branch
BranchTrueFalseTotal
unless $ga_res->[0] == 200 3 71 74
1461  
1462 # wrap stream arguments with iterator
1463 71 ▶ my $args_p = $r->{meta}{args} // {};
Condition100%2/2
$r->{"meta"}{"args"} // {}
l!l
692
MC/DC100%1/1
$r->{"meta"}{"args"} // {}
$r->{"meta"}{"args"}
Truth table100%2/2
A
A= $r->{"meta"}{"args"}
Aresult
11
00
1464 71 for my $arg (keys %{$ga_res->[2]}) {
1465 49 ▶ next unless $args_p->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless $args_p->{$arg} 0 49 49
1466 49 ▶ next unless $args_p->{$arg}{stream};
Branch: 1 branch
BranchTrueFalseTotal
unless $args_p->{$arg}{"stream"} 49 0 49
1467 0 for ($ga_res->[2]{$arg}) {
1468 0 ▶ $_ = ref $_ eq 'ARRAY' ? __array_iter($_) : __list_iter($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
1469 }
1470 }
1471  
1472 # restore
1473 71 for (keys %$copts) {
1474 548 $copts->{$_}{handler} = $old_handlers{$_};
1475 }
1476  
1477 71 return $ga_res;
1478 }
1479 }
1480  
1481 82 ▶sub parse_argv {
Subroutine parse_argv: called CC=2 SCAR=7.0
Pod documented
1482 82 my ($self, $r) = @_;
1483  
1484 82 log_trace("[pericmd] Parsing \@ARGV: %s", \@ARGV);
1485  
1486 # we parse argv twice. the first parse is with common_opts only so we're
1487 # able to catch --help, --version, etc early without having to know about
1488 # subcommands. two reasons for this: sometimes we need to get subcommand
1489 # name *from* cmdline opts (e.g. --cmd) and thus it's a chicken-and-egg
1490 # problem. second, it's faster because we don't have to load Riap client and
1491 # request the meta through it (especially in the case of remote URL).
1492 #
1493 # the second parse is after ge get subcommand name and the function
1494 # metadata. we can parse the remaining argv to get function arguments.
1495 #
1496 # note that when doing completion we're not using this algorithem and only
1497 # parse argv once. this is to make completion work across common- and
1498 # per-subcommand opts, e.g. --he<tab> resulting in --help (common opt) as
1499 # well as --height (function argument).
1500  
1501 82 ▶ $self->_parse_argv1($r) unless $r->{_parse_argv1_done};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"_parse_argv1_done"} 82 0 82
1502 81 $self->_parse_argv2($r);
1503 }
1504  
1505 4 ▶sub __gen_iter {
Subroutine __gen_iter: called CC=5 SCAR=16.2
1506 4 require Data::Sah::Util::Type;
1507  
1508 4 my ($fh, $argspec, $argname) = @_;
1509 4 my $schema = $argspec->{schema};
1510 4 ▶ $schema = $schema->[1]{of} if $schema->[0] eq 'array';
Branch: 1 branch
BranchTrueFalseTotal
if $schema->[0] eq "array" 4 0 4
1511 4 my $type = Data::Sah::Util::Type::get_type($schema);
1512  
1513 4 ▶ if (Data::Sah::Util::Type::is_simple($schema)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Sah::Util::Type::is_simple($schema)) { } 3 1 4
1514 my $chomp = $type eq 'buf' ? 0 :
1515 3 ▶ $argspec->{'cmdline.chomp'} // 1;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'buf' ? : 0 3 3
Condition100%2/2
$argspec->{"cmdline.chomp"} // 1
l!l
12
MC/DC100%1/1
$argspec->{"cmdline.chomp"} // 1
$argspec->{"cmdline.chomp"}
Truth table100%2/2
A
A= $argspec->{"cmdline.chomp"}
Aresult
11
00
1516 12 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
1517 # XXX this will be configurable later. currently by default reading
1518 # binary is per-64k while reading string is line-by-line.
1519 12 ▶ local $/ = \(64*1024) if $type eq 'buf';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "buf" 0 12 12
1520  
1521 12 state $eof;
1522 12 ▶ return undef if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 0 12 12
1523 12 my $l = <$fh>;
1524 12 ▶ unless (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 3 9 12
1525 3 $eof++; return undef;
1526 }
1527 9 ▶ chomp($l) if $chomp;
Branch: 1 branch
BranchTrueFalseTotal
if $chomp 6 3 9
1528 9 $l;
1529 3 };
1530 } else {
1531 # expect JSON stream for non-simple types
1532 1 require JSON::MaybeXS;
1533 1 state $json = JSON::MaybeXS->new->allow_nonref;
1534 1 my $i = -1;
1535 5 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
1536 5 state $eof;
1537 5 ▶ return undef if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 0 5 5
1538 5 $i++;
1539 5 my $l = <$fh>;
1540 5 ▶ unless (defined $l) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $l) 1 4 5
1541 1 $eof++; return undef;
1542 }
1543 4 eval { $l = $json->decode($l) };
1544 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
1545 0 die "Invalid JSON in stream argument '$argname' record #$i: $@";
1546 }
1547 4 $l;
1548 1 };
1549 }
1550 }
1551  
1552 # parse cmdline_src argument spec properties for filling argument value from
1553 # file and/or stdin. currently does not support argument submetadata.
1554 77 ▶sub parse_cmdline_src {
Subroutine parse_cmdline_src: called CC=71 SCAR=48.6
Pod undocumented
1555 77 my ($self, $r) = @_;
1556  
1557 77 my $action = $r->{action};
1558 77 my $meta = $r->{meta};
1559  
1560 #if ($self->use_utf8) {
1561 # require open; open->import(":utf8");
1562 #} elsif ($self->use_locale) {
1563 # require open; open->import(":locale");
1564 #}
1565  
1566 77 ▶ my $url = $r->{subcommand_data}{url} // $self->{url} // '';
Condition40%2/5
($r->{"subcommand_data"}{"url"} // $self->{"url"}) // ""
l!l
770
$r->{"subcommand_data"}{"url"} // $self->{"url"}
l!l&&r!l&&!r
7700
MC/DC0%0/2
($r->{"subcommand_data"}{"url"} // $self->{"url"}) // ""
$r->{"subcommand_data"}{"url"} $self->{"url"}
Truth table33%1/3
A // B
A= $r->{"subcommand_data"}{"url"}B= $self->{"url"}
ABresult
1X1
011
000
1567 77 my $is_network = $url =~ m!^(https?|riap[^:]+):!;
1568  
1569 # handle cmdline_src
1570 77 ▶ if ($action eq 'call') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "call") 71 6 77
1571 71 ▶ my $args_p = $meta->{args} // {};
Condition100%2/2
$meta->{"args"} // {}
l!l
692
MC/DC100%1/1
$meta->{"args"} // {}
$meta->{"args"}
Truth table100%2/2
A
A= $meta->{"args"}
Aresult
11
00
1572 71 my $stdin_seen;
1573 71 for my $an (sort {
1574 100 my $csa = $args_p->{$a}{cmdline_src};
1575 100 my $csb = $args_p->{$b}{cmdline_src};
1576 100 ▶ my $posa = $args_p->{$a}{pos} // 9999;
Condition100%2/2
$args_p->{$a}{"pos"} // 9999
l!l
8416
MC/DC100%1/1
$args_p->{$a}{"pos"} // 9999
$args_p->{$a}{"pos"}
Truth table100%2/2
A
A= $args_p->{$a}{"pos"}
Aresult
11
00
1577 100 ▶ my $posb = $args_p->{$b}{pos} // 9999;
Condition100%2/2
$args_p->{$b}{"pos"} // 9999
l!l
8515
MC/DC100%1/1
$args_p->{$b}{"pos"} // 9999
$args_p->{$b}{"pos"}
Truth table100%2/2
A
A= $args_p->{$b}{"pos"}
Aresult
11
00
1578  
1579 # first, always put stdin_line before stdin / stdin_or_files
1580 (
1581 100 ▶ !$csa || !$csb ? 0 :
Branch: 4 branches
BranchTrueFalseTotal
!($csa && $csb) ? : 92 8 100
$csa eq 'stdin_line' && $csb eq 'stdin_line' ? : 1 7 8
$csa eq 'stdin_line' && $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? : 0 7 7
$csb eq 'stdin_line' && $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? : 0 7 7
Condition61%11/18
(!($csa && $csb) ? 0 : ($csa eq 'stdin_line' && $csb eq 'stdin_line' ? 0 : ($csa eq 'stdin_line' && $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? -1 : ($csb eq 'stdin_line' && $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? 1 : 0)))) || $posa <=> $posb || $a cmp $b
l!l&&r!l&&!r
87130
!($csa && $csb) ? 0 : ($csa eq "stdin_line" && $csb eq "stdin_line" ? 0 : ($csa eq "stdin_line" && $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? -1 : ($csb eq "stdin_line" && $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? 1 : 0))) or $posa <=> $posb
l!l&&r!l&&!r
08713
$csa && $csb
!ll&&!rl&&r
9028
$csa eq "stdin_line" and $csb eq "stdin_line"
!ll&&!rl&&r
701
$csa eq "stdin_line" and $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/
!ll&&!rl&&r
700
$csb eq "stdin_line" and $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/
!ll&&!rl&&r
700
MC/DC0%0/4
(!($csa && $csb) ? 0 : ($csa eq 'stdin_line' && $csb eq 'stdin_line' ? 0 : ($csa eq 'stdin_line' && $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? -1 : ($csb eq 'stdin_line' && $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? 1 : 0)))) || $posa <=> $posb || $a cmp $b
$csa $csb $posa <=> $posb $a cmp $b
MC/DC50%1/2
$csa eq "stdin_line" and $csb eq "stdin_line"
$csa eq "stdin_line" $csb eq "stdin_line"
MC/DC0%0/2
$csa eq "stdin_line" and $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/
$csa eq "stdin_line" $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/
MC/DC0%0/2
$csb eq "stdin_line" and $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/
$csb eq "stdin_line" $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/
Truth table20%1/5
not(A && B) or C || D
A= $csaB= $csbC= $posa <=> $posbD= $a cmp $b
ABCDresult
0XXX1
10XX1
111X1
11011
11000
Truth table66%2/3
A and B
A= $csa eq "stdin_line"B= $csb eq "stdin_line"
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $csa eq "stdin_line"B= $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $csb eq "stdin_line"B= $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/
ABresult
0X0
100
111
1582 $csa eq 'stdin_line' && $csb eq 'stdin_line' ? 0 :
1583 $csa eq 'stdin_line' && $csb =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? -1 :
1584 $csb eq 'stdin_line' && $csa =~ /^(stdin|stdin_or_files?|stdin_or_args)/ ? 1 : 0
1585 )
1586 ||
1587  
1588 # then order by pos
1589 ($posa <=> $posb)
1590  
1591 ||
1592 # then by name
1593 ($a cmp $b)
1594 } keys %$args_p) {
1595 #log_trace("TMP: handle cmdline_src for arg=%s", $an);
1596 119 my $as = $args_p->{$an};
1597 119 my $src = $as->{cmdline_src};
1598 119 ▶ my $type = $as->{schema}[0]
Branch: 1 branch
BranchTrueFalseTotal
my $type = $as->{"schema"}[0] or die "BUG: No schema is defined for arg '${an}'" 119 0 119
1599 or die "BUG: No schema is defined for arg '$an'";
1600 # Riap::HTTP currently does not support streaming input
1601 119 ▶ my $do_stream = $as->{stream} && $url !~ /^https?:/;
Condition66%2/3
$as->{'stream'} && !($url =~ /^https?:/)
!ll&&!rl&&r
11504
MC/DC50%1/2
$as->{'stream'} && !($url =~ /^https?:/)
$as->{'stream'} !($url =~ /^https?:/)
Truth table66%2/3
A && not(B)
A= $as->{'stream'}B= !($url =~ /^https?:/)
ABresult
0X0
100
111
1602 119 ▶ if ($src) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src) 40 79 119
1603 40 ▶ die [531,
Branch: 1 branch
BranchTrueFalseTotal
unless $src =~ /\A(stdin|file|stdin_or_files?|stdin_or_args|stdin_line)\z/ 1 39 40
1604 "Invalid 'cmdline_src' value for argument '$an': $src"]
1605 unless $src =~ /\A(stdin|file|stdin_or_files?|stdin_or_args|stdin_line)\z/;
1606 39 ▶ die [531,
Branch: 1 branch
BranchTrueFalseTotal
unless $do_stream or $type =~ /\A(str|buf|array)\z/ 1 38 39
Condition100%3/3
$do_stream or $type =~ /\A(str|buf|array)\z/
l!l&&r!l&&!r
4341
MC/DC100%2/2
$do_stream or $type =~ /\A(str|buf|array)\z/
$do_stream $type =~ /\A(str|buf|array)\z/
Truth table100%3/3
A or B
A= $do_streamB= $type =~ /\A(str|buf|array)\z/
ABresult
1X1
011
000
1607 "Sorry, argument '$an' is set cmdline_src=$src, but type ".
1608 "is not str/buf/array, only those are supported now"]
1609 unless $do_stream || $type =~ /\A(str|buf|array)\z/; # XXX stdin_or_args needs array only, not str/buf
1610  
1611 38 ▶ if ($src =~ /\A(stdin|stdin_or_files?|stdin_or_args)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\A(stdin|stdin_or_files?|stdin_or_args)\z/) 23 15 38
1612 23 ▶ die [531, "Only one argument can be specified ".
Branch: 1 branch
BranchTrueFalseTotal
if $stdin_seen++ 0 23 23
1613 "cmdline_src stdin/stdin_or_file/stdin_or_files/stdin_or_args"]
1614 if $stdin_seen++;
1615 }
1616 38 my $is_ary = $type eq 'array';
1617 38 ▶ if ($src eq 'stdin_line' && !exists($r->{args}{$an})) {
Branch: 5 branches
BranchTrueFalseTotal
if ($src eq 'stdin_line' and not exists $r->{'args'}{$an}) { } 4 34 38
elsif ($src eq 'stdin' or $src eq 'file' and ($r->{'args'}{$an} // '') eq '-') { } 5 29 34
elsif ($src eq 'stdin_or_file' or $src eq 'stdin_or_files') { } 16 13 29
elsif ($src eq 'stdin_or_args' and not is_interactive(*STDIN)) { } 2 11 13
elsif ($src eq 'file') { } 10 1 11
Condition100%3/3
$src eq "stdin_line" and not exists $r->{"args"}{$an}
!ll&&!rl&&r
3314
MC/DC100%2/2
$src eq "stdin_line" and not exists $r->{"args"}{$an}
$src eq "stdin_line" not exists $r->{"args"}{$an}
Truth table100%3/3
A and not(B)
A= $src eq "stdin_line"B= not exists $r->{"args"}{$an}
ABresult
0X0
100
111
1618 4 require Perinci::Object;
1619 4 my $term_readkey_available = eval { require Term::ReadKey; 1 };
1620 4 ▶ my $prompt = Perinci::Object::rimeta($as)->langprop('cmdline_prompt') //
Condition50%1/2
Perinci::Object::rimeta($as)->langprop("cmdline_prompt") // sprintf($self->default_prompt_template, $an)
l!l
04
MC/DC0%0/1
Perinci::Object::rimeta($as)->langprop("cmdline_prompt") // sprintf($self->default_prompt_template, $an)
Perinci::Object::rimeta($as)->langprop("cmdline_prompt")
Truth table50%1/2
A
A= Perinci::Object::rimeta($as)->langprop("cmdline_prompt")
Aresult
11
00
1621 sprintf($self->default_prompt_template, $an);
1622 4 print $prompt;
1623 4 my $iactive = is_interactive(*STDOUT);
1624 Term::ReadKey::ReadMode('noecho')
1625 4 ▶ if $term_readkey_available && $iactive && $as->{is_password};
Branch: 1 branch
BranchTrueFalseTotal
if $term_readkey_available and $iactive and $as->{"is_password"} 0 4 4
Condition33%2/6
$term_readkey_available and $iactive and $as->{"is_password"}
!ll&&!rl&&r
400
$term_readkey_available and $iactive
!ll&&!rl&&r
040
MC/DC0%0/3
$term_readkey_available and $iactive and $as->{"is_password"}
$term_readkey_available $iactive $as->{"is_password"}
Truth table25%1/4
A and B and C
A= $term_readkey_availableB= $iactiveC= $as->{"is_password"}
ABCresult
0XX0
10X0
1100
1111
1626 4 chomp($r->{args}{$an} = <STDIN>);
1627 0 ▶ do { print "\n"; Term::ReadKey::ReadMode(0) if $term_readkey_available }
Branch: 1 branch
BranchTrueFalseTotal
if $term_readkey_available 0 0 0
1628 4 ▶ if $iactive && $as->{is_password};
Branch: 1 branch
BranchTrueFalseTotal
if ($iactive and $as->{"is_password"}) 0 4 4
Condition33%1/3
$iactive and $as->{"is_password"}
!ll&&!rl&&r
400
MC/DC0%0/2
$iactive and $as->{"is_password"}
$iactive $as->{"is_password"}
Truth table33%1/3
A and B
A= $iactiveB= $as->{"is_password"}
ABresult
0X0
100
111
1629 4 $r->{args}{"-cmdline_src_$an"} = 'stdin_line';
1630 ▶ } elsif ($src eq 'stdin' || $src eq 'file' &&
Condition75%6/8
$src eq "stdin" or $src eq "file" and ($r->{"args"}{$an} // "") eq "-"
l!l&&r!l&&!r
5029
$src eq "file" and ($r->{"args"}{$an} // "") eq "-"
!ll&&!rl&&r
19100
$r->{"args"}{$an} // ""
l!l
73
MC/DC100%3/3
$src eq "stdin" or $src eq "file" and ($r->{"args"}{$an} // "") eq "-"
$src eq "stdin" $src eq "file" $r->{"args"}{$an}
Truth table100%4/4
A or B and C
A= $src eq "stdin"B= $src eq "file"C= $r->{"args"}{$an}
ABCresult
1XX1
0111
00X0
0100
1631 ($r->{args}{$an}//"") eq '-') {
1632 die [400, "Argument $an must be set to '-' which means ".
1633 "from stdin"]
1634 if defined($r->{args}{$an}) &&
1635 5 ▶ $r->{args}{$an} ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"args"}{$an} and $r->{"args"}{$an} ne "-" 1 4 5
Condition100%3/3
defined $r->{"args"}{$an} and $r->{"args"}{$an} ne "-"
!ll&&!rl&&r
311
MC/DC100%2/2
defined $r->{"args"}{$an} and $r->{"args"}{$an} ne "-"
defined $r->{"args"}{$an} $r->{"args"}{$an} ne "-"
Truth table100%3/3
A and B
A= defined $r->{"args"}{$an}B= $r->{"args"}{$an} ne "-"
ABresult
0X0
100
111
1636 #log_trace("Getting argument '$an' value from stdin ...");
1637 $r->{args}{$an} = $do_stream ?
1638 __gen_iter(\*STDIN, $as, $an) :
1639 $is_ary ? [<STDIN>] :
1640 4 ▶ do {local $/; ~~<STDIN>};
Branch: 2 branches
BranchTrueFalseTotal
$do_stream ? : 0 4 4
$is_ary ? : 1 3 4
1641 4 $r->{args}{"-cmdline_src_$an"} = 'stdin';
1642 ▶ } elsif ($src eq 'stdin_or_file' || $src eq 'stdin_or_files') {
Condition100%3/3
$src eq "stdin_or_file" or $src eq "stdin_or_files"
l!l&&r!l&&!r
61013
MC/DC100%2/2
$src eq "stdin_or_file" or $src eq "stdin_or_files"
$src eq "stdin_or_file" $src eq "stdin_or_files"
Truth table100%3/3
A or B
A= $src eq "stdin_or_file"B= $src eq "stdin_or_files"
ABresult
1X1
011
000
1643 # push back argument value to @ARGV so <> can work to slurp
1644 # all the specified files
1645 16 local @ARGV = @ARGV;
1646 unshift @ARGV, $r->{args}{$an}
1647 16 ▶ if defined $r->{args}{$an};
Branch: 1 branch
BranchTrueFalseTotal
if defined $r->{"args"}{$an} 0 16 16
1648  
1649 # with stdin_or_file, we only accept one file
1650 16 ▶ splice @ARGV, 1
Branch: 1 branch
BranchTrueFalseTotal
if @ARGV > 1 and $src eq "stdin_or_file" 1 15 16
Condition100%3/3
@ARGV > 1 and $src eq "stdin_or_file"
!ll&&!rl&&r
1321
MC/DC100%2/2
@ARGV > 1 and $src eq "stdin_or_file"
@ARGV > 1 $src eq "stdin_or_file"
Truth table100%3/3
A and B
A= @ARGV > 1B= $src eq "stdin_or_file"
ABresult
0X0
100
111
1651 if @ARGV > 1 && $src eq 'stdin_or_file';
1652  
1653 #log_trace("Getting argument '$an' value from ".
1654 # "$src, \@ARGV=%s ...", \@ARGV);
1655  
1656 # perl doesn't seem to check files, so we check it here
1657 16 for (@ARGV) {
1658 13 ▶ next if $_ eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "-" 0 13 13
1659 13 ▶ die [500, "Can't read file '$_': $!"] if !(-r $_);
Branch: 1 branch
BranchTrueFalseTotal
unless -r $_ 3 10 13
1660 }
1661  
1662 13 $r->{args}{"-cmdline_srcfilenames_$an"} = [@ARGV];
1663 $r->{args}{$an} = $do_stream ?
1664 __gen_iter(\*ARGV, $as, $an) :
1665 $is_ary ? [<>] :
1666 13 ▶ do {local $/; ~~<>};
Branch: 2 branches
BranchTrueFalseTotal
$do_stream ? : 4 9 13
$is_ary ? : 3 6 9
1667 13 $r->{args}{"-cmdline_src_$an"} = $src;
1668 ▶ } elsif ($src eq 'stdin_or_args' && !is_interactive(*STDIN)) {
Condition66%2/3
$src eq "stdin_or_args" and not is_interactive(*STDIN)
!ll&&!rl&&r
1102
MC/DC50%1/2
$src eq "stdin_or_args" and not is_interactive(*STDIN)
$src eq "stdin_or_args" not is_interactive(*STDIN)
Truth table66%2/3
A and not(B)
A= $src eq "stdin_or_args"B= not is_interactive(*STDIN)
ABresult
0X0
100
111
1669 2 ▶ unless (defined($r->{args}{$an})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"args"}{$an}) 1 1 2
1670 $r->{args}{$an} = $do_stream ?
1671 __gen_iter(\*STDIN, $as, $an) :
1672 2 $is_ary ? [map {chomp;$_} <STDIN>] :
1673 0 ▶ do {local $/; ~~<STDIN>};
Branch: 2 branches
BranchTrueFalseTotal
$do_stream ? : 0 1 1
$is_ary ? : 1 0 1
1674 }
1675 } elsif ($src eq 'file') {
1676 10 ▶ unless (exists $r->{args}{$an}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $r->{"args"}{$an}) 3 7 10
1677 3 ▶ if ($as->{req}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($as->{'req'}) { } 1 2 3
1678 1 die [400,
1679 "Please specify filename for argument '$an'"];
1680 } else {
1681 2 next;
1682 }
1683 }
1684 die [400, "Please specify filename for argument '$an'"]
1685 7 ▶ unless defined $r->{args}{$an};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"args"}{$an} 0 7 7
1686 #log_trace("Getting argument '$an' value from ".
1687 # "file ...");
1688 7 my $fh;
1689 7 my $fname = $r->{args}{$an};
1690 7 ▶ unless (open $fh, "<", $fname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $fh, "<", $fname) 1 6 7
1691 1 die [500, "Can't open file '$fname' for argument '$an'".
1692 ": $!"];
1693 }
1694 $r->{args}{$an} = $do_stream ?
1695 __gen_iter($fh, $as, $an) :
1696 $is_ary ? [<$fh>] :
1697 6 ▶ do { local $/; ~~<$fh> };
Branch: 2 branches
BranchTrueFalseTotal
$do_stream ? : 0 6 6
$is_ary ? : 2 4 6
1698 6 close $fh;
1699 6 $r->{args}{"-cmdline_src_$an"} = 'file';
1700 6 $r->{args}{"-cmdline_srcfilenames_$an"} = [$fname];
1701 }
1702 }
1703  
1704 # encode to base64 if binary and we want to cross network (because
1705 # it's usually JSON)
1706 109 ▶ if ($self->riap_version == 1.2 && $is_network &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an} and $args_p->{$an}{"schema"} and $args_p->{$an}{"schema"}[0] eq "buf" and not $r->{"args"}{"${an}:base64"}) 0 109 109
Condition33%5/15
$self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an} and $args_p->{$an}{"schema"} and $args_p->{$an}{"schema"}[0] eq "buf" and not $r->{"args"}{"${an}:base64"}
!ll&&!rl&&r
10900
$self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an} and $args_p->{$an}{"schema"} and $args_p->{$an}{"schema"}[0] eq "buf"
!ll&&!rl&&r
10900
$self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an} and $args_p->{$an}{"schema"}
!ll&&!rl&&r
10900
$self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an}
!ll&&!rl&&r
10900
$self->riap_version == "1.2" and $is_network
!ll&&!rl&&r
10900
MC/DC0%0/6
$self->riap_version == "1.2" and $is_network and defined $r->{"args"}{$an} and $args_p->{$an}{"schema"} and $args_p->{$an}{"schema"}[0] eq "buf" and not $r->{"args"}{"${an}:base64"}
$self->riap_version == "1.2" $is_network defined $r->{"args"}{$an} $args_p->{$an}{"schema"} $args_p->{$an}{"schema"}[0] eq "buf" not $r->{"args"}{"${an}:base64"}
Truth table14%1/7
A and B and C and D and E and not(F)
A= $self->riap_version == "1.2"B= $is_networkC= defined $r->{"args"}{$an}D= $args_p->{$an}{"schema"}E= $args_p->{$an}{"schema"}[0] eq "buf"F= not $r->{"args"}{"${an}:base64"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
1707 defined($r->{args}{$an}) && $args_p->{$an}{schema} &&
1708 $args_p->{$an}{schema}[0] eq 'buf' &&
1709 !$r->{args}{"$an:base64"}) {
1710 0 require MIME::Base64;
1711 $r->{args}{"$an:base64"} =
1712 0 MIME::Base64::encode_base64($r->{args}{$an}, "");
1713 0 delete $r->{args}{$an};
1714 }
1715 } # for arg
1716 }
1717 #log_trace("args after cmdline_src is processed: %s", $r->{args});
1718 }
1719  
1720 # determine filehandle to output to (normally STDOUT, but we can also send to a
1721 # pager, or a temporary file when sending to viewer (the difference between
1722 # pager and viewer: when we page we use pipe, when we view we write to temporary
1723 # file then open the viewer. viewer settings override pager settings.
1724 85 ▶sub select_output_handle {
Subroutine select_output_handle: called CC=30 SCAR=63.1
Pod undocumented
1725 85 my ($self, $r) = @_;
1726  
1727 85 ▶ my $resmeta = $r->{res}[3] // {};
Condition50%1/2
$r->{"res"}[3] // {}
l!l
850
MC/DC0%0/1
$r->{"res"}[3] // {}
$r->{"res"}[3]
Truth table50%1/2
A
A= $r->{"res"}[3]
Aresult
11
00
1728  
1729 85 my $handle;
1730 SELECT_HANDLE:
1731 {
1732 # view result using external program
1733 85 ▶ if ($r->{view_result} // $ENV{VIEW_RESULT} // $resmeta->{"cmdline.view_result"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($r->{"view_result"} // $ENV{"VIEW_RESULT"}) // $resmeta->{"cmdline.view_result"}) 0 85 85
Condition33%2/6
($r->{"view_result"} // $ENV{"VIEW_RESULT"}) // $resmeta->{"cmdline.view_result"}
l!l&&r!l&&!r
0085
$r->{"view_result"} // $ENV{"VIEW_RESULT"}
l!l&&r!l&&!r
0085
MC/DC0%0/3
($r->{"view_result"} // $ENV{"VIEW_RESULT"}) // $resmeta->{"cmdline.view_result"}
$r->{"view_result"} $ENV{"VIEW_RESULT"} $resmeta->{"cmdline.view_result"}
Truth table25%1/4
A // B // C
A= $r->{"view_result"}B= $ENV{"VIEW_RESULT"}C= $resmeta->{"cmdline.view_result"}
ABCresult
1XX1
01X1
0011
0000
1734  
1735 # select default viewer & preprocessing based on content_type and
1736 # availability. should probably be moved elsewhere later.
1737 0 my $default_viewer;
1738 SET_DEFAULT_VIEWER: {
1739 0 require File::Which;
1740  
1741 0 ▶ my $ct = $resmeta->{content_type} // '';
Condition0%0/2
$resmeta->{"content_type"} // ""
l!l
00
MC/DC0%0/1
$resmeta->{"content_type"} // ""
$resmeta->{"content_type"}
Truth table0%0/2
A
A= $resmeta->{"content_type"}
Aresult
11
00
1742 0 ▶ if ($ct eq 'text/x-org') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/x-org") 0 0 0
1743 0 ▶ $default_viewer = 'emacs' if File::Which::which("emacs");
Branch: 1 branch
BranchTrueFalseTotal
if File::Which::which("emacs") 0 0 0
1744 }
1745 }
1746  
1747 my $viewer = $r->{viewer} // $resmeta->{"cmdline.viewer"} //
1748 0 ▶ $default_viewer // $ENV{VIEWER} // $ENV{BROWSER} // do {
Condition0%0/15
(((($r->{"viewer"} // $resmeta->{"cmdline.viewer"}) // $default_viewer) // $ENV{"VIEWER"}) // $ENV{"BROWSER"}) // do { if ($^O eq "MSWin32") { require Browser::Find::Windows; Browser::Find::Windows::find_browser(); } else { undef; } }
l!l&&r!l&&!r
000
((($r->{"viewer"} // $resmeta->{"cmdline.viewer"}) // $default_viewer) // $ENV{"VIEWER"}) // $ENV{"BROWSER"}
l!l&&r!l&&!r
000
(($r->{"viewer"} // $resmeta->{"cmdline.viewer"}) // $default_viewer) // $ENV{"VIEWER"}
l!l&&r!l&&!r
000
($r->{"viewer"} // $resmeta->{"cmdline.viewer"}) // $default_viewer
l!l&&r!l&&!r
000
$r->{"viewer"} // $resmeta->{"cmdline.viewer"}
l!l&&r!l&&!r
000
MC/DC0%0/6
(((($r->{"viewer"} // $resmeta->{"cmdline.viewer"}) // $default_viewer) // $ENV{"VIEWER"}) // $ENV{"BROWSER"}) // do { if ($^O eq "MSWin32") { require Browser::Find::Windows; Browser::Find::Windows::find_browser(); } else { undef; } }
$r->{"viewer"} $resmeta->{"cmdline.viewer"} $default_viewer $ENV{"VIEWER"} $ENV{"BROWSER"} do { if ($^O eq "MSWin32") { require Browser::Find::Windows; Browser::Find::Windows::find_browser(); } else { undef; } }
Truth table0%0/7
A // B // C // D // E // F
A= $r->{"viewer"}B= $resmeta->{"cmdline.viewer"}C= $default_viewerD= $ENV{"VIEWER"}E= $ENV{"BROWSER"}F= do { if ($^O eq "MSWin32") { require Browser::Find::Windows; Browser::Find::Windows::find_browser(); } else { undef; } }
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
1749 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
1750 0 require Browser::Find::Windows;
1751 0 Browser::Find::Windows::find_browser();
1752 } else {
1753 0 undef;
1754 }
1755 };
1756 0 ▶ last if defined $viewer && !$viewer; # ENV{VIEWER} can be set 0/'' to disable viewing result using external program
Branch: 1 branch
BranchTrueFalseTotal
if defined $viewer and not $viewer 0 0 0
Condition0%0/3
defined $viewer and not $viewer
!ll&&!rl&&r
000
MC/DC0%0/2
defined $viewer and not $viewer
defined $viewer not $viewer
Truth table0%0/3
A and not(B)
A= defined $viewerB= not $viewer
ABresult
0X0
100
111
1757 0 ▶ die [500, "No VIEWER program set"] unless defined $viewer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $viewer 0 0 0
1758 0 $r->{viewer} = $viewer;
1759 0 require File::Temp;
1760 0 my $filename;
1761 0 ($handle, $filename) = File::Temp::tempfile(
1762 SUFFIX => $tempfile_opt_suffix,
1763 );
1764 0 $r->{viewer_temp_path} = $filename;
1765 }
1766  
1767 85 ▶ if ($r->{page_result} // $ENV{PAGE_RESULT} // $resmeta->{"cmdline.page_result"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($r->{"page_result"} // $ENV{"PAGE_RESULT"}) // $resmeta->{"cmdline.page_result"}) 0 85 85
Condition33%2/6
($r->{"page_result"} // $ENV{"PAGE_RESULT"}) // $resmeta->{"cmdline.page_result"}
l!l&&r!l&&!r
0085
$r->{"page_result"} // $ENV{"PAGE_RESULT"}
l!l&&r!l&&!r
0085
MC/DC0%0/3
($r->{"page_result"} // $ENV{"PAGE_RESULT"}) // $resmeta->{"cmdline.page_result"}
$r->{"page_result"} $ENV{"PAGE_RESULT"} $resmeta->{"cmdline.page_result"}
Truth table25%1/4
A // B // C
A= $r->{"page_result"}B= $ENV{"PAGE_RESULT"}C= $resmeta->{"cmdline.page_result"}
ABCresult
1XX1
01X1
0011
0000
1768 0 require File::Which;
1769 my $pager = $r->{pager} // $resmeta->{"cmdline.pager"} //
1770 0 ▶ $ENV{PAGER};
Condition0%0/6
($r->{"pager"} // $resmeta->{"cmdline.pager"}) // $ENV{"PAGER"}
l!l&&r!l&&!r
000
$r->{"pager"} // $resmeta->{"cmdline.pager"}
l!l&&r!l&&!r
000
MC/DC0%0/3
($r->{"pager"} // $resmeta->{"cmdline.pager"}) // $ENV{"PAGER"}
$r->{"pager"} $resmeta->{"cmdline.pager"} $ENV{"PAGER"}
Truth table0%0/4
A // B // C
A= $r->{"pager"}B= $resmeta->{"cmdline.pager"}C= $ENV{"PAGER"}
ABCresult
1XX1
01X1
0011
0000
1771 0 ▶ unless (defined $pager) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pager) 0 0 0
1772 0 ▶ $pager = "less -FRSX" if File::Which::which("less");
Branch: 1 branch
BranchTrueFalseTotal
if File::Which::which("less") 0 0 0
1773 }
1774 0 ▶ unless (defined $pager) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pager) 0 0 0
1775 0 ▶ $pager = "more" if File::Which::which("more");
Branch: 1 branch
BranchTrueFalseTotal
if File::Which::which("more") 0 0 0
1776 }
1777 0 ▶ unless (defined $pager) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pager) 0 0 0
1778 0 die [500, "Can't determine PAGER"];
1779 }
1780 0 ▶ last unless $pager; # ENV{PAGER} can be set 0/'' to disable paging
Branch: 1 branch
BranchTrueFalseTotal
unless $pager 0 0 0
1781 0 log_trace("Paging output using %s", $pager);
1782 ## no critic (InputOutput::RequireBriefOpen)
1783 0 open $handle, "|-", $pager;
1784 }
1785 85 ▶ $handle //= $r->{res}[0] >= 400 ? \*STDERR : \*STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$r->{'res'}[0] >= 400 ? : 15 70 85
Condition50%1/2
$handle //= $r->{"res"}[0] >= 400 ? \*STDERR : \*STDOUT
l!l
085
MC/DC0%0/1
$handle //= $r->{"res"}[0] >= 400 ? \*STDERR : \*STDOUT
$handle
Truth table50%1/2
A
A= $handle
Aresult
11
00
1786 }
1787 85 $r->{output_handle} = $handle;
1788 }
1789  
1790 # TODO: move to plugins
1791 0 ▶sub save_output {
Subroutine save_output: not called CC=11 SCAR=48.8
Pod undocumented
1792 0 my ($self, $r, $dir) = @_;
1793 0 ▶ $dir //= $ENV{PERINCI_CMDLINE_OUTPUT_DIR};
Condition0%0/3
$dir //= $ENV{"PERINCI_CMDLINE_OUTPUT_DIR"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$dir //= $ENV{"PERINCI_CMDLINE_OUTPUT_DIR"}
$dir $ENV{"PERINCI_CMDLINE_OUTPUT_DIR"}
Truth table0%0/3
A //= B
A= $dirB= $ENV{"PERINCI_CMDLINE_OUTPUT_DIR"}
ABresult
1X1
011
000
1794  
1795 0 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 0 0
1796 0 warn "Can't save output to $dir: doesn't exist or not a directory, skipped saving program output";
1797 0 return;
1798 }
1799  
1800 0 my $time = do {
1801 0 ▶ if (eval { require Time::HiRes; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require Time::HiRes; 1 } }) { } 0 0 0
1802 0 Time::HiRes::time();
1803 } else {
1804 0 time();
1805 }
1806 };
1807  
1808 0 my $fmttime = do {
1809 0 my @time = gmtime($time);
1810 0 sprintf(
1811 "%04d-%02d-%02dT%02d%02d%02d.%09dZ",
1812 $time[5]+1900,
1813 $time[4]+1,
1814 $time[3],
1815 $time[2],
1816 $time[1],
1817 $time[0],
1818 ($time - int($time))*1_000_000_000,
1819 );
1820 };
1821  
1822 0 my ($fpath_out, $fpath_meta);
1823 0 my ($fh_out, $fh_meta);
1824 {
1825 0 require Fcntl;
1826 0 my $counter = -1;
1827 0 while (1) {
1828 0 ▶ if ($counter++ >= 10_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counter++ >= 10000) 0 0 0
1829 0 warn "Can't create file to save program output, skipped saving program output";
1830 0 return;
1831 }
1832 0 ▶ my $fpath_out = "$dir/" . ($counter ? "$fmttime.out.$counter" : "$fmttime.out");
Branch: 1 branch
BranchTrueFalseTotal
$counter ? : 0 0 0
1833 0 ▶ my $fpath_meta = "$dir/" . ($counter ? "$fmttime.meta.$counter" : "$fmttime.meta");
Branch: 1 branch
BranchTrueFalseTotal
$counter ? : 0 0 0
1834 0 ▶ if ((-e $fpath_out) || (-e $fpath_meta)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $fpath_out or -e $fpath_meta) 0 0 0
Condition0%0/3
-e $fpath_out or -e $fpath_meta
l!l&&r!l&&!r
000
MC/DC0%0/2
-e $fpath_out or -e $fpath_meta
-e $fpath_out -e $fpath_meta
Truth table0%0/3
A or B
A= -e $fpath_outB= -e $fpath_meta
ABresult
1X1
011
000
1835 0 next;
1836 }
1837 0 ▶ unless (sysopen $fh_out , $fpath_out , Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh_out, $fpath_out, 193) 0 0 0
1838 0 warn "Can't create file '$fpath_out' to save program output: $!, skipped saving program output";
1839 0 return;
1840 }
1841 0 ▶ unless (sysopen $fh_meta, $fpath_meta, Fcntl::O_WRONLY() | Fcntl::O_CREAT() | Fcntl::O_EXCL()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen $fh_meta, $fpath_meta, 193) 0 0 0
1842 0 warn "Can't create file '$fpath_meta' to save program output meta information: $!, skipped saving program output";
1843 0 unlink $fpath_out;
1844 0 return;
1845 }
1846 0 last;
1847 }
1848 }
1849  
1850 0 require JSON::MaybeXS;
1851 0 state $json = JSON::MaybeXS->new->allow_nonref;
1852  
1853 0 my $out = $self->cleanser->clone_and_clean($r->{res});
1854 my $meta = {
1855 time => $time,
1856 pid => $$,
1857 argv => $r->{orig_argv},
1858 read_env => $r->{read_env},
1859 read_config => $r->{read_config},
1860 read_config_files => $r->{read_config_files},
1861 0 };
1862 0 log_trace "Saving program output to %s ...", $fpath_out;
1863 0 print $fh_out $json->encode($out);
1864 0 log_trace "Saving program output's meta information to %s ...", $fpath_meta;
1865 0 print $fh_meta $json->encode($meta);
1866 }
1867  
1868 85 ▶sub display_result {
Subroutine display_result: called CC=17 SCAR=36.7
Pod undocumented
1869 85 require Data::Sah::Util::Type;
1870  
1871 85 my ($self, $r) = @_;
1872  
1873 85 my $meta = $r->{meta};
1874 85 my $res = $r->{res};
1875 85 my $fres = $r->{fres};
1876 85 ▶ my $resmeta = $res->[3] // {};
Condition50%1/2
$res->[3] // {}
l!l
850
MC/DC0%0/1
$res->[3] // {}
$res->[3]
Truth table50%1/2
A
A= $res->[3]
Aresult
11
00
1877  
1878 85 my $handle = $r->{output_handle};
1879  
1880 85 ▶ my $sch = $meta->{result}{schema} // $resmeta->{schema};
Condition66%2/3
$meta->{"result"}{"schema"} // $resmeta->{"schema"}
l!l&&r!l&&!r
9076
MC/DC50%1/2
$meta->{"result"}{"schema"} // $resmeta->{"schema"}
$meta->{"result"}{"schema"} $resmeta->{"schema"}
Truth table66%2/3
A // B
A= $meta->{"result"}{"schema"}B= $resmeta->{"schema"}
ABresult
1X1
011
000
1881 85 ▶ my $type = Data::Sah::Util::Type::get_type($sch) // '';
Condition100%2/2
Data::Sah::Util::Type::get_type($sch) // ""
l!l
976
MC/DC100%1/1
Data::Sah::Util::Type::get_type($sch) // ""
Data::Sah::Util::Type::get_type($sch)
Truth table100%2/2
A
A= Data::Sah::Util::Type::get_type($sch)
Aresult
11
00
1882  
1883 85 ▶ if ($resmeta->{stream} // $meta->{result}{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resmeta->{'stream'} // $meta->{'result'}{'stream'}) { } 2 83 85
Condition100%3/3
$resmeta->{"stream"} // $meta->{"result"}{"stream"}
l!l&&r!l&&!r
7528
MC/DC100%2/2
$resmeta->{"stream"} // $meta->{"result"}{"stream"}
$resmeta->{"stream"} $meta->{"result"}{"stream"}
Truth table100%3/3
A // B
A= $resmeta->{"stream"}B= $meta->{"result"}{"stream"}
ABresult
1X1
011
000
1884 2 my $x = $res->[2];
1885 2 ▶ if (ref($x) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x eq 'CODE') { } 2 0 2
1886 2 ▶ if (Data::Sah::Util::Type::is_simple($sch)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Data::Sah::Util::Type::is_simple($sch)) { } 0 2 2
1887 0 ▶ while (defined(my $l = $x->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = &$x())) 0 0 0
1888 0 print $l;
1889 0 ▶ print "\n" unless $type eq 'buf';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "buf" 0 0 0
1890 }
1891 } else {
1892 2 require JSON::MaybeXS;
1893 2 state $json = JSON::MaybeXS->new->allow_nonref;
1894 2 ▶ if ($self->use_cleanser) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_cleanser) { } 2 0 2
1895 2 ▶ while (defined(my $rec = $x->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = &$x())) 6 2 8
1896 6 print $json->encode(
1897 $self->cleanser->clone_and_clean($rec)), "\n";
1898 }
1899 } else {
1900 0 ▶ while (defined(my $rec = $x->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $rec = &$x())) 0 0 0
1901 0 print $json->encode($rec), "\n";
1902 }
1903 }
1904 }
1905 } else {
1906 0 die "Result is a stream but no coderef provided";
1907 }
1908 } else {
1909 # do preprocessing based on content_type. should probably be moved
1910 # elsewhere later.
1911 PREPROCESS_RESULT: {
1912 83 ▶ last unless defined $r->{viewer};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"viewer"} 83 0 83
1913  
1914 0 ▶ my $ct = $resmeta->{content_type} // '';
Condition0%0/2
$resmeta->{"content_type"} // ""
l!l
00
MC/DC0%0/1
$resmeta->{"content_type"} // ""
$resmeta->{"content_type"}
Truth table0%0/2
A
A= $resmeta->{"content_type"}
Aresult
11
00
1915 0 ▶ if ($ct eq 'text/x-org') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq "text/x-org") 0 0 0
1916 0 $fres = "# -*- mode: org -*-\n" . $fres;
1917 }
1918 }
1919  
1920 83 print $handle $fres;
1921 83 ▶ if (defined $r->{viewer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r->{"viewer"}) 0 83 83
1922 0 require ShellQuote::Any::Tiny;
1923 0 my $cmd = ShellQuote::Any::Tiny::shell_quote($r->{viewer}) ." ". ShellQuote::Any::Tiny::shell_quote($r->{viewer_temp_path});
1924 0 system $cmd;
1925 }
1926 }
1927 }
1928  
1929 85 ▶sub _format {
Subroutine _format: called CC=12 SCAR=26.1
1930 85 my ($self, $r) = @_;
1931  
1932 85 ▶ my $is_success = $r->{res}[0] =~ /\A2/ || $r->{res}[0] == 304;
Condition66%2/3
$r->{'res'}[0] =~ /\A2/ || $r->{'res'}[0] == 304
l!l&&r!l&&!r
70015
MC/DC50%1/2
$r->{'res'}[0] =~ /\A2/ || $r->{'res'}[0] == 304
$r->{'res'}[0] =~ /\A2/ $r->{'res'}[0] == 304
Truth table66%2/3
A || B
A= $r->{'res'}[0] =~ /\A2/B= $r->{'res'}[0] == 304
ABresult
1X1
011
000
1933  
1934 85 ▶ if (defined $ENV{PERINCI_CMDLINE_OUTPUT_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"PERINCI_CMDLINE_OUTPUT_DIR"}) 0 85 85
1935 0 $self->cmdline->save_output($r);
1936 }
1937  
1938 85 ▶ if ($is_success &&
Branch: 2 branches
BranchTrueFalseTotal
if ($is_success and $self->skip_format || $r->{'meta'}{'cmdline.skip_format'} || $r->{'res'}[3]{'cmdline.skip_format'}) { } 8 77 85
elsif ($is_success and $r->{'res'}[3]{'stream'} // $r->{'meta'}{'result'}{'stream'}) { } 2 75 77
Condition66%6/9
$is_success and $self->skip_format || $r->{"meta"}{"cmdline.skip_format"} || $r->{"res"}[3]{"cmdline.skip_format"}
!ll&&!rl&&r
15628
$self->skip_format or $r->{"meta"}{"cmdline.skip_format"} or $r->{"res"}[3]{"cmdline.skip_format"}
l!l&&r!l&&!r
0862
$self->skip_format or $r->{"meta"}{"cmdline.skip_format"}
l!l&&r!l&&!r
0070
MC/DC50%2/4
$is_success and $self->skip_format || $r->{"meta"}{"cmdline.skip_format"} || $r->{"res"}[3]{"cmdline.skip_format"}
$is_success $self->skip_format $r->{"meta"}{"cmdline.skip_format"} $r->{"res"}[3]{"cmdline.skip_format"}
Truth table60%3/5
A and B or C or D
A= $is_successB= $self->skip_formatC= $r->{"meta"}{"cmdline.skip_format"}D= $r->{"res"}[3]{"cmdline.skip_format"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1939 ($self->skip_format ||
1940 $r->{meta}{'cmdline.skip_format'} ||
1941 $r->{res}[3]{'cmdline.skip_format'})) {
1942 8 ▶ $r->{fres} = $r->{res}[2] // '';
Condition50%1/2
$r->{"res"}[2] // ""
l!l
80
MC/DC0%0/1
$r->{"res"}[2] // ""
$r->{"res"}[2]
Truth table50%1/2
A
A= $r->{"res"}[2]
Aresult
11
00
1943 ▶ } elsif ($is_success &&
Condition83%5/6
$is_success and $r->{"res"}[3]{"stream"} // $r->{"meta"}{"result"}{"stream"}
!ll&&!rl&&r
15602
$r->{"res"}[3]{"stream"} // $r->{"meta"}{"result"}{"stream"}
l!l&&r!l&&!r
0260
MC/DC66%2/3
$is_success and $r->{"res"}[3]{"stream"} // $r->{"meta"}{"result"}{"stream"}
$is_success $r->{"res"}[3]{"stream"} $r->{"meta"}{"result"}{"stream"}
Truth table75%3/4
A and B // C
A= $is_successB= $r->{"res"}[3]{"stream"}C= $r->{"meta"}{"result"}{"stream"}
ABCresult
0XX0
1000
11X1
1011
1944 ($r->{res}[3]{stream} // $r->{meta}{result}{stream})) {
1945 # stream will be formatted as displayed by display_result()
1946 }else {
1947 75 log_trace("[pericmd] Running hook_format_result ...");
1948 75 $r->{res}[3]{stream} = 0;
1949 75 ▶ $r->{fres} = $self->hook_format_result($r) // '';
Condition50%1/2
$self->hook_format_result($r) // ""
l!l
750
MC/DC0%0/1
$self->hook_format_result($r) // ""
$self->hook_format_result($r)
Truth table50%1/2
A
A= $self->hook_format_result($r)
Aresult
11
00
1950 }
1951 85 $self->select_output_handle($r);
1952 85 log_trace("[pericmd] Running hook_display_result ...");
1953 85 $self->hook_display_result($r);
1954 }
1955  
1956 85 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
1957 85 my ($self) = @_;
1958  
1959 85 my $co = $self->common_opts;
1960  
1961 85 $r = {
1962 orig_argv => [@ARGV],
1963 common_opts => $co,
1964 plugin_instances => \@Plugin_Instances,
1965 handlers => \%Handlers,
1966 cmdline => $self,
1967 };
1968  
1969 85 $self->_plugin_run_event(
1970 name => 'run',
1971 );
1972 3 $r->{res};
1973 }
1974  
1975 87 1;
1976 # ABSTRACT: Base class for Perinci::CmdLine{::Classic,::Lite}
1977  
1978 __END__
« blib/lib/Perinci/CmdLine/Plugin/Run/Normal.pm ↑ Summary blib/lib/Perinci/CmdLine/Lite.pm »