OptArgs2.pm

filter: Statementstmt 71.9% 344 / 478 Branchbran 52.6% 101 / 192 Conditioncond 32.8% 48 / 146 MC/DCmcdc 11.4% 10 / 87 Subroutinesub 74.5% 44 / 59 Podpod 39.1% 9 / 23 totaltotal 56.4% 556 / 985 CC 160 SCAR 43.3
CC
160
Cov
60%
parse
176.6
usage_string
156.1
name_alias_type_comment
63.6
CRAP 76.1
↑ Summary OptArgs2/Pager.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package OptArgs2;
5 4 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 use Exporter::Tidy
7 4 default => [qw/class_optargs cmd optargs subcmd arg opt/],
8 4 ▶ other => [qw/usage cols rows/];
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 our $VERSION = '2.0.0';
11 4 our @CARP_NOT = (
12 qw/
13 OptArgs2
14 OptArgs2::Arg
15 OptArgs2::Cmd
16 OptArgs2::CmdBase
17 OptArgs2::Opt
18 OptArgs2::OptArgBase
19 OptArgs2::SubCmd
20 /
21 );
22  
23 # constants
24 sub STYLE_USAGE() { 'Usage' } # default
25 sub STYLE_HELP() { 'Help' }
26 sub STYLE_HELPTREE() { 'HelpTree' }
27 sub STYLE_HELPSUMMARY() { 'HelpSummary' }
28  
29 4 our %CURRENT; # legacy interface
30 4 my %COMMAND;
31 my @chars;
32  
33 sub _chars {
34 0 ▶ if ( $^O eq 'MSWin32' ) {
Subroutine _chars: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
35 0 require Win32::Console;
36 0 @chars = Win32::Console->new()->Size();
37 }
38 else {
39 0 require Term::Size::Perl;
40 0 @chars = Term::Size::Perl::chars();
41 }
42 0 \@chars;
43 }
44  
45 sub cols {
46 0 ▶ $chars[0] // _chars()->[0];
Subroutine cols: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$chars[0] // _chars()->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$chars[0] // _chars()->[0]
$chars[0] _chars()->[0]
Truth table0%0/3
A // B
A= $chars[0]B= _chars()->[0]
ABresult
1X1
011
000
47 }
48  
49 sub rows {
50 0 ▶ $chars[1] // _chars()->[1];
Subroutine rows: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/3
$chars[1] // _chars()->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$chars[1] // _chars()->[1]
$chars[1] _chars()->[1]
Truth table0%0/3
A // B
A= $chars[1]B= _chars()->[1]
ABresult
1X1
011
000
51 }
52  
53 4 my %error_types = (
54 CmdExists => undef,
55 CmdNotFound => undef,
56 Conflict => undef,
57 InvalidIsa => undef,
58 ParentCmdNotFound => undef,
59 SubCmdExists => undef,
60 UndefOptArg => undef,
61 Usage => undef,
62 );
63  
64 sub throw_error {
65 1 ▶ require Carp;
Subroutine throw_error: called CC=5 SCAR=16.6
Pod undocumented
66  
67 1 my $proto = shift;
68 1 ▶ my $type = shift // Carp::croak( 'Usage', 'error($TYPE, [$msg])' );
Condition33%1/3
shift() // Carp::croak("Usage", "error(\$TYPE, [\$msg])")
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() // Carp::croak("Usage", "error(\$TYPE, [\$msg])")
shift() Carp::croak("Usage", "error(\$TYPE, [\$msg])")
Truth table33%1/3
A // B
A= shift()B= Carp::croak("Usage", "error(\$TYPE, [\$msg])")
ABresult
1X1
011
000
69 1 my $pkg = 'OptArgs2::Error::' . $type;
70 1 ▶ my $msg = shift // "($pkg)";
Condition50%1/2
shift() // "($pkg)"
l!l
10
MC/DC0%0/1
shift() // "($pkg)"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
71 1 ▶ $msg = sprintf( $msg, @_ ) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
72  
73 Carp::croak( 'Usage', "unknown error type: $type" )
74 1 ▶ unless exists $error_types{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $error_types{$type} 0 1 1
75  
76 1 $msg .= ' ' . Carp::longmess('');
77  
78 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 *{ $pkg . '::ISA' } = ['OptArgs2::Status'];
80  
81 1 die bless \$msg, $pkg;
82 }
83  
84 4 my %usage_types = (
85 ArgRequired => undef,
86 Help => undef,
87 HelpSummary => undef,
88 HelpTree => undef,
89 OptRequired => undef,
90 OptUnknown => undef,
91 SubCmdRequired => undef,
92 SubCmdUnknown => undef,
93 UnexpectedOptArg => undef,
94 );
95  
96 sub throw_usage {
97 4 ▶ my $proto = shift;
Subroutine throw_usage: called CC=7 SCAR=25.7
Pod undocumented
98 4 ▶ my $type = shift // $proto->error( 'Usage', 'usage($TYPE, $str)' );
Condition33%1/3
shift() // $proto->error("Usage", "usage(\$TYPE, \$str)")
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() // $proto->error("Usage", "usage(\$TYPE, \$str)")
shift() $proto->error("Usage", "usage(\$TYPE, \$str)")
Truth table33%1/3
A // B
A= shift()B= $proto->error("Usage", "usage(\$TYPE, \$str)")
ABresult
1X1
011
000
99 4 ▶ my $str = shift // $proto->error( 'Usage', 'usage($type, $STR)' );
Condition33%1/3
shift() // $proto->error("Usage", "usage(\$type, \$STR)")
l!l&&r!l&&!r
400
MC/DC0%0/2
shift() // $proto->error("Usage", "usage(\$type, \$STR)")
shift() $proto->error("Usage", "usage(\$type, \$STR)")
Truth table33%1/3
A // B
A= shift()B= $proto->error("Usage", "usage(\$type, \$STR)")
ABresult
1X1
011
000
100 4 my $pkg = 'OptArgs2::Usage::' . $type;
101  
102 $proto->error( 'Usage', "unknown usage reason: $type" )
103 4 ▶ unless exists $usage_types{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $usage_types{$type} 0 4 4
104  
105 4 ▶ if ( -t STDERR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDERR) 0 4 4
106 0 my $lines = scalar( split /\n/, $str );
107 0 ▶ $lines++ if $str =~ m/\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /\n\z/ 0 0 0
108  
109 0 ▶ if ( $lines >= OptArgs2::rows() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines >= rows()) 0 0 0
110 0 require OptArgs2::Pager;
111 0 my $pager = OptArgs2::Pager->new( auto => 0 );
112 0 local *STDERR = $pager->fh;
113  
114 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 *{ $pkg . '::ISA' } = ['OptArgs2::Status'];
116 0 die bless \$str, $pkg;
117 }
118 }
119  
120 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
121 4 *{ $pkg . '::ISA' } = ['OptArgs2::Status'];
122 4 die bless \$str, $pkg;
123 }
124  
125 sub class_optargs {
126 11 ▶ my $class = shift
Subroutine class_optargs: called CC=7 SCAR=20.0
Pod documented
Condition33%1/3
shift() || 'OptArgs2'->throw_error('Usage', 'class_optargs($CMD,[@argv])')
l!l&&r!l&&!r
1100
MC/DC0%0/2
shift() || 'OptArgs2'->throw_error('Usage', 'class_optargs($CMD,[@argv])')
shift() 'OptArgs2'->throw_error('Usage', 'class_optargs($CMD,[@argv])')
Truth table33%1/3
A || B
A= shift()B= 'OptArgs2'->throw_error('Usage', 'class_optargs($CMD,[@argv])')
ABresult
1X1
011
000
127 || OptArgs2->throw_error( 'Usage', 'class_optargs($CMD,[@argv])' );
128  
129 11 ▶ my $cmd = $COMMAND{$class}
Condition33%1/3
$COMMAND{$class} || 'OptArgs2'->throw_error('CmdNotFound', 'command class not found: ' . $class)
l!l&&r!l&&!r
1100
MC/DC0%0/2
$COMMAND{$class} || 'OptArgs2'->throw_error('CmdNotFound', 'command class not found: ' . $class)
$COMMAND{$class} 'OptArgs2'->throw_error('CmdNotFound', 'command class not found: ' . $class)
Truth table33%1/3
A || B
A= $COMMAND{$class}B= 'OptArgs2'->throw_error('CmdNotFound', 'command class not found: ' . $class)
ABresult
1X1
011
000
130 || OptArgs2->throw_error( 'CmdNotFound',
131 'command class not found: ' . $class );
132  
133 11 my @source = @_;
134  
135 11 ▶ if ( !@_ and @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ and @ARGV) 1 10 11
Condition100%3/3
not @_ and @ARGV
!ll&&!rl&&r
731
MC/DC100%2/2
not @_ and @ARGV
not @_ @ARGV
Truth table100%3/3
not(A) and B
A= not @_B= @ARGV
ABresult
0X0
100
111
136 my $CODESET =
137 1 eval { require I18N::Langinfo; I18N::Langinfo::CODESET() };
138  
139 1 ▶ if ($CODESET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CODESET) 1 0 1
140 1 my $codeset = I18N::Langinfo::langinfo($CODESET);
141 1 $_ = decode( $codeset, $_ ) for @ARGV;
142 }
143  
144 1 @source = @ARGV;
145 }
146  
147 11 $cmd->parse(@source);
148 }
149  
150 sub cmd {
151 7 ▶ my $class = shift || OptArgs2->throw_error( 'Usage', 'cmd($CLASS,@args)' );
Subroutine cmd: called CC=3 SCAR=12.5
Pod documented
Condition33%1/3
shift() || 'OptArgs2'->throw_error('Usage', 'cmd($CLASS,@args)')
l!l&&r!l&&!r
700
MC/DC0%0/2
shift() || 'OptArgs2'->throw_error('Usage', 'cmd($CLASS,@args)')
shift() 'OptArgs2'->throw_error('Usage', 'cmd($CLASS,@args)')
Truth table33%1/3
A || B
A= shift()B= 'OptArgs2'->throw_error('Usage', 'cmd($CLASS,@args)')
ABresult
1X1
011
000
152  
153 OptArgs2->throw_error( 'CmdExists', "command already defined: $class" )
154 7 ▶ if exists $COMMAND{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $COMMAND{$class} 0 7 7
155  
156 7 $COMMAND{$class} = OptArgs2::Cmd->new( class => $class, @_ );
157 }
158  
159 sub optargs {
160 1 ▶ my $class = caller;
Subroutine optargs: called CC=1 SCAR=0.0
Pod documented
161 1 cmd( $class, @_ );
162 1 ( class_optargs($class) )[1];
163 }
164  
165 sub subcmd {
166 6 ▶ my $class =
Subroutine subcmd: called CC=5 SCAR=17.0
Pod documented
Condition33%1/3
shift() || 'OptArgs2'->throw_error('Usage', 'subcmd($CLASS,%%args)')
l!l&&r!l&&!r
600
MC/DC0%0/2
shift() || 'OptArgs2'->throw_error('Usage', 'subcmd($CLASS,%%args)')
shift() 'OptArgs2'->throw_error('Usage', 'subcmd($CLASS,%%args)')
Truth table33%1/3
A || B
A= shift()B= 'OptArgs2'->throw_error('Usage', 'subcmd($CLASS,%%args)')
ABresult
1X1
011
000
167 shift || OptArgs2->throw_error( 'Usage', 'subcmd($CLASS,%%args)' );
168  
169 OptArgs2->throw_error( 'SubCmdExists',
170 "subcommand already defined: $class" )
171 6 ▶ if exists $COMMAND{$class};
Branch: 1 branch
BranchTrueFalseTotal
if exists $COMMAND{$class} 0 6 6
172  
173 6 ▶ OptArgs2->throw_error( 'ParentCmdNotFound',
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /(.+)::(.+)/ 1 5 6
174 "no '::' in class '$class' - must have a parent" )
175 unless $class =~ m/(.+)::(.+)/;
176  
177 5 my $parent_class = $1;
178  
179 OptArgs2->throw_error( 'ParentCmdNotFound',
180 "parent class not found: " . $parent_class )
181 5 ▶ unless exists $COMMAND{$parent_class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $COMMAND{$parent_class} 0 5 5
182  
183 5 $COMMAND{$class} = $COMMAND{$parent_class}->add_cmd(
184 class => $class,
185 @_
186 );
187 }
188  
189 sub usage {
190 0 ▶ my $class = shift || do {
Subroutine usage: not called CC=3 SCAR=24.8
Pod documented
Condition0%0/3
shift() || do { my($pkg) = caller; $pkg }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || do { my($pkg) = caller; $pkg }
shift() do { my($pkg) = caller; $pkg }
Truth table0%0/3
A || B
A= shift()B= do { my($pkg) = caller; $pkg }
ABresult
1X1
011
000
191 0 my ($pkg) = caller;
192 0 $pkg;
193 };
194 0 my $style = shift;
195  
196 OptArgs2->throw_error( 'CmdNotFound', "command not found: $class" )
197 0 ▶ unless exists $COMMAND{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $COMMAND{$class} 0 0 0
198  
199 0 return $COMMAND{$class}->usage_string($style);
200 }
201  
202 # Legacy interface, no longer documented
203  
204 sub arg {
205 0 ▶ my $name = shift;
Subroutine arg: not called CC=2 SCAR=17.9
Pod documented
206  
207 0 ▶ $OptArgs2::CURRENT //= cmd( ( scalar caller ), comment => '' );
Condition0%0/3
$OptArgs2::CURRENT //= cmd(scalar caller, "comment", "")
l!l&&r!l&&!r
000
MC/DC0%0/2
$OptArgs2::CURRENT //= cmd(scalar caller, "comment", "")
$OptArgs2::CURRENT cmd(scalar caller, "comment", "")
Truth table0%0/3
A //= B
A= $OptArgs2::CURRENTB= cmd(scalar caller, "comment", "")
ABresult
1X1
011
000
208 0 $OptArgs2::CURRENT->add_arg(
209 name => $name,
210 @_,
211 );
212 }
213  
214 sub opt {
215 0 ▶ my $name = shift;
Subroutine opt: not called CC=2 SCAR=17.9
Pod documented
216  
217 0 ▶ $OptArgs2::CURRENT //= cmd( ( scalar caller ), comment => '' );
Condition0%0/3
$OptArgs2::CURRENT //= cmd(scalar caller, "comment", "")
l!l&&r!l&&!r
000
MC/DC0%0/2
$OptArgs2::CURRENT //= cmd(scalar caller, "comment", "")
$OptArgs2::CURRENT cmd(scalar caller, "comment", "")
Truth table0%0/3
A //= B
A= $OptArgs2::CURRENTB= cmd(scalar caller, "comment", "")
ABresult
1X1
011
000
218 0 $OptArgs2::CURRENT->add_opt(
219 name => $name,
220 @_,
221 );
222 }
223  
224 4 package OptArgs2::Status {
225 use overload
226 5 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
227 0 ▶ '""' => sub { ${ $_[0] } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
228 4 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 }
230  
231 4 package OptArgs2::CODEREF {
232 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
233  
234 sub TIESCALAR {
235 0 ▶ my $class = shift;
Subroutine TIESCALAR: not called CC=2 SCAR=17.9
236 0 ▶ ( 3 == @_ )
Branch: 1 branch
BranchTrueFalseTotal
3 == @_ or "Optargs2"->throw_error("Usage", "args: optargs,name,sub") 0 0 0
237 or Optargs2->throw_error( 'Usage', 'args: optargs,name,sub' );
238 0 return bless [@_], $class;
239 }
240  
241 sub FETCH {
242 0 ▶ my $self = shift;
Subroutine FETCH: not called CC=1 SCAR=6.9
243 0 my ( $optargs, $name, $sub ) = @$self;
244 0 untie $optargs->{$name};
245 0 $optargs->{$name} = $sub->($optargs);
246 }
247 }
248  
249 4 package OptArgs2::OptArgBase {
250 use OptArgs2::OptArgBase_CI
251 4 abstract => 1,
252 has => {
253 comment => { required => 1, },
254 default => {},
255 getopt => {},
256 isa => { required => 1, },
257 isa_name => { is => 'rw', },
258 name => { required => 1, },
259 required => {},
260 show_default => {},
261 },
262 4 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
263  
264 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
265 4 our %isa2name = (
266 'ArrayRef' => 'Str',
267 'Bool' => '',
268 'Counter' => '',
269 'Flag' => '',
270 'HashRef' => 'Str',
271 'Int' => 'Int',
272 'Num' => 'Num',
273 'Str' => 'Str',
274 'SubCmd' => 'Str',
275 );
276  
277 }
278  
279 4 package OptArgs2::Arg {
280 use OptArgs2::Arg_CI
281 4 isa => 'OptArgs2::OptArgBase',
282 has => {
283 cmd => { is => 'rw', weaken => 1, },
284 fallthru => {},
285 greedy => {},
286 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
289 4 my %arg2getopt = (
290 'Str' => '=s',
291 'Int' => '=i',
292 'Num' => '=f',
293 'ArrayRef' => '=s@',
294 'HashRef' => '=s%',
295 'SubCmd' => '=s',
296 );
297  
298 sub BUILD {
299 6 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=11.7
Pod undocumented
300  
301 6 ▶ OptArgs2->throw_error( 'Conflict',
Branch: 1 branch
BranchTrueFalseTotal
if $self->required and defined $self->default 0 6 6
Condition66%2/3
$self->required and defined $self->default
!ll&&!rl&&r
240
MC/DC0%0/2
$self->required and defined $self->default
$self->required defined $self->default
Truth table66%2/3
A and B
A= $self->requiredB= defined $self->default
ABresult
0X0
100
111
302 q{'default' and 'required' conflict} )
303 if $self->required and defined $self->default;
304 }
305  
306 sub name_alias_type_comment {
307 4 ▶ my $self = shift;
Subroutine name_alias_type_comment: called CC=6 SCAR=19.5
Pod undocumented
308 4 my $value = shift;
309  
310 4 my $deftype = '';
311 4 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 4 4
312 0 $deftype = '[' . $value . ']';
313 }
314 else {
315 $deftype = $self->isa_name
316 4 ▶ // $OptArgs2::OptArgBase::isa2name{ $self->isa }
Condition33%2/6
($self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}) // "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
l!l&&r!l&&!r
400
$self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}
l!l&&r!l&&!r
040
MC/DC0%0/3
($self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}) // "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
$self->isa_name $OptArgs2::OptArgBase::isa2name{$self->isa} "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
Truth table25%1/4
A // B // C
A= $self->isa_nameB= $OptArgs2::OptArgBase::isa2name{$self->isa}C= "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
ABCresult
1XX1
01X1
0011
0000
317 // OptArgs2->throw_error( 'InvalidIsa',
318 'invalid isa type: ' . $self->isa );
319 }
320  
321 4 my $comment = $self->comment;
322 4 ▶ if ( $self->required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) 3 1 4
323 3 ▶ $comment .= ' ' if length $comment;
Branch: 1 branch
BranchTrueFalseTotal
if length $comment 3 0 3
324 3 $comment .= '*required*';
325 }
326  
327 4 return $self->name, '', $deftype, $comment;
328 }
329  
330 }
331  
332 4 package OptArgs2::Opt {
333 use OptArgs2::Opt_CI
334 4 isa => 'OptArgs2::OptArgBase',
335 has => {
336 alias => {},
337 hidden => {},
338 trigger => {},
339 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
340  
341 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
342  
343 4 my %isa2getopt = (
344 'ArrayRef' => '=s@',
345 'Bool' => '!',
346 'Counter' => '+',
347 'Flag' => '!',
348 'HashRef' => '=s%',
349 'Int' => '=i',
350 'Num' => '=f',
351 'Str' => '=s',
352 );
353  
354 sub new_from {
355 8 ▶ my $proto = shift;
Subroutine new_from: called CC=11 SCAR=32.0
Pod undocumented
356 8 my $ref = {@_};
357  
358 # legacy interface
359 8 ▶ if ( exists $ref->{ishelp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ref->{"ishelp"}) 0 8 8
360 0 delete $ref->{ishelp};
361 0 ▶ $ref->{isa} //= OptArgs2::STYLE_HELP;
Condition0%0/2
$ref->{"isa"} //= "Help"
l!l
00
MC/DC0%0/1
$ref->{"isa"} //= "Help"
$ref->{"isa"}
Truth table0%0/2
A
A= $ref->{"isa"}
Aresult
11
00
362 }
363  
364 8 ▶ if ( $ref->{isa} =~ m/^Help/ ) { # one of the STYLE_HELPs
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"isa"} =~ /^Help/) 6 2 8
365 6 my $style = $ref->{isa};
366 6 my $name = $style;
367 6 $name =~ s/([a-z])([A-Z])/$1-$2/g;
368 6 $ref->{isa} = 'Counter';
369 6 ▶ $ref->{name} //= lc $name;
Condition33%1/3
$ref->{"name"} //= lc $name
l!l&&r!l&&!r
060
MC/DC0%0/2
$ref->{"name"} //= lc $name
$ref->{"name"} lc $name
Truth table33%1/3
A //= B
A= $ref->{"name"}B= lc $name
ABresult
1X1
011
000
370 6 ▶ $ref->{alias} //= lc substr $ref->{name}, 0, 1;
Condition33%1/3
$ref->{"alias"} //= lc substr($ref->{"name"}, 0, 1)
l!l&&r!l&&!r
060
MC/DC0%0/2
$ref->{"alias"} //= lc substr($ref->{"name"}, 0, 1)
$ref->{"alias"} lc substr($ref->{"name"}, 0, 1)
Truth table33%1/3
A //= B
A= $ref->{"alias"}B= lc substr($ref->{"name"}, 0, 1)
ABresult
1X1
011
000
371 6 ▶ $ref->{comment} //= "print a $style message and exit";
Condition50%1/2
$ref->{"comment"} //= "print a $style message and exit"
l!l
06
MC/DC0%0/1
$ref->{"comment"} //= "print a $style message and exit"
$ref->{"comment"}
Truth table50%1/2
A
A= $ref->{"comment"}
Aresult
11
00
372 $ref->{trigger} //= sub {
373 0 ▶ my $cmd = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
374 0 my $val = shift;
375  
376 0 ▶ if ( $val == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val == 1) { } 0 0 0
elsif ($val == 2) { } 0 0 0
377 0 OptArgs2->throw_usage( OptArgs2::STYLE_HELP,
378 $cmd->usage_string(OptArgs2::STYLE_HELP) );
379 }
380 elsif ( $val == 2 ) {
381 0 OptArgs2->throw_usage( OptArgs2::STYLE_HELPTREE,
382 $cmd->usage_string(OptArgs2::STYLE_HELPTREE) );
383 }
384 else {
385 0 OptArgs2->throw_usage(
386 'UnexpectedOptArg',
387 $cmd->usage_string(
388 OptArgs2::STYLE_USAGE,
389 qq{"--$ref->{name}" used too many times}
390 )
391 );
392 }
393 6 ▶ };
Condition33%1/3
$ref->{"trigger"} //= sub { my $cmd = shift(); my $val = shift(); if ($val == 1) { "OptArgs2"->throw_usage("Help", $cmd->usage_string("Help")); } elsif ($val == 2) { "OptArgs2"->throw_usage("HelpTree", $cmd->usage_string("HelpTree")); } else { "OptArgs2"->throw_usage("UnexpectedOptArg", $cmd->usage_string("Usage", qq["--$ref->{"name"}" used too many times])); } ; }
l!l&&r!l&&!r
060
MC/DC0%0/2
$ref->{"trigger"} //= sub { my $cmd = shift(); my $val = shift(); if ($val == 1) { "OptArgs2"->throw_usage("Help", $cmd->usage_string("Help")); } elsif ($val == 2) { "OptArgs2"->throw_usage("HelpTree", $cmd->usage_string("HelpTree")); } else { "OptArgs2"->throw_usage("UnexpectedOptArg", $cmd->usage_string("Usage", qq["--$ref->{"name"}" used too many times])); } ; }
$ref->{"trigger"} sub { my $cmd = shift(); my $val = shift(); if ($val == 1) { "OptArgs2"->throw_usage("Help", $cmd->usage_string("Help")); } elsif ($val == 2) { "OptArgs2"->throw_usage("HelpTree", $cmd->usage_string("HelpTree")); } else { "OptArgs2"->throw_usage("UnexpectedOptArg", $cmd->usage_string("Usage", qq["--$ref->{"name"}" used too many times])); } ; }
Truth table33%1/3
A //= B
A= $ref->{"trigger"}B= sub { my $cmd = shift(); my $val = shift(); if ($val == 1) { "OptArgs2"->throw_usage("Help", $cmd->usage_string("Help")); } elsif ($val == 2) { "OptArgs2"->throw_usage("HelpTree", $cmd->usage_string("HelpTree")); } else { "OptArgs2"->throw_usage("UnexpectedOptArg", $cmd->usage_string("Usage", qq["--$ref->{"name"}" used too many times])); } ; }
ABresult
1X1
011
000
394 }
395  
396 8 ▶ if ( !exists $isa2getopt{ $ref->{isa} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $isa2getopt{$ref->{"isa"}}) 0 8 8
397 return OptArgs2->throw_error( 'InvalidIsa',
398 'invalid isa "%s" for opt "%s"',
399 0 $ref->{isa}, $ref->{name} );
400 }
401  
402 8 $ref->{getopt} = $ref->{name};
403 8 ▶ if ( $ref->{name} =~ m/_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{"name"} =~ /_/) 0 8 8
404 0 ( my $x = $ref->{name} ) =~ s/_/-/g;
405 0 $ref->{getopt} .= '|' . $x;
406 }
407 8 ▶ $ref->{getopt} .= '|' . $ref->{alias} if $ref->{alias};
Branch: 1 branch
BranchTrueFalseTotal
if $ref->{"alias"} 6 2 8
408 8 $ref->{getopt} .= $isa2getopt{ $ref->{isa} };
409  
410 8 return $proto->new(%$ref);
411 }
412  
413 sub name_alias_type_comment {
414 4 ▶ my $self = shift;
Subroutine name_alias_type_comment: called CC=15 SCAR=41.5
Pod undocumented
415 4 my $value = shift;
416  
417 4 ( my $opt = $self->name ) =~ s/_/-/g;
418 4 ▶ if ( $self->isa eq 'Bool' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa eq "Bool") 0 4 4
419 0 ▶ if ($value) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value) { } 0 0 0
elsif (not defined $value) { } 0 0 0
420 0 $opt = 'no-' . $opt;
421 }
422 elsif ( not defined $value ) {
423 0 $opt = '[no-]' . $opt;
424 }
425 }
426 4 $opt = '--' . $opt;
427  
428 4 ▶ my $alias = $self->alias // '';
Condition50%1/2
$self->alias // ""
l!l
40
MC/DC0%0/1
$self->alias // ""
$self->alias
Truth table50%1/2
A
A= $self->alias
Aresult
11
00
429 4 ▶ if ( length $alias ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $alias) 4 0 4
430 4 $opt .= ',';
431 4 $alias = '-' . $alias;
432 }
433  
434 4 my $deftype = '';
435 4 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 4 4
436 0 ▶ if ( $self->isa eq 'Flag' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->isa eq 'Flag') { } 0 0 0
elsif ($self->isa eq 'Bool') { } 0 0 0
elsif ($self->isa eq 'Counter') { } 0 0 0
437 0 $deftype = '(set)';
438 }
439 elsif ( $self->isa eq 'Bool' ) {
440 0 ▶ $deftype = '(' . ( $value ? 'true' : 'false' ) . ')';
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
441 }
442 elsif ( $self->isa eq 'Counter' ) {
443 0 $deftype = '(' . $value . ')';
444 }
445 else {
446 0 $deftype = '[' . $value . ']';
447 }
448 }
449 else {
450 $deftype = $self->isa_name
451 4 ▶ // $OptArgs2::OptArgBase::isa2name{ $self->isa }
Condition33%2/6
($self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}) // "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
l!l&&r!l&&!r
400
$self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}
l!l&&r!l&&!r
040
MC/DC0%0/3
($self->isa_name // $OptArgs2::OptArgBase::isa2name{$self->isa}) // "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
$self->isa_name $OptArgs2::OptArgBase::isa2name{$self->isa} "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
Truth table25%1/4
A // B // C
A= $self->isa_nameB= $OptArgs2::OptArgBase::isa2name{$self->isa}C= "OptArgs2"->throw_error("InvalidIsa", 'invalid isa type: ' . $self->isa)
ABCresult
1XX1
01X1
0011
0000
452 // OptArgs2->throw_error( 'InvalidIsa',
453 'invalid isa type: ' . $self->isa );
454 }
455  
456 4 my $comment = $self->comment;
457 4 ▶ if ( $self->required ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required) 0 4 4
458 0 ▶ $comment .= ' ' if length $comment;
Branch: 1 branch
BranchTrueFalseTotal
if length $comment 0 0 0
459 0 $comment .= '*required*';
460 }
461  
462 4 return $opt, $alias, $deftype, $comment;
463 }
464  
465 }
466  
467 4 package OptArgs2::CmdBase {
468 use overload
469 30 ▶ bool => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
470 0 ▶ '""' => sub { shift->class },
Subroutine __ANON__: not called CC=1 SCAR=6.9
471 4 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
472 4 ▶ use Getopt::Long qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
473 4 ▶ use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
474 use OptArgs2::CmdBase_CI
475 abstract => 1,
476 has => {
477 abbrev => { is => 'rw', },
478 11 ▶ args => { default => sub { [] }, },
Subroutine __ANON__: called CC=1 SCAR=0.0
479 class => { required => 1, },
480 comment => { required => 1, },
481 hidden => {},
482 optargs => {
483 is => 'rw',
484 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
485 },
486 11 ▶ opts => { default => sub { [] }, },
Subroutine __ANON__: called CC=1 SCAR=0.0
487 parent => { weaken => 1, },
488 _opts => {
489 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
490 },
491 _args => {
492 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
493 },
494 _subcmds => {
495 5 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
496 },
497 show_default => { default => 0, },
498 8 ▶ show_color => { default => sub { -t STDERR }, },
Subroutine __ANON__: called CC=1 SCAR=0.0
499 6 ▶ subcmds => { default => sub { [] }, },
Subroutine __ANON__: called CC=1 SCAR=0.0
500 4 _values => { is => 'rw' },
501 },
502 4 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
503  
504 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
505  
506 sub BUILD {
507 11 ▶ my $self = shift;
Subroutine BUILD: called CC=4 SCAR=14.5
Pod undocumented
508  
509 # legacy interface
510 11 ▶ if ( 'CODE' eq ref $self->optargs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $self->optargs) 0 11 11
511 0 local $OptArgs2::CURRENT = $self;
512 0 $self->optargs->();
513 0 return;
514 }
515  
516 11 ▶ while ( my ( $name, $args ) = splice @{ $self->optargs }, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@{$self->optargs;}, 0, 2)) 8 11 19
517 8 ▶ if ( $args->{isa} =~ s/^--// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'isa'} =~ s/^--//) { } 2 6 8
518 2 $self->add_opt(
519 name => $name,
520 %$args,
521 );
522 }
523 else {
524 6 $self->add_arg(
525 name => $name,
526 %$args,
527 );
528 }
529 }
530 }
531  
532 sub add_arg {
533 6 ▶ my $self = shift;
Subroutine add_arg: called CC=1 SCAR=0.0
Pod undocumented
534 6 my $arg = OptArgs2::Arg->new(
535 cmd => $self,
536 show_default => $self->show_default,
537 @_,
538 );
539  
540 6 push( @{ $self->args }, $arg );
541 6 $arg;
542 }
543  
544 sub add_cmd {
545 5 ▶ my $self = shift;
Subroutine add_cmd: called CC=2 SCAR=7.0
Pod undocumented
546 5 my $subcmd = OptArgs2::SubCmd->new(
547 abbrev => $self->abbrev,
548 show_default => $self->show_default,
549 @_,
550 parent => $self,
551 );
552  
553 OptArgs2->throw_error( 'CmdExists', 'cmd exists' )
554 5 ▶ if exists $self->_subcmds->{ $subcmd->name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->_subcmds->{$subcmd->name} 0 5 5
555  
556 5 $self->_subcmds->{ $subcmd->name } = $subcmd;
557 5 push( @{ $self->subcmds }, $subcmd );
558  
559 5 return $subcmd;
560 }
561  
562 sub add_opt {
563 8 ▶ my $self = shift;
Subroutine add_opt: called CC=1 SCAR=0.0
Pod undocumented
564 8 my $opt = OptArgs2::Opt->new_from(
565 show_default => $self->show_default,
566 @_,
567 );
568  
569 8 push( @{ $self->opts }, $opt );
570 8 $opt;
571 }
572  
573 sub parents {
574 23 ▶ my $self = shift;
Subroutine parents: called CC=2 SCAR=6.9
Pod undocumented
575 23 ▶ return unless $self->parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent 19 4 23
576 4 return ( $self->parent->parents, $self->parent );
577 }
578  
579 sub parse {
580 11 ▶ my $self = shift;
Subroutine parse: called CC=48 SCAR=51.7
Pod undocumented
581 11 my $source = \@_;
582  
583 map {
584 11 ▶ OptArgs2->throw_error( 'UndefOptArg',
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_ 0 8 8
585 'optargs argument undefined!' )
586 if !defined $_
587 } @$source;
588  
589 11 my $source_hash = { map { %$_ } grep { ref $_ eq 'HASH' } @$source };
590 11 $source = [ grep { ref $_ ne 'HASH' } @$source ];
591  
592 11 Getopt::Long::Configure(qw/pass_through no_auto_abbrev no_ignore_case/);
593  
594 11 my $reason;
595 11 my $optargs = {};
596 11 my @trigger;
597  
598 11 my $cmd = $self;
599  
600 # Start with the parents options
601 11 my @opts = map { @{ $_->opts } } $cmd->parents, $cmd;
602 11 my @args = @{ $cmd->args };
603  
604 11 ▶ OPTARGS: while ( @opts or @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts or @args) 11 11 22
Condition66%2/3
@opts or @args
l!l&&r!l&&!r
11011
MC/DC50%1/2
@opts or @args
@opts @args
Truth table66%2/3
A or B
A= @optsB= @args
ABresult
1X1
011
000
605 11 ▶ while ( my $try = shift @opts ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $try = shift @opts) 13 11 24
606 13 my $result;
607 13 my $name = $try->name;
608  
609 13 ▶ if ( exists $source_hash->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $source_hash->{$name}) { } 0 13 13
610 0 $result = delete $source_hash->{$name};
611 }
612 else {
613 13 GetOptionsFromArray( $source, $try->getopt => \$result );
614 }
615  
616 13 ▶ if ( defined($result) and my $t = $try->trigger ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $result and my $t = $try->trigger) 0 13 13
Condition33%1/3
defined $result and my $t = $try->trigger
!ll&&!rl&&r
1300
MC/DC0%0/2
defined $result and my $t = $try->trigger
defined $result my $t = $try->trigger
Truth table33%1/3
A and B
A= defined $resultB= my $t = $try->trigger
ABresult
0X0
100
111
617 0 push @trigger, [ $t, $name ];
618 }
619  
620 13 ▶ if ( defined( $result //= $try->default ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($result //= $try->default)) { } 1 12 13
elsif ($try->required) { } 0 12 12
Condition66%2/3
$result //= $try->default
l!l&&r!l&&!r
0112
MC/DC50%1/2
$result //= $try->default
$result $try->default
Truth table66%2/3
A //= B
A= $resultB= $try->default
ABresult
1X1
011
000
621  
622 1 ▶ if ( 'CODE' eq ref $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $result) { } 0 1 1
623 0 tie $optargs->{$name}, 'OptArgs2::CODEREF', $optargs,
624 $name,
625 $result;
626 }
627 else {
628 1 $optargs->{$name} = $result;
629 }
630 }
631 elsif ( $try->required ) {
632 0 $name =~ s/_/-/g;
633 0 ▶ $reason //=
Condition0%0/2
$reason //= ["OptRequired", qq[missing required option "--$name"]]
l!l
00
MC/DC0%0/1
$reason //= ["OptRequired", qq[missing required option "--$name"]]
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
634 [ 'OptRequired', qq{missing required option "--$name"} ];
635 }
636 }
637  
638 # Sub command check
639 11 ▶ if ( @$source and my @subcmds = @{ $cmd->subcmds } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$source and my(@subcmds) = @{$cmd->subcmds;}) 7 4 11
Condition100%3/3
@$source and my(@subcmds) = @{$cmd->subcmds;}
!ll&&!rl&&r
317
MC/DC100%2/2
@$source and my(@subcmds) = @{$cmd->subcmds;}
@$source my(@subcmds) = @{$cmd->subcmds;}
Truth table100%3/3
A and B
A= @$sourceB= my(@subcmds) = @{$cmd->subcmds;}
ABresult
0X0
100
111
640 7 my $result = $source->[0];
641 7 ▶ if ( $cmd->abbrev ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd->abbrev) 2 5 7
642 2 require Text::Abbrev;
643 my %abbrev =
644 2 Text::Abbrev::abbrev( map { $_->name } @subcmds );
645 2 ▶ $result = $abbrev{$result} // $result;
Condition33%1/3
$abbrev{$result} // $result
l!l&&r!l&&!r
200
MC/DC0%0/2
$abbrev{$result} // $result
$abbrev{$result} $result
Truth table33%1/3
A // B
A= $abbrev{$result}B= $result
ABresult
1X1
011
000
646 }
647  
648 7 ▶ if ( exists $cmd->_subcmds->{$result} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cmd->_subcmds->{$result}) 4 3 7
649 4 shift @$source;
650 4 $cmd = $cmd->_subcmds->{$result};
651 4 push( @opts, @{ $cmd->opts } );
652  
653 # Ignoring any remaining arguments
654 4 @args = @{ $cmd->args };
655  
656 4 next OPTARGS;
657 }
658 }
659  
660 7 ▶ while ( my $try = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $try = shift @args) 7 7 14
661 7 my $result;
662 7 my $name = $try->name;
663  
664 7 ▶ if (@$source) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$source) { } 4 3 7
elsif (exists $source_hash->{$name}) { } 0 3 3
665 4 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($source->[0] =~ /^--\S/ or $source->[0] =~ /^-\S/ and not $source->[0] =~ /^-\d/ && ($try->isa ne "Num" || $try->isa ne "Int")) 0 4 4
Condition16%2/12
$source->[0] =~ /^--\S/ or $source->[0] =~ /^-\S/ and not $source->[0] =~ /^-\d/ && ($try->isa ne "Num" || $try->isa ne "Int")
l!l&&r!l&&!r
004
$source->[0] =~ /^-\S/ and not $source->[0] =~ /^-\d/ && ($try->isa ne "Num" || $try->isa ne "Int")
!ll&&!rl&&r
400
$source->[0] =~ /^-\d/ && ($try->isa ne 'Num' || $try->isa ne 'Int')
!ll&&!rl&&r
000
$try->isa ne "Num" or $try->isa ne "Int"
l!l&&r!l&&!r
000
MC/DC0%0/5
$source->[0] =~ /^--\S/ or $source->[0] =~ /^-\S/ and not $source->[0] =~ /^-\d/ && ($try->isa ne "Num" || $try->isa ne "Int")
$source->[0] =~ /^--\S/ $source->[0] =~ /^-\S/ $source->[0] =~ /^-\d/ $try->isa ne "Num" $try->isa ne "Int"
Truth table0%0/6
A or B and not(C && D or E)
A= $source->[0] =~ /^--\S/B= $source->[0] =~ /^-\S/C= $source->[0] =~ /^-\d/D= $try->isa ne "Num"E= $try->isa ne "Int"
ABCDEresult
1XXXX1
010XX1
011001
00XXX0
0111X0
011010
666 # $try->isa ne 'SubCmd'
667 # and (
668 ( $source->[0] =~ m/^--\S/ )
669 or (
670 $source->[0] =~ m/^-\S/
671 and !(
672 $source->[0] =~ m/^-\d/
673 and ( $try->isa ne 'Num'
674 or $try->isa ne 'Int' )
675 )
676 )
677 )
678  
679 # )
680 {
681 0 my $o = shift @$source;
682 0 ▶ $reason //= [ 'OptUnknown', qq{unknown option "$o"} ];
Condition0%0/2
$reason //= ["OptUnknown", qq[unknown option "$o"]]
l!l
00
MC/DC0%0/1
$reason //= ["OptUnknown", qq[unknown option "$o"]]
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
683 0 last OPTARGS;
684 }
685  
686 4 ▶ if ( $try->greedy ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($try->greedy) { } 0 4 4
elsif ($try->isa eq 'ArrayRef') { } 0 4 4
elsif ($try->isa eq 'HashRef') { } 0 4 4
687  
688 # Interesting feature or not? "GREEDY... LATER"
689 # my @later;
690 # if ( @args and @$source > @args ) {
691 # push( @later, pop @$source ) for @args;
692 # }
693 # Should also possibly check early for post-greedy arg,
694 # except they might be wanted for display
695 # purposes
696  
697 0 ▶ if ( $try->isa eq 'ArrayRef' or $try->isa eq 'SubCmd' )
Branch: 2 branches
BranchTrueFalseTotal
if ($try->isa eq 'ArrayRef' or $try->isa eq 'SubCmd') { } 0 0 0
elsif ($try->isa eq 'HashRef') { } 0 0 0
Condition0%0/3
$try->isa eq "ArrayRef" or $try->isa eq "SubCmd"
l!l&&r!l&&!r
000
MC/DC0%0/2
$try->isa eq "ArrayRef" or $try->isa eq "SubCmd"
$try->isa eq "ArrayRef" $try->isa eq "SubCmd"
Truth table0%0/3
A or B
A= $try->isa eq "ArrayRef"B= $try->isa eq "SubCmd"
ABresult
1X1
011
000
698 {
699 0 $result = [@$source];
700 }
701 elsif ( $try->isa eq 'HashRef' ) {
702 $result = {
703 0 map { split /=/, $_ }
704 split /,/, @$source
705 };
706 }
707 else {
708 0 $result = "@$source";
709 }
710  
711 0 $source = [];
712  
713 # $source = \@later;
714 }
715 elsif ( $try->isa eq 'ArrayRef' ) {
716 0 $result = [ shift @$source ];
717 }
718 elsif ( $try->isa eq 'HashRef' ) {
719 $result =
720 0 { map { split /=/, $_ } split /,/, shift @$source };
721 }
722 else {
723 4 $result = shift @$source;
724 }
725  
726 # TODO: type check using Param::Utils?
727 }
728 elsif ( exists $source_hash->{$name} ) {
729 0 $result = delete $source_hash->{$name};
730 }
731  
732 7 ▶ if ( defined( $result //= $try->default ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined($result //= $try->default)) { } 4 3 7
elsif ($try->required) { } 2 1 3
Condition66%2/3
$result //= $try->default
l!l&&r!l&&!r
403
MC/DC50%1/2
$result //= $try->default
$result $try->default
Truth table66%2/3
A //= B
A= $resultB= $try->default
ABresult
1X1
011
000
733 $reason //= [
734 'SubCmdUnknown',
735 "unknown $name: "
736 . (
737 ( 'ARRAY' eq ref $result ) ? $result->[0]
738 : ( 'HASH' eq ref $result ) ? (
739 join ',',
740 4 ▶ map { "$_=$result->{$_}" } keys %$result
Branch: 3 branches
BranchTrueFalseTotal
if $try->isa eq "SubCmd" and not $try->fallthru 2 2 4
'ARRAY' eq ref $result ? : 0 2 2
'HASH' eq ref $result ? : 0 2 2
Condition80%4/5
$try->isa eq "SubCmd" and not $try->fallthru
!ll&&!rl&&r
112
$reason //= ["SubCmdUnknown", "unknown ${name}: " . ("ARRAY" eq ref $result ? $result->[0] : ("HASH" eq ref $result ? join(",", map({"$_=$result->{$_}";} keys %$result)) : $result))]
l!l
02
MC/DC100%2/2
$try->isa eq "SubCmd" and not $try->fallthru
$try->isa eq "SubCmd" not $try->fallthru
MC/DC0%0/1
$reason //= ["SubCmdUnknown", "unknown ${name}: " . ("ARRAY" eq ref $result ? $result->[0] : ("HASH" eq ref $result ? join(",", map({"$_=$result->{$_}";} keys %$result)) : $result))]
$reason
Truth table100%3/3
A and not(B)
A= $try->isa eq "SubCmd"B= not $try->fallthru
ABresult
0X0
100
111
Truth table50%1/2
A
A= $reason
Aresult
11
00
741 )
742 : $result
743 )
744 ]
745 if $try->isa eq 'SubCmd' and not $try->fallthru;
746  
747 4 ▶ if ( 'CODE' eq ref $result ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $result) { } 0 4 4
748 0 tie $optargs->{$name}, 'OptArgs2::CODEREF', $optargs,
749 $name,
750 $result;
751 }
752 else {
753 4 $optargs->{$name} = $result;
754 }
755 }
756 elsif ( $try->required ) {
757 2 ▶ $reason //= ['ArgRequired'];
Condition50%1/2
$reason //= ["ArgRequired"]
l!l
02
MC/DC0%0/1
$reason //= ["ArgRequired"]
$reason
Truth table50%1/2
A
A= $reason
Aresult
11
00
758 }
759 }
760 }
761  
762 11 ▶ if (@$source) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$source) { } 0 11 11
elsif (my(@unexpected) = keys %$source_hash) { } 0 11 11
763 0 ▶ $reason //= [
Condition0%0/2
$reason //= ["UnexpectedOptArg", "unexpected option(s) or argument(s): @$source"]
l!l
00
MC/DC0%0/1
$reason //= ["UnexpectedOptArg", "unexpected option(s) or argument(s): @$source"]
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
764 'UnexpectedOptArg',
765 "unexpected option(s) or argument(s): @$source"
766 ];
767 }
768 elsif ( my @unexpected = keys %$source_hash ) {
769 0 ▶ $reason //= [
Condition0%0/2
$reason //= ["UnexpectedHashOptArg", "unexpected HASH option(s) or argument(s): @unexpected"]
l!l
00
MC/DC0%0/1
$reason //= ["UnexpectedHashOptArg", "unexpected HASH option(s) or argument(s): @unexpected"]
$reason
Truth table0%0/2
A
A= $reason
Aresult
11
00
770 'UnexpectedHashOptArg',
771 "unexpected HASH option(s) or argument(s): @unexpected"
772 ];
773 }
774  
775 11 $cmd->_values($optargs);
776  
777 11 map { $_->[0]->( $cmd, $optargs->{ $_->[1] } ) } @trigger;
778  
779 11 ▶ OptArgs2->throw_usage( $reason->[0],
Branch: 1 branch
BranchTrueFalseTotal
if $reason 4 7 11
780 $cmd->usage_string( OptArgs2::STYLE_USAGE, $reason->[1] ) )
781 if $reason;
782  
783 7 return ( $cmd->class, $optargs, ( $cmd->class . '.pm' ) =~ s!::!/!gr );
784 }
785  
786 sub _usage_tree {
787 0 ▶ my $self = shift;
Subroutine _usage_tree: not called CC=2 SCAR=17.9
788 0 ▶ my $depth = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
789  
790 return [
791 $depth, $self->usage_string(OptArgs2::STYLE_HELPSUMMARY),
792 $self->comment
793 ],
794 0 map { $_->_usage_tree( $depth + 1 ) }
795 0 sort { $a->name cmp $b->name } @{ $self->subcmds };
796 }
797  
798 sub usage_string {
799 8 ▶ my $self = shift;
Subroutine usage_string: called CC=41 SCAR=50.5
Pod undocumented
800 8 ▶ my $style = shift || OptArgs2::STYLE_USAGE;
Condition50%1/2
shift() || 'Usage'
l!l
80
MC/DC0%0/1
shift() || 'Usage'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
801 8 ▶ my $error = shift // '';
Condition100%2/2
shift() // ""
l!l
26
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
802 8 my $usage = '';
803  
804 8 ▶ if ( $style eq OptArgs2::STYLE_HELPTREE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "HelpTree") 0 8 8
805 0 my ( @w1, @w2 );
806 my @items = map {
807 0 $_->[0] = ' ' x ( $_->[0] * 3 );
808 0 push @w1, length( $_->[1] ) + length( $_->[0] );
809 0 push @w2, length $_->[2];
810 0 $_
811 } $self->_usage_tree;
812 0 my ( $w1, $w2 ) = ( max(@w1), max(@w2) );
813  
814 0 my $paged = OptArgs2::rows() < scalar @items;
815 0 my $cols = OptArgs2::cols();
816 0 my $usage = '';
817 0 my $spacew = 3;
818 0 my $space = ' ' x $spacew;
819  
820 0 foreach my $i ( 0 .. $#items ) {
821 0 my $overlap = $w1 + $spacew + $w2[$i] - $cols;
822 0 ▶ if ( $overlap > 0 and not $paged ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($overlap > 0 and not $paged) 0 0 0
Condition0%0/3
$overlap > 0 and not $paged
!ll&&!rl&&r
000
MC/DC0%0/2
$overlap > 0 and not $paged
$overlap > 0 not $paged
Truth table0%0/3
A and not(B)
A= $overlap > 0B= not $paged
ABresult
0X0
100
111
823 0 $items[$i]->[2] =
824 sprintf '%-.' . ( $w2[$i] - $overlap - 3 ) . 's%s',
825 $items[$i]->[2], '.' x 3;
826 }
827 0 $usage .= sprintf "%-${w1}s${space}%-s\n",
828 $items[$i]->[0] . $items[$i]->[1],
829 $items[$i]->[2];
830 }
831 0 return $usage;
832 }
833  
834 8 my @parents = $self->parents;
835 8 my @args = @{ $self->args };
836 my @opts =
837 8 sort { $a->name cmp $b->name } map { @{ $_->opts } } @parents,
838 $self;
839  
840 8 my $optargs = $self->_values;
841  
842 # Summary line
843 8 ▶ $usage .= join( ' ', map { $_->name } @parents ) . ' '
Branch: 1 branch
BranchTrueFalseTotal
if @parents and $style ne "HelpSummary" 0 8 8
Condition66%2/3
@parents and $style ne "HelpSummary"
!ll&&!rl&&r
440
MC/DC0%0/2
@parents and $style ne "HelpSummary"
@parents $style ne "HelpSummary"
Truth table66%2/3
A and B
A= @parentsB= $style ne "HelpSummary"
ABresult
0X0
100
111
844 if @parents and $style ne OptArgs2::STYLE_HELPSUMMARY;
845 8 $usage .= $self->name;
846  
847 8 my ( $red, $grey, $reset ) = ( '', '', '' );
848 8 ▶ if ( $self->show_color ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_color) 0 8 8
849 0 $red = "\e[0;31m";
850 0 $grey = "\e[1;30m";
851 0 $reset = "\e[0m";
852  
853 # $red = "\e[0;31m";
854 # $yellow = "\e[0;33m";
855 }
856  
857 8 ▶ $error = $red . 'error:' . $reset . ' ' . $error . "\n\n"
Branch: 1 branch
BranchTrueFalseTotal
if length $error 2 6 8
858 if length $error;
859  
860 8 foreach my $arg (@args) {
861 4 $usage .= ' ';
862 4 ▶ $usage .= '[' unless $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->required 1 3 4
863 4 $usage .= uc $arg->name;
864 4 ▶ $usage .= '...' if $arg->greedy;
Branch: 1 branch
BranchTrueFalseTotal
if $arg->greedy 0 4 4
865 4 ▶ $usage .= ']' unless $arg->required;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->required 1 3 4
866 }
867  
868 8 ▶ return $usage if $style eq OptArgs2::STYLE_HELPSUMMARY;
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "HelpSummary" 4 4 8
869  
870 4 ▶ $usage .= ' [OPTIONS...]' if @opts;
Branch: 1 branch
BranchTrueFalseTotal
if @opts 4 0 4
871 4 $usage .= "\n";
872  
873 # Synopsis
874 4 ▶ $usage .= "\n Synopsis:\n " . $self->comment . "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "Help" and length $self->comment 0 4 4
Condition33%1/3
$style eq "Help" and length $self->comment
!ll&&!rl&&r
400
MC/DC0%0/2
$style eq "Help" and length $self->comment
$style eq "Help" length $self->comment
Truth table33%1/3
A and B
A= $style eq "Help"B= length $self->comment
ABresult
0X0
100
111
875 if $style eq OptArgs2::STYLE_HELP and length $self->comment;
876  
877 # Build arguments
878 4 my @sargs;
879 my @uargs;
880 4 my $have_subcmd;
881  
882 4 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 4 0 4
883 4 my $i = 0;
884 4 ARG: foreach my $arg (@args) {
885 4 ▶ if ( $arg->isa eq 'SubCmd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->isa eq 'SubCmd') { } 4 0 4
886 my ( $n, undef, undef, $c ) =
887 $arg->name_alias_type_comment(
888 $arg->show_default
889 4 ▶ ? eval { $optargs->{ $arg->name } // undef }
Branch: 1 branch
BranchTrueFalseTotal
$arg->show_default ? : 0 4 4
Condition0%0/2
$$optargs{$arg->name} // undef
l!l
00
MC/DC0%0/1
$$optargs{$arg->name} // undef
$$optargs{$arg->name}
Truth table0%0/2
A
A= $$optargs{$arg->name}
Aresult
11
00
890 : ()
891 );
892 4 push( @sargs, [ ' ' . ucfirst($n) . ':', $c ] );
893 my @sorted_subs =
894 4 map { $_->[1] }
895 0 sort { $a->[0] cmp $b->[0] }
896 4 map { [ $_->name, $_ ] }
897 4 ▶ grep { $style eq OptArgs2::STYLE_HELP or !$_->hidden }
Branch: 1 branch
BranchTrueFalseTotal
$style eq "Help" or not $_->hidden 4 0 4
898 4 @{ $arg->cmd->subcmds };
899  
900 4 foreach my $subcmd (@sorted_subs) {
901 4 push(
902 @sargs,
903 [
904 ' '
905 . $subcmd->usage_string(
906 OptArgs2::STYLE_HELPSUMMARY),
907 $subcmd->comment
908 ]
909 );
910 }
911  
912 4 $have_subcmd++;
913 4 last ARG;
914 }
915 else {
916 0 ▶ push( @uargs, [ ' Arguments:', '', '', '' ] ) if !$i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i 0 0 0
917 my ( $n, $a, $t, $c ) = $arg->name_alias_type_comment(
918 $arg->show_default
919 0 ▶ ? eval { $optargs->{ $arg->name } // undef }
Branch: 1 branch
BranchTrueFalseTotal
$arg->show_default ? : 0 0 0
Condition0%0/2
$$optargs{$arg->name} // undef
l!l
00
MC/DC0%0/1
$$optargs{$arg->name} // undef
$$optargs{$arg->name}
Truth table0%0/2
A
A= $$optargs{$arg->name}
Aresult
11
00
920 : ()
921 );
922 0 push( @uargs, [ ' ' . uc($n), $a, $t, $c ] );
923 }
924 0 $i++;
925 }
926 }
927  
928 # Build options
929 4 my @uopts;
930 4 ▶ if (@opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@opts) 4 0 4
931 4 push( @uopts, [ " Options:", '', '', '' ] );
932 4 foreach my $opt (@opts) {
933 4 ▶ next if $style ne OptArgs2::STYLE_HELP and $opt->hidden;
Branch: 1 branch
BranchTrueFalseTotal
if $style ne "Help" and $opt->hidden 0 4 4
Condition33%1/3
$style ne "Help" and $opt->hidden
!ll&&!rl&&r
040
MC/DC0%0/2
$style ne "Help" and $opt->hidden
$style ne "Help" $opt->hidden
Truth table33%1/3
A and B
A= $style ne "Help"B= $opt->hidden
ABresult
0X0
100
111
934 my ( $n, $a, $t, $c ) = $opt->name_alias_type_comment(
935 $opt->show_default
936 4 ▶ ? eval { $optargs->{ $opt->name } // undef }
Branch: 1 branch
BranchTrueFalseTotal
$opt->show_default ? : 0 4 4
Condition0%0/2
$$optargs{$opt->name} // undef
l!l
00
MC/DC0%0/1
$$optargs{$opt->name} // undef
$$optargs{$opt->name}
Truth table0%0/2
A
A= $$optargs{$opt->name}
Aresult
11
00
937 : ()
938 );
939 4 push( @uopts, [ ' ' . $n, $a, $t, $c ] );
940 }
941 }
942  
943 # Width calculation for args and opts combined
944 4 my $w1 = max( 0, map { length $_->[0] } @uargs, @uopts );
945 4 my $w2 = max( 0, map { length $_->[1] } @uargs, @uopts );
946 4 my $w3 = max( 0, map { length $_->[2] } @uargs, @uopts );
947 4 my $w4 = max( 0, map { length $_->[0] } @sargs );
948 4 my $w5 = max( $w1 + $w2 + $w3, $w4 );
949  
950 4 my $format1 = "%-${w5}s %s\n";
951 4 my $format2 = "%-${w1}s %-${w2}s %-${w3}s";
952  
953 # Output Arguments
954 4 ▶ if (@sargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sargs) 4 0 4
955 4 $usage .= "\n";
956 4 foreach my $row (@sargs) {
957 8 $usage .= sprintf( $format1, @$row ) =~
958 s/^(\s+\w+\s)(.*?)(\s\s)/$1$grey$2$reset$3/r;
959 }
960 }
961  
962 4 ▶ if (@uargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@uargs) 0 4 4
963 0 $usage .= "\n";
964 0 foreach my $row (@uargs) {
965 0 my $l = pop @$row;
966 0 $usage .= sprintf( $format1, sprintf( $format2, @$row ), $l );
967 }
968 }
969  
970 # Output Options
971 4 ▶ if (@uopts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@uopts) 4 0 4
972 4 $usage .= "\n";
973 4 foreach my $row (@uopts) {
974 8 my $l = pop @$row;
975 8 $usage .= sprintf( $format1, sprintf( $format2, @$row ), $l );
976 }
977 }
978  
979 4 return $error . 'usage: ' . $usage . "\n";
980 }
981  
982 }
983  
984 4 package OptArgs2::Cmd {
985 use OptArgs2::Cmd_CI
986 isa => 'OptArgs2::CmdBase',
987 has => {
988 name => {
989 default => sub {
990 4 ▶ my $x = $_[0]->class;
Subroutine __ANON__: called CC=2 SCAR=7.6
991  
992 # once legacy code goes move this into optargs()
993 4 ▶ if ( $x eq 'main' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x eq 'main') { } 0 4 4
994 0 require File::Basename;
995 0 File::Basename::basename($0),;
996 }
997 else {
998 4 $x =~ s/.*://;
999 4 $x =~ s/_/-/g;
1000 4 $x;
1001 }
1002 },
1003 },
1004 4 no_help => { default => 0 },
1005 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
1006  
1007 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
1008  
1009 sub BUILD {
1010 6 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=13.1
Pod undocumented
1011  
1012 6 ▶ $self->add_opt(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_help or "CODE" eq ref $self->optargs 6 0 6
Condition33%1/3
$self->no_help or "CODE" eq ref $self->optargs
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->no_help or "CODE" eq ref $self->optargs
$self->no_help "CODE" eq ref $self->optargs
Truth table33%1/3
A or B
A= $self->no_helpB= "CODE" eq ref $self->optargs
ABresult
1X1
011
000
1013 isa => OptArgs2::STYLE_HELP,
1014 show_default => 0,
1015 )
1016 unless $self->no_help
1017 or 'CODE' eq ref $self->optargs; # legacy interface
1018 }
1019 }
1020  
1021 4 package OptArgs2::SubCmd {
1022 use OptArgs2::SubCmd_CI
1023 isa => 'OptArgs2::CmdBase',
1024 has => {
1025 name => { # once legacy code goes move this into CmdBase
1026 init_arg => undef,
1027 default => sub {
1028 5 ▶ my $x = $_[0]->class;
Subroutine __ANON__: called CC=1 SCAR=0.0
1029 5 $x =~ s/.*://;
1030 5 $x =~ s/_/-/g;
1031 5 $x;
1032 },
1033 },
1034 4 parent => { required => 1, },
1035 4 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
1036  
1037 4 our @CARP_NOT = @OptArgs2::CARP_NOT;
1038 }
1039  
1040 4 1;
1041  
1042 __END__
↑ Summary OptArgs2/Pager.pm »