blib/lib/Getopt/ApacheCommonsCLI.pm

filter: Statementstmt 89.0% 89 / 100 Branchbran 63.0% 29 / 46 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 2 totaltotal 78.2% 133 / 170 SCAR 35.5
CC
28
Cov
80%
GetOptionsApacheCommonsCLI
28.4
__ANON__
12.5
__ANON__
2.0
CRAP 34.7
↑ Summary
1 package Getopt::ApacheCommonsCLI;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10  
11 1 our %EXPORT_TAGS = ();
12  
13 1 ▶use constant OPT_PREC_UNIQUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant OPT_PREC_LEFT_TO_RIGHT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant OPT_PREC_RIGHT_TO_LEFT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our @EXPORT_OK = qw(
18 GetOptionsApacheCommonsCLI
19 OPT_PREC_UNIQUE
20 OPT_PREC_LEFT_TO_RIGHT
21 OPT_PREC_RIGHT_TO_LEFT
22 );
23  
24 1 our @EXPORT = qw(
25 );
26  
27 1 our $VERSION = '0.01';
28  
29 # Preloaded methods go here.
30  
31 1 ▶use Getopt::Long 2.35;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 sub GetOptionsApacheCommonsCLI {
34 13 ▶ my ($rspec, $ropts, $roptions, $rerrsub) = @_;
Subroutine GetOptionsApacheCommonsCLI: called CC=21 SCAR=33.5
Pod undocumented
35  
36 # process user-supplied options
37 13 ▶ my $DEBUG = defined $roptions->{'DEBUG'} ? $roptions->{'DEBUG'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $roptions->{'DEBUG'} ? : 13 0 13
38 13 ▶ my $JAVA_DOPTS = defined $roptions->{'JAVA_DOPTS'} ? $roptions->{'JAVA_DOPTS'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $roptions->{'JAVA_DOPTS'} ? : 13 0 13
39 13 ▶ my $OPT_PREC = defined $roptions->{'OPT_PRECEDENCE'} ? $roptions->{'OPT_PRECEDENCE'} : OPT_PREC_RIGHT_TO_LEFT;
Branch: 1 branch
BranchTrueFalseTotal
defined $roptions->{'OPT_PRECEDENCE'} ? : 13 0 13
40 13 ▶ my $BUNDLING = defined $roptions->{'BUNDLING'} ? $roptions->{'BUNDLING'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $roptions->{'BUNDLING'} ? : 13 0 13
41 13 ▶ my $rambigs = defined $roptions->{'AMBIGUITIES'} ? $roptions->{'AMBIGUITIES'} : undef; # reserved for future use
Branch: 1 branch
BranchTrueFalseTotal
defined $roptions->{'AMBIGUITIES'} ? : 13 0 13
42  
43 13 $ropts->{__argv__} = '';
44 13 $ropts->{__errors__} = [];
45 13 $ropts->{__argv_original__} = join(' ', @ARGV);
46  
47 13 my @GO_config = qw(pass_through no_auto_abbrev no_ignore_case prefix_pattern=--|-); # passed to Getopt::Long for behavior of Apache Common CLI Java library
48  
49 13 ▶ if ($BUNDLING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($BUNDLING) 7 6 13
50 7 push @GO_config, 'bundling_override';
51 }
52  
53 13 my @GO_options;
54  
55 # setup a validation handler for missing argument
56 13 ▶ if (ref($rerrsub) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rerrsub ne "CODE") 0 13 13
57 0 ▶ $rerrsub = sub { my ($option, $value, $rhash) = @_; print "error: missing value for option: $option\n"; die "!FINISH"; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 }
59  
60 13 my %longs;
61  
62 # read user input specification and process for Getopt::Long
63 13 for my $s (@{$rspec}) {
64 260 my ($long, $short, $type) = $s =~ /([a-zA-Z0-9_-]+)\|([a-zA-Z0-9]*)[=:]?([fios]*)/;
65 260 ▶ next if $long eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $long eq "" 0 260 260
66  
67 260 ▶ if ($short eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($short eq "") 0 260 260
68 0 $short = $long;
69 }
70  
71 260 ▶ if (length($short) > length($long)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $short > length $long) 0 260 260
72 0 ($short, $long) = ($long, $short);
73 }
74  
75 260 $longs{$long} = $short;
76  
77 # use either the first or second anonymous subroutine as a reference (we are not calling them ... GO will call them)
78 push @GO_options, ($s, $type ne '' ?
79 sub {
80 26 ▶ my ($option, $value, $rhash) = @_;
Subroutine __ANON__: called CC=7 SCAR=25.2
81  
82 26 ▶ if (not defined $value or $value eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or $value eq "") 0 26 26
Condition33%1/3
not defined $value or $value eq ""
l!l&&r!l&&!r
0026
MC/DC0%0/2
not defined $value or $value eq ""
not defined $value $value eq ""
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq ""
ABresult
1X1
011
000
83 0 push @{$ropts->{__errors__}}, "no value for option $option";
84 0 &$rerrsub($option, $value, 0);
85 0 return 0;
86 }
87  
88 26 ▶ if (exists $ropts->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ropts->{$option}) { } 1 25 26
89 1 ▶ if ($OPT_PREC == OPT_PREC_UNIQUE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($OPT_PREC == 1) { } 1 0 1
elsif ($OPT_PREC == 2) { } 0 0 0
elsif ($OPT_PREC == 0) { } 0 0 0
90 1 push @{$ropts->{__errors__}}, "duplicate option $option with $value";
91 1 &$rerrsub($option, $value, 1);
92 1 return 0;
93 }
94 elsif ($OPT_PREC == OPT_PREC_RIGHT_TO_LEFT) {
95 0 $ropts->{$option} = $value;
96 }
97 elsif ($OPT_PREC == OPT_PREC_LEFT_TO_RIGHT) {
98 ; # ignore
99 }
100 }
101 else {
102 25 $ropts->{$option} = $value;
103 }
104 } :
105 sub {
106 9 ▶ my ($option, $value, $rhash) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
107  
108 9 $ropts->{$option} = 1; # boolean option
109 }
110 260 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$type ne '' ? : 117 143 260
111 }
112  
113 # # bundling_override handles this fairly well ...
114 #
115 # # args pre-processing - to reduce parsing ambiguities, replace some of the short options with long options before calling Getopt::Long
116 #
117 # if (scalar(@ARGV) > 0) {
118 # for (my $n=0; $n < scalar(@ARGV); $n++) {
119 # last if $ARGV[$n] eq '--';
120 # $ARGV[$n] =~ s/^-([\w]+)$/exists $longs{$1} ? "--$1" : "-$1"/e; # double-dash long args which only start with a single-dash
121 # $ARGV[$n] =~ s/^(--?)([\w]{2,3})$/exists $rambigs->{$2} ? "--$rambigs->{$2}" : "$1$2"/e; # convert short options to long options because of bundling ambiguity
122 # }
123 # }
124  
125 13 Getopt::Long::Configure(@GO_config);
126 13 my $result = GetOptions(@GO_options);
127  
128 # args post-processing
129 13 ▶ if (scalar(@ARGV)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ARGV) 11 2 13
130 11 ▶ if ($JAVA_DOPTS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($JAVA_DOPTS) 2 9 11
131 2 ▶ for (my $n=0; $n < scalar(@ARGV); $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < scalar @ARGV) 3 1 4
132 3 ▶ if ($ARGV[$n] eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[$n] eq "--") 1 2 3
133 1 last;
134 }
135 2 $ARGV[$n] =~ s/^ +//;
136 2 $ARGV[$n] =~ s/ +$//;
137 2 $ARGV[$n] =~ s/^--?D(\w+)=['"]?([\w.]+)['"]?$/$ropts->{$1} = $2; '';/e; # process -Dabc=z.y.z, overwrite existing values in the special case of -D (behavior is like OPT_PREC_RIGHT_TO_LEFT)
138 }
139 }
140 }
141  
142 13 my $cmd = join(' ', @ARGV);
143 13 $cmd =~ s/ +/ /g; # is there a case where we care about embedded spaces in remaining ARGV?
144 13 $cmd =~ s/^ +//g;
145 13 $cmd =~ s/ +$//g;
146  
147 13 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DEBUG) 13 0 13
148 13 ▶ debug_print($ropts) if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 13 0 13
149 13 print "cmd=$cmd\n";
150 }
151  
152 # stash remaining ARGV in the output hash
153 13 $ropts->{'__argv__'} = $cmd;
154  
155 13 ▶ if ($result == 0 or @{$ropts->{'__errors__'}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result == 0 or @{$ropts->{'__errors__'};}) { } 1 12 13
Condition66%2/3
$result == 0 or @{$ropts->{"__errors__"};}
l!l&&r!l&&!r
0112
MC/DC50%1/2
$result == 0 or @{$ropts->{"__errors__"};}
$result == 0 @{$ropts->{"__errors__"};}
Truth table66%2/3
A or B
A= $result == 0B= @{$ropts->{"__errors__"};}
ABresult
1X1
011
000
156 1 return 0; # failure (according to Getopt::Long protocol)
157 }
158 else {
159 12 return 1; # success (according to Getopt::Long protocol)
160 }
161 }
162  
163 # sub value_not_required {
164 # # option arg not expected, but we still want to set it to 1
165 # my ($option, $value, $rhash) = @_;
166 #
167 # if ($option ne "") {
168 # $ropts->{$option} = 1;
169 # }
170 # }
171 #
172 # sub value_required {
173 # # option arg expected, do error handling if missing, including a custom error message
174 # my ($option, $value, $rhash) = @_;
175 #
176 # if ($option ne "") {
177 # if (not defined $value or $value eq "") {
178 # print "Missing argument for option:$option\n";
179 # $n_errs++;
180 # die "!FINISH";
181 # }
182 # else {
183 # if (exists $ropts->{$option} and $OPT_PREC == OPT_PREC_UNIQUE) {
184 # print "Unrecognized command: $value\n";
185 # $n_errs++;
186 # die "!FINISH";
187 # }
188 # elsif (exists $ropts->{$option} and $OPT_PREC == OPT_PREC_LEFT_TO_RIGHT) {
189 # ;
190 # }
191 # else {
192 # $ropts->{$option} = $value;
193 # }
194 # }
195 # }
196 # }
197  
198 sub debug_print {
199 13 ▶ my ($ropts) = @_;
Subroutine debug_print: called CC=2 SCAR=6.9
Pod undocumented
200  
201 13 for my $o (sort keys %{$ropts}) {
202 75 print "$o=$ropts->{$o}\n";
203 }
204 }
205 1 1;
206 __END__
↑ Summary