blib/lib/Getopt/Compact/WithCmd.pm

filter: Statementstmt 96.5% 478 / 495 Branchbran 90.1% 229 / 254 Conditioncond 77.7% 98 / 126 MC/DCmcdc 53.5% 38 / 71 Subroutinesub 89.8% 44 / 49 Podpod 100% 16 / 16 totaltotal 89.3% 903 / 1011 CC 196 SCAR 27.0
CC
196
Cov
92%
usage
34.2
_parse_struct
30.7
__ANON__
17.4
CRAP 14.9
↑ Summary
1 package Getopt::Compact::WithCmd;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use constant DEFAULT_CONFIG => (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 no_auto_abbrev => 1,
12 no_ignore_case => 1,
13 bundling => 1,
14 20 );
15  
16 20 our $VERSION = '0.22';
17  
18 20 my $TYPE_MAP = {
19 'Bool' => '!',
20 'Incr' => '+',
21 'Str' => '=s',
22 'Int' => '=i',
23 'Num' => '=f',
24 'ExNum' => '=o',
25 };
26  
27 20 my $TYPE_GEN = {};
28  
29 199 ▶sub new {
Subroutine new: called CC=17 SCAR=28.5
Pod documented
30 199 my ($class, %args) = @_;
31 my $self = bless {
32 195 ▶ cmd => $args{cmd} || do { require File::Basename; File::Basename::basename($0) },
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'usage'} && !$args{'usage'} ? : 37 162 199
Condition80%8/10
$args{'version'} || $main::VERSION
l!l&&r!l&&!r
40195
exists $args{"usage"} and not $args{"usage"}
!ll&&!rl&&r
162037
$args{'args'} || ''
l!l
4195
$args{'command_struct'} || {}
l!l
79120
MC/DC50%1/2
$args{'version'} || $main::VERSION
$args{'version'} $main::VERSION
MC/DC50%1/2
exists $args{"usage"} and not $args{"usage"}
exists $args{"usage"} not $args{"usage"}
MC/DC100%1/1
$args{'args'} || ''
$args{'args'}
MC/DC100%1/1
$args{'command_struct'} || {}
$args{'command_struct'}
Truth table66%2/3
A || B
A= $args{'version'}B= $main::VERSION
ABresult
1X1
011
000
Truth table66%2/3
A and not(B)
A= exists $args{"usage"}B= not $args{"usage"}
ABresult
0X0
100
111
Truth table100%2/2
A
A= $args{'args'}
Aresult
11
00
Truth table100%2/2
A
A= $args{'command_struct'}
Aresult
11
00
33 name => $args{name},
34 version => $args{version} || $::VERSION,
35 modes => $args{modes},
36 opt => {},
37 usage => exists $args{usage} && !$args{usage} ? 0 : 1,
38 args => $args{args} || '',
39 _argv => \@ARGV,
40 struct => [],
41 summary => {},
42 requires => {},
43 ret => 0,
44 error => undef,
45 other_usage => undef,
46 commands => [],
47 _struct => $args{command_struct} || {},
48 199 ▶ }, $class;
Condition66%2/3
$args{'cmd'} || do { require File::Basename; File::Basename::basename($0) }
l!l&&r!l&&!r
41950
MC/DC0%0/2
$args{'cmd'} || do { require File::Basename; File::Basename::basename($0) }
$args{'cmd'} do { require File::Basename; File::Basename::basename($0) }
Truth table66%2/3
A || B
A= $args{'cmd'}B= do { require File::Basename; File::Basename::basename($0) }
ABresult
1X1
011
000
49  
50 199 ▶ my %config = (DEFAULT_CONFIG, %{$args{configure} || {}});
Condition50%1/2
$args{'configure'} || {}
l!l
0199
MC/DC0%0/1
$args{'configure'} || {}
$args{'configure'}
Truth table50%1/2
A
A= $args{'configure'}
Aresult
11
00
51 199 my @gconf = grep $config{$_}, keys %config;
52 199 ▶ Getopt::Long::Configure(@gconf) if @gconf;
Branch: 1 branch
BranchTrueFalseTotal
if @gconf 199 0 199
53  
54 199 $self->_init_summary($args{command_struct});
55  
56 199 ▶ $self->_init_struct($args{global_struct} || []);
Condition100%2/2
$args{'global_struct'} || []
l!l
13861
MC/DC100%1/1
$args{'global_struct'} || []
$args{'global_struct'}
Truth table100%2/2
A
A= $args{'global_struct'}
Aresult
11
00
57 199 ▶ my $opthash = $self->_parse_struct || return $self;
Condition100%2/2
$self->_parse_struct || (return $self)
l!l
1954
MC/DC100%1/1
$self->_parse_struct || (return $self)
$self->_parse_struct
Truth table100%2/2
A
A= $self->_parse_struct
Aresult
11
00
58 195 ▶ if ($args{command_struct}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'command_struct'}) { } 79 116 195
59 79 ▶ if (my @gopts = $self->_parse_argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@gopts) = $self->_parse_argv) 62 17 79
60 62 $self->{ret} = $self->_parse_option(\@gopts, $opthash);
61 62 unshift @ARGV, @gopts;
62 62 ▶ return $self unless $self->{ret};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ret"} 2 60 62
63 60 ▶ return $self if $self->_want_help;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_want_help 0 60 60
64 }
65 77 $self->_check_requires;
66 }
67 else {
68 116 $self->{ret} = $self->_parse_option(\@ARGV, $opthash);
69 116 ▶ return $self unless $self->{ret};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ret"} 6 110 116
70 110 ▶ return $self if $self->_want_help;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_want_help 7 103 110
71 103 $self->_check_requires;
72 103 return $self;
73 }
74  
75 77 $self->_parse_command_struct($args{command_struct});
76 77 return $self;
77 }
78  
79 40 ▶sub new_from_array {
Subroutine new_from_array: called CC=2 SCAR=7.4
Pod documented
80 40 my ($class, $args, %options) = @_;
81 40 ▶ unless (ref $args eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args eq "ARRAY") 0 40 40
82 0 Carp::croak("Usage: $class->new_from_array(\\\@args, %options)");
83 }
84 40 local *ARGV = $args;
85 40 return $class->new(%options);
86 }
87  
88 38 ▶sub new_from_string {
Subroutine new_from_string: called CC=2 SCAR=6.9
Pod documented
89 38 my ($class, $str, %options) = @_;
90 38 ▶ unless (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str) 1 37 38
91 1 Carp::croak("Usage: $class->new_from_string(\$str, %options)");
92 }
93 37 require Text::ParseWords;
94 37 my $args = [Text::ParseWords::shellwords($str)];
95 37 local *ARGV = $args;
96 37 return $class->new(%options);
97 }
98  
99 31 ▶sub args { $_[0]->{_argv} }
Subroutine args: called CC=1 SCAR=0.0
Pod documented
100 3 ▶sub error { $_[0]->{error}||'' }
Subroutine error: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"error"} or "" 3 0 3
101 140 ▶sub command { $_[0]->{command} }
Subroutine command: called CC=1 SCAR=0.0
Pod documented
102 38 ▶sub commands { $_[0]->{commands} }
Subroutine commands: called CC=1 SCAR=0.0
Pod documented
103 54 ▶sub status { $_[0]->{ret} }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
104 0 ▶sub is_success { $_[0]->{ret} }
Subroutine is_success: not called CC=1 SCAR=6.9
Pod documented
105 0 ▶sub pod2usage { Carp::carp('Not implemented') }
Subroutine pod2usage: not called CC=1 SCAR=6.9
Pod documented
106  
107 51 ▶sub opts {
Subroutine opts: called CC=4 SCAR=13.9
Pod documented
108 51 my($self) = @_;
109 51 my $opt = $self->{opt};
110 51 ▶ if ($self->{usage} && ($opt->{help} || $self->status == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"usage"} and $opt->{"help"} || $self->status == 0) 5 46 51
Condition83%5/6
$self->{"usage"} and $opt->{"help"} || $self->status == 0
!ll&&!rl&&r
0465
$opt->{"help"} or $self->status == 0
l!l&&r!l&&!r
2346
MC/DC66%2/3
$self->{"usage"} and $opt->{"help"} || $self->status == 0
$self->{"usage"} $opt->{"help"} $self->status == 0
Truth table75%3/4
A and B or C
A= $self->{"usage"}B= $opt->{"help"}C= $self->status == 0
ABCresult
0XX0
1000
11X1
1011
111 # display usage message & exit
112 5 print $self->usage;
113 5 exit !$self->status;
114 }
115 51 return $opt;
116 }
117  
118 37 ▶sub usage {
Subroutine usage: called CC=34 SCAR=35.3
Pod documented
119 37 my($self, @targets) = @_;
120 37 my $usage = '';
121 37 my(@help, @commands);
122  
123 37 ▶ if ((defined $self->command && $self->command eq 'help') || @targets) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->command and $self->command eq "help" or @targets) 9 28 37
Condition100%6/6
defined $self->command and $self->command eq "help" or @targets
l!l&&r!l&&!r
7228
defined $self->command and $self->command eq "help"
!ll&&!rl&&r
2287
MC/DC100%3/3
defined $self->command and $self->command eq "help" or @targets
defined $self->command $self->command eq "help" @targets
Truth table80%4/5
A and B or C
A= defined $self->commandB= $self->command eq "help"C= @targets
ABCresult
11X1
0X11
1011
0X00
1000
124 9 delete $self->{command};
125 9 ▶ @targets = @{$self->{_argv}} unless @targets;
Branch: 1 branch
BranchTrueFalseTotal
unless @targets 7 2 9
126 9 ▶ for (my $i = 0; $i < @targets; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @targets) 8 9 17
127 8 my $target = $targets[$i];
128 8 ▶ last unless defined $target;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 8 8
129 8 ▶ unless (ref $self->{_struct}{$target} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_struct'}{$target} eq 'HASH') { } 8 0 8
130 0 $self->{error} = "Unknown command: $target";
131 0 last;
132 }
133 else {
134 8 $self->{command} = $target;
135 8 push @{$self->{commands}}, $target;
136 8 $self->_init_struct($self->{_struct}{$target}{options});
137 8 $self->_extends_usage($self->{_struct}{$target});
138  
139 8 ▶ if (ref $self->{_struct}{$target}{command_struct} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'_struct'}{$target}{'command_struct'} eq 'HASH') { } 3 5 8
140 3 $self->{_struct} = $self->{_struct}{$target}{command_struct};
141 }
142 else {
143 5 $self->{summary} = {};
144 }
145 }
146 }
147 }
148  
149 my($name, $version, $cmd, $struct, $args, $summary, $error, $other_usage) = map
150 37 ▶ $self->{$_} || '', qw/name version cmd struct args summary error other_usage/;
Condition100%2/2
$self->{$_} || ''
l!l
133163
MC/DC100%1/1
$self->{$_} || ''
$self->{$_}
Truth table100%2/2
A
A= $self->{$_}
Aresult
11
00
151  
152 37 ▶ $usage .= "$error\n" if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 7 30 37
153  
154 37 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) 2 35 37
155 2 $usage .= $name;
156 2 ▶ $usage .= " v$version" if $version;
Branch: 1 branch
BranchTrueFalseTotal
if $version 1 1 2
157 2 $usage .= "\n";
158 }
159  
160 37 ▶ if ($self->command && $self->command ne 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->command and $self->command ne 'help') { } 14 23 37
Condition66%2/3
$self->command and $self->command ne "help"
!ll&&!rl&&r
23014
MC/DC50%1/2
$self->command and $self->command ne "help"
$self->command $self->command ne "help"
Truth table66%2/3
A and B
A= $self->commandB= $self->command ne "help"
ABresult
0X0
100
111
161 14 ▶ my $sub_command = join q{ }, @{$self->commands} ? @{$self->commands} : $self->command;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->commands;} ? : 14 0 14
162 14 $usage .= "usage: $cmd $sub_command [options]";
163 }
164 else {
165 23 $usage .= "usage: $cmd [options]";
166 23 ▶ $usage .= ' COMMAND' if keys %$summary;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$summary 6 17 23
167 }
168 37 ▶ $usage .= ($args ? " $args" : '') . "\n\n";
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 7 30 37
169  
170 37 for my $o (@$struct) {
171 62 my ($name_spec, $desc, $arg_spec, $dist, $opts) = @$o;
172 62 ▶ $desc = '' unless defined $desc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $desc 3 59 62
173 62 my @onames = $self->_option_names($name_spec);
174 my $optname = join
175 62 ▶ (', ', map { (length($_) > 1 ? '--' : '-').$_ } @onames);
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 62 59 121
176 62 ▶ $optname = ' '.$optname unless length($onames[0]) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $onames[0] == 1 3 59 62
177 62 my $info = do {
178 62 local $Data::Dumper::Indent = 0;
179 62 local $Data::Dumper::Terse = 1;
180 62 my $info = [];
181 62 ▶ push @$info, $self->_opt_spec2name($arg_spec) || $arg_spec || '';
Condition100%5/5
$self->_opt_spec2name($arg_spec) || $arg_spec || ''
l!l
1250
$self->_opt_spec2name($arg_spec) or $arg_spec
l!l&&r!l&&!r
9350
MC/DC100%2/2
$self->_opt_spec2name($arg_spec) || $arg_spec || ''
$self->_opt_spec2name($arg_spec) $arg_spec
Truth table100%3/3
A or B
A= $self->_opt_spec2name($arg_spec)B= $arg_spec
ABresult
1X1
011
000
182 62 ▶ push @$info, $opts->{required} ? "(required)" : '';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'required'} ? : 6 56 62
183 62 ▶ push @$info, defined $opts->{default} ? "(default: ".Data::Dumper::Dumper($opts->{default}).")" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'default'} ? : 1 61 62
184 62 $info;
185 };
186 62 push @help, [ $optname, $info, ucfirst($desc) ];
187 }
188  
189 37 ▶ if (@help) {
Branch: 1 branch
BranchTrueFalseTotal
if (@help) 37 0 37
190 37 require Text::Table;
191 37 my $sep = \' ';
192 37 $usage .= "options:\n";
193 37 $usage .= Text::Table->new($sep, '', $sep, '', $sep, '')->load($self->_format_info(@help))->stringify."\n";
194 }
195  
196 37 ▶ if (defined $other_usage && length $other_usage > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $other_usage and length $other_usage > 0) 5 32 37
Condition66%2/3
defined $other_usage and length $other_usage > 0
!ll&&!rl&&r
0325
MC/DC50%1/2
defined $other_usage and length $other_usage > 0
defined $other_usage length $other_usage > 0
Truth table66%2/3
A and B
A= defined $other_usageB= length $other_usage > 0
ABresult
0X0
100
111
197 5 $other_usage =~ s/\n$//ms;
198 5 $usage .= "$other_usage\n\n";
199 }
200  
201 37 ▶ if (!$self->command || $self->{has_sub_command}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->command or $self->{"has_sub_command"}) 26 11 37
Condition100%3/3
not $self->command or $self->{"has_sub_command"}
l!l&&r!l&&!r
23311
MC/DC100%2/2
not $self->command or $self->{"has_sub_command"}
not $self->command $self->{"has_sub_command"}
Truth table100%3/3
not(A) or B
A= not $self->commandB= $self->{"has_sub_command"}
ABresult
1X1
011
000
202 26 for my $command (sort keys %$summary) {
203 8 push @commands, [ $command, ucfirst $summary->{$command} ];
204 }
205  
206 26 ▶ if (@commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (@commands) 8 18 26
207 8 require Text::Table;
208 8 my $sep = \' ';
209 8 $usage .= "Implemented commands are:\n";
210 8 $usage .= Text::Table->new($sep, '', $sep, '')->load(@commands)->stringify."\n";
211 8 my $help_command = "$cmd help COMMAND";
212 8 ▶ if (@{$self->commands}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->commands;}) 2 6 8
213 2 my $sub_commands = join q{ }, @{$self->commands};
214 2 $help_command = "$cmd $sub_commands COMMAND --help";
215 }
216 8 $usage .= "See '$help_command' for more information on a specific command.\n\n";
217 }
218 }
219  
220 37 return $usage;
221 }
222  
223 0 ▶sub show_usage {
Subroutine show_usage: not called CC=1 SCAR=6.9
Pod documented
224 0 my $self = shift;
225 0 print $self->usage(@_);
226 0 exit !$self->status;
227 }
228  
229 30 ▶sub completion {
Subroutine completion: called CC=3 SCAR=11.0
Pod documented
230 30 my($self, $shell) = @_;
231 30 ▶ $shell ||= 'bash';
Condition50%1/2
$shell ||= "bash"
l!l
300
MC/DC0%0/1
$shell ||= "bash"
$shell
Truth table50%1/2
A
A= $shell
Aresult
11
00
232  
233 30 ▶ if ($shell eq 'bash') {
Branch: 1 branch
BranchTrueFalseTotal
if ($shell eq 'bash') { } 29 1 30
234 29 return $self->_completion_bash;
235 } else {
236 1 Carp::carp("Not implemented: completion for $shell");
237 1 return "";
238 }
239 }
240  
241 0 ▶sub show_completion {
Subroutine show_completion: not called CC=1 SCAR=6.9
Pod documented
242 0 my $self = shift;
243 0 print $self->completion(@_);
244 0 exit !$self->status;
245 }
246  
247 29 ▶sub _completion_bash {
Subroutine _completion_bash: called CC=10 SCAR=23.0
248 29 my $self = shift;
249 29 my $comp = '';
250  
251 29 ▶ my $prog = $self->{name} || substr($0, rindex($0, '/')+1);
Condition66%2/3
$self->{'name'} || substr($0, rindex($0, '/') + 1)
l!l&&r!l&&!r
2270
MC/DC0%0/2
$self->{'name'} || substr($0, rindex($0, '/') + 1)
$self->{'name'} substr($0, rindex($0, '/') + 1)
Truth table66%2/3
A || B
A= $self->{'name'}B= substr($0, rindex($0, '/') + 1)
ABresult
1X1
011
000
252 29 my $fname = $prog;
253 29 $fname =~ s/[.-]/_/g;
254  
255 29 my @global_opts;
256 my @commands;
257 29 my $case = {
258 word => '"$cmd"',
259 cases => [],
260 };
261  
262 29 @global_opts = $self->_options2optarg($self->{struct});
263  
264 29 for my $cmd (sort keys %{ $self->{_struct} }) {
265 30 my $s = $self->{_struct}{$cmd};
266  
267 30 my @opts = $self->_options2optarg($s->{options});
268 30 my @commands2;
269  
270 30 ▶ if (ref $s->{command_struct} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->{"command_struct"} eq "HASH") 5 25 30
271 5 for my $cmd (sort keys %{ $s->{command_struct} }) {
272 6 my $s = $s->{command_struct}{$cmd};
273 6 my @opts = $self->_options2optarg($s->{options});
274  
275 6 push @commands2, {
276 cmd => $cmd,
277 opts => \@opts,
278 };
279 }
280 }
281  
282 push @commands, {
283 cmd => $cmd,
284 opts => \@opts,
285 subcmd => \@commands2,
286 30 ▶ args => ($s->{args} || ''),
Condition100%2/2
$s->{'args'} || ''
l!l
1614
MC/DC100%1/1
$s->{'args'} || ''
$s->{'args'}
Truth table100%2/2
A
A= $s->{'args'}
Aresult
11
00
287 };
288 }
289  
290 29 $comp .= "_$fname() {\n";
291 29 $comp .= <<'EOC';
292 COMPREPLY=()
293 local cur=${COMP_WORDS[COMP_CWORD]}
294 local prev=${COMP_WORDS[COMP_CWORD-1]}
295 local cmd=()
296 for ((i=1; i<COMP_CWORD; i++)); do
297 # skip global opts and type to find cmd
298 if [[ "${COMP_WORDS[$i]}" != -* && "${COMP_WORDS[$i]}" != [A-Z]* ]]; then
299 cmd[${#cmd[@]}]=${COMP_WORDS[$i]}
300 fi
301 done
302  
303 EOC
304  
305 $comp .= sprintf qq{ local global_opts="%s"\n},
306 29 join(" ", map { @{$_->{opt}} } @global_opts);
307 $comp .= sprintf qq{ local cmds="%s"\n},
308 29 join(" ", map { $_->{cmd} } @commands);
309 29 $comp .= "\n";
310  
311 ### sub commands
312 29 for my $command (@commands) {
313  
314 my $case_prev = {
315 word => '"$prev"',
316 cases => [
317 30 _opts2casecmd(@{ $command->{opts} }),
318 {
319 pat => '*',
320 cmd => ['COMPREPLY=($(compgen -W "'._gen_wordlist($command).'" -- "$cur"))'],
321 },
322 ],
323 };
324  
325 30 ▶ if (scalar(@{ $command->{subcmd} }) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$command->{'subcmd'};} > 0) { } 5 25 30
326 5 my @cases;
327  
328 5 for my $subcommand (@{ $command->{subcmd} }) {
329 6 ▶ next if (scalar(@{ $subcommand->{opts} }) <= 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$subcommand->{"opts"};} <= 0 1 5 6
330 push @cases, {
331 pat => $subcommand->{cmd},
332 cmd => [{
333 word => '"$prev"',
334 cases => [
335 5 _opts2casecmd(@{ $subcommand->{opts} }),
336 {
337 pat => '*',
338 cmd => ['COMPREPLY=($(compgen -W "'._gen_wordlist($subcommand).'" -- "$cur"))'],
339 },
340 ],
341 }],
342 };
343 }
344  
345 5 push @cases, {
346 pat => '*',
347 cmd => [ $case_prev ],
348 };
349  
350 5 push @{ $case->{cases} }, {
351 pat => $command->{cmd},
352 5 cmd => [{
353 word => '"${cmd[1]}"',
354 cases => [@cases],
355 }],
356 };
357 } else {
358 25 push @{ $case->{cases} }, {
359 pat => $command->{cmd},
360 25 cmd => [ $case_prev ],
361 };
362 }
363 }
364  
365 ### global opts
366 29 push @{ $case->{cases} }, {
367 pat => '*',
368 cmd => [{
369 word => '"$prev"',
370 cases => [
371 _opts2casecmd(@global_opts),
372 {
373 pat => '*',
374 cmd => ['COMPREPLY=($(compgen -W "$global_opts $cmds" -- "$cur"))'],
375 },
376 ],
377 }],
378 };
379  
380 29 my @c = _generate_case_command($case);
381 29 $comp .= join("\n", map {" ".$_} @c)."\n";
382  
383 29 $comp .= <<"EOC";
384 }
385  
386 complete -F _$fname $prog
387 EOC
388 29 return $comp;
389 }
390  
391 # take following hashref and generate case command string
392 # +{
393 # word => WORD, # case WORD in
394 # cases => [
395 # {
396 # pat => PATTERN, # PATTERN)
397 # cmd => ['cmd1', 'cmd2', ...], # COMMANDS;;
398 # },
399 # {
400 # pat => PATTERN, # PATTERN)
401 # cmd => [ # nested case command
402 # {
403 # word => WORD,
404 # cases => [ ... ],
405 # },
406 # ],
407 # },
408 # ],
409 # }
410 98 ▶sub _generate_case_command {
Subroutine _generate_case_command: called CC=4 SCAR=13.9
411 98 my $case = shift;
412 98 my @line;
413  
414 98 push @line, "case $case->{word} in";
415 98 for my $c (@{ $case->{cases} }) {
416 139 push @line, " $c->{pat})";
417 139 for my $cmd (@{ $c->{cmd} }, ';;') {
418 278 ▶ if (ref $cmd eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cmd eq 'HASH') { } 69 209 278
419 69 push @line, map {" ".$_} _generate_case_command->($cmd);
420 } else {
421 209 push @line, " ".$cmd;
422 }
423 }
424 }
425 98 push @line, "esac";
426  
427 98 return @line;
428 }
429  
430 65 ▶sub _options2optarg {
Subroutine _options2optarg: called CC=6 SCAR=18.0
431 65 my($self, $opts) = @_;
432 65 my @optarg;
433  
434 65 for my $o (@{ $opts }) {
435 62 my ($name_spec, $desc, $arg_spec, $dist, $opts) = @$o;
436 62 ▶ my @onames = map { (length($_) > 1 ? '--' : '-').$_ } $self->_option_names($name_spec);
Branch: 1 branch
BranchTrueFalseTotal
length $_ > 1 ? : 62 62 124
437 62 ▶ my $arg = $self->_opt_spec2name($arg_spec) || $arg_spec || '';
Condition80%4/5
$self->_opt_spec2name($arg_spec) || $arg_spec || ''
l!l
656
$self->_opt_spec2name($arg_spec) or $arg_spec
l!l&&r!l&&!r
6056
MC/DC50%1/2
$self->_opt_spec2name($arg_spec) || $arg_spec || ''
$self->_opt_spec2name($arg_spec) $arg_spec
Truth table66%2/3
A or B
A= $self->_opt_spec2name($arg_spec)B= $arg_spec
ABresult
1X1
011
000
438 62 ▶ $arg = '' if $arg eq 'Incr';
Branch: 1 branch
BranchTrueFalseTotal
if $arg eq "Incr" 0 62 62
439 62 push @optarg, {
440 opt => \@onames,
441 arg => $arg,
442 };
443 }
444  
445 65 return @optarg;
446 }
447  
448 64 ▶sub _opts2casecmd {
Subroutine _opts2casecmd: called CC=2 SCAR=6.9
449 64 my @cases;
450 64 for my $o (grep { $_->{arg} } @_) {
451 push @cases, {
452 6 pat => join("|", @{ $o->{opt} }),
453 6 cmd => ['COMPREPLY=($(compgen -W "'.$o->{arg}.'" -- "$cur"))'],
454 };
455 }
456  
457 64 return @cases;
458 }
459  
460 35 ▶sub _gen_wordlist {
Subroutine _gen_wordlist: called CC=2 SCAR=6.9
461 35 my $command = shift;
462  
463 return join(" ",
464 '-h', '--help',
465 12 ▶ (map { @{$_->{opt}} } @{ $command->{opts} }),
Condition100%2/2
$command->{'args'} || ''
l!l
1619
MC/DC100%1/1
$command->{'args'} || ''
$command->{'args'}
Truth table100%2/2
A
A= $command->{'args'}
Aresult
11
00
466 ($command->{args}||''),
467 35 (map { $_->{cmd} } @{ $command->{subcmd} }),
468 );
469 }
470  
471 262 ▶sub _opt_spec2name {
Subroutine _opt_spec2name: called CC=12 SCAR=24.9
472 262 my ($self, $spec) = @_;
473 262 my $name = '';
474 262 ▶ return $name unless defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 106 156 262
475 156 my ($type, $dest) = $spec =~ /^[=:]?([!+isof])([@%])?/;
476 156 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 110 46 156
477 110 ▶ $name =
Branch: 6 branches
BranchTrueFalseTotal
$type eq '!' ? : 52 58 110
$type eq '+' ? : 1 57 58
$type eq 's' ? : 48 9 57
$type eq 'i' ? : 7 2 9
$type eq 'f' ? : 1 1 2
$type eq 'o' ? : 1 0 1
478 $type eq '!' ? 'Bool' :
479 $type eq '+' ? 'Incr' :
480 $type eq 's' ? 'Str' :
481 $type eq 'i' ? 'Int' :
482 $type eq 'f' ? 'Num' :
483 $type eq 'o' ? 'ExNum' : '';
484 }
485 156 ▶ if ($dest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest) 11 145 156
486 11 ▶ $name = $dest eq '@' ? "Array[$name]" : $dest eq '%' ? "Hash[$name]" : $name;
Branch: 2 branches
BranchTrueFalseTotal
$dest eq '@' ? : 4 7 11
$dest eq '%' ? : 7 0 7
487 }
488 156 return $name;
489 }
490  
491 37 ▶sub _format_info {
Subroutine _format_info: called CC=5 SCAR=16.1
492 37 my ($self, @help) = @_;
493  
494 37 my $type_max = 0;
495 37 my $required_max = 0;
496 37 my $default_max = 0;
497 37 for my $row (@help) {
498 62 my ($type, $required, $default) = @{$row->[1]};
499 62 $type_max = max $type_max, length($type);
500 62 $required_max = max $required_max, length($required);
501 62 $default_max = max $default_max, length($default);
502 }
503  
504 37 for my $row (@help) {
505 62 my ($type, $required, $default) = @{$row->[1]};
506 62 my $parts = [];
507 62 for my $stuff ([$type_max, $type], [$required_max, $required], [$default_max, $default]) {
508 186 ▶ push @$parts, sprintf '%-*s', @$stuff if $stuff->[0] > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $stuff->[0] > 0 38 148 186
509 }
510 62 $row->[1] = join ' ', @$parts;
511 }
512  
513 37 return @help;
514 }
515  
516 95 ▶sub _parse_command_struct {
Subroutine _parse_command_struct: called CC=16 SCAR=27.8
517 95 my ($self, $command_struct) = @_;
518 95 ▶ $command_struct ||= {};
Condition50%1/2
$command_struct ||= {}
l!l
950
MC/DC0%0/1
$command_struct ||= {}
$command_struct
Truth table50%1/2
A
A= $command_struct
Aresult
11
00
519  
520 95 my $command_map = { map { $_ => 1 } keys %$command_struct };
521 95 my $command = shift @ARGV;
522 95 ▶ unless (defined $command) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $command) 21 74 95
523 21 $self->{ret} = $self->_check_requires;
524 21 return $self;
525 }
526  
527 74 ▶ unless ($command_map->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command_map->{"help"}) 74 0 74
528 74 $command_map->{help} = 1;
529 $command_struct->{help} = {
530 74 args => '[COMMAND]',
531 desc => 'show help message',
532 };
533 }
534  
535 74 ▶ unless (exists $command_map->{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $command_map->{$command}) 4 70 74
536 4 $self->{error} = "Unknown command: $command";
537 4 $self->{ret} = 0;
538 4 return $self;
539 }
540  
541 70 ▶ $self->{command} ||= $command;
Condition66%2/3
$self->{"command"} ||= $command
l!l&&r!l&&!r
7630
MC/DC0%0/2
$self->{"command"} ||= $command
$self->{"command"} $command
Truth table66%2/3
A ||= B
A= $self->{"command"}B= $command
ABresult
1X1
011
000
542  
543 70 ▶ if ($command eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "help") 14 56 70
544 14 $self->{ret} = 0;
545 14 delete $self->{error};
546 14 ▶ if (defined $ARGV[0] && exists $command_struct->{$ARGV[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ARGV[0] and exists $$command_struct{$ARGV[0]}) 8 6 14
Condition66%2/3
defined $ARGV[0] and exists $$command_struct{$ARGV[0]}
!ll&&!rl&&r
608
MC/DC50%1/2
defined $ARGV[0] and exists $$command_struct{$ARGV[0]}
defined $ARGV[0] exists $$command_struct{$ARGV[0]}
Truth table66%2/3
A and B
A= defined $ARGV[0]B= exists $$command_struct{$ARGV[0]}
ABresult
0X0
100
111
547 8 my $nested_struct = $command_struct->{$ARGV[0]}{command_struct};
548 8 ▶ $self->_init_nested_struct($nested_struct) if $nested_struct;
Branch: 1 branch
BranchTrueFalseTotal
if $nested_struct 5 3 8
549 }
550 14 return $self;
551 }
552  
553 56 ▶ push @{$self->{commands} ||= []}, $command;
Condition100%2/2
$self->{"commands"} ||= []
l!l
515
MC/DC100%1/1
$self->{"commands"} ||= []
$self->{"commands"}
Truth table100%2/2
A
A= $self->{"commands"}
Aresult
11
00
554 56 $self->_init_struct($command_struct->{$command}{options});
555 56 $self->_extends_usage($command_struct->{$command});
556 56 ▶ my $opthash = $self->_parse_struct || return $self;
Condition50%1/2
$self->_parse_struct || (return $self)
l!l
560
MC/DC0%0/1
$self->_parse_struct || (return $self)
$self->_parse_struct
Truth table50%1/2
A
A= $self->_parse_struct
Aresult
11
00
557  
558 56 ▶ if (my $nested_struct = $command_struct->{$command}{command_struct}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nested_struct = $command_struct->{$command}{'command_struct'}) { } 10 46 56
559 10 $self->_init_nested_struct($nested_struct);
560  
561 10 my @opts = $self->_parse_argv($nested_struct);
562 10 $self->{ret} = $self->_parse_option(\@opts, $opthash);
563 10 unshift @ARGV, @opts;
564 10 $self->_check_requires;
565 10 ▶ if ($self->_want_help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_want_help) 1 9 10
566 1 delete $self->{error};
567 1 $self->{ret} = 0;
568 }
569 10 ▶ return $self unless $self->{ret};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ret"} 1 9 10
570 9 $self->_parse_command_struct($nested_struct);
571 }
572 else {
573 46 $self->{ret} = $self->_parse_option(\@ARGV, $opthash);
574 46 $self->_check_requires;
575 46 $self->{has_sub_command} = 0;
576 46 ▶ if ($self->_want_help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_want_help) 2 44 46
577 2 delete $self->{error};
578 2 $self->{ret} = 0;
579 }
580 }
581  
582 55 return $self;
583 }
584  
585 226 ▶sub _want_help {
Subroutine _want_help: called CC=3 SCAR=11.0
586 226 ▶ exists $_[0]->{opt}{help} && $_[0]->{opt}{help} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'opt'}{'help'} && $_[0]{'opt'}{'help'} ? : 10 216 226
Condition100%3/3
exists $_[0]{"opt"}{"help"} and $_[0]{"opt"}{"help"}
!ll&&!rl&&r
4017610
MC/DC100%2/2
exists $_[0]{"opt"}{"help"} and $_[0]{"opt"}{"help"}
exists $_[0]{"opt"}{"help"} $_[0]{"opt"}{"help"}
Truth table100%3/3
A and B
A= exists $_[0]{"opt"}{"help"}B= $_[0]{"opt"}{"help"}
ABresult
0X0
100
111
587 }
588  
589 15 ▶sub _init_nested_struct {
Subroutine _init_nested_struct: called CC=1 SCAR=0.0
590 15 my ($self, $nested_struct) = @_;
591 15 $self->{summary} = {}; # reset
592 15 $self->_init_summary($nested_struct);
593 15 $self->{has_sub_command} = 1;
594 }
595  
596 255 ▶sub _parse_option {
Subroutine _parse_option: called CC=2 SCAR=6.9
597 255 my ($self, $argv, $opthash) = @_;
598 16 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
599 16 $self->{error} = join '', @_;
600 16 chomp $self->{error};
601 255 };
602 255 ▶ my $ret = GetOptionsFromArray($argv, %$opthash) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&GetOptionsFromArray($argv, %$opthash) ? : 239 16 255
603  
604 255 $self->{parsed_opthash} = $opthash;
605  
606 255 return $ret;
607 }
608  
609 94 ▶sub _parse_argv {
Subroutine _parse_argv: called CC=4 SCAR=13.9
610 94 my ($self, $struct) = @_;
611 94 ▶ $struct ||= $self->{_struct};
Condition66%2/3
$struct ||= $self->{"_struct"}
l!l&&r!l&&!r
10840
MC/DC0%0/2
$struct ||= $self->{"_struct"}
$struct $self->{"_struct"}
Truth table66%2/3
A ||= B
A= $structB= $self->{"_struct"}
ABresult
1X1
011
000
612  
613 94 my @opts;
614 94 ▶ while (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 90 32 122
615 90 my $argv = shift @ARGV;
616 90 push @opts, $argv;
617 90 ▶ last if exists $struct->{$argv};
Branch: 1 branch
BranchTrueFalseTotal
if exists $struct->{$argv} 62 28 90
618 }
619 94 return @opts;
620 }
621  
622 272 ▶sub _parse_struct {
Subroutine _parse_struct: called CC=30 SCAR=34.2
623 272 my ($self) = @_;
624 272 my $struct = $self->{struct};
625  
626 272 my $opthash = {};
627 272 my $default_opthash = {};
628 272 my $default_args = [];
629 272 for my $s (@$struct) {
630 414 my($m, $descr, $spec, $ref, $opts) = @$s;
631 414 my @onames = $self->_option_names($m);
632 414 my($longname) = grep length($_) > 1, @onames;
633 414 my ($type, $cb) = $self->_compile_spec($spec);
634 414 ▶ my $o = join('|', @onames).($type||'');
Condition100%2/2
$type || ''
l!l
125289
MC/DC100%1/1
$type || ''
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
635 414 ▶ my $dest = $longname ? $longname : $onames[0];
Branch: 1 branch
BranchTrueFalseTotal
$longname ? : 414 0 414
636 414 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
58356
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
637 414 my $destination;
638 414 ▶ if (ref $cb eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 27 387 414
639 27 ▶ my $t =
Branch: 2 branches
BranchTrueFalseTotal
substr($type, -1, 1) eq '@' ? : 9 18 27
substr($type, -1, 1) eq '%' ? : 9 9 18
640 substr($type, -1, 1) eq '@' ? 'Array' :
641 substr($type, -1, 1) eq '%' ? 'Hash' : '';
642 27 ▶ if (ref $ref eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ref eq 'CODE') { } 0 27 27
elsif (ref $ref) { } 20 7 27
643 0 ▶ $destination = sub { $ref->($_[0], $cb->($_[1])) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
644 }
645 elsif (ref $ref) {
646 20 ▶ if (ref $ref eq 'SCALAR' || ref $ref eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'SCALAR' or ref $ref eq 'REF') { } 6 14 20
elsif (ref $ref eq 'ARRAY') { } 6 8 14
elsif (ref $ref eq 'HASH') { } 8 0 8
Condition66%2/3
ref $ref eq "SCALAR" or ref $ref eq "REF"
l!l&&r!l&&!r
6014
MC/DC50%1/2
ref $ref eq "SCALAR" or ref $ref eq "REF"
ref $ref eq "SCALAR" ref $ref eq "REF"
Truth table66%2/3
A or B
A= ref $ref eq "SCALAR"B= ref $ref eq "REF"
ABresult
1X1
011
000
647 6 ▶ $$ref = $t eq 'Array' ? [] : $t eq 'Hash' ? {} : undef;
Branch: 2 branches
BranchTrueFalseTotal
$t eq 'Array' ? : 2 4 6
$t eq 'Hash' ? : 2 2 4
648 }
649 elsif (ref $ref eq 'ARRAY') {
650 6 @$ref = ();
651 }
652 elsif (ref $ref eq 'HASH') {
653 8 %$ref = ();
654 }
655 20 ▶ $destination = sub {
Subroutine __ANON__: called CC=17 SCAR=28.6
656 20 ▶ if ($t eq 'Array') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'Array') { } 7 13 20
elsif ($t eq 'Hash') { } 6 7 13
657 7 ▶ if (ref $ref eq 'SCALAR' || ref $ref eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'SCALAR' or ref $ref eq 'REF') { } 2 5 7
elsif (ref $ref eq 'ARRAY') { } 2 3 5
elsif (ref $ref eq 'HASH') { } 3 0 3
Condition66%2/3
ref $ref eq "SCALAR" or ref $ref eq "REF"
l!l&&r!l&&!r
025
MC/DC50%1/2
ref $ref eq "SCALAR" or ref $ref eq "REF"
ref $ref eq "SCALAR" ref $ref eq "REF"
Truth table66%2/3
A or B
A= ref $ref eq "SCALAR"B= ref $ref eq "REF"
ABresult
1X1
011
000
658 2 push @{$$ref}, scalar $cb->($_[1]);
659 }
660 elsif (ref $ref eq 'ARRAY') {
661 2 push @$ref, scalar $cb->($_[1]);
662 }
663 elsif (ref $ref eq 'HASH') {
664 3 my @kv = split '=', $_[1], 2;
665 3 ▶ die qq(Option $_[0], key "$_[1]", requires a value\n)
Branch: 1 branch
BranchTrueFalseTotal
unless @kv == 2 1 2 3
666 unless @kv == 2;
667 2 $ref->{$kv[0]} = scalar $cb->($kv[1]);
668 }
669 }
670 elsif ($t eq 'Hash') {
671 6 ▶ if (ref $ref eq 'SCALAR' || ref $ref eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'SCALAR' or ref $ref eq 'REF') { } 2 4 6
elsif (ref $ref eq 'ARRAY') { } 2 2 4
elsif (ref $ref eq 'HASH') { } 2 0 2
Condition66%2/3
ref $ref eq "SCALAR" or ref $ref eq "REF"
l!l&&r!l&&!r
024
MC/DC50%1/2
ref $ref eq "SCALAR" or ref $ref eq "REF"
ref $ref eq "SCALAR" ref $ref eq "REF"
Truth table66%2/3
A or B
A= ref $ref eq "SCALAR"B= ref $ref eq "REF"
ABresult
1X1
011
000
672 2 $$ref->{$_[1]} = scalar $cb->($_[2]);
673 }
674 elsif (ref $ref eq 'ARRAY') {
675 # XXX but Getopt::Long is $ret = join '=', $_[1], $_[2];
676 2 push @$ref, $_[1], scalar $cb->($_[2]);
677 }
678 elsif (ref $ref eq 'HASH') {
679 2 $ref->{$_[1]} = scalar $cb->($_[2]);
680 }
681 }
682 else {
683 7 ▶ if (ref $ref eq 'SCALAR' || ref $ref eq 'REF') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $ref eq 'SCALAR' or ref $ref eq 'REF') { } 2 5 7
elsif (ref $ref eq 'ARRAY') { } 2 3 5
elsif (ref $ref eq 'HASH') { } 3 0 3
Condition66%2/3
ref $ref eq "SCALAR" or ref $ref eq "REF"
l!l&&r!l&&!r
205
MC/DC50%1/2
ref $ref eq "SCALAR" or ref $ref eq "REF"
ref $ref eq "SCALAR" ref $ref eq "REF"
Truth table66%2/3
A or B
A= ref $ref eq "SCALAR"B= ref $ref eq "REF"
ABresult
1X1
011
000
684 2 $$ref = $cb->($_[1]);
685 }
686 elsif (ref $ref eq 'ARRAY') {
687 2 @$ref = (scalar $cb->($_[1]));
688 }
689 elsif (ref $ref eq 'HASH') {
690 3 my @kv = split '=', $_[1], 2;
691 3 ▶ die qq(Option $_[0], key "$_[1]", requires a value\n)
Branch: 1 branch
BranchTrueFalseTotal
unless @kv == 2 1 2 3
692 unless @kv == 2;
693 2 %$ref = ($kv[0] => scalar $cb->($kv[1]));
694 }
695 }
696 20 };
697 }
698 else {
699 5 ▶ $destination = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
700 5 ▶ if ($t eq 'Array') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq 'Array') { } 2 3 5
elsif ($t eq 'Hash') { } 2 1 3
701 2 ▶ $self->{opt}{$dest} ||= [];
Condition100%2/2
$self->{"opt"}{$dest} ||= []
l!l
11
MC/DC100%1/1
$self->{"opt"}{$dest} ||= []
$self->{"opt"}{$dest}
Truth table100%2/2
A
A= $self->{"opt"}{$dest}
Aresult
11
00
702 2 push @{$self->{opt}{$dest}}, scalar $cb->($_[1]);
703 }
704 elsif ($t eq 'Hash') {
705 2 ▶ $self->{opt}{$dest} ||= {};
Condition100%2/2
$self->{"opt"}{$dest} ||= {}
l!l
11
MC/DC100%1/1
$self->{"opt"}{$dest} ||= {}
$self->{"opt"}{$dest}
Truth table100%2/2
A
A= $self->{"opt"}{$dest}
Aresult
11
00
706 2 $self->{opt}{$dest}{$_[1]} = $cb->($_[2]);
707 }
708 else {
709 1 $self->{opt}{$dest} = $cb->($_[1]);
710 }
711 7 };
712 }
713 }
714 else {
715 387 ▶ $destination = ref $ref ? $ref : \$self->{opt}{$dest};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref ? : 26 361 387
716 }
717 414 ▶ if (exists $opts->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"default"}) 26 388 414
718 26 my $value = $opts->{default};
719 26 ▶ if (ref $value eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 3 23 26
elsif (ref $value eq 'HASH') { } 6 17 23
elsif (not ref $value) { } 12 5 17
720 push @$default_args, map {
721 4 ("--$dest", $_)
722 3 } grep { defined $_ } @$value;
723 }
724 elsif (ref $value eq 'HASH') {
725 push @$default_args, map {
726 6 (my $key = $_) =~ s/=/\\=/g;
727 6 ("--$dest" => "$key=$value->{$_}")
728 } grep {
729 6 defined $value->{$_}
730 } keys %$value;
731 }
732 elsif (not ref $value) {
733 12 ▶ if (!$spec || ($TYPE_MAP->{$spec} || $spec) eq '!') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $spec or ($TYPE_MAP->{$spec} || $spec) eq '!') { } 3 9 12
Condition66%4/6
not $spec or ($TYPE_MAP->{$spec} || $spec) eq "!"
l!l&&r!l&&!r
039
$TYPE_MAP->{$spec} || $spec
l!l&&r!l&&!r
1110
MC/DC0%0/3
not $spec or ($TYPE_MAP->{$spec} || $spec) eq "!"
not $spec $TYPE_MAP->{$spec} $spec
Truth table50%2/4
not(A) or B || C
A= not $specB= $TYPE_MAP->{$spec}C= $spec
ABCresult
1XX1
01X1
0011
0000
734 3 ▶ push @$default_args, "--$dest" if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 3 0 3
735 }
736 else {
737 9 ▶ push @$default_args, "--$dest", $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 9 0 9
738 }
739 }
740 else {
741 5 $self->{error} = "Invalid default option for $dest";
742 5 $self->{ret} = 0;
743 }
744 26 $default_opthash->{$o} = $destination;
745 }
746 414 $opthash->{$o} = $destination;
747 414 ▶ $self->{requires}{$dest} = $o if $opts->{required};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"required"} 33 381 414
748 }
749  
750 272 ▶ return if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 5 267 272
751 267 ▶ if (@$default_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$default_args) 19 248 267
752 19 $self->{ret} = $self->_parse_option($default_args, $default_opthash);
753 19 unshift @ARGV, @$default_args;
754 19 ▶ return unless $self->{ret};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ret"} 1 18 19
755 }
756  
757 266 return $opthash;
758 }
759  
760 270 ▶sub _init_struct {
Subroutine _init_struct: called CC=7 SCAR=19.5
761 270 my ($self, $struct) = @_;
762 270 ▶ $self->{struct} = ref $struct eq 'ARRAY' ? $struct : ref $struct eq 'HASH' ? $self->_normalize_struct($struct) : [];
Branch: 2 branches
BranchTrueFalseTotal
ref $struct eq 'ARRAY' ? : 210 60 270
ref $struct eq 'HASH' ? : 41 19 60
763  
764 270 ▶ if (ref $self->{modes} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{"modes"} eq "ARRAY") 4 266 270
765 4 my @modeopt;
766 4 for my $m (@{$self->{modes}}) {
767 8 my($mc) = $m =~ /^(\w)/;
768 8 push @modeopt, [[$mc, $m], qq($m mode)];
769 }
770 4 unshift @$struct, @modeopt;
771 }
772  
773 220 unshift @{$self->{struct}}, [[qw(h help)], qq(this help message)]
774 270 ▶ if $self->{usage} && !$self->_has_option('help');
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"usage"} and not $self->_has_option("help") 220 50 270
Condition100%3/3
$self->{"usage"} and not $self->_has_option("help")
!ll&&!rl&&r
491220
MC/DC100%2/2
$self->{"usage"} and not $self->_has_option("help")
$self->{"usage"} not $self->_has_option("help")
Truth table100%3/3
A and not(B)
A= $self->{"usage"}B= not $self->_has_option("help")
ABresult
0X0
100
111
775 }
776  
777 41 ▶sub _normalize_struct {
Subroutine _normalize_struct: called CC=6 SCAR=18.0
778 41 my ($self, $struct) = @_;
779  
780 41 my $result = [];
781 41 for my $option (keys %$struct) {
782 41 ▶ my $data = $struct->{$option} || {};
Condition50%1/2
$struct->{$option} || {}
l!l
410
MC/DC0%0/1
$struct->{$option} || {}
$struct->{$option}
Truth table50%1/2
A
A= $struct->{$option}
Aresult
11
00
783 41 ▶ $data = ref $data eq 'HASH' ? $data : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' ? : 41 0 41
784 41 my $row = [];
785 push @$row, [
786 $option,
787 3 ▶ ref $data->{alias} eq 'ARRAY' ? @{$data->{alias}} :
Branch: 1 branch
BranchTrueFalseTotal
defined $data->{'alias'} ? : 1 37 38
788 41 ▶ defined $data->{alias} ? $data->{alias} : (),
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{'alias'} eq 'ARRAY' ? : 3 38 41
789 ];
790 41 push @$row, $data->{desc};
791 41 push @$row, $data->{type};
792 41 push @$row, $data->{dest};
793 41 push @$row, $data->{opts};
794 41 push @$result, $row;
795 }
796  
797 41 return $result;
798 }
799  
800 427 ▶sub _compile_spec {
Subroutine _compile_spec: called CC=8 SCAR=20.9
801 427 my ($self, $spec) = @_;
802 427 ▶ return if !defined $spec or $spec eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $spec or $spec eq "" 289 138 427
Condition100%3/3
not defined $spec or $spec eq ""
l!l&&r!l&&!r
2872138
MC/DC100%2/2
not defined $spec or $spec eq ""
not defined $spec $spec eq ""
Truth table100%3/3
not(A) or B
A= not defined $specB= $spec eq ""
ABresult
1X1
011
000
803 138 ▶ return $spec if $self->_opt_spec2name($spec);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_opt_spec2name($spec) 95 43 138
804 43 my ($type, $cb);
805 43 ▶ if ($spec =~ /^(Array|Hash)\[(\w+)\]$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec =~ /^(Array|Hash)\[(\w+)\]$/) { } 20 23 43
elsif ($type = $TYPE_MAP->{$spec}) { } 23 0 23
806 20 ▶ $type = $TYPE_MAP->{$2} || Carp::croak("Can't find type constraint '$2'");
Condition50%1/2
$TYPE_MAP->{$2} || Carp::croak("Can't find type constraint '$2'")
l!l
200
MC/DC0%0/1
$TYPE_MAP->{$2} || Carp::croak("Can't find type constraint '$2'")
$TYPE_MAP->{$2}
Truth table50%1/2
A
A= $TYPE_MAP->{$2}
Aresult
11
00
807 20 ▶ $type .= $1 eq 'Array' ? '@' : '%';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'Array' ? : 10 10 20
808 20 $cb = $TYPE_GEN->{$2};
809 }
810 elsif ($type = $TYPE_MAP->{$spec}) {
811 23 $cb = $TYPE_GEN->{$spec};
812 }
813 else {
814 0 Carp::croak("Can't find type constraint '$spec'");
815 }
816 43 return $type, $cb;
817 }
818  
819 2 ▶sub add_type {
Subroutine add_type: called CC=5 SCAR=20.3
Pod documented
820 2 my ($class, $name, $src_type, $cb) = @_;
821 2 ▶ unless (defined $name && $src_type && ref $cb eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name and $src_type and ref $cb eq "CODE") 0 2 2
Condition33%2/6
defined $name and $src_type and ref $cb eq "CODE"
!ll&&!rl&&r
002
defined $name and $src_type
!ll&&!rl&&r
002
MC/DC0%0/3
defined $name and $src_type and ref $cb eq "CODE"
defined $name $src_type ref $cb eq "CODE"
Truth table25%1/4
A and B and C
A= defined $nameB= $src_typeC= ref $cb eq "CODE"
ABCresult
0XX0
10X0
1100
1111
822 0 Carp::croak("Usage: $class->add_type(\$name, \$src_type, \$cb)");
823 }
824 2 ▶ unless ($TYPE_MAP->{$src_type}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($TYPE_MAP->{$src_type}) 0 2 2
825 0 Carp::croak("$src_type is not defined src type");
826 }
827 2 $TYPE_MAP->{$name} = $TYPE_MAP->{$src_type};
828 2 $TYPE_GEN->{$name} = $cb;
829 }
830  
831 216 ▶sub _init_summary {
Subroutine _init_summary: called CC=4 SCAR=13.9
832 216 my ($self, $command_struct) = @_;
833 216 ▶ if ($command_struct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_struct) { } 95 121 216
834 95 for my $key (keys %$command_struct) {
835 95 ▶ $self->{summary}{$key} = $command_struct->{$key}->{desc} || '';
Condition100%2/2
$command_struct->{$key}{'desc'} || ''
l!l
6629
MC/DC100%1/1
$command_struct->{$key}{'desc'} || ''
$command_struct->{$key}{'desc'}
Truth table100%2/2
A
A= $command_struct->{$key}{'desc'}
Aresult
11
00
836 }
837 }
838 else {
839 121 $self->{summary} = {};
840 }
841 }
842  
843 66 ▶sub _extends_usage {
Subroutine _extends_usage: called CC=3 SCAR=11.0
844 66 my ($self, $command_option) = @_;
845 66 for my $key (qw/args other_usage/) {
846 132 ▶ $self->{$key} = $command_option->{$key} if exists $command_option->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $command_option->{$key} 65 67 132
847 }
848 }
849  
850 260 ▶sub _check_requires {
Subroutine _check_requires: called CC=4 SCAR=13.9
851 260 my ($self) = @_;
852 260 for my $dest (sort keys %{$self->{requires}}) {
853 34 ▶ unless (defined $self->{opt}{$dest}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"opt"}{$dest}) 19 15 34
854 19 ▶ unless (defined ${$self->{parsed_opthash}{$self->{requires}{$dest}}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined ${$self->{"parsed_opthash"}{$self->{"requires"}{$dest}};}) 17 2 19
855 17 $self->{ret} = 0;
856 17 $self->{error} = "`--$dest` option must be specified";
857 17 return 0;
858 }
859 }
860 }
861 243 return 1;
862 }
863  
864 695 ▶sub _option_names {
Subroutine _option_names: called CC=4 SCAR=14.3
865 695 my($self, $m) = @_;
866 my @sorted = sort {
867 695 my ($la, $lb) = (length($a), length($b));
868 600 ▶ return $la <=> $lb if $la < 2 or $lb < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $la < 2 or $lb < 2 600 0 600
Condition66%2/3
$la < 2 or $lb < 2
l!l&&r!l&&!r
59190
MC/DC0%0/2
$la < 2 or $lb < 2
$la < 2 $lb < 2
Truth table66%2/3
A or B
A= $la < 2B= $lb < 2
ABresult
1X1
011
000
869 0 ▶ return 0;
Branch: 1 branch
BranchTrueFalseTotal
ref $m eq 'ARRAY' ? : 694 1 695
870 } ref $m eq 'ARRAY' ? @$m : $m;
871 695 return @sorted;
872 }
873  
874 223 ▶sub _has_option {
Subroutine _has_option: called CC=2 SCAR=6.9
875 223 my($self, $option) = @_;
876 223 ▶ return 1 if grep { $_ eq $option } map { $self->_option_names($_->[0]) } @{$self->{struct}};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $option;} map({$self->_option_names($_->[0]);} @{$$self{"struct"};}) 2 221 223
877 221 return 0;
878 }
879  
880 20 1;
881 __END__
↑ Summary