Mnet/Opts/Cli.pm

filter: Statementstmt 92.3% 241 / 261 Branchbran 75.3% 107 / 142 Conditioncond 71.2% 67 / 94 MC/DCmcdc 46.3% 25 / 54 Subroutinesub 100% 20 / 20 Podpod 66.6% 2 / 3 totaltotal 80.4% 462 / 574 CC 119 SCAR 41.1
CC
119
Cov
84%
_new_help
88.8
new
81.5
_define_help_usage
9.0
CRAP 60.9
↑ Summary Mnet/Log.pm »
1 package Mnet::Opts::Cli;
2  
3 =head1 NAME
4  
5 Mnet::Opts::Cli - Define and parse command line options
6  
7 =head1 SYNOPSIS
8  
9 # required to use this module
10 use Mnet::Opts::Cli;
11  
12 # define --sample cli option
13 Mnet::Opts::Cli::define({
14 getopt => "sample=s",
15 default => "",
16 help_tip => "set to input string",
17 help_text => "
18 use --sample to set an input string
19 refer to perldoc for more information
20 ",
21 });
22  
23 # optional environment variable can also be parsed for options
24 my $env = "Mnet";
25  
26 # call in list context for cli opts object and any extra args
27 my ($cli, @extras) = Mnet::Opts::Cli->new($env);
28  
29 # call in scalar context to disallow extra args
30 $cli = Mnet::Opts::Cli->new($env);
31  
32 # access parsed cli options using method calls
33 my $value = $cli->sample;
34  
35 =head1 DESCRIPTIONS
36  
37 Mnet::Opts::Cli can be used by scripts to define and parse command line
38 options, as shown in the example above.
39  
40 An optional environment variable can be used to set options, as shown in the
41 example above. This can be to secure passwords so they don't appear in system
42 process table, as below:
43  
44 export Mnet="--password secret"
45 script.pl
46  
47 The environment variable is deleted after it is parsed, to avoid sub-processes
48 called by user script having access to its contents.
49  
50 Note that the specified environment variable is not parsed if the --test option
51 is set on the command line. Refer to L<Mnet::Test> for more information.
52  
53 =head1 METHODS
54  
55 Mnet::Opts::Cli implements the methods listed below.
56  
57 =cut
58  
59 # required modules, inherits from Mnet::Opts
60 145 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 145 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 145 ▶use parent 'Mnet::Opts';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 145 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 145 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 145 ▶use Mnet;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 145 ▶use Mnet::Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 145 ▶use Mnet::Log::Conditional qw( DEBUG INFO WARN FATAL NOTICE );
Subroutine BEGIN: called CC=1 SCAR=0.0
68 145 ▶use Mnet::Opts::Cli::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 145 ▶use Mnet::Opts::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 145 ▶use Mnet::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 145 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73  
74  
75 # init global varviables in begin block
76 # $defined: set before other init blocks call Mnet::Opts::Cli::defined()
77 # @argv: copy of original @ARGV for Mnet::Opts::Cli->new, before script runs
78 145 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
79 145 my $defined = {};
80 145 our @argv = @ARGV;
81 }
82  
83  
84  
85 # init cli options used by this module
86 144 ▶INIT {
Subroutine INIT: called CC=3 SCAR=11.0
87  
88 # init stdout file handle to bypass Mnet::Tee output capture if loaded
89 144 our $stdout = undef;
90 144 ▶ if ($INC{"Mnet/Tee.pm"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'Mnet/Tee.pm'}) { } 76 68 144
91 76 $stdout = $Mnet::Tee::stdout;
92 } else {
93 68 ▶ open($stdout, ">&STDOUT") or die "error duping stdout, $!";
Branch: 1 branch
BranchTrueFalseTotal
open $Mnet::Opts::Cli::stdout, ">&STDOUT" or die "error duping stdout, $!" 68 0 68
94 }
95  
96 # define --help cli option
97 144 Mnet::Opts::Cli::define({
98 getopt => 'help:s',
99 help_tip => 'display option help, *try --help help',
100 });
101  
102 # define --version cli option
103 144 Mnet::Opts::Cli::define({
104 getopt => 'version',
105 help_tip => 'display version and system information',
106 });
107  
108 # finished init code block
109 }
110  
111  
112  
113 1322 ▶sub define {
Subroutine define: called CC=7 SCAR=19.9
Pod documented
114  
115 =head2 Mnet::Opts::Cli::define
116  
117 Mnet::Opts::Cli::define(\%specs)
118  
119 This function may be used during initialization to define cli options which can
120 be parsed by the Mnet::Opts->cli class method in this module, as in the example
121 which follows that define a --sample string option:
122  
123 use Mnet::Cli::Opts;
124 Mnet::Opts::Cli::define({ getopt => 'sample=s' });
125  
126 An error is issued if an option with the same name has already been defined.
127  
128 Note that getopt option names defined with this function must start with a
129 letter and contain only letters, numbers, and the dash character. Dashes are
130 replaced with underscores after the options are parsed, so they may be referred
131 to more easily in script code.
132  
133 The following L<Getopt::Long> option specification types are supported:
134  
135 opt --opt boolean option, set true if --opt is set
136 opt! --[no]opt negatable option, returns false if --noopt is set
137 opt=i --opt <i> required integer, error if input value is not set
138 opt:i --opt [i] optional integer, returns null if value not set
139 opt=s --opt <s> required string, error if input value is not set
140 opt:s --opt [s] optional string, returns null if value not set
141  
142 The following keys in the specs input hash reference argument are supported:
143  
144 getopt required option name and type, see perldoc Getopt::Long
145 default default value for option, defaults to undefined
146 help_hide set to hide option in --help list of available options
147 help_tip short tip text for --help list of available options
148 help_text longer help text to show in --help for specific options
149 record set so option is saved in Mnet::Test record/replay files
150 redact set to prevent option value showing in Mnet::Log output
151  
152 Refer to L<Getopt::Long> for more information.
153  
154 =cut
155  
156 # read input option definition specs
157 1322 ▶ my $specs = shift or croak("missing specs arg");
Branch: 1 branch
BranchTrueFalseTotal
my $specs = shift() or croak("missing specs arg") 1322 0 1322
158 1322 ▶ croak("invalid specs hash ref") if ref($specs) ne "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $specs ne "HASH" 0 1322 1322
159  
160 # check for required getopt key in input specs, note opt name
161 1322 ▶ croak("missing specs hash getopt key") if not defined $specs->{getopt};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $specs->{"getopt"} 0 1322 1322
162 croak("invalid specs hash getopt value $specs->{getopt}")
163 1322 ▶ if $specs->{getopt} !~ /^([a-z0-9](-|[a-z0-9])*)(!|=i|:i|=s|:s)?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $specs->{"getopt"} =~ /^([a-z0-9](-|[a-z0-9])*)(!|=i|:i|=s|:s)?$/ 0 1322 1322
164 1322 my $opt = $1;
165  
166 # abort if option was already defined
167 # options defined differently in multiple places would cause problems
168 croak("option $opt defined by $Mnet::Opts::Cli::defined->{$opt}->{caller}")
169 1322 ▶ if exists $Mnet::Opts::Cli::defined->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Mnet::Opts::Cli::defined->{$opt} 0 1322 1322
170  
171 # copy input specs to global var holding defined options
172 1322 $Mnet::Opts::Cli::defined->{$opt} = Storable::dclone($specs);
173  
174 # set caller for defined option
175 1322 $Mnet::Opts::Cli::defined->{$opt}->{caller} = caller;
176  
177 # set default help_tip if not defined
178 1322 ▶ if (not defined $Mnet::Opts::Cli::defined->{$opt}->{help_text}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Mnet::Opts::Cli::defined->{$opt}{"help_text"}) 430 892 1322
179 430 my $caller = $Mnet::Opts::Cli::defined->{$opt}->{caller};
180 430 my $text = "option defined by caller $caller, no help_text was set";
181 430 $Mnet::Opts::Cli::defined->{$opt}->{help_text} = $text;
182 }
183  
184 # set help_usage for defined option
185 # note that this aborts with an error for unsupported Getopt::Long types
186 $Mnet::Opts::Cli::defined->{$opt}->{help_usage}
187 1322 = _define_help_usage($Mnet::Opts::Cli::defined->{$opt}->{getopt});
188  
189 # finished Mnet::Opts::Cli::define
190 1322 return;
191 }
192  
193  
194  
195 1322 ▶sub _define_help_usage {
Subroutine _define_help_usage: called CC=9 SCAR=22.0
196  
197 # $help_usage = _define_help_usage($getopt)
198 # purpose: output help usage syntax given input getopt string
199 # $getopt: input option specification in Getopt::Long format
200 # $help_usage: output option usage syntax, used for --help output
201 # note: aborts with an error for unsupported Getopt::Long specs
202  
203 # read input getopt spec string
204 1322 ▶ my $getopt = shift // die "missing getopt arg";
Condition50%1/2
shift() // die("missing getopt arg")
l!l
13220
MC/DC0%0/1
shift() // die("missing getopt arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
205  
206 # init output help usage string for supported getopt types
207 1322 my $help_usage = undef;
208 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+)$/) { $help_usage = $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+)$/) 244 1078 1322
209 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+)\!$/) { $help_usage = "[no]$1"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+)\!$/) 360 962 1322
210 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+)=i$/) { $help_usage = "$1 <i>"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+)=i$/) 64 1258 1322
211 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+):i$/) { $help_usage = "$1 [i]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+):i$/) 6 1316 1322
212 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+)=s$/) { $help_usage = "$1 <s>"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+)=s$/) 255 1067 1322
213 1322 ▶ if ($getopt =~ /^([a-z][-a-z0-9]+):s$/) { $help_usage = "$1 [s]"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($getopt =~ /^([a-z][-a-z0-9]+):s$/) 393 929 1322
214  
215 # abort if unable to determine help usage
216 1322 ▶ croak("invalid or unsupported specs hash getopt value $getopt")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $help_usage 0 1322 1322
217 if not defined $help_usage;
218  
219 # finished _define_help_usage, return help_usage
220 1322 return $help_usage;
221 }
222  
223  
224 154 ▶sub new {
Subroutine new: called CC=77 SCAR=44.0
Pod documented
225  
226 =head2 new
227  
228 $opts = Mnet::Opts::Cli->new($env_var)
229 or ($cli, @extras) = Mnet::Opts::Cli->new($env_var)
230  
231 The new class method may be used to retrieve an options object containing
232 defined options parsed from the command line and an array contining any extra
233 command line arguments.
234  
235 The env_var argument is optional, and can be set to the name of an environment
236 variable where additional command line options can be securely set. This gets
237 deleted after it is parsed to avoid sub-processes being able to access it.
238  
239 If called in list context this method will return an opts object containing
240 values for defined options parsed from the command line followed by a list of
241 any other extra arguments that were present on the command line.
242  
243 use Mnet::Opts::Cli;
244 my ($cli, @extras) = Mnet::Opts::Cli->new();
245  
246 If not called in list context an error will be issued if extra command line
247 arguments exist.
248  
249 use Mnet::Opts::Cli;
250 my $cli = Mnet::Opts::Cli->new();
251  
252 Options are applied in the following order:
253  
254 child Mnet::Batch command lines
255 command line
256 replayed command line
257 optional environment variable
258 Mnet::Opts::Set use pragmas
259 Mnet::Opts::Cli::define default key
260  
261 Note that errors are not issued for unknown options that may be set for other
262 scripts in the optional env_var environment variable. Also note that this
263 environment variable is not parsed if the --test option is set on the command
264 line.
265  
266 The perl ARGV array is not modified by this module.
267  
268 =cut
269  
270 # read input class and optional envrionement variable name
271 154 ▶ my $class = shift // croak("missing class arg");
Condition50%1/2
shift() // croak("missing class arg")
l!l
1540
MC/DC0%0/1
shift() // croak("missing class arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
272 154 my $env_var = shift;
273  
274 # read batch child opts from Mnet::Opts::Cli::batch() only
275 # refer to Mnet::Opts::Cli::batch() for more information
276 154 my $batch_argv = shift;
277 154 ▶ croak("invalid call with batch_argv")
Branch: 1 branch
BranchTrueFalseTotal
if defined $batch_argv and caller ne "Mnet::Opts::Cli" 0 154 154
Condition66%2/3
defined $batch_argv and caller ne "Mnet::Opts::Cli"
!ll&&!rl&&r
14590
MC/DC0%0/2
defined $batch_argv and caller ne "Mnet::Opts::Cli"
defined $batch_argv caller ne "Mnet::Opts::Cli"
Truth table66%2/3
A and B
A= defined $batch_argvB= caller ne "Mnet::Opts::Cli"
ABresult
0X0
100
111
278 if defined $batch_argv and caller ne "Mnet::Opts::Cli";
279  
280 # returned cached cli options and extra args, if set from prior call
281 # output extra cli arg error when not called to return extras args
282 154 ▶ if (Mnet::Opts::Cli::Cache::get()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Mnet::Opts::Cli::Cache::get()) 1 153 154
283 1 my ($opts, @extras) = Mnet::Opts::Cli::Cache::get();
284 1 my $self = bless $opts, $class;
285 1 ▶ die "invalid extra args @extras\n" if $extras[0] and not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $extras[0] and not wantarray 0 1 1
Condition33%1/3
$extras[0] and not wantarray
!ll&&!rl&&r
100
MC/DC0%0/2
$extras[0] and not wantarray
$extras[0] not wantarray
Truth table33%1/3
A and not(B)
A= $extras[0]B= not wantarray
ABresult
0X0
100
111
286 1 ▶ return wantarray ? ($self, @extras) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
287 }
288  
289 # configure how command line parsing will work
290 153 Getopt::Long::Configure(qw/
291 no_auto_abbrev
292 no_getopt_compat
293 no_gnu_compat
294 no_ignore_case
295 pass_through
296 prefix=--
297 /);
298  
299 # note all getopt definitions, set from Mnet::Opt::Cli::define()
300 153 my @definitions = ();
301 153 foreach my $opt (keys %{$Mnet::Opts::Cli::defined}) {
302 push @definitions, $Mnet::Opts::Cli::defined->{$opt}->{getopt}
303 1439 }
304  
305 # parse options from command line, also note extra args on command line
306 # remove -- as the first extra options, used to stop option parsing
307 153 my ($cli_opts, @extras) = ({}, @Mnet::Opts::Cli::argv);
308 153 Getopt::Long::GetOptionsFromArray(\@extras, $cli_opts, @definitions);
309 153 ▶ shift @extras if defined $extras[0] and $extras[0] eq "--";
Branch: 1 branch
BranchTrueFalseTotal
if defined $extras[0] and $extras[0] eq "--" 0 153 153
Condition66%2/3
defined $extras[0] and $extras[0] eq "--"
!ll&&!rl&&r
114390
MC/DC0%0/2
defined $extras[0] and $extras[0] eq "--"
defined $extras[0] $extras[0] eq "--"
Truth table66%2/3
A and B
A= defined $extras[0]B= $extras[0] eq "--"
ABresult
0X0
100
111
310  
311 # merge child process batch_argv opt and extras onto of cli opts and extras
312 153 my $batch_opts = {};
313 153 ▶ my (undef, $batch_extras) = Getopt::Long::GetOptionsFromString(
Branch: 1 branch
BranchTrueFalseTotal
if defined $batch_argv 9 144 153
314 $batch_argv, $batch_opts, @definitions
315 ) if defined $batch_argv;
316 153 push @extras, $_ foreach @$batch_extras;
317 153 $cli_opts->{$_} = $batch_opts->{$_} foreach keys %$batch_opts;
318  
319 # enable filtered test logging if --test/record/replay cli opts are set
320 # adding Mnet::Log::Test to INC causes Mnet::Log to filter test outputs
321 # update coment in END block of Mnet::Log END if this changes
322 153 ▶ if (defined $cli_opts->{record} or $cli_opts->{replay}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cli_opts->{"record"} or $cli_opts->{"replay"} or $cli_opts->{"test"}) 36 117 153
Condition100%6/6
defined $cli_opts->{"record"} or $cli_opts->{"replay"} or $cli_opts->{"test"}
l!l&&r!l&&!r
2214117
defined $cli_opts->{"record"} or $cli_opts->{"replay"}
l!l&&r!l&&!r
814131
MC/DC100%3/3
defined $cli_opts->{"record"} or $cli_opts->{"replay"} or $cli_opts->{"test"}
defined $cli_opts->{"record"} $cli_opts->{"replay"} $cli_opts->{"test"}
Truth table100%4/4
A or B or C
A= defined $cli_opts->{"record"}B= $cli_opts->{"replay"}C= $cli_opts->{"test"}
ABCresult
1XX1
01X1
0011
0000
323 or $cli_opts->{test}) {
324 36 ▶ if ($INC{"Mnet/Log.pm"}
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Mnet/Log.pm"} and $INC{"Mnet/Log.pm"} =~ m[^(.*Mnet/Log)\.pm$]) 33 3 36
Condition66%2/3
$INC{"Mnet/Log.pm"} and $INC{"Mnet/Log.pm"} =~ m[^(.*Mnet/Log)\.pm$]
!ll&&!rl&&r
3033
MC/DC50%1/2
$INC{"Mnet/Log.pm"} and $INC{"Mnet/Log.pm"} =~ m[^(.*Mnet/Log)\.pm$]
$INC{"Mnet/Log.pm"} $INC{"Mnet/Log.pm"} =~ m[^(.*Mnet/Log)\.pm$]
Truth table66%2/3
A and B
A= $INC{"Mnet/Log.pm"}B= $INC{"Mnet/Log.pm"} =~ m[^(.*Mnet/Log)\.pm$]
ABresult
0X0
100
111
325 and $INC{"Mnet/Log.pm"} =~ /^(.*Mnet\/Log)\.pm$/) {
326 33 $INC{"Mnet/Log/Test.pm"} = "$1\/Test.pm";
327 }
328 }
329  
330 # enable silent pragma based on --silent, --help, and --version cli options
331 # this is done so Mnet::Test and Mnet::Log has access to this
332 # remove quiet pragma if --silent option is set on command line
333 # remove silent pragma if --nosilent option was set on cli
334 153 ▶ if ($cli_opts->{silent}
Branch: 2 branches
BranchTrueFalseTotal
if ($cli_opts->{'silent'} or defined $cli_opts->{'help'} or $cli_opts->{'version'}) { } 5 148 153
elsif (defined $cli_opts->{'silent'} and not $cli_opts->{'silent'}) { } 0 296 296
Condition100%6/6
$cli_opts->{"silent"} or defined $cli_opts->{"help"} or $cli_opts->{"version"}
l!l&&r!l&&!r
41148
$cli_opts->{"silent"} or defined $cli_opts->{"help"}
l!l&&r!l&&!r
31149
MC/DC100%3/3
$cli_opts->{"silent"} or defined $cli_opts->{"help"} or $cli_opts->{"version"}
$cli_opts->{"silent"} defined $cli_opts->{"help"} $cli_opts->{"version"}
Truth table100%4/4
A or B or C
A= $cli_opts->{"silent"}B= defined $cli_opts->{"help"}C= $cli_opts->{"version"}
ABCresult
1XX1
01X1
0011
0000
335 or defined $cli_opts->{help} or $cli_opts->{version}) {
336 5 delete $INC{"Mnet/Opts/Set/Quiet.pm"};
337 5 Mnet::Opts::Set::enable("silent");
338 ▶ } elsif (defined $cli_opts->{silent} and not $cli_opts->{silent}) {
Condition33%1/3
defined $cli_opts->{"silent"} and not $cli_opts->{"silent"}
!ll&&!rl&&r
14800
MC/DC0%0/2
defined $cli_opts->{"silent"} and not $cli_opts->{"silent"}
defined $cli_opts->{"silent"} not $cli_opts->{"silent"}
Truth table33%1/3
A and not(B)
A= defined $cli_opts->{"silent"}B= not $cli_opts->{"silent"}
ABresult
0X0
100
111
339 0 delete $INC{"Mnet/Opts/Set/Silent.pm"};
340 }
341  
342 # enable quiet pragma based on --quiet cli options
343 # this is done so Mnet::Test and Mnet::Log has access to this
344 # remove silent pragma if --quiet option is set on command line
345 # remove quiet and silent pragmas if --noquiet option was set on cli
346 153 ▶ if ($cli_opts->{quiet}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cli_opts->{'quiet'}) { } 24 129 153
elsif (defined $cli_opts->{'quiet'} and not $cli_opts->{'quiet'}) { } 14 230 244
347 24 delete $INC{"Mnet/Opts/Set/Silent.pm"};
348 24 Mnet::Opts::Set::enable("quiet");
349 ▶ } elsif (defined $cli_opts->{quiet} and not $cli_opts->{quiet}) {
Condition66%2/3
defined $cli_opts->{"quiet"} and not $cli_opts->{"quiet"}
!ll&&!rl&&r
115014
MC/DC50%1/2
defined $cli_opts->{"quiet"} and not $cli_opts->{"quiet"}
defined $cli_opts->{"quiet"} not $cli_opts->{"quiet"}
Truth table66%2/3
A and not(B)
A= defined $cli_opts->{"quiet"}B= not $cli_opts->{"quiet"}
ABresult
0X0
100
111
350 14 delete $INC{"Mnet/Opts/Set/Quiet.pm"};
351 14 delete $INC{"Mnet/Opts/Set/Silent.pm"};
352 }
353  
354 # parse options set via Mnet::Opts::Set pragma sub-modules
355 153 my $pragma_opts = Mnet::Opts::Set::pragmas();
356  
357 # output --help if set on command line and exit
358 # disable log entries during help by setting --quiet in cli cache
359 153 ▶ if (defined $cli_opts->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cli_opts->{"help"}) 1 152 153
360 1 Mnet::Opts::Cli::Cache::set({ quiet => 1 });
361 1 my $help = _new_help($cli_opts->{help});
362 1 syswrite $Mnet::Opts::Cli::stdout, "$help\n";
363 1 exit 1;
364 }
365  
366 # output --version info if set on command line and exit
367 # disable log entries during version by setting --quiet in cli cache
368 152 ▶ if ($cli_opts->{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cli_opts->{"version"}) 1 151 152
369 1 Mnet::Opts::Cli::Cache::set({ quiet => 1 });
370 1 my $info = Mnet::Version::info();
371 1 $info =~ s/^/ /mg;
372 1 syswrite $Mnet::Opts::Cli::stdout, "\n$info\n";
373 1 exit 1;
374 }
375  
376 # init cached opts from cli opts only for now, and initial logger
377 # this allows --debug option from cli when reading replay data
378 # this is ok because log env opts are not parse if replay is set
379 151 my $init_log_opts = $pragma_opts;
380 151 $init_log_opts->{$_} = $cli_opts->{$_} foreach keys %$cli_opts;
381 151 Mnet::Opts::Cli::Cache::set($init_log_opts);
382 151 my $log = Mnet::Log::Conditional->new();
383  
384 # parse options from --replay file only if --test is also set
385 # force a re-read of the --replay file for forked batch children
386 # ignore --replay file options that weren't defined with record set
387 151 my $replay_opts = {};
388 151 ▶ if (defined $cli_opts->{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cli_opts->{"replay"}) 14 137 151
389 14 my $test_data_opts = { replay => $cli_opts->{replay} };
390 14 my $test_data = Mnet::Test::data($test_data_opts, "force");
391 14 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
392 178 ▶ next if not $Mnet::Opts::Cli::defined->{$opt}->{record};
Branch: 1 branch
BranchTrueFalseTotal
unless $Mnet::Opts::Cli::defined->{$opt}{"record"} 168 10 178
393 10 ▶ next if not exists $test_data->{opts}->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $test_data->{"opts"}{$opt} 0 10 10
394 10 $replay_opts->{$opt} = $test_data->{opts}->{$opt};
395 10 my $opt_dump = Mnet::Dump::line($replay_opts->{$opt});
396 10 $log->debug("new found replay opt $opt = $opt_dump");
397 }
398 }
399  
400 # parse options from env_var if --test/record/replay opts are not set
401 # ignore warnings for env options that might not be defined at the moment
402 151 my $env_opts = {};
403 151 ▶ if (defined $env_var and defined $ENV{$env_var}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $env_var and defined $ENV{$env_var} and not defined $cli_opts->{"replay"} and not defined $cli_opts->{"record"}) 2 149 151
Condition66%6/9
defined $env_var and defined $ENV{$env_var} and not defined $cli_opts->{"replay"} and not defined $cli_opts->{"record"}
!ll&&!rl&&r
14902
defined $env_var and defined $ENV{$env_var} and not defined $cli_opts->{"replay"}
!ll&&!rl&&r
14902
defined $env_var and defined $ENV{$env_var}
!ll&&!rl&&r
14902
MC/DC25%1/4
defined $env_var and defined $ENV{$env_var} and not defined $cli_opts->{"replay"} and not defined $cli_opts->{"record"}
defined $env_var defined $ENV{$env_var} not defined $cli_opts->{"replay"} not defined $cli_opts->{"record"}
Truth table40%2/5
A and B and not(C) and not(D)
A= defined $env_varB= defined $ENV{$env_var}C= not defined $cli_opts->{"replay"}D= not defined $cli_opts->{"record"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
404 and not defined $cli_opts->{replay}
405 and not defined $cli_opts->{record}) {
406 2 eval {
407 2 local $SIG{__WARN__} = "IGNORE";
408 Getopt::Long::GetOptionsFromString(
409 2 $ENV{$env_var}, $env_opts, @definitions
410 );
411 }
412 }
413  
414 # delete env var after reading it
415 # env_var is meant to be used to securely pass secrets to mnet scripts
416 # user script cmd line options can be seen in ps ouptut, env vars cannot
417 # deleting env var here so that user script sub-processes can't access
418 # refer also to https://security.stackexchange.com/questions/197784
419 151 ▶ delete $ENV{$env_var} if defined $env_var and defined $ENV{$env_var};
Branch: 1 branch
BranchTrueFalseTotal
if defined $env_var and defined $ENV{$env_var} 2 149 151
Condition66%2/3
defined $env_var and defined $ENV{$env_var}
!ll&&!rl&&r
14902
MC/DC50%1/2
defined $env_var and defined $ENV{$env_var}
defined $env_var defined $ENV{$env_var}
Truth table66%2/3
A and B
A= defined $env_varB= defined $ENV{$env_var}
ABresult
0X0
100
111
420  
421 # prepare list to hold log entries, which will be output later
422 # log_entries keyed by opt name, set source keyword followed by dump
423 # to avoid catch-22 of properly logging opts before log opts are parsed
424 151 my $log_entries = {};
425  
426 # apply options in order of precedence, refer to perldoc for this method
427 # apply opt in order of precedence: cli, replay, env, pragma, and default
428 # log_entries hash ref is updated for each option, with source and dump
429 151 my $opts = {};
430 151 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
431 1435 my $defined_opt = $Mnet::Opts::Cli::defined->{$opt};
432  
433 # options can be reset with --test-reset cli option
434 1435 ▶ if ($cli_opts->{'test-reset'} and $cli_opts->{'test-reset'} eq $opt) {
Branch: 5 branches
BranchTrueFalseTotal
if ($cli_opts->{'test-reset'} and $cli_opts->{'test-reset'} eq $opt) { } 3 1432 1435
elsif (exists $cli_opts->{$opt}) { } 203 1229 1432
elsif (exists $replay_opts->{$opt}) { } 5 1224 1229
elsif (exists $env_opts->{$opt}) { } 1 1223 1224
elsif (exists $pragma_opts->{$opt}) { } 32 1191 1223
Condition100%3/3
$cli_opts->{"test-reset"} and $cli_opts->{"test-reset"} eq $opt
!ll&&!rl&&r
1407253
MC/DC100%2/2
$cli_opts->{"test-reset"} and $cli_opts->{"test-reset"} eq $opt
$cli_opts->{"test-reset"} $cli_opts->{"test-reset"} eq $opt
Truth table100%3/3
A and B
A= $cli_opts->{"test-reset"}B= $cli_opts->{"test-reset"} eq $opt
ABresult
0X0
100
111
435 3 $log_entries->{$opt} = "def";
436 3 $opts->{$opt} = $defined_opt->{default};
437  
438 # options from cli have highest priority
439 # this includes child batch command line options
440 } elsif (exists $cli_opts->{$opt}) {
441 203 $log_entries->{$opt} = "cli";
442 203 $opts->{$opt} = $cli_opts->{$opt};
443  
444 # recordable options from replay file are next
445 } elsif (exists $replay_opts->{$opt}) {
446 5 $log_entries->{$opt} = "cli";
447 5 $opts->{$opt} = $replay_opts->{$opt};
448  
449 # then environment variables, skipped for --test/record/replay options
450 } elsif (exists $env_opts->{$opt}) {
451 1 $log_entries->{$opt} = "env";
452 1 $opts->{$opt} = $env_opts->{$opt};
453  
454 # then pragma options, which can be overridden by the above
455 } elsif (exists $pragma_opts->{$opt}) {
456 32 $log_entries->{$opt} = "use";
457 32 $opts->{$opt} = $pragma_opts->{$opt};
458  
459 # finally defined default value for option is used
460 } else {
461 1191 $log_entries->{$opt} = "def";
462 1191 $opts->{$opt} = $defined_opt->{default};
463 }
464  
465 # note log entry for the current option, with any notes
466 # redact opts in log entries if defined and non-null
467 1435 my $opt_dump = Mnet::Dump::line($opts->{$opt});
468 1435 ▶ if (defined $defined_opt->{redact}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defined_opt->{"redact"}) 1 1434 1435
469 1 ▶ if (defined $opts->{$opt} and $opts->{$opt} ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{$opt} and $opts->{$opt} ne "") 1 0 1
Condition33%1/3
defined $opts->{$opt} and $opts->{$opt} ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $opts->{$opt} and $opts->{$opt} ne ""
defined $opts->{$opt} $opts->{$opt} ne ""
Truth table33%1/3
A and B
A= defined $opts->{$opt}B= $opts->{$opt} ne ""
ABresult
0X0
100
111
470 1 ▶ $opt_dump = "**** (redacted)" if defined $opts->{$opt}
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{$opt} 1 0 1
471 }
472 }
473 1435 $log_entries->{$opt} .= " $opt_dump";
474  
475  
476 # finish parsing loop through defined options
477 }
478  
479 # create logger object after updating cli opt cache with parsed opts
480 151 Mnet::Opts::Cli::Cache::set($opts);
481 151 $log = Mnet::Log::Conditional->new($opts);
482  
483 # log parsed options, as per prepared log_entries array
484 # log_entries keys are opt names, set to source keyword followed by dump
485 # default opts identified with 'def' log entry prefix are logged to debug
486 # notice entries used for opts that would interfere with Mnet::Test diffs
487 # so that opts without record set are not saved in --record file logs
488 151 foreach my $opt (sort keys %$log_entries) {
489 1435 my $log_entry = $log_entries->{$opt};
490 1435 $log_entry =~ s/(\S+)/opt $1 ${opt} =/;
491 1435 ▶ if ($log_entry =~ /^opt def/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($log_entry =~ /^opt def/) { } 1194 241 1435
elsif (not $Mnet::Opts::Cli::defined->{$opt}{'record'} and $opts->{'replay'} || $opts->{'record'} || $opts->{'batch'}) { } 98 143 241
492 1194 $log->debug("new parsed $log_entry");
493 ▶ } elsif (not $Mnet::Opts::Cli::defined->{$opt}->{record}
Condition100%9/9
not $Mnet::Opts::Cli::defined->{$opt}{"record"} and $opts->{"replay"} || $opts->{"record"} || $opts->{"batch"}
!ll&&!rl&&r
1612798
$opts->{"replay"} or $opts->{"record"} or $opts->{"batch"}
l!l&&r!l&&!r
4652127
$opts->{"replay"} or $opts->{"record"}
l!l&&r!l&&!r
3511179
MC/DC100%4/4
not $Mnet::Opts::Cli::defined->{$opt}{"record"} and $opts->{"replay"} || $opts->{"record"} || $opts->{"batch"}
not $Mnet::Opts::Cli::defined->{$opt}{"record"} $opts->{"replay"} $opts->{"record"} $opts->{"batch"}
Truth table100%5/5
not(A) and B or C or D
A= not $Mnet::Opts::Cli::defined->{$opt}{"record"}B= $opts->{"replay"}C= $opts->{"record"}D= $opts->{"batch"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
494 and ($opts->{replay} or $opts->{record}
495 or $opts->{batch})) {
496 98 $log->notice("new parsed $log_entry");
497 } else {
498 143 $log->info("new parsed $log_entry");
499 }
500 }
501  
502 # output extra cli arg error when not called to return extras args
503 151 ▶ die "invalid or missing args @extras\n" if $extras[0] and not wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $extras[0] and not wantarray 2 149 151
Condition100%3/3
$extras[0] and not wantarray
!ll&&!rl&&r
112372
MC/DC100%2/2
$extras[0] and not wantarray
$extras[0] not wantarray
Truth table100%3/3
A and not(B)
A= $extras[0]B= not wantarray
ABresult
0X0
100
111
504  
505 # get test data hash ref from Mnet::Test module
506 # init to empty dummy hash ref if Mnet::Test not loaded
507 149 my $test_data = {};
508 149 ▶ $test_data = Mnet::Test::data() if $INC{"Mnet/Test.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Test.pm"} 79 70 149
509  
510 # update opts in test in case --record will be used later
511 # skip opts that were defined without the record key enabled
512 149 $test_data->{opts} = {};
513 149 foreach my $opt (sort keys %$opts) {
514 1430 ▶ next if not $Mnet::Opts::Cli::defined->{$opt}->{record};
Branch: 1 branch
BranchTrueFalseTotal
unless $Mnet::Opts::Cli::defined->{$opt}{"record"} 1408 22 1430
515 22 my $value = $opts->{$opt};
516 22 my $default = $Mnet::Opts::Cli::defined->{$opt}->{default};
517 22 ▶ next if not defined $value and not defined $default;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value or defined $default 5 17 22
Condition66%2/3
defined $value or defined $default
l!l&&r!l&&!r
1705
MC/DC50%1/2
defined $value or defined $default
defined $value defined $default
Truth table66%2/3
A or B
A= defined $valueB= defined $default
ABresult
1X1
011
000
518 17 ▶ next if defined $value and defined $default and $value eq $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and defined $default and $value eq $default 1 16 17
Condition83%5/6
defined $value and defined $default and $value eq $default
!ll&&!rl&&r
1061
defined $value and defined $default
!ll&&!rl&&r
0107
MC/DC66%2/3
defined $value and defined $default and $value eq $default
defined $value defined $default $value eq $default
Truth table75%3/4
A and B and C
A= defined $valueB= defined $defaultC= $value eq $default
ABCresult
0XX0
10X0
1100
1111
519 16 $log->debug("new recordable cli opt $opt will save to Mnet::Test data");
520 16 $test_data->{opts}->{$opt} = $opts->{$opt};
521 }
522  
523 # use extra args from command line, otherwise look in --replay file
524 # replace extra args in replay file if we have new set of extra from cli
525 149 ▶ if (defined $extras[0]) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $extras[0]) { } 37 112 149
elsif (defined $opts->{'test-reset'} and $opts->{'test-reset'} eq '') { } 1 111 112
elsif (exists $test_data->{'extras'}) { } 3 108 111
526 37 $log->debug("new extra cli args will record from command line");
527 37 $test_data->{extras} = \@extras;
528 ▶ } elsif (defined $opts->{'test-reset'} and $opts->{'test-reset'} eq ""){
Condition100%3/3
defined $opts->{"test-reset"} and $opts->{"test-reset"} eq ""
!ll&&!rl&&r
10831
MC/DC100%2/2
defined $opts->{"test-reset"} and $opts->{"test-reset"} eq ""
defined $opts->{"test-reset"} $opts->{"test-reset"} eq ""
Truth table100%3/3
A and B
A= defined $opts->{"test-reset"}B= $opts->{"test-reset"} eq ""
ABresult
0X0
100
111
529 1 $log->debug("new extra cli args will reset via --test-reset");
530 1 delete $test_data->{extras};
531 } elsif (exists $test_data->{extras}) {
532 3 $log->debug("new extra cli args will replay from Mnet::Test data");
533 3 @extras = @{$test_data->{extras}};
534 }
535  
536 # log extra arguments in effect for rest of execution
537 $log->info("new parsed cli arg (extra) = ".Mnet::Dump::line($_))
538 149 foreach @extras;
539  
540 # change opt names that have a dash to an underscore
541 # i.e $opts->{opt_name} instead of $opts->{'opt-name'}
542 149 foreach my $opt (sort keys %$opts) {
543 1430 ▶ next if $opt !~ /-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt =~ /-/ 1134 296 1430
544 296 my $value = $opts->{$opt};
545 296 delete $opts->{$opt};
546 296 $opt =~ s/-/_/g;
547 296 $opts->{$opt} = $value;
548 }
549  
550 # update cli opt cache for the final time, including extra cli args
551 149 Mnet::Opts::Cli::Cache::set($opts, @extras);
552  
553 # disable --debug-error in Mnet::Log if not set on cli, to save memory
554 Mnet::Log::disable_debug_error()
555 149 ▶ if $INC{'Mnet/Log.pm'} and not $opts->{debug_error};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Mnet/Log.pm"} and not $opts->{"debug_error"} 126 23 149
Condition100%3/3
$INC{"Mnet/Log.pm"} and not $opts->{"debug_error"}
!ll&&!rl&&r
221126
MC/DC100%2/2
$INC{"Mnet/Log.pm"} and not $opts->{"debug_error"}
$INC{"Mnet/Log.pm"} not $opts->{"debug_error"}
Truth table100%3/3
A and not(B)
A= $INC{"Mnet/Log.pm"}B= not $opts->{"debug_error"}
ABresult
0X0
100
111
556  
557 149 my $self = bless $opts, $class;
558  
559 # finished new method, return cli opts object and extra args or just opts
560 149 ▶ return wantarray ? ($self, @extras) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 47 102 149
561 }
562  
563  
564  
565 1 ▶sub _new_help {
Subroutine _new_help: called CC=22 SCAR=44.9
566  
567 # $output = _new_help($help)
568 # purpose: output help for all defined options
569 # $help: from --help opt, null shows tip list, otherwise help text on matches
570 # $output: help text ready to display
571  
572  
573 # read input --help opt value
574 1 ▶ my $help = shift // "";
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
575  
576 # init output help text
577 1 my $output = "\n";
578  
579 # output list of help usage tips, one line per option
580 1 ▶ if ($help eq "" or $help eq "help") {
Branch: 1 branch
BranchTrueFalseTotal
if ($help eq '' or $help eq 'help') { } 1 0 1
Condition33%1/3
$help eq "" or $help eq "help"
l!l&&r!l&&!r
100
MC/DC0%0/2
$help eq "" or $help eq "help"
$help eq "" $help eq "help"
Truth table33%1/3
A or B
A= $help eq ""B= $help eq "help"
ABresult
1X1
011
000
581  
582 # calculate width to align usage and tip columns
583 1 my $width = 0;
584 1 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
585 2 my $defined_opt = $Mnet::Opts::Cli::defined->{$opt};
586 2 ▶ next if $width > length($defined_opt->{help_usage});
Branch: 1 branch
BranchTrueFalseTotal
if $width > length $defined_opt->{"help_usage"} 1 1 2
587 1 $width = length($defined_opt->{help_usage});
588 }
589  
590 # prepare sub to output help usage and tip aligned into columns
591 2 ▶ sub _new_help_tip {
Subroutine _new_help_tip: called CC=3 SCAR=11.3
592 2 my ($width, $defined_opt) = (shift, shift);
593 2 my $usage = $defined_opt->{help_usage};
594 2 ▶ my $tip = $defined_opt->{help_tip} // "";
Condition50%1/2
$defined_opt->{"help_tip"} // ""
l!l
20
MC/DC0%0/1
$defined_opt->{"help_tip"} // ""
$defined_opt->{"help_tip"}
Truth table50%1/2
A
A= $defined_opt->{"help_tip"}
Aresult
11
00
595 2 ▶ $tip = "*$tip" if $defined_opt->{help_hide};
Branch: 1 branch
BranchTrueFalseTotal
if $defined_opt->{"help_hide"} 0 2 2
596 2 return sprintf(" --%-${width}s $tip\n", $usage);
597 }
598  
599 # output non-Mnet script options, if any exist
600 1 my $other_options = "";
601 1 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
602 2 my $defined_opt = $Mnet::Opts::Cli::defined->{$opt};
603 2 ▶ next if $help ne "help" and $defined_opt->{help_hide};
Branch: 1 branch
BranchTrueFalseTotal
if $help ne "help" and $defined_opt->{"help_hide"} 0 2 2
Condition33%1/3
$help ne "help" and $defined_opt->{"help_hide"}
!ll&&!rl&&r
020
MC/DC0%0/2
$help ne "help" and $defined_opt->{"help_hide"}
$help ne "help" $defined_opt->{"help_hide"}
Truth table33%1/3
A and B
A= $help ne "help"B= $defined_opt->{"help_hide"}
ABresult
0X0
100
111
604 2 ▶ next if $defined_opt->{caller} =~ /^Mnet(::|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $defined_opt->{"caller"} =~ /^Mnet(::|$)/ 2 0 2
605 0 ▶ $other_options = "Script options:\n\n" if not $other_options;
Branch: 1 branch
BranchTrueFalseTotal
unless $other_options 0 0 0
606 0 $other_options .= _new_help_tip($width, $defined_opt);
607 }
608 1 ▶ $output .= "$other_options\n" if $other_options;
Branch: 1 branch
BranchTrueFalseTotal
if $other_options 0 1 1
609  
610 # output Mnet options
611 1 $output .= "Other options:\n\n";
612 1 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
613 2 my $defined_opt = $Mnet::Opts::Cli::defined->{$opt};
614 2 ▶ next if $help ne "help" and $defined_opt->{help_hide};
Branch: 1 branch
BranchTrueFalseTotal
if $help ne "help" and $defined_opt->{"help_hide"} 0 2 2
Condition33%1/3
$help ne "help" and $defined_opt->{"help_hide"}
!ll&&!rl&&r
020
MC/DC0%0/2
$help ne "help" and $defined_opt->{"help_hide"}
$help ne "help" $defined_opt->{"help_hide"}
Truth table33%1/3
A and B
A= $help ne "help"B= $defined_opt->{"help_hide"}
ABresult
0X0
100
111
615 2 ▶ next if $defined_opt->{caller} !~ /^Mnet(::|$)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $defined_opt->{"caller"} =~ /^Mnet(::|$)/ 0 2 2
616 2 $output .= _new_help_tip($width, $defined_opt);
617 }
618  
619 # output long form help text for options matching input --help value
620 } else {
621  
622 # loop through all options
623 0 foreach my $opt (sort keys %{$Mnet::Opts::Cli::defined}) {
624 0 my $defined_opt = $Mnet::Opts::Cli::defined->{$opt};
625  
626 # skip option names that don't match input --help text
627 0 ▶ next if $opt !~ /^\Q$help\E$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt =~ /^\Q$help\E$/ 0 0 0
628  
629 # output usage for current command
630 0 my $usage = $defined_opt->{help_usage};
631 0 $output .= " --$usage";
632  
633 # output if option is saved in --record files
634 0 my $recordable = "\n";
635 0 ▶ $recordable = " (recordable)\n" if $defined_opt->{record};
Branch: 1 branch
BranchTrueFalseTotal
if $defined_opt->{"record"} 0 0 0
636 0 $output .= $recordable;
637  
638 # output tip for current command, if defined
639 0 my $tip = $defined_opt->{help_tip};
640 0 ▶ $output .= "\n $tip\n" if defined $tip;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tip 0 0 0
641 0 ▶ $tip = " $tip\n" if $tip ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $tip ne "" 0 0 0
642  
643 # output long form help text for current command, if defined
644 0 my $text = $defined_opt->{help_text};
645 0 ▶ if (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
646 0 $text =~ s/(^(\n|\s)+|(\n|\s+)$)//g;
647 0 $text =~ s/^\s*/ /mg;
648 0 $output .= "\n$text\n\n";
649 }
650  
651 # continue looping through
652 }
653  
654 # finished creating help output
655 }
656  
657 # finished _new_help function, return output help text
658 1 return $output;
659 }
660  
661  
662  
663 9 ▶sub batch_fork {
Subroutine batch_fork: called CC=4 SCAR=14.0
Pod undocumented
664  
665 # \%child_opts = Mnet::Opts::Cli::batch_fork($batch_argv)
666 # (\%child_opts, @child_extras) = Mnet::Opts::Cli::batch_fork($batch_argv)
667 # purpose: called to apply child batch command line to cached cli opts/args
668 # $batch_argv: batch child command line in Getopts::Long string format
669 # \%child_opts: hash ref of options parsed from $batch_argv and cached cli opts
670 # @child_extras: extra args parsed from $batch_argv, if called in list context
671 # note: this is meant to be called from Mnet::Batch::fork() only
672  
673 # read input child batch command line
674 9 ▶ my $batch_argv = shift // croak("missing batch_argv arg");
Condition50%1/2
shift() // croak("missing batch_argv arg")
l!l
90
MC/DC0%0/1
shift() // croak("missing batch_argv arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
675 9 DEBUG("batch_fork called, batch_argv = $batch_argv");
676  
677 # unset copy of cli ARGV list if we haven't called Mnet::Opts::Cli->new yet
678 # this means Mnet::Opts::Cli->new will parse batch_argv only, not cli
679 9 ▶ @Mnet::Opts::Cli::argv = () if not defined Mnet::Opts::Cli::Cache::get();
Branch: 1 branch
BranchTrueFalseTotal
unless defined Mnet::Opts::Cli::Cache::get() 0 9 9
680  
681 # clear cached cli options and args
682 9 Mnet::Opts::Cli::Cache::set(undef);
683  
684 # finished Mnet::Opts::Cli::batch, return child_opts and child_extras
685 # what is returned depends on context Mnet::Batch::fork() was called
686 9 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 7 9
687 2 my ($child_opts, @child_extras)
688 = Mnet::Opts::Cli->new(undef, $batch_argv);
689 2 return ($child_opts, @child_extras);
690 } else {
691 7 my $child_opts = Mnet::Opts::Cli->new(undef, $batch_argv);
692 7 return $child_opts;
693 }
694 }
695  
696  
697  
698 =head1 TESTING
699  
700 When used with the L<Mnet::Test> --record option this module will save all
701 cli options to the specified file if they were defined with the record option
702 attribute set true. Any extra arguments specified on the command line will also
703 be saved. For more info about enabling the recording of individual options
704 refer to the define function in this module and the --test-reset option.
705  
706 When the --replay option is used this module will load all cli options saved
707 in the specified Mnet::Test file then apply options specified on the command
708 line on top of the replayed options.
709  
710 When the --replay option is used for an L<Mnet::Test> file which was recorded
711 with extra arguments the extra arguments from the replay file will be used
712 unless there were extra arguments on the command line, in which case the
713 command line arguments will replace the arguments read from the replay file.
714  
715 The --record option can be used to re-save the current --replay file after
716 applying new command line options and/or extra arguments.
717  
718 =head1 SEE ALSO
719  
720 L<Getopt::Long>
721  
722 L<Mnet>
723  
724 L<Mnet::Opts>
725  
726 L<Mnet::Test>
727  
728 =cut
729  
730 # normal package return
731 145 1;
732  
↑ Summary Mnet/Log.pm »