File Coverage

blib/lib/Getopt/Long/Descriptive/Opts.pm
Criterion Covered Total %
statement 55 58 94.8
branch 2 4 50.0
condition n/a
subroutine 9 10 90.0
pod n/a
total 66 72 91.6


line stmt bran cond sub pod time code
1 2     2   15 use strict;
  2         4  
  2         62  
2 2     2   10 use warnings;
  2         4  
  2         84  
3             package Getopt::Long::Descriptive::Opts 0.111;
4             # ABSTRACT: object representing command line switches
5              
6 2     2   11 use Scalar::Util qw(blessed weaken);
  2         5  
  2         941  
7              
8             #pod =head1 DESCRIPTION
9             #pod
10             #pod This class is the base class of all C<$opt> objects returned by
11             #pod L<Getopt::Long::Descriptive>. In general, you do not want to think about this
12             #pod class, look at it, or alter it. Seriously, it's pretty dumb.
13             #pod
14             #pod Every call to C<describe_options> will return a object of a new subclass of
15             #pod this class. It will have a method for the canonical name of each option
16             #pod possible given the option specifications.
17             #pod
18             #pod Method names beginning with an single underscore are public, and are named that
19             #pod way to avoid conflict with automatically generated methods. Methods with
20             #pod multiple underscores (in case you're reading the source) are private.
21             #pod
22             #pod =head1 METHODS
23             #pod
24             #pod B<Achtung!> All methods beginning with an underscore are experimental as of
25             #pod today, 2009-12-12. They are likely to be formally made permanent soon.
26             #pod
27             #pod =head2 _specified
28             #pod
29             #pod This method returns true if the given name was specified on the command line.
30             #pod
31             #pod For example, if C<@ARGS> was "C<< --foo --bar 10 >>" and C<baz> is defined by a
32             #pod default, C<_specified> will return true for foo and bar, and false for baz.
33             #pod
34             #pod =cut
35              
36             my %_CREATED_OPTS;
37             my $SERIAL_NUMBER = 1;
38              
39             sub _specified {
40 0     0   0 my ($self, $name) = @_;
41 0         0 my $meta = $_CREATED_OPTS{ blessed $self }{meta};
42 0         0 return $meta->{given}{ $name };
43             }
44              
45             #pod =head2 _specified_opts
46             #pod
47             #pod This method returns an opt object in which only explicitly specified values are
48             #pod defined. Values which were set by defaults will appear undef.
49             #pod
50             #pod =cut
51              
52             sub _specified_opts {
53 1     1   11 my ($self) = @_;
54              
55 1         7 my $class = blessed $self;
56 1         3 my $meta = $_CREATED_OPTS{ $class }{meta};
57              
58 1 50       4 return $meta->{specified_opts} if $meta->{specified_opts};
59              
60 1         3 my @keys = grep { $meta->{given}{ $_ } } (keys %{ $meta->{given} });
  2         6  
  1         5  
61              
62 1         2 my %opts;
63 1         8 @opts{ @keys } = @$self{ @keys };
64              
65 1         3 $meta->{specified_opts} = \%opts;
66              
67 1         3 bless $meta->{specified_opts} => $class;
68 1         6 weaken $meta->{specified_opts};
69              
70 1         3 $meta->{specified_opts};
71             }
72              
73             #pod =head2 _complete_opts
74             #pod
75             #pod This method returns the opts object with all values, including those set by
76             #pod defaults. It is probably not going to be very often-used.
77             #pod
78             #pod =cut
79              
80             sub _complete_opts {
81 1     1   9 my ($self) = @_;
82              
83 1         4 my $class = blessed $self;
84 1         2 my $meta = $_CREATED_OPTS{ $class }{meta};
85 1         3 return $meta->{complete_opts};
86             }
87              
88             sub ___class_for_opt {
89 26     26   67 my ($class, $arg) = @_;
90              
91 26         49 my $values = $arg->{values};
92 26         58 my @bad = grep { $_ !~ /^[a-z_]\w*$/ } keys %$values;
  83         291  
93 26 50       74 Carp::confess("perverse option names given: @bad") if @bad;
94              
95 26         108 my $new_class = "$class\::__OPT__::" . $SERIAL_NUMBER++;
96 26         96 $_CREATED_OPTS{ $new_class } = { meta => $arg };
97              
98             {
99 2     2   15 no strict 'refs';
  2         11  
  2         610  
  26         41  
100 26         142 ${"$new_class\::VERSION"} = $class->VERSION;
  26         235  
101 26         64 *{"$new_class\::ISA"} = [ 'Getopt::Long::Descriptive::Opts' ];
  26         273  
102 26         81 for my $opt (keys %$values) {
103 83     32   260 *{"$new_class\::$opt"} = sub { $_[0]->{ $opt } };
  83         354  
  32         10386  
104             }
105             }
106              
107 26         70 return $new_class;
108             }
109              
110             sub ___new_opt_obj {
111 26     26   78 my ($class, $arg) = @_;
112              
113 26         41 my $copy = { %{ $arg->{values} } };
  26         104  
114              
115 26         89 my $new_class = $class->___class_for_opt($arg);
116              
117             # This is stupid, but the traditional behavior was that if --foo was not
118             # given, there is no $opt->{foo}; it started to show up when we "needed" all
119             # the keys to generate a class, but was undef; this wasn't a problem, but
120             # broke tests of things that were relying on not-exists like tests of %$opt
121             # contents or MooseX::Getopt which wanted to use things as args for new --
122             # undef would not pass an Int TC. Easier to just do this. -- rjbs,
123             # 2009-11-27
124 26         77 delete $copy->{$_} for grep { ! defined $copy->{$_} } keys %$copy;
  83         245  
125              
126 26         113 my $self = bless $copy => $new_class;
127              
128 26         64 $_CREATED_OPTS{ $new_class }{meta}{complete_opts} = $self;
129             # weaken $_CREATED_OPTS{ $new_class }{meta}{complete_opts};
130              
131 26         71 return $self;
132             }
133              
134             1;
135              
136             __END__
137              
138             =pod
139              
140             =encoding UTF-8
141              
142             =head1 NAME
143              
144             Getopt::Long::Descriptive::Opts - object representing command line switches
145              
146             =head1 VERSION
147              
148             version 0.111
149              
150             =head1 DESCRIPTION
151              
152             This class is the base class of all C<$opt> objects returned by
153             L<Getopt::Long::Descriptive>. In general, you do not want to think about this
154             class, look at it, or alter it. Seriously, it's pretty dumb.
155              
156             Every call to C<describe_options> will return a object of a new subclass of
157             this class. It will have a method for the canonical name of each option
158             possible given the option specifications.
159              
160             Method names beginning with an single underscore are public, and are named that
161             way to avoid conflict with automatically generated methods. Methods with
162             multiple underscores (in case you're reading the source) are private.
163              
164             =head1 PERL VERSION
165              
166             This library should run on perls released even a long time ago. It should work
167             on any version of perl released in the last five years.
168              
169             Although it may work on older versions of perl, no guarantee is made that the
170             minimum required version will not be increased. The version may be increased
171             for any reason, and there is no promise that patches will be accepted to lower
172             the minimum required perl.
173              
174             =head1 METHODS
175              
176             B<Achtung!> All methods beginning with an underscore are experimental as of
177             today, 2009-12-12. They are likely to be formally made permanent soon.
178              
179             =head2 _specified
180              
181             This method returns true if the given name was specified on the command line.
182              
183             For example, if C<@ARGS> was "C<< --foo --bar 10 >>" and C<baz> is defined by a
184             default, C<_specified> will return true for foo and bar, and false for baz.
185              
186             =head2 _specified_opts
187              
188             This method returns an opt object in which only explicitly specified values are
189             defined. Values which were set by defaults will appear undef.
190              
191             =head2 _complete_opts
192              
193             This method returns the opts object with all values, including those set by
194             defaults. It is probably not going to be very often-used.
195              
196             =head1 AUTHORS
197              
198             =over 4
199              
200             =item *
201              
202             Hans Dieter Pearcey <hdp@cpan.org>
203              
204             =item *
205              
206             Ricardo Signes <cpan@semiotic.systems>
207              
208             =back
209              
210             =head1 COPYRIGHT AND LICENSE
211              
212             This software is copyright (c) 2005 by Hans Dieter Pearcey.
213              
214             This is free software; you can redistribute it and/or modify it under
215             the same terms as the Perl 5 programming language system itself.
216              
217             =cut