blib/lib/Venus/Args.pm

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