Venus/Args.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 81.8% 18 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 77.7% 7 / 9 totaltotal 93.2% 97 / 104 CC 13 SCAR 8.5
CC
13
Cov
94%
exists
3.0
get
3.0
set
3.0
CRAP 2.4
« Venus/Kind.pm ↑ Summary Venus/Core.pm »
1 package Venus::Args;
2  
3 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 base 'Venus::Kind::Utility';
11  
12 2 with 'Venus::Role::Accessible';
13 2 with 'Venus::Role::Proxyable';
14  
15 # ATTRIBUTES
16  
17 2 attr 'named';
18  
19 # BUILDERS
20  
21 sub build_proxy {
22 2 ▶ my ($self, $package, $method, $value) = @_;
Subroutine build_proxy: called CC=1 SCAR=0.2
Pod undocumented
23  
24 2 my $has_value = exists $_[3];
25  
26 return sub {
27 2 ▶ return $self->get($method) if !$has_value; # no value
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
unless $has_value 2 0 2
28 0 return $self->set($method, $value);
29 2 };
30 }
31  
32 sub build_self {
33 16 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=2 SCAR=6.9
Pod undocumented
34  
35 16 ▶ $self->named({}) if !$self->named;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->named 1 15 16
36  
37 16 return $self;
38 }
39  
40 # METHODS
41  
42 sub default {
43 2 ▶ my ($self) = @_;
Subroutine default: called CC=1 SCAR=0.0
Pod documented
44  
45 2 return [@ARGV];
46 }
47  
48 sub exists {
49 3 ▶ my ($self, $name) = @_;
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
50  
51 3 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
52  
53 3 my $pos = $self->name($name);
54  
55 3 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 2 3
56  
57 2 return CORE::exists $self->indexed->{$pos};
58 }
59  
60 sub get {
61 5 ▶ my ($self, $name) = @_;
Subroutine get: called CC=3 SCAR=11.0
Pod documented
62  
63 5 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5 5
64  
65 5 my $pos = $self->name($name);
66  
67 5 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 4 5
68  
69 4 return $self->indexed->{$pos};
70 }
71  
72 sub indexed {
73 15 ▶ my ($self) = @_;
Subroutine indexed: called CC=1 SCAR=0.0
Pod documented
74  
75 15 return {map +($_, $self->value->[$_]), 0..$#{$self->value}};
76 }
77  
78 sub name {
79 12 ▶ my ($self, $name) = @_;
Subroutine name: called CC=3 SCAR=11.0
Pod documented
80  
81 12 ▶ if (defined $self->named->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->named->{$name}) 6 6 12
82 6 return $self->named->{$name};
83 }
84  
85 6 ▶ if (defined $self->indexed->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->indexed->{$name}) 3 3 6
86 3 return $name;
87 }
88  
89 3 return undef;
90 }
91  
92 sub set {
93 3 ▶ my ($self, $name, $data) = @_;
Subroutine set: called CC=3 SCAR=11.0
Pod documented
94  
95 3 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
96  
97 3 my $pos = $self->name($name);
98  
99 3 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 2 3
100  
101 2 return $self->value->[$pos] = $data;
102 }
103  
104 sub unnamed {
105 2 ▶ my ($self) = @_;
Subroutine unnamed: called CC=3 SCAR=11.0
Pod documented
106  
107 2 my $list = [];
108  
109 2 my $argv = $self->indexed;
110 2 my $data = +{reverse %{$self->named}};
111  
112 2 for my $index (sort keys %$argv) {
113 8 ▶ unless (exists $data->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{$index}) 5 3 8
114 5 push @$list, $argv->{$index};
115 }
116 }
117  
118 2 return $list;
119 }
120  
121 2 1;
122  
123  
124  
125 =head1 NAME
126  
127 Venus::Args - Args Class
128  
129 =cut
130  
131 =head1 ABSTRACT
132  
133 Args Class for Perl 5
134  
135 =cut
136  
137 =head1 SYNOPSIS
138  
139 package main;
140  
141 use Venus::Args;
142  
143 my $args = Venus::Args->new(
144 named => { flag => 0, command => 1 }, # optional
145 value => ['--help', 'execute'],
146 );
147  
148 # $args->flag; # $ARGV[0]
149 # $args->get(0); # $ARGV[0]
150 # $args->get(1); # $ARGV[1]
151 # $args->action; # $ARGV[1]
152 # $args->exists(0); # exists $ARGV[0]
153 # $args->exists('flag'); # exists $ARGV[0]
154 # $args->get('flag'); # $ARGV[0]
155  
156 =cut
157  
158 =head1 DESCRIPTION
159  
160 This package provides methods for accessing C<@ARGS> items.
161  
162 =cut
163  
164 =head1 ATTRIBUTES
165  
166 This package has the following attributes:
167  
168 =cut
169  
170 =head2 named
171  
172 named(HashRef)
173  
174 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
175  
176 =cut
177  
178 =head1 INHERITS
179  
180 This package inherits behaviors from:
181  
182 L<Venus::Kind::Utility>
183  
184 =cut
185  
186 =head1 INTEGRATES
187  
188 This package integrates behaviors from:
189  
190 L<Venus::Role::Accessible>
191  
192 L<Venus::Role::Proxyable>
193  
194 =cut
195  
196 =head1 METHODS
197  
198 This package provides the following methods:
199  
200 =cut
201  
202 =head2 default
203  
204 default() (ArrayRef)
205  
206 The default method returns the default value, i.e. C<@ARGV>.
207  
208 I<Since C<0.01>>
209  
210 =over 4
211  
212 =item default example 1
213  
214 # given: synopsis;
215  
216 my $default = $args->default;
217  
218 # [@ARGV]
219  
220 # ["--help", "execute"]
221  
222 =back
223  
224 =cut
225  
226 =head2 exists
227  
228 exists(Str $key) (Bool)
229  
230 The exists method returns truthy or falsy if an index or alias value exists.
231  
232 I<Since C<0.01>>
233  
234 =over 4
235  
236 =item exists example 1
237  
238 # given: synopsis;
239  
240 my $exists = $args->exists(0);
241  
242 # 1
243  
244 =back
245  
246 =over 4
247  
248 =item exists example 2
249  
250 # given: synopsis;
251  
252 my $exists = $args->exists('flag');
253  
254 # 1
255  
256 =back
257  
258 =over 4
259  
260 =item exists example 3
261  
262 # given: synopsis;
263  
264 my $exists = $args->exists(2);
265  
266 # undef
267  
268 =back
269  
270 =cut
271  
272 =head2 get
273  
274 get(Str $key) (Any)
275  
276 The get method returns the value of the index or alias.
277  
278 I<Since C<0.01>>
279  
280 =over 4
281  
282 =item get example 1
283  
284 # given: synopsis;
285  
286 my $get = $args->get(0);
287  
288 # "--help"
289  
290 =back
291  
292 =over 4
293  
294 =item get example 2
295  
296 # given: synopsis;
297  
298 my $get = $args->get('flag');
299  
300 # "--help"
301  
302 =back
303  
304 =over 4
305  
306 =item get example 3
307  
308 # given: synopsis;
309  
310 my $get = $args->get(2);
311  
312 # undef
313  
314 =back
315  
316 =cut
317  
318 =head2 indexed
319  
320 indexed() (HashRef)
321  
322 The indexed method returns a set of indices and values.
323  
324 I<Since C<0.01>>
325  
326 =over 4
327  
328 =item indexed example 1
329  
330 # given: synopsis;
331  
332 my $indexed = $args->indexed;
333  
334 # { "0" => "--help", "1" => "execute" }
335  
336 =back
337  
338 =cut
339  
340 =head2 name
341  
342 name(Str $key) (Str | Undef)
343  
344 The name method resolves and returns the index for an index or alias, and
345 returns undefined if not found.
346  
347 I<Since C<0.01>>
348  
349 =over 4
350  
351 =item name example 1
352  
353 # given: synopsis;
354  
355 my $name = $args->name('flag');
356  
357 =back
358  
359 =cut
360  
361 =head2 set
362  
363 set(Str $key, Any $data) (Any)
364  
365 The set method sets and returns the value of an index or alias.
366  
367 I<Since C<0.01>>
368  
369 =over 4
370  
371 =item set example 1
372  
373 # given: synopsis;
374  
375 my $set = $args->set(0, '-?');
376  
377 # "-?"
378  
379 =back
380  
381 =over 4
382  
383 =item set example 2
384  
385 # given: synopsis;
386  
387 my $set = $args->set('flag', '-?');
388  
389 # "-?"
390  
391 =back
392  
393 =over 4
394  
395 =item set example 3
396  
397 # given: synopsis;
398  
399 my $set = $args->set('verbose', 1);
400  
401 # undef
402  
403 =back
404  
405 =cut
406  
407 =head2 unnamed
408  
409 unnamed() (ArrayRef)
410  
411 The unnamed method returns a list of unaliases indices.
412  
413 I<Since C<0.01>>
414  
415 =over 4
416  
417 =item unnamed example 1
418  
419 package main;
420  
421 use Venus::Args;
422  
423 my $args = Venus::Args->new(
424 named => { flag => 0, command => 1 },
425 value => ['--help', 'execute', '--format', 'markdown'],
426 );
427  
428 my $unnamed = $args->unnamed;
429  
430 # ["--format", "markdown"]
431  
432 =back
433  
434 =over 4
435  
436 =item unnamed example 2
437  
438 package main;
439  
440 use Venus::Args;
441  
442 my $args = Venus::Args->new(
443 named => { command => 1 },
444 value => ['execute', 'phase-1', '--format', 'markdown'],
445 );
446  
447 my $unnamed = $args->unnamed;
448  
449 # ["execute", "--format", "markdown"]
450  
451 =back
452  
453 =cut
« Venus/Kind.pm ↑ Summary Venus/Core.pm »