File Coverage

blib/lib/Data/Object/Opts.pm
Criterion Covered Total %
statement 96 97 98.9
branch 22 30 73.3
condition n/a
subroutine 20 20 100.0
pod 8 10 80.0
total 146 157 92.9


line stmt bran cond sub pod time code
1             package Data::Object::Opts;
2              
3 1     1   38963 use 5.014;
  1         5  
4              
5 1     1   6 use strict;
  1         2  
  1         23  
6 1     1   5 use warnings;
  1         2  
  1         26  
7              
8 1     1   6 use registry;
  1         2  
  1         7  
9 1     1   6051 use routines;
  1         2  
  1         8  
10              
11 1     1   2325 use Data::Object::Class;
  1         1028  
  1         11  
12 1     1   896 use Data::Object::ClassHas;
  1         10089  
  1         10  
13              
14             with 'Data::Object::Role::Buildable';
15             with 'Data::Object::Role::Proxyable';
16             with 'Data::Object::Role::Stashable';
17              
18 1     1   875 use Getopt::Long ();
  1         11094  
  1         226  
19              
20             our $VERSION = '2.00'; # VERSION
21              
22             # ATTRIBUTES
23              
24             has 'args' => (
25             is => 'ro',
26             isa => 'ArrayRef[Str]',
27             opt => 1,
28             );
29              
30             has 'spec' => (
31             is => 'ro',
32             isa => 'ArrayRef[Str]',
33             opt => 1,
34             );
35              
36             has 'named' => (
37             is => 'ro',
38             isa => 'HashRef',
39             opt => 1,
40             );
41              
42             # BUILD
43              
44 18     18 0 179794 method build_self($args) {
  18         50  
  18         33  
45 18 100       66 $self->{named} = {} if !$args->{named};
46              
47 18 50       82 $self->{args} = [] if !$args->{args};
48 18 50       48 $self->{spec} = [] if !$args->{spec};
49              
50 18 50       28 $self->{args} = [@ARGV] if !@{$self->{args}};
  18         51  
51              
52 18         32 my $warn = [];
53              
54             local $SIG{__WARN__} = sub {
55 2     2   435 push @$warn, [@_];
56              
57 2         9 return;
58 18         153 };
59              
60 18         78 $self->stash(opts => $self->parse($args->{opts}));
61 18 50       314 $self->stash(warn => $warn) if $warn;
62              
63 18         288 return $self;
64             }
65              
66 2     2 0 1032 method build_proxy($package, $method, $value) {
  2         7  
  2         3  
67 2         6 my $has_value = exists $_[2];
68              
69             return sub {
70              
71 2 50   2   23 return $self->get($method) if !$has_value; # no val
72              
73 0         0 return $self->set($method, $value);
74 2         14 };
75             }
76              
77              
78             # METHODS
79              
80 3     3 1 102 method exists($key) {
  3         7  
  3         5  
81 3 50       10 return if not defined $key;
82              
83 3         11 my $pos = $self->name($key);
84              
85 3 100       20 return if not defined $pos;
86              
87 2         6 return exists $self->stashed->{$pos};
88             }
89              
90 5     5 1 108 method get($key) {
  5         11  
  5         9  
91 5 50       15 return if not defined $key;
92              
93 5         16 my $pos = $self->name($key);
94              
95 5 100       23 return if not defined $pos;
96              
97 4         12 return $self->stashed->{$pos};
98             }
99              
100 14     14 1 126 method name($key) {
  14         30  
  14         20  
101 14 100       63 if (defined $self->named->{$key}) {
102 5         29 return $self->named->{$key};
103             }
104              
105 9 100       26 if (defined $self->stashed->{$key}) {
106 5         24 return $key;
107             }
108              
109 4         24 return undef;
110             }
111              
112 3     3 1 101 method set($key, $val) {
  3         8  
  3         6  
113 3 50       9 return if not defined $key;
114              
115 3         11 my $pos = $self->name($key);
116              
117 3 100       21 return if not defined $pos;
118              
119 2         12 return $self->stashed->{$pos} = $val;
120             }
121              
122 20     20 1 106 method parse($extras) {
  20         51  
  20         29  
123 20         57 my $args = $self->args;
124 20         37 my $spec = $self->spec;
125              
126 20         39 my $options = {};
127 20         50 my @configs = qw(default no_auto_abbrev no_ignore_case);
128              
129 20 100       52 $extras = [] if !$extras;
130              
131             # configure parser
132 20         81 Getopt::Long::Configure(Getopt::Long::Configure(@configs, @$extras));
133              
134             # parse args using spec
135 20         2026 Getopt::Long::GetOptionsFromArray([@$args], $options, @$spec);
136              
137 20         7378 return $options;
138             }
139              
140 18     18 1 56 method stashed() {
  18         32  
141 18         42 my $data = $self->stash('opts');
142              
143 18         276 return $data;
144             }
145              
146 1     1 1 35 method warned() {
  1         2  
147 1         3 my $data = $self->stash('warn');
148              
149 1         20 return scalar @$data;
150             }
151              
152 1     1 1 33 method warnings() {
  1         4  
153 1         4 my $data = $self->stash('warn');
154              
155 1         20 return $data;
156             }
157              
158             1;
159              
160             =encoding utf8
161              
162             =head1 NAME
163              
164             Data::Object::Opts
165              
166             =cut
167              
168             =head1 ABSTRACT
169              
170             Opts Class for Perl 5
171              
172             =cut
173              
174             =head1 SYNOPSIS
175              
176             package main;
177              
178             use Data::Object::Opts;
179              
180             my $opts = Data::Object::Opts->new(
181             args => ['--resource', 'users', '--help'],
182             spec => ['resource|r=s', 'help|h'],
183             named => { method => 'resource' } # optional
184             );
185              
186             # $opts->method; # $resource
187             # $opts->get('resource'); # $resource
188              
189             # $opts->help; # $help
190             # $opts->get('help'); # $help
191              
192             =cut
193              
194             =head1 DESCRIPTION
195              
196             This package provides methods for accessing command-line arguments.
197              
198             =cut
199              
200             =head1 INTEGRATES
201              
202             This package integrates behaviors from:
203              
204             L<Data::Object::Role::Buildable>
205              
206             L<Data::Object::Role::Proxyable>
207              
208             L<Data::Object::Role::Stashable>
209              
210             =cut
211              
212             =head1 LIBRARIES
213              
214             This package uses type constraints from:
215              
216             L<Types::Standard>
217              
218             =cut
219              
220             =head1 ATTRIBUTES
221              
222             This package has the following attributes:
223              
224             =cut
225              
226             =head2 args
227              
228             args(ArrayRef[Str])
229              
230             This attribute is read-only, accepts C<(ArrayRef[Str])> values, and is optional.
231              
232             =cut
233              
234             =head2 named
235              
236             named(HashRef)
237              
238             This attribute is read-only, accepts C<(HashRef)> values, and is optional.
239              
240             =cut
241              
242             =head2 spec
243              
244             spec(ArrayRef[Str])
245              
246             This attribute is read-only, accepts C<(ArrayRef[Str])> values, and is optional.
247              
248             =cut
249              
250             =head1 METHODS
251              
252             This package implements the following methods:
253              
254             =cut
255              
256             =head2 exists
257              
258             exists(Str $key) : Any
259              
260             The exists method takes a name or index and returns truthy if an associated
261             value exists.
262              
263             =over 4
264              
265             =item exists example #1
266              
267             # given: synopsis
268              
269             $opts->exists('resource'); # truthy
270              
271             =back
272              
273             =over 4
274              
275             =item exists example #2
276              
277             # given: synopsis
278              
279             $opts->exists('method'); # truthy
280              
281             =back
282              
283             =over 4
284              
285             =item exists example #3
286              
287             # given: synopsis
288              
289             $opts->exists('resources'); # falsy
290              
291             =back
292              
293             =cut
294              
295             =head2 get
296              
297             get(Str $key) : Any
298              
299             The get method takes a name or index and returns the associated value.
300              
301             =over 4
302              
303             =item get example #1
304              
305             # given: synopsis
306              
307             $opts->get('resource'); # users
308              
309             =back
310              
311             =over 4
312              
313             =item get example #2
314              
315             # given: synopsis
316              
317             $opts->get('method'); # users
318              
319             =back
320              
321             =over 4
322              
323             =item get example #3
324              
325             # given: synopsis
326              
327             $opts->get('resources'); # undef
328              
329             =back
330              
331             =cut
332              
333             =head2 name
334              
335             name(Str $key) : Any
336              
337             The name method takes a name or index and returns index if the the associated
338             value exists.
339              
340             =over 4
341              
342             =item name example #1
343              
344             # given: synopsis
345              
346             $opts->name('resource'); # resource
347              
348             =back
349              
350             =over 4
351              
352             =item name example #2
353              
354             # given: synopsis
355              
356             $opts->name('method'); # resource
357              
358             =back
359              
360             =over 4
361              
362             =item name example #3
363              
364             # given: synopsis
365              
366             $opts->name('resources'); # undef
367              
368             =back
369              
370             =cut
371              
372             =head2 parse
373              
374             parse(Maybe[ArrayRef] $config) : HashRef
375              
376             The parse method optionally takes additional L<Getopt::Long> parser
377             configuration options and retuns the options found based on the object C<args>
378             and C<spec> values.
379              
380             =over 4
381              
382             =item parse example #1
383              
384             # given: synopsis
385              
386             $opts->parse;
387              
388             =back
389              
390             =over 4
391              
392             =item parse example #2
393              
394             # given: synopsis
395              
396             $opts->parse(['bundling']);
397              
398             =back
399              
400             =cut
401              
402             =head2 set
403              
404             set(Str $key, Maybe[Any] $value) : Any
405              
406             The set method takes a name or index and sets the value provided if the
407             associated argument exists.
408              
409             =over 4
410              
411             =item set example #1
412              
413             # given: synopsis
414              
415             $opts->set('method', 'people'); # people
416              
417             =back
418              
419             =over 4
420              
421             =item set example #2
422              
423             # given: synopsis
424              
425             $opts->set('resource', 'people'); # people
426              
427             =back
428              
429             =over 4
430              
431             =item set example #3
432              
433             # given: synopsis
434              
435             $opts->set('resources', 'people'); # undef
436              
437             # is not set
438              
439             =back
440              
441             =cut
442              
443             =head2 stashed
444              
445             stashed() : HashRef
446              
447             The stashed method returns the stashed data associated with the object.
448              
449             =over 4
450              
451             =item stashed example #1
452              
453             # given: synopsis
454              
455             $opts->stashed;
456              
457             =back
458              
459             =cut
460              
461             =head2 warned
462              
463             warned() : Num
464              
465             The warned method returns the number of warnings emitted during option parsing.
466              
467             =over 4
468              
469             =item warned example #1
470              
471             package main;
472              
473             use Data::Object::Opts;
474              
475             my $opts = Data::Object::Opts->new(
476             args => ['-vh'],
477             spec => ['verbose|v', 'help|h']
478             );
479              
480             $opts->warned;
481              
482             =back
483              
484             =cut
485              
486             =head2 warnings
487              
488             warnings() : ArrayRef[ArrayRef[Str]]
489              
490             The warnings method returns the set of warnings emitted during option parsing.
491              
492             =over 4
493              
494             =item warnings example #1
495              
496             package main;
497              
498             use Data::Object::Opts;
499              
500             my $opts = Data::Object::Opts->new(
501             args => ['-vh'],
502             spec => ['verbose|v', 'help|h']
503             );
504              
505             $opts->warnings;
506              
507             =back
508              
509             =cut
510              
511             =head1 AUTHOR
512              
513             Al Newkirk, C<awncorp@cpan.org>
514              
515             =head1 LICENSE
516              
517             Copyright (C) 2011-2019, Al Newkirk, et al.
518              
519             This is free software; you can redistribute it and/or modify it under the terms
520             of the The Apache License, Version 2.0, as elucidated in the L<"license
521             file"|https://github.com/iamalnewkirk/data-object-opts/blob/master/LICENSE>.
522              
523             =head1 PROJECT
524              
525             L<Wiki|https://github.com/iamalnewkirk/data-object-opts/wiki>
526              
527             L<Project|https://github.com/iamalnewkirk/data-object-opts>
528              
529             L<Initiatives|https://github.com/iamalnewkirk/data-object-opts/projects>
530              
531             L<Milestones|https://github.com/iamalnewkirk/data-object-opts/milestones>
532              
533             L<Contributing|https://github.com/iamalnewkirk/data-object-opts/blob/master/CONTRIBUTE.md>
534              
535             L<Issues|https://github.com/iamalnewkirk/data-object-opts/issues>
536              
537             =cut