blib/lib/Getopt/Alt.pm

filter: Statementstmt 91.2% 239 / 262 Branchbran 80.6% 100 / 124 Conditioncond 62.6% 67 / 107 MC/DCmcdc 40.9% 25 / 61 Subroutinesub 96.9% 32 / 33 Podpod 83.3% 5 / 6 totaltotal 78.9% 468 / 593 CC 105 SCAR 28.6
CC
105
Cov
82%
complete
56.0
process
34.3
best_option
24.0
CRAP 17.5
« blib/lib/Getopt/Alt/Option.pm ↑ Summary blib/lib/Getopt/Alt/Command.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 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Getopt::Alt::Option qw/build_option/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Getopt::Alt::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 use overload (
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 6 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 6 Moose::Exporter->setup_import_methods(
28 as_is => [qw/get_options/],
29 );
30  
31 6 our $VERSION = version->new('0.3.7');
32 6 our $EXIT = 1;
33  
34 6 has options => (
35 is => 'rw',
36 isa => 'Str',
37 default => 'Getopt::Alt::Dynamic',
38 );
39 6 has opt => (
40 is => 'rw',
41 isa => 'Getopt::Alt::Dynamic',
42 clearer => 'clear_opt',
43 );
44 has default => (
45 is => 'rw',
46 isa => 'HashRef',
47 23 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
48 6 );
49 has files => (
50 is => 'rw',
51 isa => 'ArrayRef[Str]',
52 30 ▶ default => sub {[]},
Subroutine __ANON__: called CC=1 SCAR=0.0
53 6 );
54 6 has bundle => (
55 is => 'rw',
56 isa => 'Bool',
57 default => 1,
58 );
59 6 has ignore_case => (
60 is => 'rw',
61 isa => 'Bool',
62 default => 1,
63 );
64 6 has help_package => (
65 is => 'rw',
66 isa => 'Str',
67 );
68 6 has helper => (
69 is => 'rw',
70 isa => 'Bool',
71 );
72 has cmds => (
73 is => 'rw',
74 isa => 'ArrayRef[Getopt::Alt::Command]',
75 30 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 6 );
77 6 has cmd => (
78 is => 'rw',
79 isa => 'Str',
80 clearer => 'clear_cmd',
81 );
82 6 has sub_command => (
83 is => 'rw',
84 #isa => 'Bool | HashRef[ArrayRef] | CodeRef',
85 predicate => 'has_sub_command',
86 documentation => <<'DOC',
87 if true (== 1) processing of args stops at first non-defined parameter, if
88 a HASH ref the keys are assumed to be the allowed sub commands and the values
89 are assumed to be parameters to passed to get_options where the generated
90 options will be a sub object of generated options object. Finally if this
91 is a sub ref it will be called with self and the rest of ARGV
92 DOC
93 );
94 has aliases => (
95 is => 'rw',
96 isa => 'HashRef[ArrayRef]',
97 30 ▶ default => sub {{}},
Subroutine __ANON__: called CC=1 SCAR=0.0
98 6 documentation => 'Stores the list of aliases sub-commands can have',
99 );
100 6 has default_sub_command => (
101 is => 'rw',
102 isa => 'Str',
103 predicate => 'has_default_sub_command',
104 );
105 6 has auto_complete => (
106 is => 'rw',
107 isa => 'CodeRef',
108 predicate => 'has_auto_complete',
109 );
110 has name => (
111 is => 'rw',
112 isa => 'Str',
113 29 ▶ default => sub { path($0)->basename },
Subroutine __ANON__: called CC=1 SCAR=0.0
114 6 );
115 6 has conf_prefix => (
116 is => 'rw',
117 isa => 'Str',
118 default => '.',
119 );
120  
121 6 my $count = 1;
122 around BUILDARGS => sub {
123 30 ▶ my ($orig, $class, @params) = @_;
Subroutine __ANON__: called CC=9 SCAR=22.5
124 30 my %param;
125  
126 30 ▶ 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') { } 21 9 30
elsif (not %param and ref $params[0] eq 'HASH') { } 0 9 9
Condition66%2/3
ref $params[0] eq "HASH" and ref $params[1] eq "ARRAY"
!ll&&!rl&&r
9021
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
127 21 %param = %{ $params[0] };
128 21 @params = @{ $params[1] };
129 }
130 ▶ elsif ( !%param && ref $params[0] eq 'HASH' ) {
Condition33%1/3
not %param and ref $params[0] eq "HASH"
!ll&&!rl&&r
090
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
131 0 %param = %{ shift @params };
132 }
133  
134 30 ▶ if ( !exists $param{helper} || $param{helper} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $param{"helper"} or $param{"helper"}) 25 5 30
Condition100%3/3
not exists $param{"helper"} or $param{"helper"}
l!l&&r!l&&!r
2415
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
135 25 unshift @params, (
136 'help',
137 'man',
138 'version',
139 'auto_complete|auto-complete',
140 'auto_complete_list|auto-complete-list!',
141 );
142 }
143  
144 30 ▶ if ( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) 29 1 30
145 29 ▶ $param{options} = _build_option_class($param{options} || 'Getopt::Alt::Dynamic', @params);
Condition100%2/2
$param{'options'} || 'Getopt::Alt::Dynamic'
l!l
326
MC/DC100%1/1
$param{'options'} || 'Getopt::Alt::Dynamic'
$param{'options'}
Truth table100%2/2
A
A= $param{'options'}
Aresult
11
00
146  
147 29 if (0 && $param{sub_command} && ref $param{sub_command} eq 'HASH') {
148  
149 # build up all the sub command options
150 for my $sub (keys %{ $param{sub_command} }) {
151 $param{sub_command}{$sub} = _build_option_class($param{options}, $param{sub_command}{$sub});
152 }
153 }
154 }
155  
156 30 return $class->$orig(%param);
157 6 };
158  
159 sub _build_option_class {
160 29 ▶ my ($base_class, @params) = @_;
Subroutine _build_option_class: called CC=2 SCAR=6.9
161  
162 # construct a class of options passing
163 29 my $class_name = 'Getopt::Alt::Dynamic::A' . $count++;
164 29 my $option_class = Moose::Meta::Class->create(
165 $class_name,
166 superclasses => [ $base_class ],
167 );
168  
169 29 ▶ while ( my $option = shift @params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $option = shift @params) 186 29 215
170 186 build_option($option_class, $option);
171 }
172  
173 29 return $class_name;
174 }
175  
176 sub BUILD {
177 30 ▶ my ($self) = @_;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
178  
179 30 my $basename = $self->name;
180 30 my $prefix = $self->conf_prefix;
181 30 my $conf = Config::Any->load_stems({
182 stems => [ "$prefix$basename", File::HomeDir->my_home . "/$prefix$basename", "/etc/$basename" ],
183 use_ext => 1,
184 });
185  
186 $conf = {
187 2 map { %$_ }
188 2 map { values %$_ }
189 30 @{ $conf }
190 };
191  
192 # perlcritic is confused here combining hashes is not the same as comma separated arguments
193 30 $self->default({ %{$self->default}, %$conf, }); ## no critic
194  
195 30 ▶ if ($conf->{aliases}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"aliases"}) 1 29 30
196 1 for my $alias (keys %{ $conf->{aliases} }) {
197 1 $self->aliases->{$alias} = [ split /\s+/xms, $conf->{aliases}{$alias} ];
198 }
199 }
200  
201 30 return;
202 }
203  
204 sub get_options {
205 14 ▶ my @args = @_;
Subroutine get_options: called CC=6 SCAR=18.8
Pod documented
206 14 my $caller = caller;
207  
208 14 ▶ 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 11 14
Condition83%5/6
@args > 2 and ref $args[0] eq "HASH" and ref $args[1] ne "ARRAY"
!ll&&!rl&&r
1103
@args > 2 and ref $args[0] eq "HASH"
!ll&&!rl&&r
1013
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
209 3 my $options = shift @args;
210 3 @args = ( { default => $options}, [ @args ] );
211 }
212  
213 14 my $self;
214 try {
215 14 ▶ $self = __PACKAGE__->new(@args);
Subroutine __ANON__: called CC=3 SCAR=12.5
216  
217 14 ▶ $self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__;
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
218  
219 14 $self->process();
220 }
221 catch {
222 6 ▶ if ( ref $_ && ref $_ eq 'Getopt::Alt::Exception' && $_->help ) {
Subroutine __ANON__: called CC=6 SCAR=19.6
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help) 3 3 6
Condition50%3/6
ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help
!ll&&!rl&&r
033
ref $_ and ref $_ eq "Getopt::Alt::Exception"
!ll&&!rl&&r
006
MC/DC33%1/3
ref $_ and ref $_ eq "Getopt::Alt::Exception" and $_->help
ref $_ ref $_ eq "Getopt::Alt::Exception" $_->help
Truth table50%2/4
A and B and C
A= ref $_B= ref $_ eq "Getopt::Alt::Exception"C= $_->help
ABCresult
0XX0
10X0
1100
1111
223 3 die $_;
224 }
225  
226 3 warn $_;
227 3 $self = __PACKAGE__->new();
228  
229 3 ▶ $self->help_package($caller) if !$self->help_package || $self->help_package eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->help_package or $self->help_package eq __PACKAGE__ 3 0 3
Condition33%1/3
not $self->help_package or $self->help_package eq __PACKAGE__
l!l&&r!l&&!r
300
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
230  
231 3 $self->_show_help(1);
232 14 };
233  
234 8 ▶ return if !defined $self;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self 0 8 8
235  
236 8 ▶ return wantarray ? ( $self->opt, $self->cmd, $self ) : $self->opt;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 8 8
237 }
238  
239 sub process {
240 87 ▶ my ($self, @args) = @_;
Subroutine process: called CC=28 SCAR=35.3
Pod documented
241 87 my $passed_args = scalar @args;
242 87 ▶ @args = $passed_args ? @args : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
$passed_args ? : 71 16 87
243 87 $self->clear_opt;
244 87 $self->clear_cmd;
245 87 $self->files([]);
246  
247 87 my $class = $self->options;
248 87 $self->opt( $class->new( %{ $self->default } ) );
249 87 my @errors;
250  
251 ARG:
252 87 ▶ while (my $arg = shift @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @args) 94 73 167
253 94 my $action = '';
254 try {
255 94 ▶ my ($long, $short, $arg_data);
Subroutine __ANON__: called CC=11 SCAR=24.2
256 94 ▶ if ( $arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/xms ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg =~ /^-- (\w[^=\s]+) (?:= (.*) )?/msx) { } 33 61 94
elsif ($arg =~ /^- (\w) =? (.*)/msx) { } 55 6 61
257 33 $long = $1;
258 33 $arg_data = $2;
259 }
260 elsif ( $arg =~ /^- (\w) =? (.*)/xms ) {
261 55 $short = $1;
262 55 $arg_data = $2;
263 }
264 else {
265 6 push @{ $self->files }, $arg;
266 6 ▶ die $self->sub_command ? "last\n" : "next\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->sub_command ? : 4 2 6
267 }
268  
269 88 my ($opt, $new_value) = $self->best_option( $long, $short );
270 83 ▶ if (defined $new_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 3 80 83
271 3 $long = $opt->name;
272 3 $short = undef;
273 3 ($arg_data) = $arg =~ /^--?(\d+)$/;
274 }
275 83 $opt->value( $self->opt->{ $opt->name } );
276  
277 83 my ($value, $used) = $opt->process( $long, $short, $arg_data, \@args );
278 78 my $opt_name = $opt->name;
279 78 ▶ if ( $self->opt->auto_complete && $opt_name eq 'auto_complete_list' ) {
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
280 0 print join ' ', $self->list_options;
281 0 exit 0;
282 }
283 78 $self->opt->{$opt->name} = $value;
284  
285 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
286 2 unshift @args, '-' . $arg_data;
287 }
288 }
289 catch {
290 16 ▶ if ( $_ eq "next\n" ) {
Subroutine __ANON__: called CC=8 SCAR=23.9
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq "next\n") { } 2 14 16
elsif ($_ eq "last\n") { } 4 10 14
291 2 $action = 'next';
292 }
293 elsif ( $_ eq "last\n" ) {
294 # last means we have found a sub command we should see if it is an alias
295 4 ▶ if ($self->aliases->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->aliases->{$arg}) 0 4 4
296 0 $self->files->[-1] = shift @{ $self->aliases->{$arg} };
297 0 unshift @args, @{ $self->aliases->{$arg} };
298 }
299  
300 4 $action = 'last';
301 }
302 else {
303 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
304  
305 10 ▶ if ( $self->auto_complete && $self->opt->auto_complete ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_complete and $self->opt->auto_complete) { } 0 10 10
Condition33%1/3
$self->auto_complete and $self->opt->auto_complete
!ll&&!rl&&r
1000
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
306 0 push @errors, $_;
307 }
308 else {
309 10 die $_;
310 }
311 }
312 94 };
313 84 ▶ next if $action eq 'next';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "next" 2 82 84
314 82 ▶ last if $action eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $action eq "last" 4 78 82
315 }
316  
317 77 ▶ $self->cmd( shift @{ $self->files } ) if @{ $self->files } && $self->sub_command;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->files;} and $self->sub_command 4 73 77
Condition100%3/3
@{$self->files;} and $self->sub_command
!ll&&!rl&&r
7124
MC/DC100%2/2
@{$self->files;} and $self->sub_command
@{$self->files;} $self->sub_command
Truth table100%3/3
A and B
A= @{$self->files;}B= $self->sub_command
ABresult
0X0
100
111
318 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
319 13 @ARGV = ( @{ $self->files }, @args ); ## no critic
320 }
321  
322 77 ▶ if ( ref $self->sub_command eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->sub_command eq "HASH" and !$self->auto_complete || $self->cmd && $self->sub_command->{$self->cmd}) 3 74 77
Condition33%3/9
ref $self->sub_command eq "HASH" and !$self->auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
!ll&&!rl&&r
7430
not $self->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/DC0%0/4
ref $self->sub_command eq "HASH" and !$self->auto_complete || $self->cmd && $self->sub_command->{$self->cmd}
ref $self->sub_command eq "HASH" not $self->auto_complete $self->cmd $self->sub_command->{$self->cmd}
Truth table20%1/5
A and not(B) or C and D
A= ref $self->sub_command eq "HASH"B= not $self->auto_completeC= $self->cmdD= $self->sub_command->{$self->cmd}
ABCDresult
0XXX0
100X0
10100
11XX1
10111
323 && (
324 ! $self->auto_complete
325 || ( $self->cmd && $self->sub_command->{ $self->cmd } )
326 )
327 ) {
328 3 ▶ if (!$self->sub_command->{$self->cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sub_command->{$self->cmd}) 0 3 3
329 0 warn 'Unknown command "' . $self->cmd . "\"!\n";
330 0 ▶ die Getopt::Alt::Exception->new( message => "Unknown command '$self->cmd'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->help_package 0 0 0
331 if !$self->help_package;
332 0 $self->_show_help(1);
333 }
334  
335 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
336 # make a copy of the sub command
337 3 my $sub = [ @{$self->sub_command->{$self->cmd}} ];
338 # check the style
339 3 ▶ my $options = @$sub == 2 && ref $sub->[0] eq 'HASH' && ref $sub->[1] eq 'ARRAY' ? shift @$sub : {};
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
340 3 ▶ my $opt_args = %$options ? $sub->[0] : $sub;
Branch: 1 branch
BranchTrueFalseTotal
%$options ? : 2 1 3
341  
342 # build sub command object
343 my $sub_obj = Getopt::Alt->new(
344 {
345 3 %{ $options }, ## no critic
346 options => $self->options, # inherit this objects options
347 3 ▶ default => { %{ $self->opt }, %{ $options->{default} || {} } },
Branch: 1 branch
BranchTrueFalseTotal
$options->{"default"} or {} 1 2 3
348 },
349 $opt_args
350 );
351 3 local @ARGV = ();
352 3 $sub_obj->process(@args);
353 3 $self->opt( $sub_obj->opt );
354 3 $self->files( $sub_obj->files );
355 }
356 }
357  
358 77 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) 11 66 77
359 11 ▶ if ( $self->opt->{version} ) {
Branch: 4 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 16 16
360 1 my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
361 1 ▶ my $version = defined $main::VERSION ? $main::VERSION : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $main::VERSION ? : 1 0 1
362 1 die Getopt::Alt::Exception->new( message => "$name Version = $version\n", help => 1);
363 }
364 elsif ( $self->opt->{man} ) {
365 1 $self->_show_help(2);
366 }
367 elsif ( $self->opt->{help} ) {
368 1 $self->_show_help(1);
369 }
370 ▶ 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
371 0 $self->complete(\@errors);
372 }
373 }
374  
375 74 return $self;
376 }
377  
378 sub complete {
379 0 ▶ my ($self, $errors) = @_;
Subroutine complete: not called CC=7 SCAR=40.3
Pod undocumented
380  
381 0 ▶ if ( $self->sub_command && !$self->cmd && !$self->sub_command->{ $self->cmd } ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->sub_command and not $self->cmd and not $self->sub_command->{$self->cmd}) { } 0 0 0
elsif ($ARGV[-1] and $ARGV[-1] =~ /^-/msx) { } 0 0 0
Condition0%0/6
$self->sub_command and not $self->cmd and not $self->sub_command->{$self->cmd}
!ll&&!rl&&r
000
$self->sub_command and not $self->cmd
!ll&&!rl&&r
000
MC/DC0%0/3
$self->sub_command and not $self->cmd and not $self->sub_command->{$self->cmd}
$self->sub_command not $self->cmd not $self->sub_command->{$self->cmd}
Truth table0%0/4
A and not(B) and not(C)
A= $self->sub_commandB= not $self->cmdC= not $self->sub_command->{$self->cmd}
ABCresult
0XX0
10X0
1100
1111
382 0 my $cmd = $ARGV[1];
383 0 ▶ my @sub_command = grep { $cmd ? /$cmd/ : 1 } sort keys %{ $self->sub_command };
Branch: 1 branch
BranchTrueFalseTotal
$cmd ? : 0 0 0
384 0 print join ' ', @sub_command;
385 }
386 ▶ elsif ( $ARGV[-1] && $ARGV[-1] =~ /^-/xms ) {
Condition0%0/3
$ARGV[-1] and $ARGV[-1] =~ /^-/msx
!ll&&!rl&&r
000
MC/DC0%0/2
$ARGV[-1] and $ARGV[-1] =~ /^-/msx
$ARGV[-1] $ARGV[-1] =~ /^-/msx
Truth table0%0/3
A and B
A= $ARGV[-1]B= $ARGV[-1] =~ /^-/msx
ABresult
0X0
100
111
387 0 print join ' ', $self->list_options;
388 }
389 else {
390 # run the auto complete method
391 0 $self->auto_complete->($self, $self->opt->auto_complete, $errors);
392 }
393  
394 # exit here as auto complete should stop processing
395 0 return exit 0;
396 }
397  
398 sub list_options {
399 2 ▶ my ($self) = @_;
Subroutine list_options: called CC=5 SCAR=16.1
Pod documented
400 2 my @names;
401  
402 2 my $meta = $self->options->meta;
403  
404 2 for my $name ( $meta->get_attribute_list ) {
405 7 my $opt = $meta->get_attribute($name);
406 7 for my $name (@{ $opt->names }) {
407  
408 # skip auto-complete commands (they are hidden options)
409 11 ▶ next if grep {$name eq $_} qw/auto_complete auto-complete auto_complete_list auto-complete-list/;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$name eq $_;} "auto_complete", "auto-complete", "auto_complete_list", "auto-complete-list" 4 7 11
410 7 push @names, $name
411 }
412 }
413  
414 return map {
415 7 ▶ length $_ == 1 ? "-$_" : "--$_"
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 2 5 7
416 }
417 2 uniq sort { lc $a cmp lc $b } @names;
418 }
419  
420 sub best_option {
421 94 ▶ my ($self, $long, $short, $has_no) = @_;
Subroutine best_option: called CC=24 SCAR=31.8
Pod documented
422  
423 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
424 3 $long =~ s/^no-//xms;
425 }
426  
427 94 my $meta = $self->options->meta;
428  
429 94 for my $name ( $meta->get_attribute_list ) {
430 651 my $opt = $meta->get_attribute($name);
431  
432 651 ▶ return ($opt, undef) if $long && $opt->name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if $long and $opt->name eq $long 30 621 651
Condition100%3/3
$long and $opt->name eq $long
!ll&&!rl&&r
39522630
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
433  
434 621 for my $name (@{ $opt->names }) {
435 1085 ▶ return ($opt, undef) if defined $long && $name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if defined $long and $name eq $long 0 1085 1085
Condition66%2/3
defined $long and $name eq $long
!ll&&!rl&&r
6913940
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
436 1085 ▶ return ($opt, undef) if defined $short && $name eq $short;
Branch: 1 branch
BranchTrueFalseTotal
if defined $short and $name eq $short 50 1035 1085
Condition100%3/3
defined $short and $name eq $short
!ll&&!rl&&r
39464150
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
437 }
438 }
439  
440 14 ▶ if (($long && $long =~ /^\d+$/xms) || (defined $short && $short =~ /^\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
1310
$long and $long =~ /^\d+$/msx
!ll&&!rl&&r
851
defined $short and $short =~ /^\d$/msx
!ll&&!rl&&r
562
MC/DC50%2/4
$long and $long =~ /^\d+$/msx or defined $short and $short =~ /^\d$/msx
$long $long =~ /^\d+$/msx defined $short $short =~ /^\d$/msx
Truth table42%3/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
441 3 $meta = $self->opt->meta;
442 3 for my $name ( $meta->get_attribute_list ) {
443 9 my $opt = $meta->get_attribute($name);
444 9 ▶ return ($opt, $long || $short) if $opt->number;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->number 3 6 9
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
445 }
446 }
447  
448 11 ▶ return $self->best_option($long, $short, 1) if !$has_no;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_no 6 5 11
449  
450 5 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) { } 3 2 5
451 3 ▶ die [ Getopt::Alt::Exception->new(
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 2 3
$long ? : 1 2 3
452 message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n",
453 option => ($long ? "--$long" : "-$short"),
454 ) ];
455 }
456 else {
457 2 ▶ die [ Getopt::Alt::Exception->new(
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 1 2
$long ? : 1 1 2
458 help => 1,
459 message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n",
460 option => ($long ? "--$long" : "-$short"),
461 ) ];
462 }
463 }
464  
465 sub _show_help {
466 5 ▶ my ($self, $verbosity, $msg) = @_;
Subroutine _show_help: called CC=5 SCAR=16.6
467  
468 5 my %input;
469 5 ▶ if ( $self->help_package && $self->help_package ne "1" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package and $self->help_package ne 1) 5 0 5
Condition33%1/3
$self->help_package and $self->help_package ne 1
!ll&&!rl&&r
005
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
470 5 my $help = $self->help_package;
471 5 ▶ if ( !-f $help ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $help) 5 0 5
472 5 $help .= '.pm';
473 5 $help =~ s{::}{/}gxms;
474 }
475 5 %input = ( -input => $INC{$help} );
476 }
477  
478 5 require Tie::Handle::Scalar;
479 5 my $out = '';
480 5 tie *FH, 'Tie::Handle::Scalar', \$out;
481 5 require Pod::Usage;
482 5 ▶ Pod::Usage::pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
$msg ? : 0 5 5
483 $msg ? ( -msg => $msg ) : (),
484 -verbose => $verbosity,
485 -exitval => 'NOEXIT',
486 -output => \*FH,
487 %input,
488 );
489 5 die Getopt::Alt::Exception->new( message => $out, help => 1 );
490 }
491  
492 6 1;
493  
494 __END__
495  
496 =head1 NAME
497  
498 Getopt::Alt - Command line option passing with with lots of features
499  
500 =head1 VERSION
501  
502 This documentation refers to Getopt::Alt version 0.3.7.
503  
504 =head1 SYNOPSIS
505  
506 use Getopt::Alt;
507  
508 # OO Style usage
509 # Create a new options object
510 my $opt = Getopt::Alt->new(
511 {
512 default => { string => 'default' },
513 },
514 [
515 'string|s=s',
516 ...
517 ],
518 );
519 print "String = " . $opt->opt->{string} . "\n";
520  
521 # Getopt::Long like usage
522 use Getopt::Alt qw/get_options/;
523  
524 # most basic form
525 my $options = get_options(
526 'string|s=s',
527 'int|i=i',
528 'hash|h=s%',
529 'array|a=s@',
530 'increment|c+',
531 'nullable|n=s?',
532 'negatable|b!',
533 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
534 );
535 print Dumper $options->opt; # passed parameters
536 print join ',', @{ $options->files }; # non option parameters
537  
538 # with defaults
539 my $options = get_options(
540 { negatable => 1 },
541 'string|s=s',
542 'int|i=i',
543 'hash|h=s%',
544 'array|a=s@',
545 'increment|c+',
546 'nullable|n=s?',
547 'negatable|b!',
548 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
549 );
550  
551 # with configuration
552 my $options = get_options(
553 {
554 helper => 1, # default when using get_options
555 sub_command => 1, # stop processing at first non argument parameter
556 },
557 [
558 'string|s=s',
559 'int|i=i',
560 'hash|h=s%',
561 'array|a=s@',
562 'increment|c+',
563 'nullable|n=s?',
564 'negatable|b!',
565 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
566 ],
567 );
568 print $cmd; # sub command
569  
570 # with sub command details
571 my $options = get_options(
572 {
573 helper => 1, # default when using get_options
574 sub_command => {
575 sub => [ 'suboption' ],
576 other => [ 'verbose|v' ],
577 },
578 },
579 [
580 'string|s=s',
581 'int|i=i',
582 'hash|h=s%',
583 'array|a=s@',
584 'increment|c+',
585 'nullable|n=s?',
586 'negatable|b!',
587 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
588 ],
589 );
590 print Dumper $option->opt; # command with sub command options merged in
591  
592 # auto_complete
593 my $options = get_options(
594 {
595 helper => 1, # default when using get_options
596 auto_complete => sub {
597 my ($opt, $auto) = @_;
598 # ... code for auto completeion
599 # called if --auto-complete specified on the command line
600 },
601 },
602 [
603 'string|s=s',
604 'int|i=i',
605 ],
606 );
607  
608 =head1 DESCRIPTION
609  
610 The aim of C<Getopt::Alt> is to provide an alternative to L<Getopt::Long> that
611 allows a simple command line program to easily grow in complexity. L<Getopt::Lon>
612 can be expanded from a simple command line option passer to allow sub-commands.
613 Option processing may stop at the sub-command or with the help of modules can
614 cascade the processing into the sub command's module or config.
615  
616 The simple usage is quite similar to L<Getopt::Long>:
617  
618 In C<Getopt::Long> you might get your options like:
619  
620 use Getopt::Long;
621 my %options = ( string => 'default' );
622 GetOptions(
623 \%options,
624 'string|s=s',
625 ...
626 );
627  
628 The found options are now stored in the C<%options> hash.
629  
630 In C<Getopt::Alt> you might do the following:
631  
632 use Getopt::Alt qw/get_options/;
633 my %default = ( string => 'default' );
634 my $opt = get_options(
635 \%default,
636 'string|s=s',
637 ...
638 );
639 my %options = %{ $opt->opt };
640  
641 This will also result in the options stored in the C<%options> hash.
642  
643 Some other differences between Getopt::Alt and Getopt::Long include:
644  
645 =over 4
646  
647 =item *
648  
649 Bundling - is on by default
650  
651 =item *
652  
653 Case sensitivity is on by default
654  
655 =item *
656  
657 Throws error rather than returning errors.
658  
659 =item *
660  
661 Can work with sub commands
662  
663 =back
664  
665 =head1 SUBROUTINES/METHODS
666  
667 =head2 Exported
668  
669 =head3 C<get_options (@options | $setup, $options)>
670  
671 =head3 C<get_options ($default, 'opt1', 'opt2' ... )>
672  
673 This is the equivalent of calling new(...)->process but it does some extra
674 argument processing.
675  
676 B<Note>: The second form is the same basically the same as Getopt::Long's
677 GetOptions called with a hash ref as the first parameter.
678  
679 =head2 Class Methods
680  
681 =head3 C<new ( \%config, \@optspec )>
682  
683 =head4 config
684  
685 =over 4
686  
687 =item C<default> - HashRef
688  
689 Sets the default values for all the options. The values in opt will be reset
690 with the values in here each time process is called
691  
692 =item C<files> - ArrayRef[Str]
693  
694 Any arguments that not consumed as part of options (usually files), if no
695 arguments were passed to C<process> then this value would also be put back
696 into C<@ARGV>.
697  
698 =item C<bundle> - bool (Default true)
699  
700 Turns on bundling of arguments eg C<-rv> is equivalent to C<-r -v>. This is
701 on by default.
702  
703 =item C<ignore_case> - bool (Default true)
704  
705 Turns ignoring of the case of arguments, off by default.
706  
707 =item C<helper> - bool
708  
709 If set to a true value this will cause the help, man, and version options to
710 be added the end of your options list. (i.e. you get --help --man and
711 --version arguments for you program.)
712  
713 =item C<help_package> - Str
714  
715 The Perl package with the POD documentation for --help and --man, by default
716 it's the callers package.
717  
718 =item C<name> - Str (Default $0's basename)
719  
720 Used when displaying --version info
721  
722 =item C<cmds> - ArrayRef[Getopt::Alt::Command]
723  
724 If the Getopt::Alt is being used as part of a package where individual
725 commands have their own modules this parameter stores an instance of each
726 commands. (Not yet fully implemented.
727  
728 =item C<options> - Str (Default Getopt::Alt::Dynamic)
729  
730 The parent class for generating options.
731  
732 =item C<opt> - HashRef
733  
734 The values processed from the C<$ARGV> or arguments passed to the C<process>
735 method..
736  
737 =item C<default> - HashRef
738  
739 The default values for each option. The default value is not modified by
740 processing, so if set the same default will be used from call to call.
741  
742 =item C<aliases> - HashRef[ArrayRef[Str]]
743  
744 When using sub-commands this allows you to configure aliases for those
745 commands, aliases are recursed, they can have extra arguments though.
746 If a configuration file is used aliases can be specified in that file.
747  
748 =item C<conf_prefix> - Str (Default ".")
749  
750 The prefix for finding the configuration files. By default the following
751 is used:
752  
753 =over 4
754  
755 =item *
756  
757 ./$conf_prefix$name
758  
759 =item *
760  
761 ~/$conf_prefix$name
762  
763 =item *
764  
765 /etc/$conf_prefix$name
766  
767 =back
768  
769 =back
770  
771 =head2 Object Methods
772  
773 =head3 C<BUILD ()>
774  
775 internal method
776  
777 =head3 C<process ()>
778  
779 =head3 C<list_options ()>
780  
781 Returns a list of all command line options in the current object.
782  
783 =head3 C<best_option ()>
784  
785 =head1 DIAGNOSTICS
786  
787 =head1 CONFIGURATION AND ENVIRONMENT
788  
789 Configuration files can be used to specify default values and aliases. They
790 can be located in the current directory, $HOME or /etc.The file name is
791 specified by the C<name> attribute (which defaults to the program's name)
792 and is prepended with a dot. eg:
793  
794 For a program called as C<$ ./foo> or C<$ foo> C<name> would be set to foo
795 and possible configuration names would be
796  
797 =over 4
798  
799 =item *
800  
801 .foo.yml
802  
803 =item *
804  
805 ~/.foo.rc
806  
807 =item *
808  
809 /etc/.foo.yml
810  
811 =back
812  
813 See L<Config::Any> for information about config formats and file extensions.
814  
815 =head1 DEPENDENCIES
816  
817 =head1 INCOMPATIBILITIES
818  
819 =head1 BUGS AND LIMITATIONS
820  
821 There are no known bugs in this module.
822  
823 Please report problems to Ivan Wills (ivan.wills@gmail.com).
824  
825 Patches are welcome.
826  
827 =head1 AUTHOR
828  
829 Ivan Wills - (ivan.wills@gmail.com)
830  
831 =head1 LICENSE AND COPYRIGHT
832  
833 Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
834 All rights reserved.
835  
836 This module is free software; you can redistribute it and/or modify it under
837 the same terms as Perl itself. See L<perlartistic>. This program is
838 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
839 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
840 PARTICULAR PURPOSE.
841  
842 =cut
« blib/lib/Getopt/Alt/Option.pm ↑ Summary blib/lib/Getopt/Alt/Command.pm »