blib/lib/Getopt/Alt/Option.pm

filter: Statementstmt 100% 129 / 129 Branchbran 100% 74 / 74 Conditioncond 94.1% 48 / 51 MC/DCmcdc 82.7% 24 / 29 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 97.2% 286 / 294 CC 56 SCAR 32.6
CC
56
Cov
99%
process
33.0
build_option
24.0
BEGIN
1.0
CRAP 26.1
↑ Summary blib/lib/Getopt/Alt/Command.pm »
1 package Getopt::Alt::Option;
2  
3 # Created on: 2009-07-17 14:52:26
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Getopt::Alt::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 Moose::Exporter->setup_import_methods(
18 as_is => [qw/build_option/],
19 #with_meta => ['operation'],
20 );
21  
22 9 our $VERSION = version->new('0.3.5');
23  
24 9 Moose::Util::meta_attribute_alias('Getopt::Alt::Option');
25  
26 9 has opt => (
27 is => 'ro',
28 required => 1,
29 );
30 9 has name => (
31 is => 'rw',
32 isa => 'Str',
33 required => 1,
34 );
35 9 has names => (
36 is => 'rw',
37 isa => 'ArrayRef[Str]',
38 required => 1,
39 );
40 9 has increment => (
41 is => 'rw',
42 isa => 'Bool',
43 );
44 9 has number => (
45 is => 'rw',
46 isa => 'Bool',
47 );
48 9 has negatable => (
49 is => 'rw',
50 isa => 'Bool',
51 );
52 9 has nullable => (
53 is => 'rw',
54 isa => 'Bool',
55 );
56 9 has config => (
57 is => 'ro',
58 isa => 'Bool',
59 );
60 9 has project => (
61 is => 'ro',
62 isa => 'Bool',
63 );
64 9 has ref => (
65 is => 'ro',
66 isa => 'Str',
67 );
68 9 has type => (
69 is => 'ro',
70 isa => 'Str',
71 );
72 9 has value => (
73 is => 'rw',
74 isa => 'Any',
75 predicate => 'has_value',
76 );
77 9 has values => (
78 is => 'rw',
79 isa => 'ArrayRef',
80 predicate => 'has_values',
81 );
82  
83 9 my $r_name = qr/ [^|\s=+!?@%-][^|\s=+!?@%]* /xms;
84 9 my $r_alt_name = qr/ $r_name | \\d /xms;
85 9 my $r_names = qr/ $r_name (?: [|] $r_alt_name)* /xms;
86 9 my $r_type = qr/ [nifsd] /xms;
87 9 my $r_ref = qr/ [%@] /xms;
88 9 my $r_sub_val = qr/ [\w-]+ /xms;
89 9 my $r_values = qr/ \[ $r_sub_val (?: [|] $r_sub_val )* \] /xms;
90 9 my $r_type_ref = qr/ = (?: $r_type $r_ref? | $r_values ) /xms;
91 9 my $r_inc = qr/ [+] /xms;
92 9 my $r_number = qr/ [+][+] /xms;
93 9 my $r_neg = qr/ [!] /xms;
94 9 my $r_null = qr/ [?] /xms;
95 9 my $r_spec = qr/^ ( $r_names ) ( $r_inc | $r_number | $r_neg | $r_type_ref )? ( $r_null )? $/xms;
96  
97 # calling new => ->new( 'test|t' )
98 # ->new( name => 'text', names => [qw/test tes te t/], ... )
99 # ->new({ name => 'text', names => [qw/test tes te t/], ... )
100 sub build_option {
101 530 ▶ my ($class, @params) = @_;
Subroutine build_option: called CC=24 SCAR=31.8
Pod documented
102  
103 530 ▶ if (@params == 1 && ref $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1 and ref $params[0]) 6 524 530
Condition100%3/3
@params == 1 and ref $params[0]
!ll&&!rl&&r
15236
MC/DC100%2/2
@params == 1 and ref $params[0]
@params == 1 ref $params[0]
Truth table100%3/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
104 @params =
105 4 ▶ ref $params[0] eq 'ARRAY' ? @{ $params[0] }
Branch: 1 branch
BranchTrueFalseTotal
ref $params[0] eq 'HASH' ? : 1 1 2
106 6 ▶ : ref $params[0] eq 'HASH' ? %{ $params[0] }
Branch: 1 branch
BranchTrueFalseTotal
ref $params[0] eq 'ARRAY' ? : 4 2 6
107 : confess "Can't supply a " . (ref $params[0]) . " ref to new!";
108 }
109  
110 # construct attribute params string if one element
111 529 ▶ if (@params == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params == 1) 527 2 529
112 527 my $spec = pop @params;
113 527 push @params, (opt => $spec);
114  
115 527 ▶ confess "$spec doesn't match the specification definition! (qr/$r_spec/)" if $spec !~ /$r_spec/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $spec =~ /$r_spec/msx 3 524 527
116  
117 524 my ($names, $options, $null) = $spec =~ /$r_spec/xms;
118 524 my @names = split /\|/xms, $names;
119 524 push @params, 'names', \@names;
120 524 push @params, 'name', $names[0];
121  
122 524 ▶ if ($null) {
Branch: 1 branch
BranchTrueFalseTotal
if ($null) 2 522 524
123 2 push @params, 'nullable' => 1;
124 }
125  
126 524 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 250 274 524
127 250 my ($type, $extra);
128 250 my ($option) = substr $options, 0, 1;
129  
130 250 ▶ if ($option eq '=') {
Branch: 3 branches
BranchTrueFalseTotal
if ($option eq '=') { } 210 40 250
elsif ($options eq '++') { } 2 38 40
elsif ($option eq '+') { } 4 34 38
131 210 ($type, $extra) = split /;/xms, $options;
132 }
133 elsif ($options eq '++') {
134 2 push @params, 'increment' => 1;
135 2 push @params, 'number' => 1;
136 2 push @params, 'type' => 'Int';
137 }
138 elsif ($option eq '+') {
139 4 push @params, 'increment' => 1;
140 }
141 else {
142 # $option == !
143 34 push @params, 'negatable' => 1;
144 }
145  
146 250 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 210 40 250
147 210 my ($text, $ref);
148 210 $type =~ s/^=//xms;
149  
150 210 ▶ if ( length $type == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $type == 1) { } 159 51 210
elsif (length $type == 2) { } 48 3 51
151 159 ($text) = $type =~ /^ ($r_type) $/xms;
152 }
153 elsif ( length $type == 2 ) {
154 48 ($text, $ref) = $type =~ /^ ($r_type) ($r_ref) $/xms;
155 48 ▶ push @params, ref => $ref eq '%' ? 'HashRef' : 'ArrayRef';
Branch: 1 branch
BranchTrueFalseTotal
$ref eq '%' ? : 18 30 48
156 }
157 else {
158 3 $type =~ s/(?: ^\[ | \]$ )//gxms;
159 3 my @values = split /[|]/, $type;
160 3 $text = 's';
161 3 push @params, values => \@values;
162 }
163  
164 210 ▶ push @params,
Branch: 4 branches
BranchTrueFalseTotal
$text eq 'd' ? : 1 209 210
$text eq 'i' ? : 45 164 209
$text eq 'f' ? : 4 160 164
$text eq 'n' ? : 5 155 160
165 type =>
166 $text eq 'd' ? 'Int'
167 : $text eq 'i' ? 'Int'
168 : $text eq 'f' ? 'Num'
169 : $text eq 'n' ? 'Num'
170 : 'Str';
171 }
172 }
173 }
174  
175 526 my %params = @params;
176 526 $params{traits} = ['Getopt::Alt::Option'];
177  
178 my $type
179 = $params{type} && $params{ref} ? "$params{ref}\[$params{type}\]"
180 : $params{type} ? $params{type}
181 526 ▶ : 'Str';
Branch: 2 branches
BranchTrueFalseTotal
$params{'type'} && $params{'ref'} ? : 48 478 526
$params{'type'} ? : 164 314 478
Condition100%3/3
$params{"type"} and $params{"ref"}
!ll&&!rl&&r
31416448
MC/DC100%2/2
$params{"type"} and $params{"ref"}
$params{"type"} $params{"ref"}
Truth table100%3/3
A and B
A= $params{"type"}B= $params{"ref"}
ABresult
0X0
100
111
182  
183 526 ▶ if ( $params{nullable} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"nullable"}) 2 524 526
184 2 $type = "Maybe[$type]";
185 }
186  
187 $class->add_attribute(
188 $params{name},
189 526 is => 'rw',
190 isa => $type,
191 %params,
192 );
193  
194 526 return $class->get_attribute( $params{name} );
195 }
196  
197 sub process {
198 90 ▶ my ($self, $long, $short, $arg_data, $args) = @_;
Subroutine process: called CC=33 SCAR=35.0
Pod documented
199  
200 90 ▶ my $name = $long ? "--$long" : "-$short";
Branch: 1 branch
BranchTrueFalseTotal
$long ? : 35 55 90
201 90 my $value;
202 90 my $used = 0;
203 90 ▶ if ($self->type) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->type) { } 66 24 90
elsif ($self->increment) { } 8 16 24
elsif ($self->negatable) { } 8 8 16
204 66 $used = 1;
205 66 ▶ if ( !defined $arg_data || length $arg_data == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $arg_data or length $arg_data == 0) { } 37 29 66
elsif ($arg_data and not $self->nullable) { } 23 7 30
Condition100%3/3
not defined $arg_data or length $arg_data == 0
l!l&&r!l&&!r
122529
MC/DC100%2/2
not defined $arg_data or length $arg_data == 0
not defined $arg_data length $arg_data == 0
Truth table100%3/3
not(A) or B
A= not defined $arg_dataB= length $arg_data == 0
ABresult
1X1
011
000
206 37 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args->[0] || $self->nullable or $args->[0] and $args->[0] =~ /^-/msx and not $self->type eq "Int" || $self->type eq "Num") 3 34 37
Condition80%12/15
not defined $args->[0] || $self->nullable or $args->[0] and $args->[0] =~ /^-/msx and not $self->type eq "Int" || $self->type eq "Num"
l!l&&r!l&&!r
3034
defined $args->[0] || $self->nullable
l!l&&r!l&&!r
3223
$args->[0] and $args->[0] =~ /^-/msx and not $self->type eq "Int" || $self->type eq "Num"
!ll&&!rl&&r
2860
$args->[0] and $args->[0] =~ /^-/msx
!ll&&!rl&&r
2266
$self->type eq 'Int' || $self->type eq 'Num'
l!l&&r!l&&!r
420
MC/DC16%1/6
not defined $args->[0] || $self->nullable or $args->[0] and $args->[0] =~ /^-/msx and not $self->type eq "Int" || $self->type eq "Num"
defined $args->[0] $self->nullable $args->[0] $args->[0] =~ /^-/msx $self->type eq 'Int' $self->type eq 'Num'
Truth table27%3/11
not(A || B) or C and D and not(E || F)
A= defined $args->[0]B= $self->nullableC= $args->[0]D= $args->[0] =~ /^-/msxE= $self->type eq 'Int'F= $self->type eq 'Num'
ABCDEFresult
00XXXX1
1X11001
0111001
1X0XXX0
1X10XX0
1X111X0
1X11010
010XXX0
0110XX0
01111X0
0111010
207 ( ! defined $args->[0] && !$self->nullable )
208 || (
209 $args->[0]
210 && $args->[0] =~ /^-/xms
211 && !( $self->type eq 'Int' || $self->type eq 'Num' )
212 )
213 ) {
214 3 die [
215 Getopt::Alt::Exception->new(
216 message => "The option '$name' requires an " . $self->type . " argument\n",
217 option => $name,
218 type => $self->type
219 )
220 ];
221 }
222  
223 34 $arg_data = shift @$args;
224 }
225 ▶ elsif ( $arg_data && !$self->nullable ) {
Condition100%3/3
$arg_data and not $self->nullable
!ll&&!rl&&r
1523
MC/DC100%2/2
$arg_data and not $self->nullable
$arg_data not $self->nullable
Truth table100%3/3
A and not(B)
A= $arg_dataB= not $self->nullable
ABresult
0X0
100
111
226 23 $arg_data =~ s/^=//xms;
227 }
228  
229 63 my $key;
230 63 ▶ if ($self->ref && $self->ref eq 'HashRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ref and $self->ref eq "HashRef") 4 59 63
Condition100%3/3
$self->ref and $self->ref eq "HashRef"
!ll&&!rl&&r
5364
MC/DC100%2/2
$self->ref and $self->ref eq "HashRef"
$self->ref $self->ref eq "HashRef"
Truth table100%3/3
A and B
A= $self->refB= $self->ref eq "HashRef"
ABresult
0X0
100
111
231 4 ($key, $arg_data) = split /=/xms, $arg_data, 2;
232 }
233  
234 63 $DB::single = 1;
235 63 ▶ $value =
Branch: 4 branches
BranchTrueFalseTotal
$self->nullable && !$arg_data ? : 2 61 63
$self->type eq 'Int' && $arg_data =~ /^ -? \d+$/msx ? : 27 34 61
$self->type eq 'Num' && $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/msx ? : 10 24 34
$self->type eq 'Str' ? : 21 3 24
Condition100%9/9
$self->nullable and not $arg_data
!ll&&!rl&&r
5292
$self->type eq "Int" and $arg_data =~ /^ -? \d+$/msx
!ll&&!rl&&r
32227
$self->type eq "Num" and $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/msx
!ll&&!rl&&r
23110
MC/DC100%2/2
$self->nullable and not $arg_data
$self->nullable not $arg_data
MC/DC100%2/2
$self->type eq "Int" and $arg_data =~ /^ -? \d+$/msx
$self->type eq "Int" $arg_data =~ /^ -? \d+$/msx
MC/DC100%2/2
$self->type eq "Num" and $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/msx
$self->type eq "Num" $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/msx
Truth table100%3/3
A and not(B)
A= $self->nullableB= not $arg_data
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $self->type eq "Int"B= $arg_data =~ /^ -? \d+$/msx
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $self->type eq "Num"B= $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/msx
ABresult
0X0
100
111
236 $self->nullable && ! $arg_data ? undef
237 : $self->type eq 'Int' && $arg_data =~ /^ -? \d+$/xms ? $arg_data
238 : $self->type eq 'Num' && $arg_data =~ /^ -? (?: \d* (?: [.]\d+ )? | \d+ )$/xms ? $arg_data
239 : $self->type eq 'Str' ? $arg_data
240 : confess "Unknown type '".$self->type."'\n";
241  
242 60 ▶ if ($self->values && !grep { $value eq $_ } @{ $self->values }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->values and not grep({$value eq $_;} @{$self->values;})) 1 59 60
Condition100%3/3
$self->values and not grep({$value eq $_;} @{$self->values;})
!ll&&!rl&&r
5631
MC/DC100%2/2
$self->values and not grep({$value eq $_;} @{$self->values;})
$self->values not grep({$value eq $_;} @{$self->values;})
Truth table100%3/3
A and not(B)
A= $self->valuesB= not grep({$value eq $_;} @{$self->values;})
ABresult
0X0
100
111
243 die [ Getopt::Alt::Exception->new(
244 1 message => "The option '$name' must be one of " . ( join ', ', @{ $self->values } ) . "\n",
245 option => $name,
246 type => $self->type
247 ) ]
248 }
249  
250 59 ▶ if ($self->ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ref) 10 49 59
251 10 my $old;
252 10 ▶ if ($self->ref eq 'ArrayRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ref eq 'ArrayRef') { } 6 4 10
253 6 ▶ $old = $self->value || [];
Condition100%2/2
$self->value || []
l!l
33
MC/DC100%1/1
$self->value || []
$self->value
Truth table100%2/2
A
A= $self->value
Aresult
11
00
254 6 push @$old, $value;
255 }
256 else {
257 4 ▶ $old = $self->value || {};
Condition100%2/2
$self->value || {}
l!l
13
MC/DC100%1/1
$self->value || {}
$self->value
Truth table100%2/2
A
A= $self->value
Aresult
11
00
258 4 $old->{$key} = $value;
259 }
260 10 $value = $old;
261 }
262 }
263 elsif ($self->increment) {
264 8 ▶ $value = ($self->value || 0) + 1;
Condition100%2/2
$self->value || 0
l!l
35
MC/DC100%1/1
$self->value || 0
$self->value
Truth table100%2/2
A
A= $self->value
Aresult
11
00
265 }
266 elsif ($self->negatable) {
267 8 ▶ $value = $long && $long =~ /^no-/xms ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$long && $long =~ /^no-/msx ? : 2 6 8
Condition100%3/3
$long and $long =~ /^no-/msx
!ll&&!rl&&r
422
MC/DC100%2/2
$long and $long =~ /^no-/msx
$long $long =~ /^no-/msx
Truth table100%3/3
A and B
A= $longB= $long =~ /^no-/msx
ABresult
0X0
100
111
268 }
269 else {
270 8 $value = 1;
271 }
272  
273 83 $self->value($value);
274  
275 83 return ( $self->value, $used );
276 }
277  
278 9 1;
279  
280 __END__
281  
282 =head1 NAME
283  
284 Getopt::Alt::Option - Sets up a particular command line option
285  
286 =head1 VERSION
287  
288 This documentation refers to Getopt::Alt::Option version 0.3.5.
289  
290 =head1 SYNOPSIS
291  
292 use Getopt::Alt::Option;
293  
294 # Brief but working code example(s) here showing the most common usage(s)
295 # This section will be as far as many users bother reading, so make it as
296 # educational and exemplary as possible.
297  
298 =head1 DESCRIPTION
299  
300 =head1 SUBROUTINES/METHODS
301  
302 =head2 C<build_option ( $class, @params )>
303  
304 This is a helper function to create an C<Getopt::Alt::Option> attribute on the
305 supplied C<$class> object.
306  
307 =head2 C<process ($long, $short, $arg_data, $args)>
308  
309 Processes the option against the supplied $arg_data or $args->[0] if no $arg_data is set
310  
311 =head1 DIAGNOSTICS
312  
313 =head1 CONFIGURATION AND ENVIRONMENT
314  
315 =head1 DEPENDENCIES
316  
317 =head1 INCOMPATIBILITIES
318  
319 =head1 BUGS AND LIMITATIONS
320  
321 There are no known bugs in this module.
322  
323 Please report problems to Ivan Wills (ivan.wills@gmail.com).
324  
325 Patches are welcome.
326  
327 =head1 AUTHOR
328  
329 Ivan Wills - (ivan.wills@gmail.com)
330  
331 =head1 LICENSE AND COPYRIGHT
332  
333 Copyright (c) 2009 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
334 All rights reserved.
335  
336 This module is free software; you can redistribute it and/or modify it under
337 the same terms as Perl itself. See L<perlartistic>. This program is
338 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
339 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
340 PARTICULAR PURPOSE.
341  
342 =cut
↑ Summary blib/lib/Getopt/Alt/Command.pm »