blib/lib/Venus/Args.pm

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