Base/CLI_Opts.pm

filter: Statementstmt 82.0% 229 / 279 Branchbran 79.8% 107 / 134 Conditioncond 79.1% 38 / 48 MC/DCmcdc 66.6% 18 / 27 Subroutinesub 88.0% 22 / 25 Podpod 10.0% 1 / 10 totaltotal 79.3% 415 / 523 CC 98 SCAR 39.3
CC
98
Cov
81%
cmd_help
272.0
parse_opts_for_cli_opts
29.1
configure_for_cli_opts
27.0
CRAP 50.9
« blib/lib/MOP4Import/Util/FindMethods.pm ↑ Summary blib/lib/MOP4Import/Util/Inspector.pm »
1 package MOP4Import::Base::CLI_Opts;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 use MOP4Import::Base::CLI -as_base
5 # , [extend => FieldSpec => qw/type alias/]
6 , [fields => qw/_cmd __cmd/]
7 7 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use MOP4Import::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 use MOP4Import::Types::Extend
15 7 ▶ FieldSpec => [[fields => qw/type alias command real_name required for_subcmd/]];
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 print STDERR "FieldSpec = ", FieldSpec, "\n" if DEBUG;
18  
19 sub new {
20 3 ▶ my MY $self = fields::new(shift);
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 3 my $pre = {};
22 3 $self->configure_for_cli_opts($pre, @_);
23 1 $self->after_new;
24 1 $self;
25 }
26  
27 sub invoke {
28 2 ▶ my ( $self, $cmd, @args ) = @_;
Subroutine invoke: called CC=2 SCAR=7.2
Pod undocumented
29 2 $self->{_cmd} = $cmd;
30 2 ▶ if ( my $subref = $self->can("cmd_$cmd") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subref = $self->can("cmd_$cmd")) { } 2 0 2
31 2 $self->configure_for_cli_opts({for_subcmd => 1}, @args);
32 2 $subref->($self, @args);
33 }
34 else {
35 0 Carp::croak("Cmd `$cmd` is not implemented.\n");
36 }
37 }
38  
39  
40 7 ▶use MOP4Import::Opts qw/Opts m4i_args m4i_opts/;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 7 ▶use MOP4Import::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 sub import {
44 15 ▶ my ($myPack, @decls) = @_;
Subroutine import: called CC=5 SCAR=16.1
45  
46 15 m4i_log_start() if DEBUG;
47  
48 15 ▶ if ( (grep { $_ eq '-as_base' } @decls) && not (grep { $_ eq 'options' } map { ref($_) ? @$_ : $_ } @decls) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ eq "-as_base";} @decls and not grep({$_ eq "options";} map({ref $_ ? @$_ : $_;} @decls))) 3 12 15
ref $_ ? : 10 13 23
Condition100%3/3
grep {$_ eq "-as_base";} @decls and not grep({$_ eq "options";} map({ref $_ ? @$_ : $_;} @decls))
!ll&&!rl&&r
2103
MC/DC100%2/2
grep {$_ eq "-as_base";} @decls and not grep({$_ eq "options";} map({ref $_ ? @$_ : $_;} @decls))
grep {$_ eq "-as_base";} @decls not grep({$_ eq "options";} map({ref $_ ? @$_ : $_;} @decls))
Truth table100%3/3
A and not(B)
A= grep {$_ eq "-as_base";} @declsB= not grep({$_ eq "options";} map({ref $_ ? @$_ : $_;} @decls))
ABresult
0X0
100
111
49 3 DEBUG && print STDERR "Because of no 'options', we set 'help' and 'version' automatically.\n";
50 3 push @decls, [
51 options =>
52 ['help|h', 'command', 'help'],
53 ['version', 'command', 'version'],
54 ];
55 }
56  
57 15 my Opts $opts = m4i_opts([caller]);
58  
59 15 ▶ @decls = $myPack->default_exports unless @decls;
Branch: 1 branch
BranchTrueFalseTotal
unless @decls 2 13 15
60  
61 15 $myPack->dispatch_declare($opts, $myPack->always_exports, @decls);
62  
63 15 m4i_log_end($opts->{callpack}) if DEBUG;
64 }
65  
66  
67 sub default_options {
68 return (
69 13 ▶ help => ['command' => 'help', 'type' => 'flag', 'alias' => 'h'],
Subroutine default_options: called CC=1 SCAR=0.0
Pod undocumented
70 version => ['command' => 'version', 'type' => 'flag'],
71 );
72 }
73  
74  
75 sub _default_opt {
76 13 ▶ my ( $myPack, $decls ) = @_;
Subroutine _default_opt: called CC=12 SCAR=24.9
77 13 my %auto = $myPack->default_options;
78 my %rev_alias = map { # aliasの逆リンクをつくる
79 13 my %pair = @{ $auto{$_} };
80 26 ▶ defined $pair{alias} ? ($pair{alias} => $_) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $pair{'alias'} ? : 13 13 26
81 } keys %auto;
82  
83 13 for my $dec (@$decls) {
84 46 ▶ if ( $dec->[0] =~ /^([-a-zA-Z0-9]+)(?:\|([a-zA-Z0-9]))?(?:=[is]@?)?$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dec->[0] =~ /^([-a-zA-Z0-9]+)(?:\|([a-zA-Z0-9]))?(?:=[is]\@?)?$/) 46 0 46
85 46 my $optname = $1;
86 46 ▶ my $alias = $2 // '';
Condition100%2/2
$2 // ""
l!l
388
MC/DC100%1/1
$2 // ""
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
87 46 for my $k ( $optname, $alias ) {
88 92 my $rev_alias = $rev_alias{$k};
89 92 ▶ my %opt = @{$auto{$k} || []};
Branch: 1 branch
BranchTrueFalseTotal
$auto{$k} or [] 85 7 92
90 92 ▶ if ( exists $opt{alias} ) { # オプションがユーザー定義されているならaliasも不要
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"alias"}) 4 88 92
91 4 delete $auto{$opt{alias}};
92 }
93 92 ▶ if ( $rev_alias ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rev_alias) 7 85 92
94 7 ▶ my %pair = @{ $auto{$rev_alias} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$auto{$rev_alias} or [] 3 4 7
95 7 delete $pair{alias};
96 7 ▶ $auto{$rev_alias} = [%pair] if exists $auto{$rev_alias};
Branch: 1 branch
BranchTrueFalseTotal
if exists $auto{$rev_alias} 4 3 7
97 }
98 92 delete $auto{$k};
99 }
100 }
101 }
102  
103 13 for my $k ( sort keys %auto ) {
104 19 push @$decls, [$k, @{$auto{$k}}];
105 }
106 #print "decls:", Dumper($decls);
107 }
108  
109 sub declare_options {
110 13 ▶ (my $myPack, my Opts $opts, my (@decls)) = m4i_args(@_);
Subroutine declare_options: called CC=8 SCAR=20.8
Pod undocumented
111  
112 13 $myPack->_default_opt(\@decls);
113  
114 $myPack->declare_fields($opts, map {
115 13 ▶ my $o = ref $_ ? $_ : [$_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 65 0 65
116  
117 65 ▶ unless ( $o->[0] =~ /([-\w]+)(?:\|([-\w]+))?(?:=([is]@?))?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->[0] =~ /([-\w]+)(?:\|([-\w]+))?(?:=([is]\@?))?/) 0 65 65
118 0 Carp::croak("Invalid option format - " . $o->[0]);
119 }
120  
121 65 my ($optline, %pair) = @$o;
122 65 ▶ if (exists $pair{'for_subcmd'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $pair{"for_subcmd"}) 12 53 65
123 12 ▶ if ( ref $pair{'for_subcmd'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $pair{'for_subcmd'}) { } 4 8 12
elsif ($pair{'for_subcmd'} ne '1') { } 4 4 8
124 4 $pair{'for_subcmd'} = +{ map { $_ => 1 } @{ $pair{'for_subcmd'} } };
125 }
126 elsif ( $pair{'for_subcmd'} ne '1' ) {
127 4 $pair{'for_subcmd'} = { $pair{'for_subcmd'} => 1 };
128 }
129 12 @$o = ($optline, %pair);
130 }
131  
132 65 my ($name, $alias, $type) = ($1, $2, $3);
133 65 ▶ $type ||= 'flag';
Condition100%2/2
$type ||= "flag"
l!l
2936
MC/DC100%1/1
$type ||= "flag"
$type
Truth table100%2/2
A
A= $type
Aresult
11
00
134 65 if (DEBUG) {
135 print STDERR $name;
136 if (defined $alias) {print " - $alias"}
137 print STDERR " = $type";
138 print "\n";
139 }
140 65 push @$o, real_name => $name;
141 65 $name =~ s/-/_/g;
142 65 $o->[0] = $name;
143 65 ▶ push @$o, alias => $alias if defined $alias;
Branch: 1 branch
BranchTrueFalseTotal
if defined $alias 40 25 65
144 65 push @$o, type => $type;
145 65 $o;
146 } @decls);
147 }
148  
149 sub parse_opts_for_cli_opts {
150 106 ▶ my ($class, $list, $result, @rest) = @_;
Subroutine parse_opts_for_cli_opts: called CC=29 SCAR=33.7
Pod undocumented
151 106 my $fields = MOP4Import::Declare::fields_hash($class);
152 106 print STDERR "fields for $class : ", Data::Dumper->new([$fields])->Dump, "\n" if DEBUG;
153  
154 106 my %alias;
155 106 my $form = {};
156 106 ▶ my $preserve = (ref($result) && scalar(@$result) && $result->[0]) ? shift(@$result) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $result && scalar @$result && $result->[0] ? : 47 59 106
Condition80%4/5
ref $result and scalar @$result and $result->[0]
!ll&&!rl&&r
59047
ref $result and scalar @$result
!ll
5947
MC/DC50%1/2
ref $result and scalar @$result and $result->[0]
ref $result $result->[0]
Truth table66%2/3
A and B
A= ref $resultB= $result->[0]
ABresult
0X0
100
111
157 106 unshift @{ $result }, { %$preserve, without_value => {} }; # SUPER::parse_optsで失われると困る情報を保持
158  
159 {
160 106 foreach my $name (keys %$fields) {
161 1021 my FieldSpec $spec = $fields->{$name};
162 1021 ▶ if ( UNIVERSAL::isa($spec, FieldSpec) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($spec, "MOP4Import::Base::CLI_Opts::FieldSpec")) 809 212 1021
163 809 ▶ if ( $preserve->{for_subcmd} ) { # at this time, $class is a created object.
Branch: 2 branches
BranchTrueFalseTotal
if ($preserve->{'for_subcmd'}) { } 364 445 809
elsif ($spec->{'for_subcmd'}) { } 87 358 445
164 364 ▶ next if not $spec->{for_subcmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"for_subcmd"} 286 78 364
165 78 ▶ if ( ref $spec->{for_subcmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec->{"for_subcmd"}) 40 38 78
166 40 ▶ next unless $spec->{for_subcmd}->{ $class->{_cmd} };
Branch: 1 branch
BranchTrueFalseTotal
unless $spec->{"for_subcmd"}{$class->{"_cmd"}} 23 17 40
167 }
168 }
169 elsif ( $spec->{for_subcmd} ) {
170 87 next;
171 }
172  
173 413 ▶ $form->{ $name } = $spec if $spec->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"type"} 413 0 413
174 413 ▶ if ( $spec->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"alias"}) 303 110 413
175 303 $alias{$spec->{alias}} = $name;
176 303 $form->{ $spec->{alias} } = $form->{ $name };
177 }
178 }
179 }
180 }
181 106 print STDERR "option format: ", Data::Dumper->new([$form])->Dump, "\n" if DEBUG;
182 106 my $key = 1;
183 106 my @stack;
184 my $cmd;
185 106 my $req_value;
186 106 for my $arg ( @$list ) {
187 206 ▶ if ( $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) { } 154 52 206
188 154 ▶ if ( $arg =~ /^-([^=]+)=(.+)$/ ) { # -f=foo
Branch: 4 branches
BranchTrueFalseTotal
if ($arg =~ /^-([^=]+)=(.+)$/) { } 7 147 154
elsif ($arg =~ /^--([^=]+)=(.+)$/) { } 0 147 147
elsif (not $cmd and $arg =~ /^-([^-=]+)/) { } 38 109 147
elsif (not $cmd and $arg =~ /^--([^=]+)/) { } 38 71 109
189 7 push @stack, $arg;
190 }
191 elsif ( $arg =~ /^--([^=]+)=(.+)$/ ) { # --foo=baz
192 0 push @stack, $arg;
193 }
194 ▶ elsif ( !$cmd && $arg =~ /^-([^-=]+)/ ) { # -fb( value)
Condition100%3/3
not $cmd and $arg =~ /^-([^-=]+)/
!ll&&!rl&&r
337638
MC/DC100%2/2
not $cmd and $arg =~ /^-([^-=]+)/
not $cmd $arg =~ /^-([^-=]+)/
Truth table100%3/3
not(A) and B
A= not $cmdB= $arg =~ /^-([^-=]+)/
ABresult
0X0
100
111
195 38 my $myaby_opts = $1;
196 38 for my $s (split//, $myaby_opts) {
197 50 ▶ if ( not defined $form->{$s} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $form->{$s}) 3 47 50
198 3 Carp::croak("Invalid option format - " . $arg);
199 }
200 47 ▶ if ( $form->{$s}->{type} ne 'flag' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form->{$s}{'type'} ne 'flag') { } 26 21 47
201 26 push @stack, '-' . $s;
202 26 $key = 0;
203 26 ▶ if (not $req_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req_value) 26 0 26
204 26 $req_value = $myaby_opts;
205 }
206 }
207 else {
208 21 push @stack, '-' . $s;
209 }
210 }
211 }
212 ▶ elsif ( !$cmd && $arg =~ /^--([^=]+)/ ) { # --bar( value)
Condition100%3/3
not $cmd and $arg =~ /^--([^=]+)/
!ll&&!rl&&r
333838
MC/DC100%2/2
not $cmd and $arg =~ /^--([^=]+)/
not $cmd $arg =~ /^--([^=]+)/
Truth table100%3/3
not(A) and B
A= not $cmdB= $arg =~ /^--([^=]+)/
ABresult
0X0
100
111
213 38 ▶ if ( not defined $form->{_hyphen2underscore($1)} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$form{_hyphen2underscore($1)}) 1 37 38
214 1 Carp::croak("Invalid option format - " . $arg);
215 }
216 37 ▶ if ( $form->{_hyphen2underscore($1)}->{type} ne 'flag' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$form{_hyphen2underscore($1)}{'type'} ne 'flag') { } 28 9 37
217 28 push @stack, $arg;
218 28 $key = 0;
219 28 ▶ if (not $req_value) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($req_value) 28 0 28
220 28 $req_value = _hyphen2underscore($1);
221 }
222 }
223 else {
224 9 push @stack, $arg;
225 }
226 }
227 else { # perhaps this is command!
228 71 $cmd = $arg;
229 71 push @stack, $arg;
230 }
231 }
232 else { # change to Base::CLI aware format
233 52 $stack[-1] .= '=' . $arg;
234 52 $key = 1;
235 52 $req_value = '';
236 }
237 }
238 #print Dumper($list);
239 #print Dumper(\@stack);
240 102 @$list = @stack;
241  
242 102 ▶ if ($req_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_value) 2 100 102
243 2 $result->[0]->{without_value}->{ $req_value } = 1;
244 }
245  
246 102 $class->parse_opts($list, $result, \%alias, @rest);
247 }
248  
249  
250 sub configure_for_cli_opts {
251 107 ▶ my ( $self, @args ) = @_;
Subroutine configure_for_cli_opts: called CC=27 SCAR=33.0
Pod undocumented
252 107 my $fields = MOP4Import::Declare::fields_hash($self);
253 107 my @res;
254 my %map;
255 107 my $command;
256  
257 107 my $preserved = shift @args;
258  
259 107 my (%required, %default);
260 107 for my $spec ( values %$fields ) {
261 1031 ▶ next unless UNIVERSAL::isa($spec, FieldSpec);
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($spec, "MOP4Import::Base::CLI_Opts::FieldSpec") 214 817 1031
262 817 ▶ next if ( $preserved->{for_subcmd} and not $spec->{for_subcmd} );
Branch: 1 branch
BranchTrueFalseTotal
if $preserved->{"for_subcmd"} and not $spec->{"for_subcmd"} 290 527 817
Condition100%3/3
$preserved->{"for_subcmd"} and not $spec->{"for_subcmd"}
!ll&&!rl&&r
44483290
MC/DC100%2/2
$preserved->{"for_subcmd"} and not $spec->{"for_subcmd"}
$preserved->{"for_subcmd"} not $spec->{"for_subcmd"}
Truth table100%3/3
A and not(B)
A= $preserved->{"for_subcmd"}B= not $spec->{"for_subcmd"}
ABresult
0X0
100
111
263 527 ▶ next if ( not $preserved->{for_subcmd} and $spec->{for_subcmd} );
Branch: 1 branch
BranchTrueFalseTotal
if not $preserved->{"for_subcmd"} and $spec->{"for_subcmd"} 98 429 527
Condition100%3/3
not $preserved->{"for_subcmd"} and $spec->{"for_subcmd"}
!ll&&!rl&&r
8334698
MC/DC100%2/2
not $preserved->{"for_subcmd"} and $spec->{"for_subcmd"}
not $preserved->{"for_subcmd"} $spec->{"for_subcmd"}
Truth table100%3/3
not(A) and B
A= not $preserved->{"for_subcmd"}B= $spec->{"for_subcmd"}
ABresult
0X0
100
111
264 429 ▶ if ( $preserved->{for_subcmd} and ref($spec->{for_subcmd}) and not $spec->{for_subcmd}->{ $self->{_cmd} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($preserved->{"for_subcmd"} and ref $spec->{"for_subcmd"} and not $spec->{"for_subcmd"}{$self->{"_cmd"}}) 24 405 429
Condition100%6/6
$preserved->{"for_subcmd"} and ref $spec->{"for_subcmd"} and not $spec->{"for_subcmd"}{$self->{"_cmd"}}
!ll&&!rl&&r
3852024
$preserved->{"for_subcmd"} and ref $spec->{"for_subcmd"}
!ll&&!rl&&r
3463944
MC/DC100%3/3
$preserved->{"for_subcmd"} and ref $spec->{"for_subcmd"} and not $spec->{"for_subcmd"}{$self->{"_cmd"}}
$preserved->{"for_subcmd"} ref $spec->{"for_subcmd"} not $spec->{"for_subcmd"}{$self->{"_cmd"}}
Truth table100%4/4
A and B and not(C)
A= $preserved->{"for_subcmd"}B= ref $spec->{"for_subcmd"}C= not $spec->{"for_subcmd"}{$self->{"_cmd"}}
ABCresult
0XX0
10X0
1100
1111
265 24 next;
266 }
267 405 ▶ $required{ $spec->{name} } = $spec->{required} if exists $spec->{required};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"required"} 50 355 405
268 405 ▶ $default{ $spec->{name} } = $spec->{default} if exists $spec->{default};
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec->{"default"} 84 321 405
269 405 ▶ if ( $spec->{alias} && exists $preserved->{without_value}->{ $spec->{alias} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"alias"} and exists $preserved->{"without_value"}{$spec->{"alias"}}) 1 404 405
Condition100%3/3
$spec->{"alias"} and exists $preserved->{"without_value"}{$spec->{"alias"}}
!ll&&!rl&&r
1112931
MC/DC100%2/2
$spec->{"alias"} and exists $preserved->{"without_value"}{$spec->{"alias"}}
$spec->{"alias"} exists $preserved->{"without_value"}{$spec->{"alias"}}
Truth table100%3/3
A and B
A= $spec->{"alias"}B= exists $preserved->{"without_value"}{$spec->{"alias"}}
ABresult
0X0
100
111
270 1 $preserved->{without_value}->{ $spec->{name} } = $preserved->{without_value}->{ $spec->{alias} };
271 1 delete $preserved->{without_value}->{ $spec->{alias} };
272 }
273 }
274  
275 107 ▶ while ( defined(my $name = shift @args) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = shift @args)) 94 102 196
276 94 ▶ my $type = $fields->{$name}->{type} // '';
Condition50%1/2
$fields->{$name}{"type"} // ""
l!l
940
MC/DC0%0/1
$fields->{$name}{"type"} // ""
$fields->{$name}{"type"}
Truth table50%1/2
A
A= $fields->{$name}{"type"}
Aresult
11
00
277 94 my $val = shift(@args);
278  
279 94 ▶ if ( $type =~ /(.)@/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ /(.)@/) { } 10 84 94
elsif ($type eq 'i') { } 22 62 84
280 10 ▶ if ( $1 eq 'i' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "i") 4 6 10
281 4 ▶ if ( $val !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^[0-9]+$/) 1 3 4
282 1 Carp::croak("option `$name` takes integer.");
283 }
284 }
285  
286 9 ▶ if (not exists $map{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $map{$name}) { } 5 4 9
287 5 push @res, $name;
288 5 push @res, [$val];
289 5 $map{$name} = $#res;
290 }
291 else {
292 4 push @{ $res[ $map{$name} ] }, $val;
293 }
294 9 next;
295 }
296 elsif ( $type eq 'i' ) {
297 22 ▶ if ( $val !~ /^[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /^[0-9]+$/) 3 19 22
298 3 Carp::croak("option `$name` takes integer.");
299 }
300 }
301  
302 81 ▶ if ( $fields->{$name}->{command} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields->{$name}{"command"}) 15 66 81
303 15 ▶ if ( defined $command ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $command) 1 14 15
304 1 Carp::croak("command invoking option was already called before `$name`.");
305 }
306 14 $command = $fields->{$name}->{command};
307 14 $self->{__cmd} = [$command, $val];
308 14 next;
309 }
310  
311 66 ▶ delete $required{$name} if not exists $preserved->{without_value}->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $preserved->{"without_value"}{$name} 64 2 66
312 66 delete $default{$name};
313  
314 66 push @res, $name;
315 66 push @res, $val;
316 }
317  
318 102 for my $key (keys %default) {
319 60 push @res, $key, $default{$key};
320 60 delete $required{$key};
321 }
322 102 ▶ if (%required) {
Branch: 1 branch
BranchTrueFalseTotal
if (%required) 7 95 102
323 7 my $opt = (sort keys %required)[0];
324 7 Carp::croak("$opt is required.");
325 }
326 #print Dumper([@res]);
327 95 $self->configure(@res);
328 }
329  
330  
331 ▶sub cmd_default { }
Subroutine cmd_default: called CC=1 SCAR=0.0
Pod undocumented
332  
333 sub cmd_version {
334 0 ▶ my $v = shift->VERSION // '0.0';
Subroutine cmd_version: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
(shift())->VERSION // "0.0"
l!l
00
MC/DC0%0/1
(shift())->VERSION // "0.0"
(shift())->VERSION
Truth table0%0/2
A
A= (shift())->VERSION
Aresult
11
00
335 0 print "$v\n";
336 }
337  
338 sub cmd_help { # From MOP4Import::Base::CLI, original cmd_help do 'die'
339 0 ▶ my $self = shift;
Subroutine cmd_help: not called CC=16 SCAR=56.1
Pod undocumented
340 0 ▶ my $pack = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
341 0 my $fields = MOP4Import::Declare::fields_hash($self);
342 0 my $names = MOP4Import::Declare::fields_array($self);
343  
344 0 require MOP4Import::Util::FindMethods;
345  
346 0 ▶ my @methods = MOP4Import::Util::FindMethods::FindMethods($pack, sub {s/^cmd_//});
Subroutine __ANON__: not called CC=1 SCAR=6.9
347  
348 0 print join("\n", <<END);
349 0 Usage: @{[File::Basename::basename($0)]} [--opt=value].. <command> [--opt=value].. ARGS...
350  
351 Commands:
352 0 @{[join("\n ", @methods)]}
353 END
354  
355 0 my $max_len = 0;
356 0 my %opt;
357 0 for my $name ( grep {/^[a-z]/} @$names ) {
358 0 my FieldSpec $fs = $fields->{$name};
359 0 my $cmds = ['__common__'];
360 0 my $str;
361 0 ▶ if (ref $fs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fs) { } 0 0 0
362 $str =
363 0 ▶ do{ ' --' . (UNIVERSAL::isa($fs, FieldSpec) ? ($fs->{real_name} // '') : $name) } .
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($fs, 'MOP4Import::Base::CLI_Opts::FieldSpec') ? : 0 0 0
Condition0%0/2
$fs->{"real_name"} // ""
l!l
00
MC/DC0%0/1
$fs->{"real_name"} // ""
$fs->{"real_name"}
Truth table0%0/2
A
A= $fs->{"real_name"}
Aresult
11
00
364 0 ▶ do{ UNIVERSAL::isa($fs, FieldSpec) and $fs->{ alias } ? ', -' . $fs->{ alias } : '' },
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($fs, "MOP4Import::Base::CLI_Opts::FieldSpec") and $fs->{"alias"} ? ', -' . $fs->{"alias"} : "" 0 0 0
$fs->{'alias'} ? : 0 0 0
365 ;
366 0 ▶ if ( exists $fs->{for_subcmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fs->{"for_subcmd"}) 0 0 0
367 0 ▶ if ( ref $fs->{for_subcmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fs->{"for_subcmd"}) 0 0 0
368 0 $cmds = [ sort keys %{ $fs->{for_subcmd} } ];
369 }
370 }
371 }
372 else {
373 0 $str = $name;
374 }
375 0 ▶ $max_len = length $str if length $str > $max_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $str > $max_len 0 0 0
376  
377 0 ▶ next if $str eq ' --';
Branch: 1 branch
BranchTrueFalseTotal
if $str eq " --" 0 0 0
378  
379 0 for my $name ( @$cmds ) {
380 0 ▶ push @{ $opt{ $name } }, [$str, $fs->{doc} ? $fs->{doc} : ''];
Branch: 1 branch
BranchTrueFalseTotal
$fs->{'doc'} ? : 0 0 0
381 }
382 }
383  
384 0 my @opts = [ 'common option', delete $opt{'__common__'} ];
385 0 push @opts, map { [ 'for ' . $_ => $opt{$_} ] } sort keys %opt;
386  
387 0 print join("\n", <<END);
388  
389 Options:
390 END
391  
392 0 $max_len += 2;
393 0 for my $optset ( @opts ) {
394 0 my ( $subcmd, $opts ) = @$optset;
395 0 print " [$subcmd]\n";
396 0 for my $opt (@$opts) {
397 0 printf( "%-${max_len}s%s\n", @$opt );
398 }
399 0 print "\n";
400 }
401  
402 0 exit();
403 }
404  
405 sub run {
406 59 ▶ my ($class, $arglist) = @_;
Subroutine run: called CC=4 SCAR=14.0
Pod undocumented
407 59 my $default_cmd = 'default';
408 59 my $fields = MOP4Import::Declare::fields_hash($class);
409 # $arglist を parse し、 $class->new 用のパラメータリストを作る
410 59 my @opts = $class->parse_opts_for_cli_opts($arglist);
411 56 my MY $obj = fields::new($class);
412 56 $obj->configure_for_cli_opts(@opts);
413 47 $obj->after_new;
414 # 次の引数を取り出して、サブコマンドとして解釈を試みる
415 47 ▶ my $cmd = shift @$arglist || _set_cmd_by_option($obj, $arglist) || $default_cmd;
Condition83%5/6
shift @$arglist || _set_cmd_by_option($obj, $arglist) || $default_cmd
l!l&&r!l&&!r
30170
shift @$arglist or _set_cmd_by_option($obj, $arglist)
l!l&&r!l&&!r
171317
MC/DC0%0/3
shift @$arglist || _set_cmd_by_option($obj, $arglist) || $default_cmd
shift @$arglist _set_cmd_by_option($obj, $arglist) $default_cmd
Truth table75%3/4
A or B || C
A= shift @$arglistB= _set_cmd_by_option($obj, $arglist)C= $default_cmd
ABCresult
1XX1
01X1
0011
0000
416 47 $obj->{_cmd} = $cmd;
417 47 my $result = [ { for_subcmd => 1 } ];
418 47 $obj->configure_for_cli_opts(@{ $obj->parse_opts_for_cli_opts($arglist, $result) });
419 # サブコマンド毎の処理を行う
420 # 結果を何らかの形式で出力する
421 # 望ましい終了コードを返す(差し当たり、必要ならば各メソッド内で指定する)
422 45 ▶ if (my $sub = $obj->can("cmd_$cmd")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = $obj->can("cmd_$cmd")) { } 45 0 45
423 45 $sub->($obj, @$arglist);
424 } else {
425 0 print STDERR "Cmd `$cmd` is not implemented.\n";
426 0 exit(1);
427 }
428 }
429  
430 sub _hyphen2underscore {
431 103 ▶ my ($v) = @_;
Subroutine _hyphen2underscore: called CC=1 SCAR=0.0
432 103 $v =~ tr/-/_/;
433 103 return $v;
434 }
435  
436 sub _set_cmd_by_option {
437 30 ▶ my ($obj, $arglist) = @_;
Subroutine _set_cmd_by_option: called CC=2 SCAR=6.9
438 30 ▶ return unless $obj->{__cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->{"__cmd"} 17 13 30
439 13 push @$arglist, $obj->{__cmd}->[1];
440 13 return $obj->{__cmd}->[0];
441 }
442  
443 7 1;
« blib/lib/MOP4Import/Util/FindMethods.pm ↑ Summary blib/lib/MOP4Import/Util/Inspector.pm »