| 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
| |||||||||||||||||||||||||||||
| 38 | 13 | ▶ | my $JAVA_DOPTS = defined $roptions->{'JAVA_DOPTS'} ? $roptions->{'JAVA_DOPTS'} : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 13 | ▶ | my $OPT_PREC = defined $roptions->{'OPT_PRECEDENCE'} ? $roptions->{'OPT_PRECEDENCE'} : OPT_PREC_RIGHT_TO_LEFT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 13 | ▶ | my $BUNDLING = defined $roptions->{'BUNDLING'} ? $roptions->{'BUNDLING'} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 13 | ▶ | my $rambigs = defined $roptions->{'AMBIGUITIES'} ? $roptions->{'AMBIGUITIES'} : undef; # reserved for future use | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 260 | ▶ | if ($short eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | $short = $long; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 260 | ▶ | if (length($short) > length($long)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not defined $value or $value eq ""
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 ""
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if ($OPT_PREC == OPT_PREC_UNIQUE) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 130 | 11 | ▶ | if ($JAVA_DOPTS) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 2 | ▶ | for (my $n=0; $n < scalar(@ARGV); $n++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 3 | ▶ | if ($ARGV[$n] eq '--') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 148 | 13 | ▶ | debug_print($ropts) if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$result == 0 or @{$ropts->{"__errors__"};}
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__"};}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||