blib/lib/Getopt/Alt.pm

filter: Statementstmt 93.7% 239 / 255 Branchbran 83.3% 100 / 120 Conditioncond 69.5% 64 / 92 MC/DCmcdc 46.3% 25 / 54 Subroutinesub 100% 32 / 32 Podpod 100% 5 / 5 totaltotal 83.3% 465 / 558 CC 98 SCAR 27.2
CC
98
Cov
86%
process
33.9
best_option
24.0
__ANON__
11.3
CRAP 15.2
« blib/lib/Getopt/Alt/Command.pm ↑ Summary Build »
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.5');
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=27 SCAR=35.2
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") 3 74 77
323 3 ▶ if (!$self->sub_command->{$self->cmd}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->sub_command->{$self->cmd}) 0 3 3
324 0 warn "Unknown command '$self->cmd'!\n";
325 0 ▶ die Getopt::Alt::Exception->new( message => "Unknown command '$self->cmd'" )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->help_package 0 0 0
326 if !$self->help_package;
327 0 $self->_show_help(1);
328 }
329  
330 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
331 # make a copy of the sub command
332 3 my $sub = [ @{$self->sub_command->{$self->cmd}} ];
333 # check the style
334 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
335 3 ▶ my $opt_args = %$options ? $sub->[0] : $sub;
Branch: 1 branch
BranchTrueFalseTotal
%$options ? : 2 1 3
336  
337 # build sub command object
338 my $sub_obj = Getopt::Alt->new(
339 {
340 3 %{ $options }, ## no critic
341 options => $self->options, # inherit this objects options
342 3 ▶ default => { %{ $self->opt }, %{ $options->{default} || {} } },
Branch: 1 branch
BranchTrueFalseTotal
$options->{"default"} or {} 1 2 3
343 },
344 $opt_args
345 );
346 3 local @ARGV = ();
347 3 $sub_obj->process(@args);
348 3 $self->opt( $sub_obj->opt );
349 3 $self->files( $sub_obj->files );
350 }
351 }
352  
353 77 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) 11 66 77
354 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
355 1 my ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
356 1 ▶ my $version = defined $main::VERSION ? $main::VERSION : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $main::VERSION ? : 1 0 1
357 1 die Getopt::Alt::Exception->new( message => "$name Version = $version\n", help => 1);
358 }
359 elsif ( $self->opt->{man} ) {
360 1 $self->_show_help(2);
361 }
362 elsif ( $self->opt->{help} ) {
363 1 $self->_show_help(1);
364 }
365 ▶ 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
366 0 ▶ if ( $ARGV[-1] && $ARGV[-1] =~ /^-/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGV[-1] and $ARGV[-1] =~ /^-/msx) { } 0 0 0
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
367 0 print join ' ', $self->list_options;
368 }
369 else {
370 # run the auto complete method
371 0 $self->auto_complete->($self, $self->opt->auto_complete, \@errors);
372 }
373  
374 # exit here as auto complete should stop processing
375 0 exit 0;
376 }
377 }
378  
379 74 return $self;
380 }
381  
382 sub list_options {
383 2 ▶ my ($self) = @_;
Subroutine list_options: called CC=5 SCAR=16.1
Pod documented
384 2 my @names;
385  
386 2 my $meta = $self->options->meta;
387  
388 2 for my $name ( $meta->get_attribute_list ) {
389 7 my $opt = $meta->get_attribute($name);
390 7 for my $name (@{ $opt->names }) {
391  
392 # skip auto-complete commands (they are hidden options)
393 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
394 7 push @names, $name
395 }
396 }
397  
398 return map {
399 7 ▶ length $_ == 1 ? "-$_" : "--$_"
Branch: 1 branch
BranchTrueFalseTotal
length $_ == 1 ? : 2 5 7
400 }
401 2 uniq sort { lc $a cmp lc $b } @names;
402 }
403  
404 sub best_option {
405 94 ▶ my ($self, $long, $short, $has_no) = @_;
Subroutine best_option: called CC=24 SCAR=31.8
Pod documented
406  
407 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
408 3 $long =~ s/^no-//xms;
409 }
410  
411 94 my $meta = $self->options->meta;
412  
413 94 for my $name ( $meta->get_attribute_list ) {
414 737 my $opt = $meta->get_attribute($name);
415  
416 737 ▶ return ($opt, undef) if $long && $opt->name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if $long and $opt->name eq $long 30 707 737
Condition100%3/3
$long and $opt->name eq $long
!ll&&!rl&&r
46424330
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
417  
418 707 for my $name (@{ $opt->names }) {
419 1318 ▶ return ($opt, undef) if defined $long && $name eq $long;
Branch: 1 branch
BranchTrueFalseTotal
if defined $long and $name eq $long 0 1318 1318
Condition66%2/3
defined $long and $name eq $long
!ll&&!rl&&r
8674510
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
420 1318 ▶ return ($opt, undef) if defined $short && $name eq $short;
Branch: 1 branch
BranchTrueFalseTotal
if defined $short and $name eq $short 50 1268 1318
Condition100%3/3
defined $short and $name eq $short
!ll&&!rl&&r
45181750
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
421 }
422 }
423  
424 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
425 3 $meta = $self->opt->meta;
426 3 for my $name ( $meta->get_attribute_list ) {
427 42 my $opt = $meta->get_attribute($name);
428 42 ▶ return ($opt, $long || $short) if $opt->number;
Branch: 1 branch
BranchTrueFalseTotal
if $opt->number 3 39 42
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
429 }
430 }
431  
432 11 ▶ return $self->best_option($long, $short, 1) if !$has_no;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_no 6 5 11
433  
434 5 ▶ if ( $self->help_package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->help_package) { } 3 2 5
435 3 ▶ die [ Getopt::Alt::Exception->new(
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 2 3
$long ? : 1 2 3
436 message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n",
437 option => ($long ? "--$long" : "-$short"),
438 ) ];
439 }
440 else {
441 2 ▶ die [ Getopt::Alt::Exception->new(
Branch: 2 branches
BranchTrueFalseTotal
$long ? : 1 1 2
$long ? : 1 1 2
442 help => 1,
443 message => "Unknown option '" . ($long ? "--$long" : "-$short") . "'\n",
444 option => ($long ? "--$long" : "-$short"),
445 ) ];
446 }
447 }
448  
449 sub _show_help {
450 5 ▶ my ($self, $verbosity, $msg) = @_;
Subroutine _show_help: called CC=5 SCAR=16.6
451  
452 5 my %input;
453 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
454 5 my $help = $self->help_package;
455 5 ▶ if ( !-f $help ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $help) 5 0 5
456 5 $help .= '.pm';
457 5 $help =~ s{::}{/}gxms;
458 }
459 5 %input = ( -input => $INC{$help} );
460 }
461  
462 5 require Tie::Handle::Scalar;
463 5 my $out = '';
464 5 tie *FH, 'Tie::Handle::Scalar', \$out;
465 5 require Pod::Usage;
466 5 ▶ Pod::Usage::pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
$msg ? : 0 5 5
467 $msg ? ( -msg => $msg ) : (),
468 -verbose => $verbosity,
469 -exitval => 'NOEXIT',
470 -output => \*FH,
471 %input,
472 );
473 5 die Getopt::Alt::Exception->new( message => $out, help => 1 );
474 }
475  
476 6 1;
477  
478 __END__
479  
480 =head1 NAME
481  
482 Getopt::Alt - Command line option passing with with lots of features
483  
484 =head1 VERSION
485  
486 This documentation refers to Getopt::Alt version 0.3.5.
487  
488 =head1 SYNOPSIS
489  
490 use Getopt::Alt;
491  
492 # OO Style usage
493 # Create a new options object
494 my $opt = Getopt::Alt->new(
495 {
496 default => { string => 'default' },
497 },
498 [
499 'string|s=s',
500 ...
501 ],
502 );
503 print "String = " . $opt->opt->{string} . "\n";
504  
505 # Getopt::Long like usage
506 use Getopt::Alt qw/get_options/;
507  
508 # most basic form
509 my $options = get_options(
510 'string|s=s',
511 'int|i=i',
512 'hash|h=s%',
513 'array|a=s@',
514 'increment|c+',
515 'nullable|n=s?',
516 'negatable|b!',
517 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
518 );
519 print Dumper $options->opt; # passed parameters
520 print join ',', @{ $options->files }; # non option parameters
521  
522 # with defaults
523 my $options = get_options(
524 { negatable => 1 },
525 'string|s=s',
526 'int|i=i',
527 'hash|h=s%',
528 'array|a=s@',
529 'increment|c+',
530 'nullable|n=s?',
531 'negatable|b!',
532 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
533 );
534  
535 # with configuration
536 my $options = get_options(
537 {
538 helper => 1, # default when using get_options
539 sub_command => 1, # stop processing at first non argument parameter
540 },
541 [
542 'string|s=s',
543 'int|i=i',
544 'hash|h=s%',
545 'array|a=s@',
546 'increment|c+',
547 'nullable|n=s?',
548 'negatable|b!',
549 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
550 ],
551 );
552 print $cmd; # sub command
553  
554 # with sub command details
555 my $options = get_options(
556 {
557 helper => 1, # default when using get_options
558 sub_command => {
559 sub => [ 'suboption' ],
560 other => [ 'verbose|v' ],
561 },
562 },
563 [
564 'string|s=s',
565 'int|i=i',
566 'hash|h=s%',
567 'array|a=s@',
568 'increment|c+',
569 'nullable|n=s?',
570 'negatable|b!',
571 'fixed_values|fixed-values|f=[a|bunch|of|fixed|values]',
572 ],
573 );
574 print Dumper $option->opt; # command with sub command options merged in
575  
576 # auto_complete
577 my $options = get_options(
578 {
579 helper => 1, # default when using get_options
580 auto_complete => sub {
581 my ($opt, $auto) = @_;
582 # ... code for auto completeion
583 # called if --auto-complete specified on the command line
584 },
585 },
586 [
587 'string|s=s',
588 'int|i=i',
589 ],
590 );
591  
592 =head1 DESCRIPTION
593  
594 The aim of C<Getopt::Alt> is to provide an alternative to L<Getopt::Long> that
595 allows a simple command line program to easily grow in complexity. L<Getopt::Lon>
596 can be expanded from a simple command line option passer to allow sub-commands.
597 Option processing may stop at the sub-command or with the help of modules can
598 cascade the processing into the sub command's module or config.
599  
600 The simple usage is quite similar to L<Getopt::Long>:
601  
602 In C<Getopt::Long> you might get your options like:
603  
604 use Getopt::Long;
605 my %options = ( string => 'default' );
606 GetOptions(
607 \%options,
608 'string|s=s',
609 ...
610 );
611  
612 The found options are now stored in the C<%options> hash.
613  
614 In C<Getopt::Alt> you might do the following:
615  
616 use Getopt::Alt qw/get_options/;
617 my %default = ( string => 'default' );
618 my $opt = get_options(
619 \%default,
620 'string|s=s',
621 ...
622 );
623 my %options = %{ $opt->opt };
624  
625 This will also result in the options stored in the C<%options> hash.
626  
627 Some other differences between Getopt::Alt and Getopt::Long include:
628  
629 =over 4
630  
631 =item *
632  
633 Bundling - is on by default
634  
635 =item *
636  
637 Case sensitivity is on by default
638  
639 =item *
640  
641 Throws error rather than returning errors.
642  
643 =item *
644  
645 Can work with sub commands
646  
647 =back
648  
649 =head1 SUBROUTINES/METHODS
650  
651 =head2 Exported
652  
653 =head3 C<get_options (@options | $setup, $options)>
654  
655 =head3 C<get_options ($default, 'opt1', 'opt2' ... )>
656  
657 This is the equivalent of calling new(...)->process but it does some extra
658 argument processing.
659  
660 B<Note>: The second form is the same basically the same as Getopt::Long's
661 GetOptions called with a hash ref as the first parameter.
662  
663 =head2 Class Methods
664  
665 =head3 C<new ( \%config, \@optspec )>
666  
667 =head4 config
668  
669 =over 4
670  
671 =item C<default> - HashRef
672  
673 Sets the default values for all the options. The values in opt will be reset
674 with the values in here each time process is called
675  
676 =item C<files> - ArrayRef[Str]
677  
678 Any arguments that not consumed as part of options (usually files), if no
679 arguments were passed to C<process> then this value would also be put back
680 into C<@ARGV>.
681  
682 =item C<bundle> - bool (Default true)
683  
684 Turns on bundling of arguments eg C<-rv> is equivalent to C<-r -v>. This is
685 on by default.
686  
687 =item C<ignore_case> - bool (Default true)
688  
689 Turns ignoring of the case of arguments, off by default.
690  
691 =item C<helper> - bool
692  
693 If set to a true value this will cause the help, man, and version options to
694 be added the end of your options list. (i.e. you get --help --man and
695 --version arguments for you program.)
696  
697 =item C<help_package> - Str
698  
699 The Perl package with the POD documentation for --help and --man, by default
700 it's the callers package.
701  
702 =item C<name> - Str (Default $0's basename)
703  
704 Used when displaying --version info
705  
706 =item C<cmds> - ArrayRef[Getopt::Alt::Command]
707  
708 If the Getopt::Alt is being used as part of a package where individual
709 commands have their own modules this parameter stores an instance of each
710 commands. (Not yet fully implemented.
711  
712 =item C<options> - Str (Default Getopt::Alt::Dynamic)
713  
714 The parent class for generating options.
715  
716 =item C<opt> - HashRef
717  
718 The values processed from the C<$ARGV> or arguments passed to the C<process>
719 method..
720  
721 =item C<default> - HashRef
722  
723 The default values for each option. The default value is not modified by
724 processing, so if set the same default will be used from call to call.
725  
726 =item C<aliases> - HashRef[ArrayRef[Str]]
727  
728 When using sub-commands this allows you to configure aliases for those
729 commands, aliases are recursed, they can have extra arguments though.
730 If a configuration file is used aliases can be specified in that file.
731  
732 =item C<conf_prefix> - Str (Default ".")
733  
734 The prefix for finding the configuration files. By default the following
735 is used:
736  
737 =over 4
738  
739 =item *
740  
741 ./$conf_prefix$name
742  
743 =item *
744  
745 ~/$conf_prefix$name
746  
747 =item *
748  
749 /etc/$conf_prefix$name
750  
751 =back
752  
753 =back
754  
755 =head2 Object Methods
756  
757 =head3 C<BUILD ()>
758  
759 internal method
760  
761 =head3 C<process ()>
762  
763 =head3 C<list_options ()>
764  
765 Returns a list of all command line options in the current object.
766  
767 =head3 C<best_option ()>
768  
769 =head1 DIAGNOSTICS
770  
771 =head1 CONFIGURATION AND ENVIRONMENT
772  
773 Configuration files can be used to specify default values and aliases. They
774 can be located in the current directory, $HOME or /etc.The file name is
775 specified by the C<name> attribute (which defaults to the program's name)
776 and is prepended with a dot. eg:
777  
778 For a program called as C<$ ./foo> or C<$ foo> C<name> would be set to foo
779 and possible configuration names would be
780  
781 =over 4
782  
783 =item *
784  
785 .foo.yml
786  
787 =item *
788  
789 ~/.foo.rc
790  
791 =item *
792  
793 /etc/.foo.yml
794  
795 =back
796  
797 See L<Config::Any> for information about config formats and file extensions.
798  
799 =head1 DEPENDENCIES
800  
801 =head1 INCOMPATIBILITIES
802  
803 =head1 BUGS AND LIMITATIONS
804  
805 There are no known bugs in this module.
806  
807 Please report problems to Ivan Wills (ivan.wills@gmail.com).
808  
809 Patches are welcome.
810  
811 =head1 AUTHOR
812  
813 Ivan Wills - (ivan.wills@gmail.com)
814  
815 =head1 LICENSE AND COPYRIGHT
816  
817 Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
818 All rights reserved.
819  
820 This module is free software; you can redistribute it and/or modify it under
821 the same terms as Perl itself. See L<perlartistic>. This program is
822 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
823 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
824 PARTICULAR PURPOSE.
825  
826 =cut
« blib/lib/Getopt/Alt/Command.pm ↑ Summary Build »