blib/lib/Zoidberg/Utils/GetOpt.pm

filter: Statementstmt 74.2% 121 / 163 Branchbran 65.9% 87 / 132 Conditioncond 53.5% 30 / 56 MC/DCmcdc 38.2% 13 / 34 Subroutinesub 70.0% 7 / 10 Podpod 80.0% 4 / 5 totaltotal 65.5% 262 / 400 CC 90 SCAR 48.1
CC
90
Cov
68%
help
342.0
getopt
85.6
path2hashref
4.0
CRAP 122.1
« blib/lib/Zoidberg/Fish/Intel.pm ↑ Summary blib/lib/Zoidberg/Fish/Commands.pm »
1 package Zoidberg::Utils::GetOpt;
2  
3 63 our $VERSION = '0.981';
4  
5 63 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 63 ▶use Zoidberg::Utils::Error qw/error bug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 63 ▶use Zoidberg::Utils::Output qw/output debug/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use Exporter::Tidy
9 63 ▶ default => ['getopt'],
Subroutine BEGIN: called CC=1 SCAR=0.0
10 63 other => [qw/help usage version path2hashref/] ;
11  
12 63 our $ERROR_CALLER = 1;
13  
14 273 ▶sub getopt { # hic sunt leones
Subroutine getopt: called CC=69 SCAR=44.5
Pod documented
15 273 my ($conf, @args) = @_;
16 273 my (%conf, @opts, %opts, $args);
17 273 ▶ if (ref $conf) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf) 1 272 273
18 1 %conf = %$conf;
19 1 goto PARSE_OPTS;
20 }
21  
22 # parse config
23 272 ▶ $conf{_args} = $1 if $conf =~ s/(?<!\S)([\@\%\*])\s*$//;
Branch: 1 branch
BranchTrueFalseTotal
if $conf =~ s/(?<!\S)([\@\%\*])\s*$// 262 10 272
24 272 ▶ goto PARSE_ARGS unless $conf and $args[0] =~ /^[+-]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $conf and $args[0] =~ /^[+-]/ 212 60 272
Condition100%3/3
$conf and $args[0] =~ /^[+-]/
!ll&&!rl&&r
6814460
MC/DC100%2/2
$conf and $args[0] =~ /^[+-]/
$conf $args[0] =~ /^[+-]/
Truth table100%3/3
A and B
A= $confB= $args[0] =~ /^[+-]/
ABresult
0X0
100
111
25 60 for (split /\s+/, $conf) {
26 908 ▶ my $arg = s/([\$\@\%])$// ? $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
s/([\$\@\%])$// ? : 282 626 908
27 908 my ($opt, @al) = split ',', $_;
28 908 ▶ unless ($opt =~ s/\*$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ s/\*$//) { } 182 726 908
29 726 $conf{$opt} = $arg;
30 726 $conf{_alias}{$_} = $opt for @al;
31 }
32 else {
33 182 ▶ error 'config syntax error' if @al || ! length $_;
Branch: 1 branch
BranchTrueFalseTotal
if @al or not length $_ 0 182 182
Condition33%1/3
@al or not length $_
l!l&&r!l&&!r
00182
MC/DC0%0/2
@al or not length $_
@al not length $_
Truth table33%1/3
A or not(B)
A= @alB= not length $_
ABresult
1X1
011
000
34 182 $conf{$opt} = $arg;
35 182 ▶ $conf{_glob} ||= [];
Condition100%2/2
$conf{"_glob"} ||= []
l!l
13646
MC/DC100%1/1
$conf{"_glob"} ||= []
$conf{"_glob"}
Truth table100%2/2
A
A= $conf{"_glob"}
Aresult
11
00
36 182 push @{$conf{_glob}}, $opt;
37 }
38 }
39 60 ▶ $conf{_glob} = '^('.join('|', map {s/^\+/\\+/; $_} @{$conf{_glob}}).')(?!-)' if $conf{_glob};
Branch: 1 branch
BranchTrueFalseTotal
if $conf{"_glob"} 46 14 60
40 #use Data::Dumper; print STDERR 'conf: ', Dumper \%conf;
41  
42 PARSE_OPTS:
43 61 for ( # set default options
44 [qw/h help/, \&help ],
45 [qw/u usage/, \&usage ],
46 [qw/v version/, \&version]
47 ) {
48 183 ▶ next if exists $conf{$$_[1]};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{$_->[1]} 90 93 183
49 93 $conf{$$_[1]} = $$_[2];
50 $conf{_alias}{'-'.$$_[0]} = $$_[1]
51 93 ▶ unless exists $conf{_alias}{'-'.$$_[0]} or exists $conf{_alias}{$$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $conf{"_alias"}{'-' . $_->[0]} or exists $conf{"_alias"}{$_->[0]} 44 49 93
Condition66%2/3
exists $conf{"_alias"}{'-' . $_->[0]} or exists $conf{"_alias"}{$_->[0]}
l!l&&r!l&&!r
04944
MC/DC50%1/2
exists $conf{"_alias"}{'-' . $_->[0]} or exists $conf{"_alias"}{$_->[0]}
exists $conf{"_alias"}{'-' . $_->[0]} exists $conf{"_alias"}{$_->[0]}
Truth table66%2/3
A or B
A= exists $conf{"_alias"}{'-' . $_->[0]}B= exists $conf{"_alias"}{$_->[0]}
ABresult
1X1
011
000
52 }
53  
54 61 my $delim = 0;
55 61 ▶ while (@args) { # parse opts
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 124 53 177
56 124 ▶ last unless $args[0] =~ /^(-|\+.)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $args[0] =~ /^(-|\+.)/ 6 118 124
57 118 $_ = shift @args;
58 118 /^(--|-|\+)(.*)/;
59 118 ▶ ++$delim and last unless length $2;
Branch: 1 branch
BranchTrueFalseTotal
unless length $2 1 117 118
Condition60%3/5
length $2 || ++$delim && (last)
l!l&&r!l&&!r
11710
++$delim and last
!ll
01
MC/DC0%0/2
length $2 || ++$delim && (last)
length $2 ++$delim
Truth table66%2/3
A || B
A= length $2B= ++$delim
ABresult
1X1
011
000
60 117 my ($pre, $opt, $arg) = ($1, split '=', $2, 2);
61  
62 117 my (@chars, $type);
63 117 my $raw = $pre.$opt;
64 117 ▶ if (exists $conf{_alias}{$raw} or exists $conf{$raw}) { $opt = $raw }
Branch: 3 branches
BranchTrueFalseTotal
if (exists $conf{'_alias'}{$raw} or exists $conf{$raw}) { } 100 17 117
elsif (exists $conf{'_glob'} and $raw =~ /$conf{'_glob'}/) { } 3 14 17
elsif ($pre ne '--' and length $opt > 1) { } 3 17 20
Condition100%3/3
exists $conf{"_alias"}{$raw} or exists $conf{$raw}
l!l&&r!l&&!r
19917
MC/DC100%2/2
exists $conf{"_alias"}{$raw} or exists $conf{$raw}
exists $conf{"_alias"}{$raw} exists $conf{$raw}
Truth table100%3/3
A or B
A= exists $conf{"_alias"}{$raw}B= exists $conf{$raw}
ABresult
1X1
011
000
65 ▶ elsif (exists $conf{_glob} and $raw =~ /$conf{_glob}/) {
Condition66%2/3
exists $conf{"_glob"} and $raw =~ /$conf{"_glob"}/
!ll&&!rl&&r
1403
MC/DC50%1/2
exists $conf{"_glob"} and $raw =~ /$conf{"_glob"}/
exists $conf{"_glob"} $raw =~ /$conf{"_glob"}/
Truth table66%2/3
A and B
A= exists $conf{"_glob"}B= $raw =~ /$conf{"_glob"}/
ABresult
0X0
100
111
66 3 $opt = $raw;
67 3 $type = $conf{$1};
68 }
69 ▶ elsif ($pre ne '--' and length $opt > 1) { # try short options
Condition100%3/3
$pre ne "--" and length $opt > 1
!ll&&!rl&&r
653
MC/DC100%2/2
$pre ne "--" and length $opt > 1
$pre ne "--" length $opt > 1
Truth table100%3/3
A and B
A= $pre ne "--"B= length $opt > 1
ABresult
0X0
100
111
70 3 @chars = split '', $opt;
71 }
72  
73 PARSE_OPT:
74 120 ▶ $opt = shift @chars if @chars;
Branch: 1 branch
BranchTrueFalseTotal
if @chars 6 114 120
75 120 ▶ $opt = $conf{_alias}{$opt} if exists $conf{_alias}{$opt};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conf{"_alias"}{$opt} 10 110 120
76
77 120 ▶ unless (defined $type) { # type is set if glob
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 117 3 120
78 117 ▶ if (exists $conf{$opt}) { $type = $conf{$opt} }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $conf{$opt}) { } 116 1 117
79 1 else { error "unrecognized option '$opt'" }
80 }
81  
82 119 push @opts, $opt;
83 119 ▶ if (! $type) { # no arg
Branch: 2 branches
BranchTrueFalseTotal
if (not $type) { } 18 101 119
elsif (ref $type) { } 0 101 101
84 18 ▶ error "option '$opt' doesn't take an argument" if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 0 18 18
85 18 ▶ $opts{$opt} = ($pre eq '+') ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$pre eq '+' ? : 2 16 18
86 }
87 elsif (ref $type) { # CODE ... for default opts
88 0 output $type->( (caller(1))[3], (caller)[0] ); # subroutine, package
89 0 error {silent => 1, exit_status => 0}, 'getopt needed to pop stack';
90 }
91 else {
92 101 ▶ $arg = defined($arg) ? $arg : shift(@args);
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 2 99 101
93 101 ▶ error "option '$opt' requires an argument" unless defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 101 101
94 101 ▶ if ($type eq '$') { $opts{$opt} = $arg }
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq '$') { } 4 97 101
elsif ($type eq '@') { } 97 0 97
95 elsif ($type eq '@') {
96 97 ▶ if (ref $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg) { } 1 96 97
97 1 ▶ error 'argument is not a ARRAY reference'
Branch: 1 branch
BranchTrueFalseTotal
if ref $arg ne "ARRAY" 0 1 1
98 if ref($arg) ne 'ARRAY';
99 0 ▶ if ($opts{$opt}) { push @{$opts{$opt}}, @$arg }
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$opt}) { } 0 1 1
100 1 else { $opts{$opt} = $arg }
101 }
102 else {
103 96 ▶ $opts{$opt} ||= [];
Condition100%2/2
$opts{$opt} ||= []
l!l
4848
MC/DC100%1/1
$opts{$opt} ||= []
$opts{$opt}
Truth table100%2/2
A
A= $opts{$opt}
Aresult
11
00
104 96 push @{$opts{$opt}}, $arg;
105 }
106 }
107 }
108 119 $arg = $type = undef;
109 119 ▶ goto PARSE_OPT if @chars;
Branch: 1 branch
BranchTrueFalseTotal
if @chars 3 116 119
110 };
111 60 ▶ error @opts
Branch: 2 branches
BranchTrueFalseTotal
if not $delim and grep /^-/, @args 1 59 60
@opts ? : 1 0 1
Condition100%3/3
not $delim and grep /^-/, @args
!ll&&!rl&&r
1581
MC/DC100%2/2
not $delim and grep /^-/, @args
not $delim grep /^-/, @args
Truth table100%3/3
not(A) and B
A= not $delimB= grep /^-/, @args
ABresult
0X0
100
111
112 ? "option '$opts[-1]' doesn't take an argument"
113 : 'options found after first argument' if !$delim and grep /^-/, @args;
114 59 ▶ $opts{_opts} = \@opts if @opts; # keep %opts empty unless there are opts
Branch: 1 branch
BranchTrueFalseTotal
if @opts 59 0 59
115  
116 PARSE_ARGS: # parse args
117 271 ▶ unless ($conf{_args}) { $args = [@args] }
Branch: 4 branches
BranchTrueFalseTotal
if ($conf{'_args'}) { } 262 9 271
if (do { $conf{'_args'} eq '@' }) { } 175 87 262
elsif ($conf{'_args'} eq '%') { } 66 21 87
elsif ($conf{'_args'} eq '*') { } 21 0 21
118 elsif ($conf{_args} eq '@') {
119 error "argument should be a ARRAY reference"
120 175 ▶ if grep {ref($_) and ref($_) ne 'ARRAY'} @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ and ref $_ ne "ARRAY";} @args 0 175 175
Condition33%1/3
ref $_ && ref $_ ne 'ARRAY'
!ll&&!rl&&r
13100
MC/DC0%0/2
ref $_ && ref $_ ne 'ARRAY'
ref $_ ref $_ ne 'ARRAY'
Truth table33%1/3
A && B
A= ref $_B= ref $_ ne 'ARRAY'
ABresult
0X0
100
111
121 0 ▶ if (ref $args[0] and @args == 1) { $args = $args[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] and @args == 1) { } 0 175 175
Condition33%1/3
ref $args[0] and @args == 1
!ll&&!rl&&r
17500
MC/DC0%0/2
ref $args[0] and @args == 1
ref $args[0] @args == 1
Truth table33%1/3
A and B
A= ref $args[0]B= @args == 1
ABresult
0X0
100
111
122 175 ▶ else { $args = [ map {ref($_) ? @$_ : $_} @args] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 131 131
123 }
124 elsif ($conf{_args} eq '%') {
125 error "argument should be a HASH reference"
126 66 ▶ if grep {ref($_) and ref($_) ne 'HASH'} @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ and ref $_ ne "HASH";} @args 0 66 66
Condition66%2/3
ref $_ && ref $_ ne 'HASH'
!ll&&!rl&&r
3640
MC/DC0%0/2
ref $_ && ref $_ ne 'HASH'
ref $_ ref $_ ne 'HASH'
Truth table66%2/3
A && B
A= ref $_B= ref $_ ne 'HASH'
ABresult
0X0
100
111
127 66 ▶ if (ref $args[0] and @args == 1) { $args = $args[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] and @args == 1) { } 64 2 66
Condition66%2/3
ref $args[0] and @args == 1
!ll&&!rl&&r
2064
MC/DC50%1/2
ref $args[0] and @args == 1
ref $args[0] @args == 1
Truth table66%2/3
A and B
A= ref $args[0]B= @args == 1
ABresult
0X0
100
111
128 else {
129 2 my $error;
130 $args = { map {
131 0 ▶ if (ref $_) { (%$_) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 3 3
132 else {
133 3 ▶ m/(.*?)=(.*)/ or $error++;
Branch: 1 branch
BranchTrueFalseTotal
/(.*?)=(.*)/ or ++$error 3 0 3
134 3 ($1 => $2)
135 }
136 } @args };
137 2 ▶ error 'syntax error, should be \'key=value\'' if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 2 2
138 }
139 }
140 elsif ($conf{_args} eq '*') {
141 21 my (@keys, %vals);
142 21 for (@args) {
143 28 ▶ if (ref $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 2 26 28
elsif (/(.*?)=(.*)/) { } 12 14 26
144 2 my $t = ref $_;
145 2 ▶ if ($t eq 'ARRAY') { push @keys, @$_ }
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'ARRAY') { } 1 1 2
elsif ($t eq 'HASH') { } 1 0 1
146 elsif ($t eq 'HASH') {
147 1 push @keys, keys %$_;
148 1 %vals = (%vals, %$_);
149 }
150 0 else { error "can't handle $t reference argument" }
151 }
152 elsif (m/(.*?)=(.*)/) {
153 12 push @keys, $1;
154 12 $vals{$1} = $2;
155 }
156 14 else { push @keys, $_ }
157 }
158 21 return \%opts, \@keys, \%vals;
159 }
160  
161 250 return \%opts, $args;
162 }
163  
164 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod documented
165 0 $_[2] = 1;
166 0 goto &help;
167 }
168  
169 0 ▶sub help {
Subroutine help: not called CC=18 SCAR=58.3
Pod undocumented
170 0 my ($cmd, $file, $usage) = @_;
171  
172 0 ▶ $cmd ||= (caller(1))[3];
Condition0%0/3
$cmd ||= (caller 1)[3]
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd ||= (caller 1)[3]
$cmd (caller 1)[3]
Truth table0%0/3
A ||= B
A= $cmdB= (caller 1)[3]
ABresult
1X1
011
000
173 0 ▶ $file = $1 if $cmd =~ s/(.*):://;
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ s/(.*)::// 0 0 0
174 0 $file =~ s/::/\//g;
175 0 $file =~ s/\.pm$//;
176 0 ▶ ($file) = grep {-e $_} map {("$_/$file.pod", "$_/$file.pm")} @INC
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[^/] 0 0 0
177 unless $file =~ m#^/#;
178  
179 0 ▶ open POD, $file || error "Could not read $file";
Condition0%0/3
$file || error("Could not read $file")
l!l&&r!l&&!r
000
MC/DC0%0/2
$file || error("Could not read $file")
$file error("Could not read $file")
Truth table0%0/3
A || B
A= $fileB= error("Could not read $file")
ABresult
1X1
011
000
180 0 my ($help, $p, $o) = ('', 0, 0);
181 0 ▶ while (<POD>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline POD)) 0 0 0
182 0 ▶ if ($p) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p) { } 0 0 0
elsif (/^=item\s+$cmd/) { } 0 0 0
183 0 ▶ if (/^=over/) { $o++ }
Branch: 3 branches
BranchTrueFalseTotal
if (/^=over/) { } 0 0 0
elsif (/^=back/) { } 0 0 0
elsif (/^=(item(?!\s+$cmd)|back|cut)/) { } 0 0 0
184 0 elsif (/^=back/) { $o-- }
185 elsif (/^=(item(?!\s+$cmd)|back|cut)/) {
186 0 ▶ last unless $o;
Branch: 1 branch
BranchTrueFalseTotal
unless $o 0 0 0
187 }
188 0 ▶ $help .= $_ unless $usage and ! $o and ! /^=item\s+$cmd/;
Branch: 1 branch
BranchTrueFalseTotal
unless $usage and not $o and not /^=item\s+$cmd/ 0 0 0
Condition0%0/6
$usage and not $o and not /^=item\s+$cmd/
!ll&&!rl&&r
000
$usage and not $o
!ll&&!rl&&r
000
MC/DC0%0/3
$usage and not $o and not /^=item\s+$cmd/
$usage not $o not /^=item\s+$cmd/
Truth table0%0/4
A and not(B) and not(C)
A= $usageB= not $oC= not /^=item\s+$cmd/
ABCresult
0XX0
10X0
1100
1111
189 # only return 'item' lines if short format
190 }
191 elsif (/^=item\s+$cmd/) {
192 0 $p = 1;
193 0 $help = $_;
194 }
195 }
196 0 close POD;
197  
198 0 $help =~ s/^\s+|\s+$//g;
199 0 ▶ if ($usage) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usage) { } 0 0 0
200 0 $help =~ s/^=item\s+/ /gm;
201 0 $help = "usage:\n".$help;
202 }
203 0 else { $help =~ s/^=\w+\s+/= /gm }
204 0 $help =~ s/(\w)<<(.*?)>>|\w<(.*?)>/
205 0 ▶ ($1 eq 'B') ? "\"$2$3\"" :
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'B' ? : 0 0 0
$1 eq 'C' ? : 0 0 0
206 ($1 eq 'C') ? "`$2$3`" : "'$2$3'"
207 /ge;
208 0 return $help;
209 }
210  
211 0 ▶sub version {
Subroutine version: not called CC=2 SCAR=11.6
Pod documented
212 0 my (undef, $class) = @_;
213 63 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
214 0 ▶ return ${$class.'::LONG_VERSION'} || $class.' '.${$class.'::VERSION'};
Condition0%0/2
${$class . '::LONG_VERSION';} || $class . ' ' . ${$class . '::VERSION';}
l!l
00
MC/DC0%0/1
${$class . '::LONG_VERSION';} || $class . ' ' . ${$class . '::VERSION';}
${$class . '::LONG_VERSION';}
Truth table0%0/2
A
A= ${$class . '::LONG_VERSION';}
Aresult
11
00
215 }
216  
217 10 ▶sub path2hashref {
Subroutine path2hashref: called CC=4 SCAR=14.0
Pod documented
218 10 my ($hash, $key) = @_;
219 10 my $path = '/';
220 10 ▶ while ($key =~ s#^/*(.+?)/##) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ s[^/*(.+?)/][]) 10 10 20
221 10 $path .= $1 . '/';
222 10 ▶ if (! defined $$hash{$1}) { $$hash{$1} = {} }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $hash->{$1}) { } 2 8 10
elsif (ref $hash->{$1} ne 'HASH') { } 0 8 8
223 0 elsif (ref($$hash{$1}) ne 'HASH') { return undef, undef, $path } # bail out
224 10 $hash = $$hash{$1};
225 }
226 10 return $hash, $key, $path;
227 }
228  
229 63 1;
230  
231 __END__
« blib/lib/Zoidberg/Fish/Intel.pm ↑ Summary blib/lib/Zoidberg/Fish/Commands.pm »