Alt.pm

filter: Statementstmt 87.5% 266 / 304 Branchbran 74.6% 118 / 158 Conditioncond 54.1% 84 / 155 MC/DCmcdc 32.5% 28 / 86 Subroutinesub 100% 32 / 32 Podpod 100% 6 / 6 totaltotal 72.0% 534 / 741 CC 139 SCAR 39.4
CC
139
Cov
76%
process
135.4
__ANON__
82.8
best_option
24.0
CRAP 51.6
↑ Summary Alt/Option.pm »
1 package Getopt::Alt;
2  
3 # Created on: 2009-07-17 07:40:56
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 7 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Carp qw/longmess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Getopt::Alt::Option qw/build_option/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Getopt::Alt::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 7 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ '@{}' => sub { $_[0]->files },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 6 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 7 );
26  
27 7 Moose::Exporter->setup_import_methods( as_is => [qw/get_options/], );
28  
29 7 our $VERSION = version->new('0.5.5');
30 7 our $EXIT = 1;
31  
32 7 has options => (
33 is => 'rw',
34 isa => 'Str',
35 default => 'Getopt::Alt::Dynamic',
36 );
37 7 has opt => (
38 is => 'rw',
39 isa => 'Getopt::Alt::Dynamic',
40 clearer => 'clear_opt',
41 );
42 has default => (
43 is => 'rw',
44 isa => 'HashRef',
45 24 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 7 );
47 has files => (
48 is => 'rw',
49 isa => 'ArrayRef[Str]',
50 34 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 7 );
52 7 has bundle => (
53 is => 'rw',
54 isa => 'Bool',
55 default => 1,
56 );
57 7 has ignore_case => (
58 is => 'rw',
59 isa => 'Bool',
60 default => 1,
61 );
62 7 has help_package => (
63 is => 'rw',
64 isa => 'Str',
65 );
66 7 has help_packages => (
67 is => 'rw',
68 isa => 'HashRef[Str]',
69 );
70 7 has help_pre => (
71 is => 'rw',
72 isa => 'CodeRef',
73 );
74 7 has help_post => (
75 is => 'rw',
76 isa => 'CodeRef',
77 );
78 7 has helper => (
79 is => 'rw',
80 isa => 'Bool',
81 );
82 7 has cmd => (
83 is => 'rw',
84 isa => 'Str',
85 clearer => 'clear_cmd',
86 documentation => 'The found sub-command',
87 );
88 7 has sub_command => (
89 is => 'rw',
90 predicate => 'has_sub_command',
91 documentation => <<'DOC',
92 if true (== 1) processing of args stops at first non-defined parameter, if
93 a HASH ref the keys are assumed to be the allowed sub commands and the values
94 are assumed to be parameters to passed to get_options where the generated
95 options will be a sub object of generated options object. Finally if this
96 is a sub ref it will be called with self and the rest of ARGV
97 DOC
98 );
99 has aliases => (
100 is => 'rw',
101 isa => 'HashRef[ArrayRef]',
102 34 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 7 documentation => 'Stores the list of aliases sub-commands can have',
104 );
105 7 has default_sub_command => (
106 is => 'rw',
107 isa => 'Str',
108 predicate => 'has_default_sub_command',
109 );
110 7 has auto_complete => (
111 is => 'rw',
112 isa => 'CodeRef',
113 predicate => 'has_auto_complete',
114 );
115 7 has auto_complete_shortener => (
116 is => 'rw',
117 isa => 'CodeRef',
118 predicate => 'has_auto_complete_shortener',
119 );
120 has name => (
121 is => 'rw',
122 isa => 'Str',
123 33 ▶ default => sub { path($0)->basename },
Subroutine __ANON__: called CC=1 SCAR=0.0
124 7 );
125 7 has config => (
126 is => 'rw',
127 isa => 'HashRef',
128 predicate => 'has_config',
129 );
130 7 has conf_prefix => (
131 is => 'rw',
132 isa => 'Str',
133 default => '.',
134 );
135 7 has conf_section => (
136 is => 'rw',
137 isa => 'Str',
138 predicate => 'has_conf_section',
139 );
140  
141 7 my $count = 1;
142 35 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=9 SCAR=22.5
143 35 my ( $orig, $class, @params ) = @_;
144 35 my %param;
145  
146 35 ▶ if ( ref $params[0] eq 'HASH' && ref $params[1] eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH' and ref $params[1] eq 'ARRAY') { } 24 11 35
elsif (not %param and ref $params[0] eq 'HASH') { } 0 11 11
Condition66%2/3
ref $params[0] eq "HASH" and ref $params[1] eq "ARRAY"
!ll&&!rl&&r
11024
MC/DC50%1/2
ref $params[0] eq "HASH" and ref $params[1] eq "ARRAY"
ref $params[0] eq "HASH" ref $params[1] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $params[0] eq "HASH"B= ref $params[1] eq "ARRAY"
ABresult
0X0
100
111
147 24 %param = %{ $params[0] };
148 24 @params = @{ $params[1] };
149 }
150 ▶ elsif ( !%param && ref $params[0] eq 'HASH' ) {
Condition33%1/3
not %param and ref $params[0] eq "HASH"
!ll&&!rl&&r
0110
MC/DC0%0/2
not %param and ref $params[0] eq "HASH"
not %param ref $params[0] eq "HASH"
Truth table33%1/3
not(A) and B
A= not %paramB= ref $params[0] eq "HASH"
ABresult
0X0
100
111
151 0 %param = %{ shift @params };
152 }
153  
154 35 ▶ if ( !exists $param{helper} || $param{helper} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $param{"helper"} or $param{"helper"}) 27 8 35
Condition100%3/3
not exists $param{"helper"} or $param{"helper"}
l!l&&r!l&&!r
2618
MC/DC100%2/2
not exists $param{"helper"} or $param{"helper"}
not exists $param{"helper"} $param{"helper"}
Truth table100%3/3
not(A) or B
A= not exists $param{"helper"}B= $param{"helper"}
ABresult
1X1
011
000
155 27 unshift @params,
156 (
157 'help', 'man', 'version',
158 'auto_complete|auto-complete=i',
159 'auto_complete_list|auto-complete-list!',
160 );
161 }
162  
163 35 ▶ if (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 34 1 35
164 $param{options} =
165 34 ▶ _build_option_class( $param{options} || 'Getopt::Alt::Dynamic',
Condition100%2/2
$param{'options'} || 'Getopt::Alt::Dynamic'
l!l
331
MC/DC100%1/1
$param{'options'} || 'Getopt::Alt::Dynamic'
$param{'options'}
Truth table100%2/2
A
A= $param{'options'}
Aresult
11
00
166 @params, );
167  
168 33 if ( 0 && $param{sub_command} && ref $param{sub_command} eq 'HASH' ) {
169  
170 # build up all the sub command options
171 for my $sub ( keys %{ $param{sub_command} } ) {
172 $param{sub_command}{$sub} =
173 _build_option_class( $param{options},
174 $param{sub_command}{$sub},
175 );
176 }
177 }
178 }
179  
180 34 return $class->$orig(%param);
181 7 };
182  
183 34 ▶sub _build_option_class {
Subroutine _build_option_class: called CC=2 SCAR=6.9
184 34 my ( $base_class, @params ) = @_;
185  
186 # construct a class of options passing
187 34 my $class_name = 'Getopt::Alt::Dynamic::A' . $count++;
188 34 my $option_class =
189 Moose::Meta::Class->create( $class_name, superclasses => [$base_class], );
190  
191 34 ▶ while ( my $option = shift @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = shift @params) 203 33 236
192 203 build_option( $option_class, $option );
193 }
194  
195 33 return $class_name;
196 }
197  
198 34 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod documented
199 34 my ($self) = @_;
200  
201 34 my $basename = $self->name;
202 34 my $prefix = $self->conf_prefix;
203 34 ▶ my $conf = eval {
Condition50%1/2
eval { do { 'Config::Any'->load_stems({'stems', ["$prefix$basename", 'File::HomeDir'->my_home . "/$prefix$basename", "/etc/$basename"], 'use_ext', 1}) } } || []
l!l
340
MC/DC0%0/1
eval { do { 'Config::Any'->load_stems({'stems', ["$prefix$basename", 'File::HomeDir'->my_home . "/$prefix$basename", "/etc/$basename"], 'use_ext', 1}) } } || []
eval { do { 'Config::Any'->load_stems({'stems', ["$prefix$basename", 'File::HomeDir'->my_home . "/$prefix$basename", "/etc/$basename"], 'use_ext', 1}) } }
Truth table50%1/2
A
A= eval { do { 'Config::Any'->load_stems({'stems', ["$prefix$basename", 'File::HomeDir'->my_home . "/$prefix$basename", "/etc/$basename"], 'use_ext', 1}) } }
Aresult
11
00
204 34 Config::Any->load_stems(
205 {
206 stems => [
207 "$prefix$basename",
208 File::HomeDir->my_home . "/$prefix$basename",
209 "/etc/$basename",
210 ],
211 use_ext => 1,
212 }
213 );
214 } || [];
215  
216 $conf = {
217 2 map { %$_ }
218 2 map { values %$_ }
219 34 reverse @{$conf}
220 };
221  
222 # perlcritic is confused here combining hashes is not the same as comma separated arguments
223 34 $self->default( { %{ $self->default }, %$conf, } ); ## no critic
224 34 $self->config($conf);
225  
226 34 ▶ if ( $conf->{aliases} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"aliases"}) 1 33 34
227 1 for my $alias ( keys %{ $conf->{aliases} } ) {
228 $self->aliases->{$alias} =
229 1 [ split /\s+/xms, $conf->{aliases}{$alias} ];
230 }
231 }
232  
233 34 return;
234 }
235  
236 15 ▶sub get_options {
Subroutine get_options: called CC=6 SCAR=18.4
Pod documented
237 15 my @args = @_;
238 15 my $caller = caller;
239  
240 15 ▶ if ( @args > 2 && ref $args[0] eq 'HASH' && ref $args[1] ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args > 2 and ref $args[0] eq "HASH" and ref $args[1] ne "ARRAY") 3 12 15
Condition83%5/6
@args > 2 and ref $args[0] eq "HASH" and ref $args[1] ne "ARRAY"
!ll&&!rl&&r
1203
@args > 2 and ref $args[0] eq "HASH"
!ll&&!rl&&r
1113
MC/DC66%2/3
@args > 2 and ref $args[0] eq "HASH" and ref $args[1] ne "ARRAY"
@args > 2 ref $args[0] eq "HASH" ref $args[1] ne "ARRAY"
Truth table75%3/4
A and B and C
A= @args > 2B= ref $args[0] eq "HASH"C= ref $args[1] ne "ARRAY"
ABCresult
0XX0
10X0
1100
1111
241 3 my $options = shift @args;
242 3 @args = ( { default => $options }, [@args] );
243 }
244  
245 15 my $self;
246 15 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=12.5
247 15 $self = __PACKAGE__->new(@args);
248  
249 14 ▶ $self->help_package($caller)
Branch: 1 branch
BranchTrueFalseTotal
if not $self->help_package or $self->help_package eq __PACKAGE__ 14 0 14
Condition33%1/3
not $self->help_package or $self->help_package eq __PACKAGE__
l!l&&r!l&&!r
1400
MC/DC0%0/2
not $self->help_package or $self->help_package eq __PACKAGE__
not $self->help_package $self->help_package eq __PACKAGE__
Truth table33%1/3
not(A) or B
A= not $self->help_packageB= $self->help_package eq __PACKAGE__
ABresult
1X1
011
000
250 if !$self->help_package || $self->help_package eq __PACKAGE__;
251  
252 14 $self->process();
253 }
254 7 ▶ catch {
Subroutine __ANON__: called CC=6 SCAR=18.5
255 7 ▶ if ( ref $_ && ref $_ eq 'Getopt::Alt::Exception' && $_->help ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help) 3 4 7
Condition83%5/6
ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help
!ll&&!rl&&r
133
ref $_ and ref $_ eq "Getopt::Alt::Exception"
!ll&&!rl&&r
016
MC/DC66%2/3
ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help
ref $_ ref $_ eq "Getopt::Alt::Exception" $_->help
Truth table75%3/4
A and B and C
A= ref $_B= ref $_ eq "Getopt::Alt::Exception"C= $_->help
ABCresult
0XX0
10X0
1100
1111
256 3 die $_;
257 }
258  
259 4 warn $_;
260 4 $self = __PACKAGE__->new();
261  
262 4 ▶ $self->help_package($caller)
Branch: 1 branch
BranchTrueFalseTotal
if not $self->help_package or $self->help_package eq __PACKAGE__ 4 0 4
Condition33%1/3
not $self->help_package or $self->help_package eq __PACKAGE__
l!l&&r!l&&!r
400
MC/DC0%0/2
not $self->help_package or $self->help_package eq __PACKAGE__
not $self->help_package $self->help_package eq __PACKAGE__
Truth table33%1/3
not(A) or B
A= not $self->help_packageB= $self->help_package eq __PACKAGE__
ABresult
1X1
011
000
263 if !$self->help_package || $self->help_package eq __PACKAGE__;
264  
265 4 $self->_show_help(1);
266 15 };
267  
268 8 ▶ return if !defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 8 8
269  
270 8 ▶ return wantarray ? ( $self->opt, $self->cmd, $self ) : $self->opt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
271 }
272  
273 87 ▶sub process {
Subroutine process: called CC=42 SCAR=49.1
Pod documented
274 87 my ( $self, @args ) = @_;
275 87 my $passed_args = scalar @args;
276 87 ▶ @args = $passed_args ? @args : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$passed_args ? : 71 16 87
277 87 $self->clear_opt;
278 87 $self->clear_cmd;
279 87 $self->files( [] );
280  
281 87 my @args_orig = @args;
282 87 my $class = $self->options;
283 87 $self->opt( $class->new( %{ $self->default } ) );
284 87 my @errors;
285  
286 ARG:
287 87 ▶ while ( my $arg = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @args) 94 73 167
288 94 my $action = '';
289 94 ▶ try {
Subroutine __ANON__: called CC=23 SCAR=44.2
290 94 my ( $long, $short, $arg_data );
291 94 ▶ if ( $arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/xms ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/msx) { } 33 61 94
elsif ($arg =~ /^- (\w) =? (.*)/msx) { } 55 6 61
elsif ($arg eq '--') { } 0 6 6
292 33 $long = $1;
293 33 $arg_data = $2;
294 }
295 elsif ( $arg =~ /^- (\w) =? (.*)/xms ) {
296 55 $short = $1;
297 55 $arg_data = $2;
298 }
299 elsif ( $arg eq '--' ) {
300 0 ▶ if ( $self->auto_complete
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_complete and $self->opt->auto_complete and path($0)->basename eq path($args[0])->basename) 0 0 0
Condition0%0/6
$self->auto_complete and $self->opt->auto_complete and path($0)->basename eq path($args[0])->basename
!ll&&!rl&&r
000
$self->auto_complete and $self->opt->auto_complete
!ll&&!rl&&r
000
MC/DC0%0/3
$self->auto_complete and $self->opt->auto_complete and path($0)->basename eq path($args[0])->basename
$self->auto_complete $self->opt->auto_complete path($0)->basename eq path($args[0])->basename
Truth table0%0/4
A and B and C
A= $self->auto_completeB= $self->opt->auto_completeC= path($0)->basename eq path($args[0])->basename
ABCresult
0XX0
10X0
1100
1111
301 && $self->opt->auto_complete
302 && path($0)->basename eq path( $args[0] )->basename )
303 {
304 0 shift @args;
305 }
306  
307 0 ▶ if ( $self->opt->auto_complete
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt->auto_complete and $self->sub_command and $self->has_auto_complete_shortener) 0 0 0
Condition0%0/6
$self->opt->auto_complete and $self->sub_command and $self->has_auto_complete_shortener
!ll&&!rl&&r
000
$self->opt->auto_complete and $self->sub_command
!ll&&!rl&&r
000
MC/DC0%0/3
$self->opt->auto_complete and $self->sub_command and $self->has_auto_complete_shortener
$self->opt->auto_complete $self->sub_command $self->has_auto_complete_shortener
Truth table0%0/4
A and B and C
A= $self->opt->auto_completeB= $self->sub_commandC= $self->has_auto_complete_shortener
ABCresult
0XX0
10X0
1100
1111
308 && $self->sub_command
309 && $self->has_auto_complete_shortener )
310 {
311 0 @args = $self->auto_complete_shortener->( $self, @args );
312 }
313 0 push @{ $self->files }, @args;
314 0 die "last\n";
315 }
316 else {
317 6 push @{ $self->files }, $arg;
318 6 ▶ die $self->sub_command ? "last\n" : "next\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->sub_command ? : 4 2 6
319 }
320  
321 88 my ( $opt, $new_value ) = $self->best_option( $long, $short );
322 83 ▶ if ( defined $new_value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 3 80 83
323 3 $long = $opt->name;
324 3 $short = undef;
325 3 ($arg_data) = $arg =~ /^--?(\d+)$/;
326 }
327 83 $opt->value( $self->opt->{ $opt->name } );
328  
329 83 my ( $value, $used ) =
330 $opt->process( $long, $short, $arg_data, \@args );
331 78 my $opt_name = $opt->name;
332 78 ▶ if ( $self->opt->auto_complete
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt->auto_complete and $opt_name eq "auto_complete_list") 0 78 78
Condition33%1/3
$self->opt->auto_complete and $opt_name eq "auto_complete_list"
!ll&&!rl&&r
7800
MC/DC0%0/2
$self->opt->auto_complete and $opt_name eq "auto_complete_list"
$self->opt->auto_complete $opt_name eq "auto_complete_list"
Truth table33%1/3
A and B
A= $self->opt->auto_completeB= $opt_name eq "auto_complete_list"
ABresult
0X0
100
111
333 && $opt_name eq 'auto_complete_list' )
334 {
335 0 print join ' ', $self->list_options;
336 0 ▶ $EXIT ? exit 0 : return;
Branch: 1 branch
BranchTrueFalseTotal
$Getopt::Alt::EXIT ? : 0 0 0
337 }
338 78 $self->opt->{ $opt->name } = $value;
339  
340 78 ▶ if ( !$used && $short && defined $arg_data && length $arg_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $used and $short and defined $arg_data and length $arg_data) 2 76 78
Condition88%8/9
not $used and $short and defined $arg_data and length $arg_data
!ll&&!rl&&r
6882
not $used and $short and defined $arg_data
!ll&&!rl&&r
68010
not $used and $short
!ll&&!rl&&r
581010
MC/DC75%3/4
not $used and $short and defined $arg_data and length $arg_data
not $used $short defined $arg_data length $arg_data
Truth table80%4/5
not(A) and B and C and D
A= not $usedB= $shortC= defined $arg_dataD= length $arg_data
ABCDresult
0XXX0
10XX0
110X0
11100
11111
341 2 unshift @args, '-' . $arg_data;
342 }
343 78 ▶ if ( $self->has_conf_section
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_conf_section and $self->conf_section and $self->conf_section eq $opt_name and @args_orig) 0 78 78
Condition33%3/9
$self->has_conf_section and $self->conf_section and $self->conf_section eq $opt_name and @args_orig
!ll&&!rl&&r
7800
$self->has_conf_section and $self->conf_section and $self->conf_section eq $opt_name
!ll&&!rl&&r
7800
$self->has_conf_section and $self->conf_section
!ll&&!rl&&r
7800
MC/DC0%0/4
$self->has_conf_section and $self->conf_section and $self->conf_section eq $opt_name and @args_orig
$self->has_conf_section $self->conf_section $self->conf_section eq $opt_name @args_orig
Truth table20%1/5
A and B and C and D
A= $self->has_conf_sectionB= $self->conf_sectionC= $self->conf_section eq $opt_nameD= @args_orig
ABCDresult
0XXX0
10XX0
110X0
11100
11111
344 && $self->conf_section
345 && $self->conf_section eq $opt_name
346 && @args_orig )
347 {
348 $self->opt(
349 $class->new(
350 0 %{ $self->default },
351 0 %{ $self->config->{ $self->conf_section }{$value} },
352 )
353 );
354  
355 # restart the process
356 0 @args = @args_orig;
357 0 @args_orig = ();
358 }
359 }
360 16 ▶ catch {
Subroutine __ANON__: called CC=8 SCAR=24.4
361 16 ▶ if ( $_ eq "next\n" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq "next\n") { } 2 14 16
elsif ($_ eq "last\n") { } 4 10 14
362 2 $action = 'next';
363 }
364 elsif ( $_ eq "last\n" ) {
365  
366 # last means we have found a sub command we should see if it is an alias
367 4 ▶ if ( $self->aliases->{$arg} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->aliases->{$arg}) 0 4 4
368 0 $self->files->[-1] = shift @{ $self->aliases->{$arg} };
369 0 my @new_args = @{ $self->aliases->{$arg} };
370 0 unshift @args, @new_args;
371 }
372  
373 4 $action = 'last';
374 }
375 else {
376 10 ▶ $_ = $_->[0] if ref $_ eq 'ARRAY' && @$_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "ARRAY" and @$_ == 1 7 3 10
Condition66%2/3
ref $_ eq "ARRAY" and @$_ == 1
!ll&&!rl&&r
307
MC/DC50%1/2
ref $_ eq "ARRAY" and @$_ == 1
ref $_ eq "ARRAY" @$_ == 1
Truth table66%2/3
A and B
A= ref $_ eq "ARRAY"B= @$_ == 1
ABresult
0X0
100
111
377  
378 10 ▶ if ( $self->has_auto_complete && $self->opt->auto_complete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_auto_complete and $self->opt->auto_complete) { } 0 10 10
Condition33%1/3
$self->has_auto_complete and $self->opt->auto_complete
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->has_auto_complete and $self->opt->auto_complete
$self->has_auto_complete $self->opt->auto_complete
Truth table33%1/3
A and B
A= $self->has_auto_completeB= $self->opt->auto_complete
ABresult
0X0
100
111
379 0 push @errors, $_;
380 }
381 else {
382 10 die $_;
383 }
384 }
385 94 };
386 84 ▶ next if $action eq 'next';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "next" 2 82 84
387 82 ▶ last if $action eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "last" 4 78 82
388 }
389  
390 77 ▶ if ( $self->has_sub_command ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sub_command) 4 73 77
391 0 shift @{ $self->files }
392 4 ▶ if @{ $self->files } && $self->files->[0] eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->files;} and $self->files->[0] eq "--" 0 4 4
Condition33%1/3
@{$self->files;} and $self->files->[0] eq "--"
!ll&&!rl&&r
040
MC/DC0%0/2
@{$self->files;} and $self->files->[0] eq "--"
@{$self->files;} $self->files->[0] eq "--"
Truth table33%1/3
A and B
A= @{$self->files;}B= $self->files->[0] eq "--"
ABresult
0X0
100
111
393  
394 4 ▶ if ( !@{ $self->files } && @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->files;} and @args) 0 4 4
Condition33%1/3
not @{$self->files;} and @args
!ll&&!rl&&r
400
MC/DC0%0/2
not @{$self->files;} and @args
not @{$self->files;} @args
Truth table33%1/3
not(A) and B
A= not @{$self->files;}B= @args
ABresult
0X0
100
111
395 0 $self->files( [@args] );
396 }
397  
398 4 $self->cmd( shift @{ $self->files } )
399 4 ▶ if !$self->cmd && @{ $self->files };
Branch: 1 branch
BranchTrueFalseTotal
if not $self->cmd and @{$self->files;} 4 0 4
Condition33%1/3
not $self->cmd and @{$self->files;}
!ll&&!rl&&r
004
MC/DC0%0/2
not $self->cmd and @{$self->files;}
not $self->cmd @{$self->files;}
Truth table33%1/3
not(A) and B
A= not $self->cmdB= @{$self->files;}
ABresult
0X0
100
111
400 }
401 77 ▶ if ( !$passed_args && $self->files ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $passed_args and $self->files) 13 64 77
Condition66%2/3
not $passed_args and $self->files
!ll&&!rl&&r
64013
MC/DC50%1/2
not $passed_args and $self->files
not $passed_args $self->files
Truth table66%2/3
not(A) and B
A= not $passed_argsB= $self->files
ABresult
0X0
100
111
402 13 @ARGV = ( @{ $self->files }, @args ); ## no critic
403 }
404  
405 77 ▶ if ( $self->has_sub_command ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_sub_command) 4 73 77
406 4 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->sub_command eq 'HASH' and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}) { } 3 1 4
elsif ($self->sub_command =~ /^[A-Z].*::$/ and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}) { } 0 2 2
Condition33%3/9
ref $self->sub_command eq "HASH" and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
!ll&&!rl&&r
103
not $self->has_auto_complete or $self->cmd and $self->sub_command->{$self->cmd}
l!l&&r!l&&!r
300
$self->cmd and $self->sub_command->{$self->cmd}
!ll&&!rl&&r
000
MC/DC25%1/4
ref $self->sub_command eq "HASH" and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
ref $self->sub_command eq "HASH" not $self->has_auto_complete $self->cmd $self->sub_command->{$self->cmd}
Truth table40%2/5
A and not(B) or C and D
A= ref $self->sub_command eq "HASH"B= not $self->has_auto_completeC= $self->cmdD= $self->sub_command->{$self->cmd}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
407 ref $self->sub_command eq 'HASH'
408 && ( !$self->has_auto_complete
409 || ( $self->cmd && $self->sub_command->{ $self->cmd } ) )
410 )
411 {
412 3 ▶ if ( !$self->sub_command->{ $self->cmd } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sub_command->{$self->cmd}) 0 3 3
413 0 warn 'Unknown command "' . $self->cmd . "\"!\n";
414 0 ▶ die Getopt::Alt::Exception->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->help_package 0 0 0
415 message => "Unknown command '$self->cmd'",
416 help => 1,
417 ) if !$self->help_package;
418 0 $self->_show_help( 1,
419 'Unknown command "' . $self->cmd . "\"!\n" );
420 }
421  
422 3 ▶ if ( ref $self->sub_command->{ $self->cmd } eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->sub_command->{$self->cmd} eq "ARRAY") 3 0 3
423  
424 # make a copy of the sub command
425 3 my $sub = [ @{ $self->sub_command->{ $self->cmd } } ];
426  
427 # check the style
428 3 ▶ my $options =
Branch: 1 branch
BranchTrueFalseTotal
@$sub == 2 && ref $sub->[0] eq 'HASH' && ref $sub->[1] eq 'ARRAY' ? : 2 1 3
Condition66%4/6
@$sub == 2 and ref $sub->[0] eq "HASH" and ref $sub->[1] eq "ARRAY"
!ll&&!rl&&r
102
@$sub == 2 and ref $sub->[0] eq "HASH"
!ll&&!rl&&r
102
MC/DC33%1/3
@$sub == 2 and ref $sub->[0] eq "HASH" and ref $sub->[1] eq "ARRAY"
@$sub == 2 ref $sub->[0] eq "HASH" ref $sub->[1] eq "ARRAY"
Truth table50%2/4
A and B and C
A= @$sub == 2B= ref $sub->[0] eq "HASH"C= ref $sub->[1] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
429 @$sub == 2
430 && ref $sub->[0] eq 'HASH'
431 && ref $sub->[1] eq 'ARRAY' ? shift @$sub : {};
432 3 ▶ my $opt_args = %$options ? $sub->[0] : $sub;
Branch: 1 branch
BranchTrueFalseTotal
%$options ? : 2 1 3
433  
434 # build sub command object
435 my $sub_obj = Getopt::Alt->new(
436 {
437 helper => $self->helper,
438 3 %{$options}, ## no critic
439 options =>
440 $self->options, # inherit this objects options
441 default =>
442 3 ▶ { %{ $self->opt }, %{ $options->{default} || {} } },
Condition100%2/2
$options->{'default'} || {}
l!l
21
MC/DC100%1/1
$options->{'default'} || {}
$options->{'default'}
Truth table100%2/2
A
A= $options->{'default'}
Aresult
11
00
443 },
444 $opt_args
445 );
446 3 local @ARGV = ();
447 3 ▶ if ( $self->opt->auto_complete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->opt->auto_complete) 0 3 3
448 0 push @args, '--auto-complete', $self->opt->auto_complete,
449 '--';
450 }
451 3 $sub_obj->process(@args);
452 3 $self->opt( $sub_obj->opt );
453 3 $self->files( $sub_obj->files );
454 }
455 }
456 ▶ elsif (
Condition11%1/9
$self->sub_command =~ /^[A-Z].*::$/ and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
!ll&&!rl&&r
100
not $self->has_auto_complete or $self->cmd and $self->sub_command->{$self->cmd}
l!l&&r!l&&!r
000
$self->cmd and $self->sub_command->{$self->cmd}
!ll&&!rl&&r
000
MC/DC0%0/4
$self->sub_command =~ /^[A-Z].*::$/ and !$self->has_auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
$self->sub_command =~ /^[A-Z].*::$/ not $self->has_auto_complete $self->cmd $self->sub_command->{$self->cmd}
Truth table20%1/5
A and not(B) or C and D
A= $self->sub_command =~ /^[A-Z].*::$/B= not $self->has_auto_completeC= $self->cmdD= $self->sub_command->{$self->cmd}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
457 $self->sub_command =~ /^[A-Z].*::$/
458 && ( !$self->has_auto_complete
459 || ( $self->cmd && $self->sub_command->{ $self->cmd } ) )
460 )
461 {
462 # object based subcommands
463 0 ▶ my $run = $self->sub_module_method || 'run';
Condition0%0/2
$self->sub_module_method || 'run'
l!l
00
MC/DC0%0/1
$self->sub_module_method || 'run'
$self->sub_module_method
Truth table0%0/2
A
A= $self->sub_module_method
Aresult
11
00
464 }
465 }
466  
467 77 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) 11 66 77
468 11 ▶ if ( $self->opt->{version} ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->opt->{'version'}) { } 1 10 11
elsif ($self->opt->{'man'}) { } 1 9 10
elsif ($self->opt->{'help'}) { } 1 8 9
elsif ($self->auto_complete and $self->opt->auto_complete) { } 0 8 8
elsif ($self->sub_command and not $self->cmd) { } 0 15 15
469 1 my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
470 1 ▶ my $version = defined $main::VERSION ? $main::VERSION : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $main::VERSION ? : 1 0 1
471 1 die Getopt::Alt::Exception->new(
472 message => "$name Version = $version\n",
473 help => 1,
474 );
475 }
476 elsif ( $self->opt->{man} ) {
477 1 $self->_show_help(2);
478 }
479 elsif ( $self->opt->{help} ) {
480 1 $self->_show_help(1);
481 }
482 ▶ elsif ( $self->auto_complete && $self->opt->auto_complete ) {
Condition33%1/3
$self->auto_complete and $self->opt->auto_complete
!ll&&!rl&&r
800
MC/DC0%0/2
$self->auto_complete and $self->opt->auto_complete
$self->auto_complete $self->opt->auto_complete
Truth table33%1/3
A and B
A= $self->auto_completeB= $self->opt->auto_complete
ABresult
0X0
100
111
483 0 $self->complete( \@errors );
484 }
485 ▶ elsif ( $self->sub_command && !$self->cmd ) {
Condition66%2/3
$self->sub_command and not $self->cmd
!ll&&!rl&&r
710
MC/DC0%0/2
$self->sub_command and not $self->cmd
$self->sub_command not $self->cmd
Truth table66%2/3
A and not(B)
A= $self->sub_commandB= not $self->cmd
ABresult
0X0
100
111
486 0 $self->_show_help(1);
487 }
488 }
489  
490 74 return $self;
491 }
492  
493 4 ▶sub complete {
Subroutine complete: called CC=9 SCAR=23.4
Pod documented
494 4 my ( $self, $errors ) = @_;
495  
496 4 ▶ if ( $self->sub_command && ref $self->sub_command && !$self->cmd ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->sub_command and ref $self->sub_command and not $self->cmd) { } 2 2 4
elsif ($ARGV[-1] and $ARGV[-1] =~ /^-/msx) { } 2 0 2
Condition66%4/6
$self->sub_command and ref $self->sub_command and not $self->cmd
!ll&&!rl&&r
202
$self->sub_command and ref $self->sub_command
!ll&&!rl&&r
202
MC/DC33%1/3
$self->sub_command and ref $self->sub_command and not $self->cmd
$self->sub_command ref $self->sub_command not $self->cmd
Truth table50%2/4
A and B and not(C)
A= $self->sub_commandB= ref $self->sub_commandC= not $self->cmd
ABCresult
0XX0
10X0
1100
1111
497 2 my $cmd = shift @ARGV;
498 my @sub_command =
499 2 ▶ grep { $cmd ? /$cmd/ : 1 } sort keys %{ $self->sub_command };
Branch: 1 branch
BranchTrueFalseTotal
$cmd ? : 2 2 4
500 2 print join ' ', @sub_command;
501 }
502 ▶ elsif ( $ARGV[-1] && $ARGV[-1] =~ /^-/xms ) {
Condition33%1/3
$ARGV[-1] and $ARGV[-1] =~ /^-/msx
!ll&&!rl&&r
002
MC/DC0%0/2
$ARGV[-1] and $ARGV[-1] =~ /^-/msx
$ARGV[-1] $ARGV[-1] =~ /^-/msx
Truth table33%1/3
A and B
A= $ARGV[-1]B= $ARGV[-1] =~ /^-/msx
ABresult
0X0
100
111
503 2 my $cmd = $ARGV[-1];
504 2 ▶ print join ' ', grep { $cmd ? /^$cmd/ : 1 } sort $self->list_options;
Branch: 1 branch
BranchTrueFalseTotal
$cmd ? : 14 0 14
505 }
506 else {
507 # run the auto complete method
508 0 $self->auto_complete->( $self, $self->opt->auto_complete, $errors );
509 }
510  
511 # exit here as auto complete should stop processing
512 4 ▶ return $EXIT ? exit 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$Getopt::Alt::EXIT ? : 0 4 4
513 }
514  
515 4 ▶sub list_options {
Subroutine list_options: called CC=5 SCAR=16.1
Pod documented
516 4 my ($self) = @_;
517 4 my @names;
518  
519 4 my $meta = $self->options->meta;
520  
521 4 for my $name ( $meta->get_attribute_list ) {
522 21 my $opt = $meta->get_attribute($name);
523 21 for my $name ( @{ $opt->names } ) {
524  
525 # skip auto-complete commands (they are hidden options)
526 next
527 33 ▶ if grep { $name eq $_ }
Branch: 1 branch
BranchTrueFalseTotal
if grep {$name eq $_;} "auto_complete", "auto-complete", "auto_complete_list", "auto-complete-list" 12 21 33
528 qw/auto_complete auto-complete auto_complete_list auto-complete-list/;
529 21 push @names, $name;
530 }
531 }
532  
533 return
534 21 ▶ map { length $_ == 1 ? "-$_" : "--$_" }
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 6 15 21
535 4 uniq sort { lc $a cmp lc $b } @names;
536 }
537  
538 94 ▶sub best_option {
Subroutine best_option: called CC=24 SCAR=31.8
Pod documented
539 94 my ( $self, $long, $short, $has_no ) = @_;
540  
541 94 ▶ if ( $has_no && $long ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_no and $long) 3 91 94
Condition100%3/3
$has_no and $long
!ll&&!rl&&r
8833
MC/DC100%2/2
$has_no and $long
$has_no $long
Truth table100%3/3
A and B
A= $has_noB= $long
ABresult
0X0
100
111
542 3 $long =~ s/^no-//xms;
543 }
544  
545 94 my $meta = $self->options->meta;
546  
547 94 for my $name ( $meta->get_attribute_list ) {
548 690 my $opt = $meta->get_attribute($name);
549  
550 690 ▶ return ( $opt, undef ) if $long && $opt->name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if $long and $opt->name eq $long 30 660 690
Condition100%3/3
$long and $opt->name eq $long
!ll&&!rl&&r
40925130
MC/DC100%2/2
$long and $opt->name eq $long
$long $opt->name eq $long
Truth table100%3/3
A and B
A= $longB= $opt->name eq $long
ABresult
0X0
100
111
551  
552 660 for my $name ( @{ $opt->names } ) {
553 1198 ▶ return ( $opt, undef ) if defined $long && $name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if defined $long and $name eq $long 0 1198 1198
Condition66%2/3
defined $long and $name eq $long
!ll&&!rl&&r
7414570
MC/DC0%0/2
defined $long and $name eq $long
defined $long $name eq $long
Truth table66%2/3
A and B
A= defined $longB= $name eq $long
ABresult
0X0
100
111
554 1198 ▶ return ( $opt, undef ) if defined $short && $name eq $short;
Branch: 1 branch
BranchTrueFalseTotal
if defined $short and $name eq $short 50 1148 1198
Condition100%3/3
defined $short and $name eq $short
!ll&&!rl&&r
45769150
MC/DC100%2/2
defined $short and $name eq $short
defined $short $name eq $short
Truth table100%3/3
A and B
A= defined $shortB= $name eq $short
ABresult
0X0
100
111
555 }
556 }
557  
558 14 ▶ if ( ( $long && $long =~ /^\d+$/xms )
Branch: 1 branch
BranchTrueFalseTotal
if ($long and $long =~ /^\d+$/msx or defined $short and $short =~ /^\d$/msx) 3 11 14
Condition100%9/9
$long and $long =~ /^\d+$/msx or defined $short and $short =~ /^\d$/msx
l!l&&r!l&&!r
1211
$long and $long =~ /^\d+$/msx
!ll&&!rl&&r
851
defined $short and $short =~ /^\d$/msx
!ll&&!rl&&r
562
MC/DC75%3/4
$long and $long =~ /^\d+$/msx or defined $short and $short =~ /^\d$/msx
$long $long =~ /^\d+$/msx defined $short $short =~ /^\d$/msx
Truth table57%4/7
A and B or C and D
A= $longB= $long =~ /^\d+$/msxC= defined $shortD= $short =~ /^\d$/msx
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
559 || ( defined $short && $short =~ /^\d$/xms ) )
560 {
561 3 $meta = $self->opt->meta;
562 3 for my $name ( $meta->get_attribute_list ) {
563 27 my $opt = $meta->get_attribute($name);
564 27 ▶ return ( $opt, $long || $short ) if $opt->number;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->number 3 24 27
Condition100%3/3
$long || $short
l!l&&r!l&&!r
111
MC/DC100%2/2
$long || $short
$long $short
Truth table100%3/3
A || B
A= $longB= $short
ABresult
1X1
011
000
565 }
566 }
567  
568 11 ▶ return $self->best_option( $long, $short, 1 ) if !$has_no;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_no 6 5 11
569  
570 5 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) { } 3 2 5
571 3 ▶ die [
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 2 3
$long ? : 1 2 3
572 Getopt::Alt::Exception->new(
573 message => "Unknown option '"
574 . ( $long ? "--$long" : "-$short" ) . "'\n",
575 option => ( $long ? "--$long" : "-$short" ),
576 )
577 ];
578 }
579 else {
580 2 ▶ die [
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 1 2
$long ? : 1 1 2
581 Getopt::Alt::Exception->new(
582 help => 1,
583 message => "Unknown option '"
584 . ( $long ? "--$long" : "-$short" ) . "'\n",
585 option => ( $long ? "--$long" : "-$short" ),
586 )
587 ];
588 }
589 }
590  
591 6 ▶sub _show_help {
Subroutine _show_help: called CC=10 SCAR=29.2
592 6 my ( $self, $verbosity, $msg ) = @_;
593  
594 6 my %input;
595 6 ▶ if ( $self->help_packages && $self->cmd ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->help_packages and $self->cmd) { } 0 6 6
elsif ($self->help_package and $self->help_package ne '1') { } 6 0 6
Condition33%1/3
$self->help_packages and $self->cmd
!ll&&!rl&&r
600
MC/DC0%0/2
$self->help_packages and $self->cmd
$self->help_packages $self->cmd
Truth table33%1/3
A and B
A= $self->help_packagesB= $self->cmd
ABresult
0X0
100
111
596 0 my $package = $self->help_packages->{ $self->cmd };
597 0 ▶ if ($package) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package) 0 0 0
598 0 $package =~ s{::}{/}gxms;
599 0 $package .= '.pm';
600 0 require $package;
601 0 %input = ( -input => $INC{$package} );
602 }
603 }
604 ▶ elsif ( $self->help_package && $self->help_package ne "1" ) {
Condition33%1/3
$self->help_package and $self->help_package ne 1
!ll&&!rl&&r
006
MC/DC0%0/2
$self->help_package and $self->help_package ne 1
$self->help_package $self->help_package ne 1
Truth table33%1/3
A and B
A= $self->help_packageB= $self->help_package ne 1
ABresult
0X0
100
111
605 6 my $help = $self->help_package;
606 6 ▶ if ( !-f $help ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $help) 6 0 6
607 6 $help .= '.pm';
608 6 $help =~ s{::}{/}gxms;
609 }
610 6 %input = ( -input => $INC{$help} );
611 }
612  
613 6 require Tie::Handle::Scalar;
614 6 ▶ my $out = $self->help_pre ? $self->help_pre() : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->help_pre ? : 0 6 6
615 6 tie *FH, 'Tie::Handle::Scalar', \$out;
616 6 require Pod::Usage;
617 6 ▶ Pod::Usage::pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
$msg ? : 0 6 6
618 $msg ? ( -msg => $msg ) : (),
619 -verbose => $verbosity,
620 -exitval => 'NOEXIT',
621 -output => \*FH,
622 %input,
623 );
624 6 ▶ $out .= $self->help_post ? $self->help_post->() : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->help_post ? : 0 6 6
625 6 die Getopt::Alt::Exception->new( message => $out, help => 1 );
626 }
627  
628 7 1;
629  
630 __END__
↑ Summary Alt/Option.pm »