Venus/Opts.pm

filter: Statementstmt 93.9% 77 / 82 Branchbran 78.1% 25 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.5% 14 / 16 Podpod 70.0% 7 / 10 totaltotal 87.8% 123 / 140 CC 18 SCAR 10.5
CC
18
Cov
89%
build_self
5.1
parse
3.0
exists
3.0
CRAP 2.9
« Venus.pm ↑ Summary Venus/Error.pm »
1 package Venus::Opts;
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 2 attr 'parsed';
19 2 attr 'specs';
20 2 attr 'warns';
21  
22 # BUILDERS
23  
24 sub build_arg {
25 0 ▶ my ($self, $data) = @_;
Subroutine build_arg: not called CC=1 SCAR=6.9
Pod undocumented
26  
27 return {
28 0 specs => $data,
29 };
30 }
31  
32 sub build_self {
33 18 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=5 SCAR=16.2
Pod undocumented
34  
35 18 ▶ $self->named({}) if !$self->named;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->named 1 17 18
36 18 ▶ $self->parsed({}) if !$self->parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parsed 18 0 18
37 18 ▶ $self->specs([]) if !$self->specs;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->specs 1 17 18
38 18 ▶ $self->warns([]) if !$self->warns;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->warns 18 0 18
39  
40 18 return $self->parse;
41 }
42  
43 sub build_proxy {
44 2 ▶ my ($self, $package, $method, $value) = @_;
Subroutine build_proxy: called CC=1 SCAR=0.2
Pod undocumented
45  
46 2 my $has_value = exists $_[3];
47  
48 return sub {
49 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
50 0 return $self->set($method, $value);
51 2 };
52 }
53  
54 # METHODS
55  
56 sub default {
57 2 ▶ my ($self) = @_;
Subroutine default: called CC=1 SCAR=0.0
Pod documented
58  
59 2 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->parsed->{$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->parsed->{$pos};
84 }
85  
86 sub parse {
87 20 ▶ my ($self, $extras) = @_;
Subroutine parse: called CC=3 SCAR=11.1
Pod documented
88  
89 20 ▶ return $self->parsed if %{$self->parsed};
Branch: 1 branch
BranchTrueFalseTotal
if %{$self->parsed;} 2 18 20
90  
91 18 my $value = $self->value;
92 18 my $specs = $self->specs;
93  
94 18 my $parsed = {};
95 18 my @configs = qw(default no_auto_abbrev no_ignore_case);
96  
97 18 ▶ $extras = [] if !$extras;
Branch: 1 branch
BranchTrueFalseTotal
unless $extras 18 0 18
98  
99 18 require Getopt::Long;
100  
101 18 my $warns = [];
102 local $SIG{__WARN__} = sub {
103 0 ▶ push @$warns, map s/\n+$//gr, @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 return;
105 18 };
106  
107 # configure parser
108 18 Getopt::Long::Configure(Getopt::Long::Configure(@configs, @$extras));
109  
110 # parse args using spec
111 18 Getopt::Long::GetOptionsFromArray([@$value], $parsed, @$specs);
112  
113 18 $self->parsed($parsed);
114 18 $self->warns($warns);
115  
116 18 return $self;
117 }
118  
119 sub name {
120 14 ▶ my ($self, $name) = @_;
Subroutine name: called CC=3 SCAR=11.0
Pod documented
121  
122 14 ▶ if (defined $self->named->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->named->{$name}) 5 9 14
123 5 return $self->named->{$name};
124 }
125  
126 9 ▶ if (defined $self->parsed->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->parsed->{$name}) 5 4 9
127 5 return $name;
128 }
129  
130 4 return undef;
131 }
132  
133 sub set {
134 3 ▶ my ($self, $name, $data) = @_;
Subroutine set: called CC=3 SCAR=11.0
Pod documented
135  
136 3 ▶ return if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 3 3
137  
138 3 my $pos = $self->name($name);
139  
140 3 ▶ return if not defined $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos 1 2 3
141  
142 2 return $self->parsed->{$pos} = $data;
143 }
144  
145 sub unnamed {
146 1 ▶ my ($self) = @_;
Subroutine unnamed: called CC=3 SCAR=11.0
Pod documented
147  
148 1 my $list = [];
149  
150 1 my $opts = $self->parsed;
151 1 my $data = +{reverse %{$self->named}};
152  
153 1 for my $index (sort keys %$opts) {
154 2 ▶ unless (exists $data->{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $data->{$index}) 1 1 2
155 1 push @$list, $opts->{$index};
156 }
157 }
158  
159 1 return $list;
160 }
161  
162 2 1;
163  
164  
165  
166 =head1 NAME
167  
168 Venus::Opts - Opts Class
169  
170 =cut
171  
172 =head1 ABSTRACT
173  
174 Opts Class for Perl 5
175  
176 =cut
177  
178 =head1 SYNOPSIS
179  
180 package main;
181  
182 use Venus::Opts;
183  
184 my $opts = Venus::Opts->new(
185 value => ['--resource', 'users', '--help'],
186 specs => ['resource|r=s', 'help|h'],
187 named => { method => 'resource' } # optional
188 );
189  
190 # $opts->method; # $resource
191 # $opts->get('resource'); # $resource
192  
193 # $opts->help; # $help
194 # $opts->get('help'); # $help
195  
196 =cut
197  
198 =head1 DESCRIPTION
199  
200 This package provides methods for handling command-line arguments.
201  
202 =cut
203  
204 =head1 ATTRIBUTES
205  
206 This package has the following attributes:
207  
208 =cut
209  
210 =head2 named
211  
212 named(HashRef)
213  
214 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
215  
216 =cut
217  
218 =head2 parsed
219  
220 parsed(HashRef)
221  
222 This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>.
223  
224 =cut
225  
226 =head2 specs
227  
228 specs(ArrayRef)
229  
230 This attribute is read-write, accepts C<(ArrayRef)> values, is optional, and defaults to C<[]>.
231  
232 =cut
233  
234 =head2 warns
235  
236 warns(ArrayRef)
237  
238 This attribute is read-write, accepts C<(ArrayRef)> values, is optional, and defaults to C<[]>.
239  
240 =cut
241  
242 =head1 INHERITS
243  
244 This package inherits behaviors from:
245  
246 L<Venus::Kind::Utility>
247  
248 =cut
249  
250 =head1 INTEGRATES
251  
252 This package integrates behaviors from:
253  
254 L<Venus::Role::Accessible>
255  
256 L<Venus::Role::Proxyable>
257  
258 =cut
259  
260 =head1 METHODS
261  
262 This package provides the following methods:
263  
264 =cut
265  
266 =head2 default
267  
268 default() (ArrayRef)
269  
270 The default method returns the default value, i.e. C<[@ARGV]>.
271  
272 I<Since C<0.01>>
273  
274 =over 4
275  
276 =item default example 1
277  
278 # given: synopsis;
279  
280 my $default = $opts->default;
281  
282 # []
283  
284 =back
285  
286 =cut
287  
288 =head2 exists
289  
290 exists(Str $key) (Bool)
291  
292 The exists method takes a name or index and returns truthy if an associated
293 value exists.
294  
295 I<Since C<0.01>>
296  
297 =over 4
298  
299 =item exists example 1
300  
301 # given: synopsis;
302  
303 my $exists = $opts->exists('resource');
304  
305 # 1
306  
307 =back
308  
309 =over 4
310  
311 =item exists example 2
312  
313 # given: synopsis;
314  
315 my $exists = $opts->exists('method');
316  
317 # 1
318  
319 =back
320  
321 =over 4
322  
323 =item exists example 3
324  
325 # given: synopsis;
326  
327 my $exists = $opts->exists('resources');
328  
329 # undef
330  
331 =back
332  
333 =cut
334  
335 =head2 get
336  
337 get(Str $key) (Any)
338  
339 The get method takes a name or index and returns the associated value.
340  
341 I<Since C<0.01>>
342  
343 =over 4
344  
345 =item get example 1
346  
347 # given: synopsis;
348  
349 my $get = $opts->get('resource');
350  
351 # "users"
352  
353 =back
354  
355 =over 4
356  
357 =item get example 2
358  
359 # given: synopsis;
360  
361 my $get = $opts->get('method');
362  
363 # "users"
364  
365 =back
366  
367 =over 4
368  
369 =item get example 3
370  
371 # given: synopsis;
372  
373 my $get = $opts->get('resources');
374  
375 # undef
376  
377 =back
378  
379 =cut
380  
381 =head2 name
382  
383 name(Str $key) (Str | Undef)
384  
385 The name method takes a name or index and returns index if the the associated
386 value exists.
387  
388 I<Since C<0.01>>
389  
390 =over 4
391  
392 =item name example 1
393  
394 # given: synopsis;
395  
396 my $name = $opts->name('resource');
397  
398 # "resource"
399  
400 =back
401  
402 =over 4
403  
404 =item name example 2
405  
406 # given: synopsis;
407  
408 my $name = $opts->name('method');
409  
410 # "resource"
411  
412 =back
413  
414 =over 4
415  
416 =item name example 3
417  
418 # given: synopsis;
419  
420 my $name = $opts->name('resources');
421  
422 # undef
423  
424 =back
425  
426 =cut
427  
428 =head2 parse
429  
430 parse(ArrayRef $args) (Opts)
431  
432 The parse method optionally takes additional L<Getopt::Long> parser
433 configuration options and retuns the options found based on the object C<args>
434 and C<spec> values.
435  
436 I<Since C<0.01>>
437  
438 =over 4
439  
440 =item parse example 1
441  
442 # given: synopsis;
443  
444 my $parse = $opts->parse;
445  
446 # { help => 1, resource => "users" }
447  
448 =back
449  
450 =over 4
451  
452 =item parse example 2
453  
454 # given: synopsis;
455  
456 my $parse = $opts->parse(['bundling']);
457  
458 # { help => 1, resource => "users" }
459  
460 =back
461  
462 =cut
463  
464 =head2 set
465  
466 set(Str $key, Any $data) (Any)
467  
468 The set method takes a name or index and sets the value provided if the
469 associated argument exists.
470  
471 I<Since C<0.01>>
472  
473 =over 4
474  
475 =item set example 1
476  
477 # given: synopsis;
478  
479 my $set = $opts->set('method', 'people');
480  
481 # "people"
482  
483 =back
484  
485 =over 4
486  
487 =item set example 2
488  
489 # given: synopsis;
490  
491 my $set = $opts->set('resource', 'people');
492  
493 # "people"
494  
495 =back
496  
497 =over 4
498  
499 =item set example 3
500  
501 # given: synopsis;
502  
503 my $set = $opts->set('resources', 'people');
504  
505 # undef
506  
507 =back
508  
509 =cut
510  
511 =head2 unnamed
512  
513 unnamed() (ArrayRef)
514  
515 The unnamed method returns an arrayref of values which have not been named
516 using the C<named> attribute.
517  
518 I<Since C<0.01>>
519  
520 =over 4
521  
522 =item unnamed example 1
523  
524 # given: synopsis;
525  
526 my $unnamed = $opts->unnamed;
527  
528 # [1]
529  
530 =back
531  
532 =cut
« Venus.pm ↑ Summary Venus/Error.pm »