blib/lib/Getopt/OO.pm

filter: Statementstmt 91.2% 281 / 308 Branchbran 87.0% 209 / 240 Conditioncond 85.8% 109 / 127 MC/DCmcdc 75.3% 55 / 73 Subroutinesub 100% 15 / 15 Podpod 44.4% 4 / 9 totaltotal 87.1% 673 / 772 CC 174 SCAR 34.4
CC
174
Cov
89%
parse_options
43.4
build_help
42.0
new
33.2
CRAP 31.1
↑ Summary
1 #!/usr/local/bin/perl -w
2 # $Log: OO.pm,v $
3 # Revision 1.2 2005/07/30 01:25:16 builder
4 # fix to display bad key when values called with invalid key.
5 #
6 # Revision 1.13 2005/02/05 17:20:34 sjs
7 # Changed other_options to use required.
8 # Fixed checks for n_values in other_options.
9 #
10 # Revision 1.12 2005/02/03 03:41:13 sjs
11 # Fixed a couple of minor errors in other_values Values return.
12 # Rewrote the part of the Values method that decides what
13 # type to return to simplify debugging.
14 #
15 # Revision 1.11 2005/01/31 04:02:43 sjs
16 # - Fixed a problem with indent.
17 # - Fixed a problem with Getopt and mulit-value.
18 # - Added ability of other_values to use callback and ClientData.
19 # - Added several tests.
20 # - Modified error message for callback so that name of option
21 # generating error is displayed.
22 # - This should be it for feature modifications.
23 #
24 # Revision 1.10 2005/01/28 07:47:40 sjs
25 # modified other_values behaviour
26 #
27 # Revision 1.9 2005/01/27 15:38:02 sjs
28 # Change the way indent works on help. We now make string one char
29 # longer than option so help will be a little narrower rather than
30 # using a tab stop of 4.
31 #
32 # Revision 1.8 2005/01/27 15:35:34 sjs
33 # Change version to 0.05.
34 # Fix problem with indent.
35 #
36 # Revision 1.7 2005/01/23 21:38:45 sjs
37 # Fixed some problems with the pod and pod2man on older versions.
38 #
39 # Revision 1.6 2005/01/23 20:59:31 sjs
40 # Fixed a problem with multi_value not catching end of arguments
41 # correctly if option was not a '-'.
42 #
43 # Revision 1.5 2005/01/23 20:34:04 sjs
44 # - Renamed the other_args to be other_values to make things
45 # more consistent.
46 # - other_values no takes a number instead of a string and can
47 # be used to help the parser know how many arguments are expected
48 # after all the options have been parsed.
49 # - Changes so we would work under 5.4 perl.
50 # - Changes to docs to cleanup and reflect changes to code.
51 # - Added multi-valued option. Syntax is --arg ... - where
52 # the final '-' can be either the start of the next argument
53 # or a free-standing dash.
54 # - Fixed a problem with calculation of the indent for the
55 # help strings.
56 # - Added code to better check the non-dashed tags for validity.
57 # - Fixed a problem that was causing options to be dropped from
58 # the first line of the usage output.
59 #
60 # Revision 1.4 2005/01/18 03:44:02 sjs
61 # Added new other_values option.
62 # Added additional error checking.
63 # Added changes to support PERL 5.004.
64 # Modified USAGE message to also show mutual_exclusive options.
65 # Modified USAGE to separate long and short optional options.
66 #
67 # Revision 1.3 2005/01/17 06:54:57 sjs
68 #
69 # Makefile: move required version to 5.005.
70 # Bumped version to 2.
71 #
72 #
73 # Clean up documentaion.
74 # Make use of arg vs option more consistent.
75 # Get rid of 'our' variables so we could use 5.005 perl.
76 # Modified mutual_exclusive so it could take either a
77 # list or list of lists.
78 #
79 # Revision 1.2 2005/01/11 07:50:30 sjs
80 # Fixed mutual_exclude and required.
81 #
82 # Revision 1.1.1.1 2005/01/10 05:23:52 sjs
83 # Import of Getopt::OO
84 #
85 package Getopt::OO;
86  
87 1 ▶use 5.00404;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 # Use warnings if possible. Don't worry if you can't. Package was developed
90 # with warnings on, but it wasn't around by default before 5.6.
91 1 eval { require 'warnings.pm' };
92 1 ▶use vars qw($VERSION @ISA @EXPORT_OK $Revision);
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 1 require Exporter;
95  
96 1 @ISA = qw(Exporter);
97  
98 1 @EXPORT_OK = qw(Debug Verbose);
99  
100 1 $VERSION = '0.07';
101 1 $Revision = '$Id:$';
102  
103 =head1 NAME
104  
105 Getopt::OO - An object oriented command line parser. It handles
106 short, long and multi (--x ... -) value options. It also incorporates
107 help for options to simplify generation of usage statements.
108  
109 =head1 SYNOPSIS
110  
111 use Getopt::OO qw(Debug Verbose);
112  
113 my ($handle) = Getopt::OO->new(\@ARGV,
114 '-d' => {
115 help => 'turn on debug output',
116 callback => sub {Debug(1); 0},
117 },
118 '-o' => {
119 help => 'another option.',
120 },
121 '-f' => {
122 help => 'option that expects one more value.',
123 n_values => 1,
124 },
125 '--long' {
126 help => 'long option'
127 },
128 '--multiple_' => {
129 help => [
130 "Everything between '--multiple_values' and '-' is",
131 "an value for this options",
132 ],
133 'multi_value' => 1,
134 'multiple= => 1, # Can happen more than once on command line.
135 },
136 other_values => {
137 help => 'file_1 ... file_n',
138 multi_value => 1,
139 },
140 );
141 if ($handle->Values()) {
142 Debug("You will get output if -d was on command line");
143 if (my $f = handle->Values(-f)) {
144 print "Got $f with the -f value.\n";
145 }
146 }
147 else {
148 print "No options found on command line.\n";
149 }
150  
151 =head1 DESCRIPTION
152  
153 Getopt::OO is an object oriented tool for parsing command line arguments.
154 It expects a reference to the input arguments and uses a perl hash
155 to describe how the command line arguments should be parsed. Note
156 that by parsed, we mean what options expect values, etc. We check
157 to make sure values exist on the command line as necessary -- nothing
158 else. The caller is responsible for making sure that a value that
159 he knows should be a file exists, is writable, or whatever.
160  
161 Command line arguments can be broken into two distinct types: options
162 and values that are associated with these options. In windows,
163 options often start with a '/' but sometimes with a '-', but
164 in unix they almost universally start with a '-'. For this module
165 options start with a '-'. We support two types of options:
166 the short single dashed options and the long double dashed options.
167 The difference between these two is that with this module the
168 short options can be combined into a single option, but the
169 long options can not. For example, most of us will be familiar
170 with the C<tar -xvf file> command which can also be expressed
171 as C<-x -v -f file>. Long options can not be combined this way,
172 so '--help' for example must always stand by itself.
173  
174 The input template expects the option names as its keys. For instance
175 if you were expecting C<-xv --hello> as possible command line options,
176 the keys for your template hash would be C<-x>, C<-v>, and C<--hello>.
177  
178 =head1 Valid values for each dashed options are:
179  
180 =head2 help
181  
182  
183 A help string associated with the options.
184  
185 =head2 n_values
186  
187 Number of values the option expects. Any value greater than or
188 equal to 0 is valid with 0 being the default.
189  
190 =head2 multiple
191  
192 If this exists, it means the option may be encountered multiple times.
193 For example --
194  
195 '-a' => {
196 n_values => 3,
197 multiple => 1,
198 },
199  
200 says that if C<-a> is encountered on the command line, the next
201 three arguments on the command line are associated with it and
202 that it may be encountered multiple times.
203  
204  
205 =head2 multi_value
206  
207 Use this if you know that the option expects multiple values, but
208 you don't know how many till the user executes the script. This tag
209 is only valid for long options. Everything between the option and
210 a '-' is considered a value. For example, suppose you wanted to pass
211 in a group of user names, your command line might look like:
212 --users fred joe mary gandalf frodo -
213  
214 =head2 callback
215  
216 This must be a code reference. If the template entry looked like:
217  
218 '-a' => {
219 n_values => 1,
220 multiple => 1,
221 callback => \&xyz,
222 },
223  
224 then we would call the function xyz with the a Getopt::OO handle
225 and the option found and the argument reference. For instance
226 if the function looked like:
227  
228 sub Callback {
229 my ($handle, $option) = @_
230 ...
231  
232 the caller could get help with $handle->Help() or its values with
233 $handle->Values($option).
234  
235 Note that the only option information available at this point is
236 what has been found on the command line up to this point. For
237 example, if the callback were associated with the C<-f> option and
238 the command line looked like C<-xvfz 1 2 3>, we haven't yet parsed
239 the C<-z> option, so no information associated with this option
240 is available.
241  
242 If the callback returns a non-0 value, it failed. We
243 execute 'die $string' where $string is the returned value.
244  
245  
246 =head1 Template non-dashed arguments
247  
248 Only four non-dashed keys are allowed: 'usage', 'other_values',
249 'required', and 'mutual_exclusive'.
250  
251 =head2 usage
252  
253 This is a string. Typically it wil be the first part of a
254 help statement and combined with the 'help' arguments for
255 the various dashed arguments in the template, creates the complete
256 usage message. By default, we will create a usage string that
257 is the base name of the executable ($0) and just the string
258 '[options]'.
259  
260  
261 =head2 other_values
262  
263 Usually all the argments on a command line aren't associated
264 with options. For instance, a function may always require
265 a file name but have several other options too. It's
266 signature might look like
267  
268 script [-v] input_file
269  
270 other_values allows you to supply help for the usage message
271 and tell the parser how many args to expect. Use might look like
272  
273 other_values => {
274 help => 'file_1 ... file_n',
275 },
276  
277 For the call to script above, the first output line of
278 the usage statement would look like:
279  
280 script [-v] file_1 ... file_n
281  
282 and since multi_value is set, an error would occur unless at
283 least one value were passed in.
284  
285 Use multi_value or n_values to tell the parser how many values
286 to expect. Both of these values are optional and if not supplied,
287 the parser doesn't check for values after the parsing is done.
288  
289 help is also optional. If it is not supplied, we use n_values
290 or multi_value to print a message that makes sense. Note that
291 if n_values or multi_value are set, it is an error to not have
292 other values after the options are parsed, but you can just
293 supply the help value and no other checking of the other_values
294 will occur.
295  
296 As discussed below, other_values will also accept a callback and
297 use ClientData and return its values using
298 $handle->Values('other_values'). Unlike the other options,
299 other_values can not be 'multiple'.
300  
301 By default, any values not used by other arguments will get assigned
302 to the 'other_values' option. This is done mostly to allow error
303 checking. One of the decisions early in programming this module
304 was that I wanted to allow parsed arguments to start with a
305 '-'. Thus, something like
306  
307 --args => {'n_values' => 3},
308  
309 would allow a command line like '--args -a 1 5' and $h->Values('--args')
310 would return a 3 element array consisting of -a, 1, and 5. Unfortunatly
311 this makes both parsing and checking of command line arguments more
312 difficult. For example, if you had something like
313  
314 -a => {'n_values' => 2},
315  
316 and your command line looks like '-a 1 2 3 -s', the values after
317 3 don't get parsed and are left on the argument list.
318  
319 To simplify checking this situation, two changes were made
320 in version 0.07 of this module: 1) you can now set the 'other_values'
321 'n_values' option to 0 and we will die if any unparsed command
322 line values exist, or 2) unparsed command line values are now
323 placed on the 'other_values' option so you can use $h->Values('other_values')
324 to examine the un-parsed arguments.
325  
326 =head2 required
327  
328  
329 This is an array reference to required arguments. It is an error
330 if none of these are found on the command line.
331  
332 =head2 mutual_exclusive
333  
334 This is an list reference. It says "it is an error to receive
335 these arguments at the same time." For example, "tar cx" would not
336 make sense because you can't both create and extract at the
337 same time. Give a reference for each set of mutually exclusive
338 arguments. In the trivial case where you only have one set, the
339 argument can be just a reference to a list, but in the more complicated
340 case where you have sets of mutually exclusive arguments, this will
341 be a refrence to an list of list references. The template to express
342 this might look like:
343  
344 mutual_exclusive => [ qw( -x -c ) ],
345 -x => {
346 help => 'Extract a tar file',
347 },
348 -c => {
349 help => 'Create a tar file',
350 }
351  
352 As stated above, this would also be correct.
353  
354 mutual_exclusive => [
355 [qw( -x -c )],
356 ],
357 -x => {
358 help => 'Extract a tar file',
359 },
360 -c => {
361 help => 'Create a tar file',
362 }
363  
364 =head1 Methods associated with the OO module:
365  
366 =head2 my $handle = Getopt::OO->new(\@ARGV, %Template)
367  
368 Creator function. Expects a reference to the argument list and
369 a template that explanes how to parse the input arguments and returns
370 an object reference. If you want to catch parse errors
371 rather than having the parser print an error message and
372 exit, do this:
373  
374 my $handle = eval {Getopt::OO>new(\@ARGV, %template)};
375 if ($@) {...
376  
377 $@ will contain your error string if one exists and be empty
378 otherwise.
379  
380 =head2 $handle->Values(argument);
381  
382 Values() returns a list of command line options that
383 were matched in the order they were found. In scalar
384 context, this is the number of matches.
385  
386 Values($option) depends on the 'n_values' and the 'multiple'
387 for the option in the template. If the option had no
388 n_values element or n_values was 0, Values(option) will return
389 0 if the option was not found on the command line and 1 if
390 it was found. If n_values was set to 1 and multiple was not
391 set or was set to 0, we return nothing if the argument was
392 not found and the value of the argument if one was found.
393 If n_values > 1 and multiple was not set or if n_values is
394 1 and multiple was set, we return a list containing the
395 values if the values were found and nothing otherwise.
396 If the of n_values is greater than 1 and multiple is set,
397 we retrun a list of list references -- each contining n_values
398 elements, or nothing if no matches were found.
399  
400 The example below shows a template and accesing the values
401 returned by the parser. The template is ordered from the
402 simplest use to the most complex.
403  
404 Given the command line arguments:
405  
406 -abcde b c0 d0 d1 e0 e1 -c c1 -e e2 es
407
408 and the following to create our GetOpt handle:
409  
410 use Getopt::OO qw(Debug);
411 my @argv = qw (-abcde b c0 d0 d1 e0 e1 -c c1 -e e2 es);
412 my $h = Getopt::OO->new(\@argv,
413 '-a' => {},
414 '-b' => { n_values => 1, },
415 '-c' => { n_values => 1, multiple => 1, },
416 '-d' => { n_values => 2, },
417 '-e' => { n_values => 2, multiple => 1, },
418 );
419 my $n_options = $h->Values();
420 my $a = $h->Values('-a');
421 my $b = $h->Values('-b');
422 my @c = $h->Values('-c');
423 my @d = $h->Values('-d');
424 my @e = $h->Values('-e');
425  
426 Example 1. ValuesDemo.pl
427  
428 =head2 my $help_string = $handle->Help();
429  
430 Get the string string we built for this template. Note
431 that this can be used to check the template to make sure
432 it is doing what you expect. It will contain optional
433 arguments separated from non optional, indicates required
434 and mutually exclusive options and indicates which options
435 expect values and how many values.
436  
437 =head2 my $client_data = $handle->ClientData($option);
438  
439 The ClientData method is supplied to allow data to be
440 associated with an option. The data must be scalar or
441 a reference. All calls to this method return what ever
442 the data was replied to, but it is only set if data is
443 passed in.
444  
445 To set the data:
446  
447 $h->ClientData($option, $x);
448  
449 To get the data:
450  
451 $x = $h->ClientData($option);
452  
453 =head1 Debug and Verbose Functions
454  
455 We also supply two functions the user can export. These are the
456 Debug and the Verbose functions. If the functions are exported
457 and we find --debug or --verbose in the command line arguments,
458 the associated function is enabled. These two functions behave
459 in multiplt ways: If called with just a '0' or '1', the function
460 is disabled or disabled. If called with no arguments, we return
461 the state of the function: 0 if disabled and 1 if enabled. If
462 called with a list and the first element of the list looks
463 like a printf format statement, we behave like printf, and
464 otherwise we behave like a simple print statement. If the
465 function is called with a single argument that is a reference
466 to an IO::File object, we will attempt to send all further output
467 to this handle. Note that the object must be enabled before
468 any output will occur though.
469  
470 =head1 EXPORT
471  
472 None by default.
473  
474 =head1 SEE ALSO
475  
476 Several example scripts are included in the release under
477 the directory 'Demo'.
478  
479  
480 =head1 AUTHOR
481  
482 Steven Smith, E<lt>sjs@chaos-tools.comE<gt>
483  
484 =head1 COPYRIGHT AND LICENSE
485  
486 Copyright (C) 2004 by Steven Smith
487  
488 This library is free software; you can redistribute it and/or modify
489 it under the same terms as Perl itself, either Perl version 5.8.3 or,
490 at your option, any later version of Perl 5 you may have available.
491  
492 =cut
493  
494 {
495 # Debug and Verbose are functions used to enable
496 # and disable debug output.
497 # to use:
498 # To send subsequent output to $fh if $ffh is an IO::File file handle.
499 # Debug($fh);
500 # Debug(1); turns on debug output.
501 # Debug(0); turns of debug output.
502 # Debug("%2d\n", $x); for printf style output.
503 # Debug($string); for print style output.
504 # Verose behaviour is identical to Debug output.
505 # Any call to Debug returns its state -- on or off.
506 # Generic routine called from Debug and Verbose.
507 1 ▶ sub _print_ {
Subroutine _print_: called CC=10 SCAR=23.3
508 16 my $fh_ref = shift @_;
509 16 my $enabled_ref = shift @_;
510 16 ▶ if (defined $_[0] && @_ == 1
Branch: 2 branches
BranchTrueFalseTotal
if (defined $_[0] and @_ == 1 and $_[0] =~ /^[01]$/ || ref $_[0] && ref $_[0] eq 'IO::File') { } 10 6 16
elsif ($$enabled_ref and @_) { } 2 6 8
Condition83%10/12
defined $_[0] and @_ == 1 and $_[0] =~ /^[01]$/ || ref $_[0] && ref $_[0] eq "IO::File"
!ll&&!rl&&r
4210
defined $_[0] and @_ == 1
!ll&&!rl&&r
4012
$_[0] =~ /^[01]$/ or ref $_[0] and ref $_[0] eq "IO::File"
l!l&&r!l&&!r
822
ref $_[0] and ref $_[0] eq "IO::File"
!ll&&!rl&&r
202
MC/DC60%3/5
defined $_[0] and @_ == 1 and $_[0] =~ /^[01]$/ || ref $_[0] && ref $_[0] eq "IO::File"
defined $_[0] @_ == 1 $_[0] =~ /^[01]$/ ref $_[0] ref $_[0] eq "IO::File"
Truth table66%4/6
A and B and C or D and E
A= defined $_[0]B= @_ == 1C= $_[0] =~ /^[01]$/D= ref $_[0]E= ref $_[0] eq "IO::File"
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
511 && (
512 $_[0] =~ /^[01]$/
513 || (ref $_[0] && ref $_[0] eq 'IO::File')
514 )
515 ) {
516 10 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 2 8 10
517 2 $$fh_ref = $_[0];
518 }
519 else {
520 8 $$enabled_ref = $_[0];
521 }
522 }
523 ▶ elsif ($$enabled_ref && @_) {
Condition100%3/3
$$enabled_ref and @_
!ll&&!rl&&r
222
MC/DC100%2/2
$$enabled_ref and @_
$$enabled_ref @_
Truth table100%3/3
A and B
A= $$enabled_refB= @_
ABresult
0X0
100
111
524 2 ▶ if ($_[0] =~ /\%\d*[sdfcx]/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\%\d*[sdfcx]/i) { } 0 2 2
525 0 my $format = shift @_;
526 0 $$fh_ref->printf($format, @_);
527 }
528 else {
529 2 $$fh_ref->print (@_);
530 }
531 }
532 16 return($$enabled_ref);
533 }
534 # Verbose function.
535 {
536 1 my $fh = do {local *STDOUT};
537 1 my $verbose = 0;
538 8 ▶ sub Verbose {return(_print_(\$fh, \$verbose, @_)); }
Subroutine Verbose: called CC=1 SCAR=0.0
Pod undocumented
539 }
540 # Debug function.
541 {
542 1 my $fh = do {local *STDOUT};
543 1 my $debug = 0;
544 8 ▶ sub Debug {return(_print_(\$fh, \$debug, @_)); }
Subroutine Debug: called CC=1 SCAR=0.0
Pod undocumented
545 }
546 1 }
547  
548 # Build and return a help string from the imput template.
549 38 ▶sub build_help {
Subroutine build_help: called CC=41 SCAR=37.4
Pod undocumented
550 76 ▶ sub _short_args_list_ {
Subroutine _short_args_list_: called CC=6 SCAR=17.9
551 76 my ($template, @list) = @_;
552 76 my (@options, @args);
553 76 foreach my $option (sort @list) {
554 28 my $o = ($option =~ /^-(.)/)[0];
555 28 push @options, $o;
556 28 ▶ if ($template->{$option}{'n_values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{$option}{"n_values"}) 7 21 28
557 7 foreach my $i (0..($template->{$option}{'n_values'} - 1)) {
558 9 ▶ push @args, "${o}_arg" . (($i) ? (${i} + 1) : '');
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 2 7 9
559 }
560 }
561 }
562 76 ▶ (@options)
Branch: 1 branch
BranchTrueFalseTotal
@options ? : 18 58 76
563 ? return('-' . join('', @options) . ' ' . join(' ', @args))
564 : return('');
565 }
566 76 ▶ sub _long_args_list_ {
Subroutine _long_args_list_: called CC=7 SCAR=19.5
567 76 my ($template, @list) = @_;
568 76 my (@options);
569 76 foreach my $option (sort @list) {
570 11 push @options, $option;
571 11 ▶ if ($template->{$option}{'n_values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{$option}{"n_values"}) 1 10 11
572 1 my $o = ($option =~ /^--(.*)/)[0];
573 1 foreach my $i (0..($template->{$option}{'n_values'} - 1)) {
574 1 ▶ $options[-1] .= " ${o}_arg" . (($i) ? ${i} : '');
Branch: 1 branch
BranchTrueFalseTotal
$i ? : 0 1 1
575 }
576 }
577 11 ▶ if ($template->{$option}{'multi_value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{$option}{"multi_value"}) 6 5 11
578 6 $options[-1] .= ' ... -'
579 }
580 }
581 76 ▶ (@options)
Branch: 1 branch
BranchTrueFalseTotal
@options ? : 9 67 76
582 ? return(join(' ', @options))
583 : return('');
584 }
585  
586 38 my ($template) = @_;
587 38 my $name = ($0 =~ m{^(?:.*/)*(.*)})[0];
588 my %required = (exists $template->{'required'})
589 38 ▶ ? (map {$_, 1} @{$template->{'required'}})
Branch: 1 branch
BranchTrueFalseTotal
exists $template->{'required'} ? : 5 33 38
590 : ();
591 38 ▶ my @optional = grep /^-/ && !/^--/ && !$required{$_}, keys %$template;
Condition100%6/6
/^-/ && !/^--/ && !$required{$_}
!ll&&!rl&&r
37325
/^-/ and not /^--/
!ll&&!rl&&r
261128
MC/DC100%3/3
/^-/ && !/^--/ && !$required{$_}
/^-/ not /^--/ !$required{$_}
Truth table100%4/4
A and not(B) && not(C)
A= /^-/B= not /^--/C= !$required{$_}
ABCresult
0XX0
10X0
1100
1111
592 my $short_optional_arg_list = _short_args_list_ (
593 $template,
594 38 ▶ grep(/^-/ && !/^--/ && !$required{$_}, keys %$template),
Condition100%6/6
/^-/ && !/^--/ && !$required{$_}
!ll&&!rl&&r
37325
/^-/ and not /^--/
!ll&&!rl&&r
261128
MC/DC100%3/3
/^-/ && !/^--/ && !$required{$_}
/^-/ not /^--/ !$required{$_}
Truth table100%4/4
A and not(B) && not(C)
A= /^-/B= not /^--/C= !$required{$_}
ABCresult
0XX0
10X0
1100
1111
595 );
596 my $long_optional_arg_list = _long_args_list_ (
597 $template,
598 38 ▶ grep(/^--/ && !$required{$_}, keys %$template),
Condition100%3/3
/^--/ && !$required{$_}
!ll&&!rl&&r
54110
MC/DC100%2/2
/^--/ && !$required{$_}
/^--/ !$required{$_}
Truth table100%3/3
A && not(B)
A= /^--/B= !$required{$_}
ABresult
0X0
100
111
599 );
600 my $short_required_arg_list = _short_args_list_ (
601 $template,
602 38 ▶ grep(/^-/ && !/^--/ && $required{$_}, keys %$template),
Condition100%6/6
/^-/ && !/^--/ && $required{$_}
!ll&&!rl&&r
37253
/^-/ and not /^--/
!ll&&!rl&&r
261128
MC/DC100%3/3
/^-/ && !/^--/ && $required{$_}
/^-/ not /^--/ $required{$_}
Truth table100%4/4
A and not(B) && C
A= /^-/B= not /^--/C= $required{$_}
ABCresult
0XX0
10X0
1100
1111
603 );
604 my $long_required_arg_list = _long_args_list_ (
605 $template,
606 38 ▶ grep(/^--/ && $required{$_}, keys %$template),
Condition100%3/3
/^--/ && $required{$_}
!ll&&!rl&&r
54101
MC/DC100%2/2
/^--/ && $required{$_}
/^--/ $required{$_}
Truth table100%3/3
A && B
A= /^--/B= $required{$_}
ABresult
0X0
100
111
607 );
608 38 my $other_values = do {
609 38 my $rv = '';
610 38 ▶ if ($template->{'other_values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{"other_values"}) 17 21 38
611 17 my $ref = $template->{'other_values'};
612 17 ▶ if (ref $ref && ref $ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref and ref $ref eq "HASH") 16 1 17
Condition66%2/3
ref $ref and ref $ref eq "HASH"
!ll&&!rl&&r
1016
MC/DC50%1/2
ref $ref and ref $ref eq "HASH"
ref $ref ref $ref eq "HASH"
Truth table66%2/3
A and B
A= ref $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
613 $rv = ($ref->{'help'})
614 ? " $ref->{help}"
615 : ($ref->{multi_value})
616 ? ' value_1 ... value_n'
617 : ($ref->{n_values})
618 ? ($ref->{n_values} !~ /^\d+$/)
619 ? ''
620 : ($ref->{n_values} == 1)
621 ? ' value'
622 : ($ref->{n_values} == 2)
623 ? ' value_1 value_2'
624 : " value_1 ... value_"
625 . $ref->{n_values}
626 16 ▶ : '';
Branch: 6 branches
BranchTrueFalseTotal
$ref->{'help'} ? : 2 14 16
$ref->{'multi_value'} ? : 1 13 14
$ref->{'n_values'} ? : 11 2 13
!($ref->{'n_values'} =~ /^\d+$/) ? : 1 10 11
$ref->{'n_values'} == 1 ? : 5 5 10
$ref->{'n_values'} == 2 ? : 2 3 5
627 }
628 }
629 38 $rv;
630 };
631 38 ▶ my $usage = join ('', "USAGE: $name",
Branch: 4 branches
BranchTrueFalseTotal
$short_optional_arg_list ? : 15 23 38
$long_optional_arg_list ? : 8 30 38
$short_required_arg_list ? : 3 35 38
$long_required_arg_list ? : 1 37 38
632 ($short_optional_arg_list) ? " [$short_optional_arg_list]" : '',
633 ($long_optional_arg_list) ? " [$long_optional_arg_list]" : '',
634 ($short_required_arg_list) ? " $short_required_arg_list" : '',
635 ($long_required_arg_list) ? " $long_required_arg_list" : '',
636 $other_values,
637 ) . "\n";
638 # the template usage may be either a scalar or a list ref.
639 # in either case, indent by 4 spaces and terminate with a
640 # linefeed.
641 38 ▶ if($template->{'usage'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{"usage"}) 0 38 38
642 0 ▶ my @use = map {" $_\n"}
Branch: 1 branch
BranchTrueFalseTotal
ref $template->{'usage'} ? : 0 0 0
643 (ref $template->{'usage'})
644 0 ? @{$template->{'usage'}}
645 0 : ($template->{'usage'});
646 0 $usage .= join('', @use);
647 }
648 38 ▶ if (%required) {
Branch: 1 branch
BranchTrueFalseTotal
if (%required) 5 33 38
649 5 my @r = sort keys %required;
650 5 ▶ $usage .= (@r > 1)
Branch: 1 branch
BranchTrueFalseTotal
@r > 1 ? : 2 3 5
651 ? " Arguments " . join(', ', @r) . " are required.\n"
652 : " Argument @r is required.\n";
653 }
654 38 ▶ if (my $mutual_exclusive = $template->{'mutual_exclusive'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mutual_exclusive = $template->{"mutual_exclusive"}) 3 35 38
655 3 ▶ if (ref $mutual_exclusive->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mutual_exclusive->[0]) { } 3 0 3
656 3 my @r = @$mutual_exclusive;
657 $usage .= " Arguments \""
658 3 . join("\", \"", map {"@$_"} @r)
659 . "\" are mutually exclusive.\n";
660 }
661 else {
662 0 $usage .= " Arguments \"@{$mutual_exclusive}\""
663 . " are mutually excluive.\n";
664 }
665 }
666 38 ▶ if (my @m = grep /^-/ && $template->{$_}{'multiple'}, keys %$template) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@m) = grep((/^-/ && $template->{$_}{"multiple"}), keys %$template)) 3 35 38
Condition100%3/3
/^-/ && $template->{$_}{'multiple'}
!ll&&!rl&&r
26354
MC/DC100%2/2
/^-/ && $template->{$_}{'multiple'}
/^-/ $template->{$_}{'multiple'}
Truth table100%3/3
A && B
A= /^-/B= $template->{$_}{'multiple'}
ABresult
0X0
100
111
667 3 ▶ $usage .= join('',
Branch: 1 branch
BranchTrueFalseTotal
@m > 1 ? : 1 2 3
668 (@m > 1)
669 ? " Arguments " . join(', ', sort @m)
670 : " Argument @m",
671 " may occur more than once.\n",
672 );
673 }
674  
675 38 my %options_list;
676 38 my $max_len = 0;
677 38 my @help;
678 map {
679 13 my $options = $_;
680 # add 'arg' for each value in n_values.
681 13 ▶ if ($template->{$_}{'n_values'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($template->{$_}{'n_values'}) { } 3 10 13
elsif ($template->{$_}{'multi_value'}) { } 2 8 10
682 3 foreach my $i (1..$template->{$_}{'n_values'}) {
683 3 ▶ $options .= ($i > 1) ? " arg_$i" : ' arg';
Branch: 1 branch
BranchTrueFalseTotal
$i > 1 ? : 0 3 3
684 }
685 }
686 elsif ($template->{$_}{'multi_value'}) {
687 2 $options .= ' ... -';
688 }
689 13 $options_list{$_} = $options;
690 13 ▶ $max_len = length $options if (length $options > $max_len);
Branch: 1 branch
BranchTrueFalseTotal
if length $options > $max_len 6 7 13
Condition100%6/6
ref $template->{$_} eq 'HASH' && /^-+/ && exists $template->{$_}{'help'}
!ll&&!rl&&r
262613
ref $template->{$_} eq "HASH" and /^-+/
!ll&&!rl&&r
101639
MC/DC100%3/3
ref $template->{$_} eq 'HASH' && /^-+/ && exists $template->{$_}{'help'}
ref $template->{$_} eq "HASH" /^-+/ exists $template->{$_}{'help'}
Truth table100%4/4
A and B && C
A= ref $template->{$_} eq "HASH"B= /^-+/C= exists $template->{$_}{'help'}
ABCresult
0XX0
10X0
1100
1111
691 } sort grep ref $template->{$_} eq 'HASH'
692 && /^-+/
693 38 && exists $template->{$_}{'help'}
694 , keys %$template;
695 38 $max_len = (int($max_len / 4) + 1) * 4;
696 # output is set so that the arg_list is put out the
697 # first time only and all the actual help is justified
698 # to the right of the argument list.
699 38 foreach my $key (sort keys %options_list) {
700 # the help element may be either a string or a list ref.
701 # output should look like:
702 # -a value first line of help
703 # second line of help
704 # etc and so on.
705 my @help_list = (ref $template->{$key}{'help'})
706 0 ? @{$template->{$key}{'help'}}
707 13 ▶ : ($template->{$key}{'help'});
Branch: 1 branch
BranchTrueFalseTotal
ref $template->{$key}{'help'} ? : 0 13 13
708 13 my $h = $options_list{$key};
709 map {
710 13 push @help, sprintf(" %-${max_len}s%s\n", $h, $_);
711 13 $h = ''
712 } @help_list;
713 }
714 38 return(join('', $usage, @help));
715 }
716  
717 # Parse the template for correctness.
718 # Make sure we have only valid arguments for each of the
719 # elements of the template.
720 36 ▶sub parse_template {
Subroutine parse_template: called CC=28 SCAR=34.8
Pod undocumented
721 36 my ($this, $template) = @_;
722 36 my @errors;
723 my %defined;
724 36 ▶ if (defined $template->{'other_values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $template->{"other_values"}) 17 19 36
725 17 my $ref = $template->{'other_values'};
726 17 ▶ if (ref $ref && ref $ref eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref and ref $ref eq 'HASH') { } 16 1 17
Condition66%2/3
ref $ref and ref $ref eq "HASH"
!ll&&!rl&&r
1016
MC/DC50%1/2
ref $ref and ref $ref eq "HASH"
ref $ref ref $ref eq "HASH"
Truth table66%2/3
A and B
A= ref $refB= ref $ref eq "HASH"
ABresult
0X0
100
111
727 # These are valid tags for the other_values key.
728 my %valid_tags = map {
729 16 $_, 1
730 } qw(multi_value n_values help callback required);
731 16 ▶ if (my @bad = grep !$valid_tags{$_}, keys %$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if (my(@bad) = grep((!$valid_tags{$_}), keys %$ref)) { } 1 15 16
elsif ($ref->{'multi_value'}) { } 1 14 15
elsif ($ref->{'n_values'} and not $ref->{'n_values'} =~ /^\d+/) { } 1 16 17
732 1 push @errors, "other_values: bad tags: @bad\n";
733 }
734 elsif ($ref->{multi_value}) {
735 1 push @errors,
736 "other_values: Can't have multi_value\n";
737 }
738 ▶ elsif ($ref->{n_values} && $ref->{n_values} !~ /^\d+/) {
Condition100%3/3
$ref->{"n_values"} and not $ref->{"n_values"} =~ /^\d+/
!ll&&!rl&&r
3101
MC/DC100%2/2
$ref->{"n_values"} and not $ref->{"n_values"} =~ /^\d+/
$ref->{"n_values"} not $ref->{"n_values"} =~ /^\d+/
Truth table100%3/3
A and not(B)
A= $ref->{"n_values"}B= not $ref->{"n_values"} =~ /^\d+/
ABresult
0X0
100
111
739 1 push @errors, "other_values: n_values must be a number.\n";
740 }
741 }
742 else {
743 1 push @errors, "other_values: should be reference to a hash.\n";
744 }
745 17 ▶ %{$this->{other_values}} = %{$template->{other_values}} unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 13 4 17
746 }
747  
748 36 foreach my $option (sort grep !/^-+/, keys %$template) {
749 25 my $ref = $template->{$option};
750 25 ▶ if ($option eq 'mutual_exclusive') {
Branch: 2 branches
BranchTrueFalseTotal
if ($option eq 'mutual_exclusive') { } 3 22 25
elsif ($option eq 'required') { } 5 17 22
751 3 ▶ unless (ref $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref eq "ARRAY") 0 3 3
752 0 push @errors, "Bad mutual_exclusive argument. Should be ",
753 "a list or a list of lists.\n";
754 }
755 }
756 elsif ($option eq 'required') {
757 5 ▶ unless (ref $ref && ref $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref and ref $ref eq "ARRAY") 0 5 5
Condition33%1/3
ref $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
005
MC/DC0%0/2
ref $ref and ref $ref eq "ARRAY"
ref $ref ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= ref $refB= ref $ref eq "ARRAY"
ABresult
0X0
100
111
758 0 push @errors, "required should be a list reference.\n";
759 }
760 }
761 25 ▶ last if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 4 21 25
762 }
763 36 foreach my $option (sort grep /^-+/, keys %$template) {
764 38 ▶ if ($option =~ /^-[a-zA-Z]\w+/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^-[a-zA-Z]\w+/) 1 37 38
765 1 push @errors, "Bad template option: \"$option\". Short arguments "
766 . "(i.e. arguments starting with a\n single dash) "
767 . "can not be longer than one character.\n";
768 1 last;
769 }
770 37 my $ref = $template->{$option};
771 37 my @bad = grep !/^(help|n_values|multiple|multi_value|callback)$/
772 , keys %$ref;
773 37 ▶ if (@bad) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bad) { } 0 37 37
774 0 push @errors, "$option: \"@bad\" are not recognized "
775 . "options.\n";
776 }
777 else {
778 37 foreach my $key (sort keys %$ref) {
779 32 ▶ if ($key eq 'n_values') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "n_values") 8 24 32
780 8 ▶ if ($ref->{'n_values'} !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref->{"n_values"} =~ /^\d+$/) 0 8 8
781 0 push @errors,
782 "$key: n_values is $ref->{'n_values'} and should be an ",
783 "integer\n";
784 }
785 }
786 # Make sure keys for template entry are valid.
787 32 ▶ if ($ref->{'multi_value'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"multi_value"}) 9 23 32
788 9 ▶ if ($option =~ /^--/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /^--/) { } 9 0 9
789 9 ▶ if (my @b = grep /^(n_values)$/, keys %$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@b) = grep(/^(n_values)$/, keys %$ref)) 0 9 9
790 0 push @errors, "$key is multi value. \"@b\" are not "
791 . "valid for this option.";
792 }
793 }
794 else {
795 0 push @errors, "$option is a short option. multi_value is "
796 . "only valid for long options.\n";
797 }
798 }
799 32 ▶ last if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 32 32
800 }
801 }
802 37 ▶ %{$this->{$option}} = %{$template->{$option}} unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 37 0 37
803 }
804 36 ▶ (@errors) ? return(@errors) : return;
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 5 31 36
805 }
806 31 ▶sub parse_options {
Subroutine parse_options: called CC=43 SCAR=37.7
Pod undocumented
807 31 my ($this, $argv, $template) = @_;
808 31 my @errors = (); $this->{'errors'} = \@errors;
809 31 my @options_found = (); $this->{'options'} = \@options_found;
810 31 ▶ while (@$argv && $argv->[0] =~ /^-/ && !@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv and $argv->[0] =~ /^-/ and not @errors) 27 28 55
Condition83%5/6
@$argv and $argv->[0] =~ /^-/ and not @errors
!ll&&!rl&&r
28027
@$argv and $argv->[0] =~ /^-/
!ll&&!rl&&r
151327
MC/DC66%2/3
@$argv and $argv->[0] =~ /^-/ and not @errors
@$argv $argv->[0] =~ /^-/ not @errors
Truth table75%3/4
A and B and not(C)
A= @$argvB= $argv->[0] =~ /^-/C= not @errors
ABCresult
0XX0
10X0
1100
1111
811 # If the option starts with a single dash, split it into smaller
812 # one character args preceeded by a dash.
813 my @options = ($argv->[0] =~ /^--/)
814 ? ($argv->[0])
815 27 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$argv->[0] =~ /^--/ ? : 10 17 27
816 17 my $a = ($argv->[0] =~ /^-(.*)/)[0];
817 17 map {"-$_"} split //, $a;
818 };
819 27 shift @$argv;
820 27 ▶ while (defined (my $option = shift @options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $option = shift @options)) 31 27 58
821 31 ▶ if ($template->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{$option}) { } 31 0 31
822 31 push @options_found, $option;
823 31 my $ref = $template->{$option};
824 # If this option has already been encountered and multiple
825 # isn't set, we have an error.
826 31 ▶ if (exists $this->{$option}{'exists'} && !$ref->{'multiple'}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $this->{$option}{'exists'} and not $ref->{'multiple'}) { } 1 30 31
elsif (my $n_values = $ref->{'n_values'}) { } 7 23 30
elsif ($this->{$option}{'multi_value'}) { } 6 17 23
Condition100%3/3
exists $this->{$option}{"exists"} and not $ref->{"multiple"}
!ll&&!rl&&r
2731
MC/DC100%2/2
exists $this->{$option}{"exists"} and not $ref->{"multiple"}
exists $this->{$option}{"exists"} not $ref->{"multiple"}
Truth table100%3/3
A and not(B)
A= exists $this->{$option}{"exists"}B= not $ref->{"multiple"}
ABresult
0X0
100
111
827 1 push @errors,
828 "$option encountered more than once and multiple ",
829 "is not set.\n";
830 }
831 # If we have n_values set, we're pulling one or more
832 # values off the command line for this argument.
833 elsif (my $n_values = $ref->{'n_values'}) {
834 7 $this->{$option}{'n_values'} = $ref->{'n_values'};
835 7 ▶ $this->{$option}{'multiple'} = $ref->{'multiple'} || 0;
Condition100%2/2
$ref->{'multiple'} || 0
l!l
43
MC/DC100%1/1
$ref->{'multiple'} || 0
$ref->{'multiple'}
Truth table100%2/2
A
A= $ref->{'multiple'}
Aresult
11
00
836 # If n_values is greater than 1, pull the next
837 # n_values values off of the command line and save
838 # it in the values list as an array ref.
839 7 ▶ if ($n_values > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_values > 1) { } 3 4 7
840 3 my @in;
841 3 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
--$n_values and not @errors and do { do { if (@$argv) { push @in, shift @$argv; } else { push @errors, "Insufficent values for $option\n"; } } } 3 3 6
Condition66%2/3
--$n_values and not @errors
!ll&&!rl&&r
303
MC/DC50%1/2
--$n_values and not @errors
--$n_values not @errors
Truth table66%2/3
A and not(B)
A= --$n_valuesB= not @errors
ABresult
0X0
100
111
842 6 ▶ if (@$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) { } 6 0 6
843 6 push @in, shift @$argv;
844 }
845 else {
846 0 push @errors,
847 "Insufficent values for $option\n";
848 }
849 } while (--$n_values && !@errors);
850 # Multiple, we save it as a list of lists,
851 # non-multiple, save as a list ref.
852 3 ▶ if ($ref->{'multiple'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{'multiple'}) { } 2 1 3
853 2 push(@{$this->{$option}{'values'}}, \@in);
854 }
855 else {
856 1 $this->{$option}{'values'} = \@in;
857 }
858 }
859 else {
860 (@$argv)
861 ? ($this->{$option}{'multiple'})
862 2 ? push(@{$this->{$option}->{'values'}},
863 shift @$argv)
864 4 ▶ : ($this->{$option}{'values'} = shift @$argv)
Branch: 2 branches
BranchTrueFalseTotal
@$argv ? : 4 0 4
$this->{$option}{'multiple'} ? : 2 2 4
865 : push @errors, "Insufficent values for $option\n";
866 }
867 }
868 elsif ($this->{$option}{'multi_value'}) {
869 6 my @o;
870 6 ▶ while (@$argv && $argv->[0] !~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv and not $argv->[0] =~ /^-/) 18 6 24
Condition100%3/3
@$argv and not $argv->[0] =~ /^-/
!ll&&!rl&&r
1518
MC/DC100%2/2
@$argv and not $argv->[0] =~ /^-/
@$argv not $argv->[0] =~ /^-/
Truth table100%3/3
A and not(B)
A= @$argvB= not $argv->[0] =~ /^-/
ABresult
0X0
100
111
871 18 push @o, shift @$argv;
872 }
873 6 ▶ if(@$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv) { } 5 1 6
874 5 ▶ shift @$argv if $argv->[0] eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $argv->[0] eq "-" 5 0 5
875 5 ▶ if ($this->{$option}{'multiple'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{$option}{'multiple'}) { } 2 3 5
876 2 ▶ unless (exists $this->{$option}{'values'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $this->{$option}{"values"}) 1 1 2
877 1 $this->{$option}{'values'} = [];
878 }
879 2 push @{$this->{$option}{'values'}}, \@o;
880 }
881 else {
882 3 @{$this->{$option}{'values'}} = @o;
883 }
884 }
885 else {
886 1 push @errors, "Failed to find end to "
887 . "multi_value option $option.\n";
888 }
889 }
890 # n_values isn't set. Just increment the values for
891 # this guy.
892 else {
893 17 $this->{$option}->{'values'}++;
894 }
895 31 ▶ if (!@errors && $ref->{'callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @errors and $ref->{"callback"}) 2 29 31
Condition100%3/3
not @errors and $ref->{"callback"}
!ll&&!rl&&r
2272
MC/DC100%2/2
not @errors and $ref->{"callback"}
not @errors $ref->{"callback"}
Truth table100%3/3
not(A) and B
A= not @errorsB= $ref->{"callback"}
ABresult
0X0
100
111
896 2 ▶ if (my $error = &{$ref->{'callback'}}($this, $option)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &{$ref->{"callback"};}($this, $option)) 1 1 2
897 1 push @errors, "Option callback for \"$option\" "
898 . "returned an error:\n\t$error\n";
899 }
900 }
901 }
902 else {
903 0 push @errors, "unrecognized option: $option\n";
904 }
905 31 $this->{$option}{'exists'}++;
906 }
907 27 ▶ last if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 3 24 27
908 }
909 # Initialize othe_values values. Set its exists to the number
910 # of values in it.
911 31 $this->{other_values}{'exists'} = @$argv;
912 31 ▶ $this->{other_values}{'values'} = [@$argv] if @$argv;
Branch: 1 branch
BranchTrueFalseTotal
if @$argv 14 17 31
913 31 ▶ if ($template->{'other_values'} && !@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template->{"other_values"} and not @errors) 13 18 31
Condition66%2/3
$template->{"other_values"} and not @errors
!ll&&!rl&&r
18013
MC/DC50%1/2
$template->{"other_values"} and not @errors
$template->{"other_values"} not @errors
Truth table66%2/3
A and not(B)
A= $template->{"other_values"}B= not @errors
ABresult
0X0
100
111
914 13 my $ref = $template->{'other_values'};
915 13 ▶ if (exists $ref->{'n_values'} && $ref->{'n_values'} == 0 && @$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{'n_values'} and $ref->{'n_values'} == 0 and @$argv) { } 1 12 13
Condition83%5/6
exists $ref->{"n_values"} and $ref->{"n_values"} == 0 and @$argv
!ll&&!rl&&r
1201
exists $ref->{"n_values"} and $ref->{"n_values"} == 0
!ll&&!rl&&r
2101
MC/DC66%2/3
exists $ref->{"n_values"} and $ref->{"n_values"} == 0 and @$argv
exists $ref->{"n_values"} $ref->{"n_values"} == 0 @$argv
Truth table75%3/4
A and B and C
A= exists $ref->{"n_values"}B= $ref->{"n_values"} == 0C= @$argv
ABCresult
0XX0
10X0
1100
1111
916 1 push @errors, "other_values n_values set to 0 but received "
917 . scalar @$argv, " values.\n";
918 }
919 else {
920 12 ▶ my $n = $ref->{'n_values'} || 0;
Condition100%2/2
$ref->{'n_values'} || 0
l!l
102
MC/DC100%1/1
$ref->{'n_values'} || 0
$ref->{'n_values'}
Truth table100%2/2
A
A= $ref->{'n_values'}
Aresult
11
00
921 12 ▶ if ($n && @$argv && $n != @$argv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n and @$argv and $n != @$argv) { } 2 10 12
Condition100%6/6
$n and @$argv and $n != @$argv
!ll&&!rl&&r
372
$n and @$argv
!ll&&!rl&&r
219
MC/DC100%3/3
$n and @$argv and $n != @$argv
$n @$argv $n != @$argv
Truth table100%4/4
A and B and C
A= $nB= @$argvC= $n != @$argv
ABCresult
0XX0
10X0
1100
1111
922 2 push @errors, "other_values got "
923 . scalar @$argv
924 . " values and expected $n\n";
925 }
926 else {
927 10 ▶ $this->{'other_values'}{'values'} =
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 4 6 10
928 ($n == 1) ? $argv->[0] : [@$argv];
929 }
930 # If we have an other_values callback, do it.
931 12 ▶ if (!@errors && $ref->{'callback'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @errors and $ref->{"callback"}) 3 9 12
Condition100%3/3
not @errors and $ref->{"callback"}
!ll&&!rl&&r
273
MC/DC100%2/2
not @errors and $ref->{"callback"}
not @errors $ref->{"callback"}
Truth table100%3/3
not(A) and B
A= not @errorsB= $ref->{"callback"}
ABresult
0X0
100
111
932 3 ▶ if (my $error = &{$ref->{'callback'}}($this, 'other_values')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = &{$ref->{"callback"};}($this, "other_values")) 1 2 3
933 1 push @errors, "other_values callback returned an error:\n\t"
934 . "$error\n";
935 }
936 }
937 }
938 }
939 }
940  
941 # Object creater.
942 38 ▶sub new {
Subroutine new: called CC=29 SCAR=35.0
Pod documented
943 38 my $self = shift @_;
944 38 my (@errors, $this, @mutual_exclusive, @required);
945 # Check for correctness of input arguments.
946 38 ▶ if (!ref $_[0] || ref $_[0] ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne 'ARRAY') { } 0 38 38
Condition33%1/3
not ref $_[0] or ref $_[0] ne "ARRAY"
l!l&&r!l&&!r
0038
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne "ARRAY"
not ref $_[0] ref $_[0] ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne "ARRAY"
ABresult
1X1
011
000
947 0 push @errors, "Usage: Getopt::OO::new(ref array, hash);\n",
948 "first argment must be a reference to an array.\n";
949 }
950 else {
951 # Check for an odd number of elements in the @_. This is
952 # even for the hash +1 for the argv reference.
953 38 ▶ unless (@_ & 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ & 1) 0 38 38
954 0 push @errors, "Usage: Getopt::OO::new(ref array, hash);\n",
955 "hash has an odd number of elements.\n";
956 }
957 }
958 38 ▶ my ($argv, %template) = @_ unless @errors;
Branch: 1 branch
BranchTrueFalseTotal
unless @errors 38 0 38
959 38 $this->{'help'} = build_help(\%template);
960 # check valid options. Do this after help so we get a
961 # help message -- even if it's bogus.
962 38 my %valid = map {$_,1} qw(
963 other_values usage required mutual_exclusive
964 );
965 38 ▶ if (my @bad = grep !/^-/ && !$valid{$_}, keys %template) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad) = grep((!(/^-/ || $valid{$_})), keys %template)) 1 37 38
Condition100%3/3
/^-/ || $valid{$_}
l!l&&r!l&&!r
39251
MC/DC100%2/2
/^-/ || $valid{$_}
/^-/ $valid{$_}
Truth table100%3/3
A || B
A= /^-/B= $valid{$_}
ABresult
1X1
011
000
966 1 push @errors, "Unrecognized tags: @bad\n";
967 }
968 38 bless($this, $self);
969 38 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 37 1 38
970 # Check odd elements for uniqueness. We must check before
971 # the template before it becomes a hash or we lose the
972 # error that the same option was declared multiple times.
973 37 my %keys;
974 37 my $i = 0;
975 37 ▶ if (my @bad = grep $i++ && $keys{$_}++, @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@bad) = grep(($i++ && $keys{$_}++), @_)) 1 36 37
Condition100%3/3
$i++ && $keys{$_}++
!ll&&!rl&&r
371291
MC/DC100%2/2
$i++ && $keys{$_}++
$i++ $keys{$_}++
Truth table100%3/3
A && B
A= $i++B= $keys{$_}++
ABresult
0X0
100
111
976 1 push @errors, "Options \"@bad\" declared more than once.\n";
977 }
978 }
979 38 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 36 2 38
980 # Build help first so we have something to print on error exit.
981  
982 # Check to make sure we have valid input args. All args must have
983 # 1 or 2 leading dashes or be 'required', 'mutual_exclusive' or
984 # 'usage'.
985 36 @errors = parse_template($this, \%template);
986 }
987 38 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 31 7 38
988 31 parse_options($this, $argv, \%template);
989 31 ▶ @errors = (exists $this->{'errors'}) ? @{$this->{'errors'}} : ();
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'errors'} ? : 31 0 31
990 }
991 # Check for required options.
992 38 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 24 14 38
993 my %required = ($template{'required'})
994 24 ▶ ? map {$_, 1} @{$template{'required'}}
Branch: 1 branch
BranchTrueFalseTotal
$template{'required'} ? : 5 19 24
995 : ();
996 24 ▶ if (%required) {
Branch: 1 branch
BranchTrueFalseTotal
if (%required) 5 19 24
997 # pull any required options we encountered out,
998 # compare the number of required found against
999 # the number of required options and if they are
1000 # different, figure out what's missing and make
1001 # an error message.
1002 5 my %x;
1003 5 ▶ my @r = grep !$x{$_}++ && $required{$_} && $this->{$_}{'exists'}
Condition83%5/6
!$x{$_}++ && $required{$_} && $this->{$_}{'exists'}
!ll&&!rl&&r
2624
not $x{$_}++ and $required{$_}
!ll&&!rl&&r
0266
MC/DC66%2/3
!$x{$_}++ && $required{$_} && $this->{$_}{'exists'}
not $x{$_}++ $required{$_} $this->{$_}{'exists'}
Truth table75%3/4
not(A) and B && C
A= not $x{$_}++B= $required{$_}C= $this->{$_}{'exists'}
ABCresult
0XX0
10X0
1100
1111
1004 , keys %$this;
1005 5 ▶ unless(@r && @r == scalar(keys %required)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@r and @r == scalar keys %required) 2 3 5
Condition66%2/3
@r and @r == scalar keys %required
!ll&&!rl&&r
203
MC/DC50%1/2
@r and @r == scalar keys %required
@r @r == scalar keys %required
Truth table66%2/3
A and B
A= @rB= @r == scalar keys %required
ABresult
0X0
100
111
1006 0 my %r = map {$_,1} @r;
1007 2 my @missing = grep !$r{$_}, keys %required;
1008 2 push @errors, "Missing required options: @missing\n";
1009 }
1010 }
1011 }
1012 # Check for mutually exclusive options.
1013 38 ▶ unless (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@errors) 22 16 38
1014 22 ▶ if (exists $template{mutual_exclusive}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $template{"mutual_exclusive"}) 3 19 22
1015 3 ▶ if (ref $template{mutual_exclusive}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template{'mutual_exclusive'}) { } 3 0 3
1016 3 my @mutual_exclusive = @{$template{mutual_exclusive}};
1017 3 ▶ my @options = grep $_ =~ /^-/ && $this->{$_}{'exists'}, keys %$this;
Condition100%3/3
$_ =~ /^-/ && $this->{$_}{'exists'}
!ll&&!rl&&r
1256
MC/DC100%2/2
$_ =~ /^-/ && $this->{$_}{'exists'}
$_ =~ /^-/ $this->{$_}{'exists'}
Truth table100%3/3
A && B
A= $_ =~ /^-/B= $this->{$_}{'exists'}
ABresult
0X0
100
111
1018 3 ▶ if (ref $mutual_exclusive[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mutual_exclusive[0]) { } 3 0 3
1019 3 foreach my $ref (@mutual_exclusive) {
1020 3 my %check_hash = map {$_, 1} @$ref;
1021 3 ▶ if ((my @bad = grep $check_hash{$_}, @options) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my(@bad) = grep($check_hash{$_}, @options)) > 1) 1 2 3
1022 1 push @errors, "Found mutually exclusive options: ",
1023 "@bad\n";
1024 }
1025 }
1026 }
1027 # simple case: this could be just a list.
1028 else {
1029 0 my %check_hash = map {$_, 1} @mutual_exclusive;
1030 0 ▶ if ((my @bad = grep $check_hash{$_}, @options) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my(@bad) = grep($check_hash{$_}, @options)) > 1) 0 0 0
1031 0 push @errors, "Found mutually exclusive options: ",
1032 "@bad\n";
1033 }
1034 }
1035 }
1036 else {
1037 0 die "argument to mutual_exclusive should be an ",
1038 "array reference.\n";
1039 }
1040 }
1041 }
1042 38 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 17 21 38
1043 17 die $this->{'help'}, "Found following errors:\n", @errors;
1044 }
1045 21 return($this);
1046 }
1047 12 ▶sub Help {return $_[0]->{'help'}}
Subroutine Help: called CC=1 SCAR=0.0
Pod documented
1048 # If no key is given, return the number of options found.
1049 # If single value and no multiple set, unless user wants an
1050 # array back, return a scalar. If they want an array, give
1051 # 'em an array.
1052 # If user wants multiple value single time, give 'em an
1053 # array back.
1054 # If user wants multiple value multiple times, give 'em a
1055 # an array of list ref's.
1056 24 ▶sub Values {
Subroutine Values: called CC=13 SCAR=25.8
Pod documented
1057 24 my ($this, $key) = @_;
1058 24 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 18 6 24
1059 18 ▶ if (exists $this->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{$key}) { } 18 0 18
1060 18 my $ref = $this->{$key};
1061 18 ▶ if (exists $ref->{'values'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{'values'}) { } 16 2 18
1062 16 ▶ if ($ref->{'multi_value'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{'multi_value'}) { } 4 12 16
elsif ($ref->{'n_values'}) { } 7 5 12
1063 4 return (@{$ref->{'values'}})
1064 }
1065 elsif ($ref->{'n_values'}) {
1066 7 ▶ if ($ref->{'multiple'}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref->{'multiple'}) { } 2 5 7
elsif ($ref->{'n_values'} == 1) { } 1 4 5
1067 2 return(@{$ref->{'values'}})
1068 }
1069 elsif ($ref->{'n_values'} == 1) {
1070 1 return($ref->{'values'})
1071 }
1072 else {
1073 4 return(@{$ref->{'values'}})
1074 }
1075 }
1076 else {
1077 return(
1078 (wantarray())
1079 ? (ref $ref->{'values'}
1080 && ref $ref->{'values'} eq 'ARRAY')
1081 2 ? @{$ref->{'values'}}
1082 : ($ref->{'values'})
1083 5 ▶ : $ref->{'values'}
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 2 3 5
ref $ref->{'values'} && ref $ref->{'values'} eq 'ARRAY' ? : 2 0 2
Condition33%1/3
ref $ref->{"values"} and ref $ref->{"values"} eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $ref->{"values"} and ref $ref->{"values"} eq "ARRAY"
ref $ref->{"values"} ref $ref->{"values"} eq "ARRAY"
Truth table33%1/3
A and B
A= ref $ref->{"values"}B= ref $ref->{"values"} eq "ARRAY"
ABresult
0X0
100
111
1084 );
1085 }
1086 }
1087 else {
1088 2 return;
1089 }
1090 }
1091 else {
1092 0 die "Values called undefined option: $key\n";
1093 }
1094 }
1095 else {
1096 6 my @rv = @{$this->{'options'}};
1097 6 ▶ push @rv, 'other_values' if $this->{'other_values'}{'exists'};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"other_values"}{"exists"} 1 5 6
1098 6 ▶ (@rv) ? return(@rv) : return;
Branch: 1 branch
BranchTrueFalseTotal
@rv ? : 5 1 6
1099 }
1100 }
1101  
1102 3 ▶sub ClientData {
Subroutine ClientData: called CC=5 SCAR=17.2
Pod documented
1103 3 my ($this, $option, $data) = @_;
1104 3 ▶ if ($option && $this->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option and $this->{$option}) { } 3 0 3
Condition33%1/3
$option and $this->{$option}
!ll&&!rl&&r
003
MC/DC0%0/2
$option and $this->{$option}
$option $this->{$option}
Truth table33%1/3
A and B
A= $optionB= $this->{$option}
ABresult
0X0
100
111
1105 3 ▶ $this->{$option}{'client_data'} = $data if @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 3 1 2 3
1106 }
1107 else {
1108 0 die "ClientData called on undefined option.\n";
1109 }
1110 (exists $this->{$option}{'client_data'})
1111 3 ▶ ? return($this->{$option}{'client_data'})
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{$option}{'client_data'} ? : 2 1 3
1112 : return;
1113 }
1114 1 1;
1115  
1116 __END__
↑ Summary