blib/lib/Venus/Opts.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 80.5% 29 / 36 Conditioncond 75.0% 3 / 4 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 17 / 17 Podpod 72.7% 8 / 11 totaltotal 91.9% 149 / 162 CC 22 SCAR 11.5
CC
22
Cov
93%
build_self
6.1
reparse
4.1
exists
3.0
CRAP 3.2
« blib/lib/Venus/Check.pm ↑ Summary blib/lib/Venus/Atom.pm »
1 package Venus::Opts;
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 3 attr 'parsed';
27 3 attr 'specs';
28 3 attr 'warns';
29 3 attr 'unused';
30  
31 # BUILDERS
32  
33 8 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
34 8 my ($self, $data) = @_;
35  
36 return {
37 8 value => $data,
38 };
39 }
40  
41 31 ▶sub build_self {
Subroutine build_self: called CC=6 SCAR=18.0
Pod undocumented
42 31 my ($self, $data) = @_;
43  
44 31 ▶ $self->named({}) if !$self->named;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->named 12 19 31
45 31 ▶ $self->parsed({}) if !$self->parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parsed 29 2 31
46 31 ▶ $self->specs([]) if !$self->specs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->specs 12 19 31
47 31 ▶ $self->warns([]) if !$self->warns;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->warns 31 0 31
48 31 ▶ $self->unused([]) if !$self->unused;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->unused 31 0 31
49  
50 31 return $self->parse;
51 }
52  
53 3 ▶sub build_proxy {
Subroutine build_proxy: called CC=1 SCAR=0.2
Pod undocumented
54 3 my ($self, $package, $method, $value) = @_;
55  
56 3 my $has_value = exists $_[3];
57  
58 3 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
59 3 ▶ return $self->get($method) if !$has_value; # no value
Branch: 1 branch
BranchTrueFalseTotal
unless $has_value 3 0 3
60 0 return $self->set($method, $value);
61 3 };
62 }
63  
64 # METHODS
65  
66 2 ▶sub default {
Subroutine default: called CC=1 SCAR=0.0
Pod documented
67 2 my ($self) = @_;
68  
69 2 return [@ARGV];
70 }
71  
72 3 ▶sub exists {
Subroutine exists: called CC=3 SCAR=11.0
Pod documented
73 3 my ($self, $name) = @_;
74  
75 3 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
76  
77 3 my $pos = $self->name($name);
78  
79 3 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 2 3
80  
81 2 return CORE::exists $self->parsed->{$pos};
82 }
83  
84 7 ▶sub get {
Subroutine get: called CC=3 SCAR=11.0
Pod documented
85 7 my ($self, $name) = @_;
86  
87 7 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
88  
89 7 my $pos = $self->name($name);
90  
91 7 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 6 7
92  
93 6 return $self->parsed->{$pos};
94 }
95  
96 41 ▶sub parse {
Subroutine parse: called CC=3 SCAR=11.0
Pod documented
97 41 my ($self, $extras) = @_;
98  
99 41 ▶ return $self if %{$self->parsed};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->parsed;} 3 38 41
100  
101 38 my $value = $self->value;
102 38 my $specs = $self->specs;
103  
104 38 my $parsed = {};
105 38 my @configs = qw(default no_auto_abbrev no_ignore_case);
106  
107 38 ▶ $extras = [] if !$extras;
Branch: 1 branch
BranchTrueFalseTotal
unless $extras 30 8 38
108  
109 38 require Getopt::Long;
110 38 require Text::ParseWords;
111  
112 38 my $warns = [];
113 11 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 11 push @$warns, map s/\n+$//gr, @_;
115 11 return;
116 38 };
117  
118 # configure parser
119 38 Getopt::Long::Configure(Getopt::Long::Configure(@configs, @$extras));
120  
121 # parse args using spec
122 38 my ($returned, $unused) = Getopt::Long::GetOptionsFromString(
123 join(' ', Text::ParseWords::shellwords(map quotemeta, @$value)),
124 $parsed,
125 @$specs
126 );
127  
128 38 $self->unused($unused);
129 38 $self->parsed($parsed);
130 38 $self->warns($warns);
131  
132 38 return $self;
133 }
134  
135 16 ▶sub name {
Subroutine name: called CC=3 SCAR=11.0
Pod documented
136 16 my ($self, $name) = @_;
137  
138 16 ▶ if (defined $self->named->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->named->{$name}) 5 11 16
139 5 return $self->named->{$name};
140 }
141  
142 11 ▶ if (defined $self->parsed->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->parsed->{$name}) 7 4 11
143 7 return $name;
144 }
145  
146 4 return undef;
147 }
148  
149 8 ▶sub reparse {
Subroutine reparse: called CC=4 SCAR=14.2
Pod documented
150 8 my ($self, $specs, $extras) = @_;
151  
152 8 $self->parsed({});
153 8 ▶ $self->specs($specs || []) if defined $specs;
Branch: 1 branch
BranchTrueFalseTotal
if defined $specs 8 0 8
Condition50%1/2
$specs || []
l!l
80
MC/DC0%0/1
$specs || []
$specs
Truth table50%1/2
A
A= $specs
Aresult
11
00
154  
155 8 ▶ return $self->parse($extras || []);
Condition100%2/2
$extras || []
l!l
17
MC/DC100%1/1
$extras || []
$extras
Truth table100%2/2
A
A= $extras
Aresult
11
00
156 }
157  
158 3 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
159 3 my ($self, $name, $data) = @_;
160  
161 3 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
162  
163 3 my $pos = $self->name($name);
164  
165 3 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 2 3
166  
167 2 return $self->parsed->{$pos} = $data;
168 }
169  
170 1 ▶sub unnamed {
Subroutine unnamed: called CC=3 SCAR=11.0
Pod documented
171 1 my ($self) = @_;
172  
173 1 my $list = [];
174  
175 1 my $opts = $self->parsed;
176 1 my $data = +{reverse %{$self->named}};
177  
178 1 for my $index (sort keys %$opts) {
179 2 ▶ unless (exists $data->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{$index}) 1 1 2
180 1 push @$list, $opts->{$index};
181 }
182 }
183  
184 1 return $list;
185 }
186  
187 3 1;
188  
189  
190  
191 =head1 NAME
192  
193 Venus::Opts - Opts Class
194  
195 =cut
196  
197 =head1 ABSTRACT
198  
199 Opts Class for Perl 5
200  
201 =cut
202  
203 =head1 SYNOPSIS
204  
205 package main;
206  
207 use Venus::Opts;
208  
209 my $opts = Venus::Opts->new(
210 value => ['--resource', 'users', '--help'],
211 specs => ['resource|r=s', 'help|h'],
212 named => { method => 'resource' } # optional
213 );
214  
215 # $opts->method; # $resource
216 # $opts->get('resource'); # $resource
217  
218 # $opts->help; # $help
219 # $opts->get('help'); # $help
220  
221 =cut
222  
223 =head1 DESCRIPTION
224  
225 This package provides methods for handling command-line arguments.
226  
227 =cut
228  
229 =head1 ATTRIBUTES
230  
231 This package has the following attributes:
232  
233 =cut
234  
235 =head2 named
236  
237 named(HashRef)
238  
239 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
240  
241 =cut
242  
243 =head2 parsed
244  
245 parsed(HashRef)
246  
247 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
248  
249 =cut
250  
251 =head2 specs
252  
253 specs(ArrayRef)
254  
255 This attribute is read-write, accepts C<(ArrayRef)> values, is optional, and defaults to C<[]>.
256  
257 =cut
258  
259 =head2 warns
260  
261 warns(ArrayRef)
262  
263 This attribute is read-write, accepts C<(ArrayRef)> values, is optional, and defaults to C<[]>.
264  
265 =cut
266  
267 =head2 unused
268  
269 unused(ArrayRef)
270  
271 This attribute is read-write, accepts C<(ArrayRef)> values, is optional, and defaults to C<[]>.
272  
273 =cut
274  
275 =head1 INHERITS
276  
277 This package inherits behaviors from:
278  
279 L<Venus::Kind::Utility>
280  
281 =cut
282  
283 =head1 INTEGRATES
284  
285 This package integrates behaviors from:
286  
287 L<Venus::Role::Accessible>
288  
289 L<Venus::Role::Buildable>
290  
291 L<Venus::Role::Proxyable>
292  
293 L<Venus::Role::Valuable>
294  
295 =cut
296  
297 =head1 METHODS
298  
299 This package provides the following methods:
300  
301 =cut
302  
303 =head2 default
304  
305 default() (arrayref)
306  
307 The default method returns the default value, i.e. C<[@ARGV]>.
308  
309 I<Since C<0.01>>
310  
311 =over 4
312  
313 =item default example 1
314  
315 # given: synopsis;
316  
317 my $default = $opts->default;
318  
319 # []
320  
321 =back
322  
323 =cut
324  
325 =head2 exists
326  
327 exists(string $key) (boolean)
328  
329 The exists method takes a name or index and returns truthy if an associated
330 value exists.
331  
332 I<Since C<0.01>>
333  
334 =over 4
335  
336 =item exists example 1
337  
338 # given: synopsis;
339  
340 my $exists = $opts->exists('resource');
341  
342 # 1
343  
344 =back
345  
346 =over 4
347  
348 =item exists example 2
349  
350 # given: synopsis;
351  
352 my $exists = $opts->exists('method');
353  
354 # 1
355  
356 =back
357  
358 =over 4
359  
360 =item exists example 3
361  
362 # given: synopsis;
363  
364 my $exists = $opts->exists('resources');
365  
366 # undef
367  
368 =back
369  
370 =cut
371  
372 =head2 get
373  
374 get(string $key) (any)
375  
376 The get method takes a name or index and returns the associated value.
377  
378 I<Since C<0.01>>
379  
380 =over 4
381  
382 =item get example 1
383  
384 # given: synopsis;
385  
386 my $get = $opts->get('resource');
387  
388 # "users"
389  
390 =back
391  
392 =over 4
393  
394 =item get example 2
395  
396 # given: synopsis;
397  
398 my $get = $opts->get('method');
399  
400 # "users"
401  
402 =back
403  
404 =over 4
405  
406 =item get example 3
407  
408 # given: synopsis;
409  
410 my $get = $opts->get('resources');
411  
412 # undef
413  
414 =back
415  
416 =cut
417  
418 =head2 name
419  
420 name(string $key) (string | undef)
421  
422 The name method takes a name or index and returns index if the the associated
423 value exists.
424  
425 I<Since C<0.01>>
426  
427 =over 4
428  
429 =item name example 1
430  
431 # given: synopsis;
432  
433 my $name = $opts->name('resource');
434  
435 # "resource"
436  
437 =back
438  
439 =over 4
440  
441 =item name example 2
442  
443 # given: synopsis;
444  
445 my $name = $opts->name('method');
446  
447 # "resource"
448  
449 =back
450  
451 =over 4
452  
453 =item name example 3
454  
455 # given: synopsis;
456  
457 my $name = $opts->name('resources');
458  
459 # undef
460  
461 =back
462  
463 =cut
464  
465 =head2 new
466  
467 new(any @args) (Venus::Opts)
468  
469 The new method constructs an instance of the package.
470  
471 I<Since C<4.15>>
472  
473 =over 4
474  
475 =item new example 1
476  
477 package main;
478  
479 use Venus::Opts;
480  
481 my $new = Venus::Opts->new;
482  
483 # bless(..., "Venus::Opts")
484  
485 =back
486  
487 =over 4
488  
489 =item new example 2
490  
491 package main;
492  
493 use Venus::Opts;
494  
495 my $new = Venus::Opts->new(
496 ['--resource', 'users', '--help'],
497 );
498  
499 # bless(..., "Venus::Opts")
500  
501 =back
502  
503 =over 4
504  
505 =item new example 3
506  
507 package main;
508  
509 use Venus::Opts;
510  
511 my $new = Venus::Opts->new(
512 value => ['--resource', 'users', '--help'],
513 );
514  
515 # bless(..., "Venus::Opts")
516  
517 =back
518  
519 =cut
520  
521 =head2 parse
522  
523 parse(arrayref $args) (Venus::Opts)
524  
525 The parse method optionally takes additional L<Getopt::Long> parser
526 configuration options and retuns the options found based on the object C<args>
527 and C<spec> values.
528  
529 I<Since C<0.01>>
530  
531 =over 4
532  
533 =item parse example 1
534  
535 # given: synopsis;
536  
537 my $parse = $opts->parse;
538  
539 # bless({...}, 'Venus::Opts')
540  
541 =back
542  
543 =over 4
544  
545 =item parse example 2
546  
547 # given: synopsis;
548  
549 my $parse = $opts->parse(['bundling']);
550  
551 # bless({...}, 'Venus::Opts')
552  
553 =back
554  
555 =cut
556  
557 =head2 reparse
558  
559 reparse(arrayref $specs, arrayref $args) (Venus::Opts)
560  
561 The reparse method resets the parser, calls the L</parse> method and returns
562 the result.
563  
564 I<Since C<2.55>>
565  
566 =over 4
567  
568 =item reparse example 1
569  
570 # given: synopsis;
571  
572 my $reparse = $opts->reparse(['resource|r=s']);
573  
574 # bless({...}, 'Venus::Opts')
575  
576 =back
577  
578 =over 4
579  
580 =item reparse example 2
581  
582 # given: synopsis;
583  
584 my $reparse = $opts->reparse(['resource|r=s'], ['bundling']);
585  
586 # bless({...}, 'Venus::Opts')
587  
588 =back
589  
590 =cut
591  
592 =head2 set
593  
594 set(string $key, any $data) (any)
595  
596 The set method takes a name or index and sets the value provided if the
597 associated argument exists.
598  
599 I<Since C<0.01>>
600  
601 =over 4
602  
603 =item set example 1
604  
605 # given: synopsis;
606  
607 my $set = $opts->set('method', 'people');
608  
609 # "people"
610  
611 =back
612  
613 =over 4
614  
615 =item set example 2
616  
617 # given: synopsis;
618  
619 my $set = $opts->set('resource', 'people');
620  
621 # "people"
622  
623 =back
624  
625 =over 4
626  
627 =item set example 3
628  
629 # given: synopsis;
630  
631 my $set = $opts->set('resources', 'people');
632  
633 # undef
634  
635 =back
636  
637 =cut
638  
639 =head2 unnamed
640  
641 unnamed() (arrayref)
642  
643 The unnamed method returns an arrayref of values which have not been named
644 using the C<named> attribute.
645  
646 I<Since C<0.01>>
647  
648 =over 4
649  
650 =item unnamed example 1
651  
652 # given: synopsis;
653  
654 my $unnamed = $opts->unnamed;
655  
656 # [1]
657  
658 =back
659  
660 =cut
661  
662 =head1 AUTHORS
663  
664 Awncorp, C<awncorp@cpan.org>
665  
666 =cut
667  
668 =head1 LICENSE
669  
670 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
671  
672 This program is free software, you can redistribute it and/or modify it under
673 the terms of the Apache license version 2.0.
674  
675 =cut
« blib/lib/Venus/Check.pm ↑ Summary blib/lib/Venus/Atom.pm »