blib/lib/MouseX/Getopt.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 100% 11 / 11 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary blib/lib/MouseX/Getopt/Meta/Attribute.pm »
1 package MouseX::Getopt;
2 # ABSTRACT: A Mouse role for processing command line options
3  
4 21 our $VERSION = "0.37";
5  
6 21 ▶use Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 with 'MouseX::Getopt::GLD';
9  
10 21 ▶no Mouse::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 21 1;
13  
14 =for stopwords metaclass commandline params configfile
15  
16 =head1 SYNOPSIS
17  
18 ## In your class
19 package My::App;
20 use Mouse;
21  
22 with 'MouseX::Getopt';
23  
24 has 'out' => (is => 'rw', isa => 'Str', required => 1);
25 has 'in' => (is => 'rw', isa => 'Str', required => 1);
26  
27 # ... rest of the class here
28  
29 ## in your script
30 #!/usr/bin/perl
31  
32 use My::App;
33  
34 my $app = My::App->new_with_options();
35 # ... rest of the script here
36  
37 ## on the command line
38 % perl my_app_script.pl -in file.input -out file.dump
39  
40 =head1 DESCRIPTION
41  
42 This is a role which provides an alternate constructor for creating
43 objects using parameters passed in from the command line.
44  
45 This module attempts to DWIM as much as possible with the command line
46 params by introspecting your class's attributes. It will use the name
47 of your attribute as the command line option, and if there is a type
48 constraint defined, it will configure Getopt::Long to handle the option
49 accordingly.
50  
51 You can use the trait L<MouseX::Getopt::Meta::Attribute::Trait> or the
52 attribute metaclass L<MouseX::Getopt::Meta::Attribute> to get non-default
53 commandline option names and aliases.
54  
55 You can use the trait L<MouseX::Getopt::Meta::Attribute::Trait::NoGetopt>
56 or the attribute metaclass L<MouseX::Getopt::Meta::Attribute::NoGetopt>
57 to have C<MouseX::Getopt> ignore your attribute in the commandline options.
58  
59 By default, attributes which start with an underscore are not given
60 commandline argument support, unless the attribute's metaclass is set
61 to L<MouseX::Getopt::Meta::Attribute>. If you don't want your accessors
62 to have the leading underscore in their name, you can do this:
63  
64 # for read/write attributes
65 has '_foo' => (accessor => 'foo', ...);
66  
67 # or for read-only attributes
68 has '_bar' => (reader => 'bar', ...);
69  
70 This will mean that Getopt will not handle a --foo param, but your
71 code can still call the C<foo> method.
72  
73 If your class also uses a configfile-loading role based on
74 L<MouseX::ConfigFromFile>, such as L<MouseX::SimpleConfig>,
75 L<MouseX::Getopt>'s C<new_with_options> will load the configfile
76 specified by the C<--configfile> option (or the default you've
77 given for the configfile attribute) for you.
78  
79 Options specified in multiple places follow the following
80 precedence order: commandline overrides configfile, which
81 overrides explicit new_with_options parameters.
82  
83 =head2 Supported Type Constraints
84  
85 =over 4
86  
87 =item I<Bool>
88  
89 A I<Bool> type constraint is set up as a boolean option with
90 Getopt::Long. So that this attribute description:
91  
92 has 'verbose' => (is => 'rw', isa => 'Bool');
93  
94 would translate into C<verbose!> as a Getopt::Long option descriptor,
95 which would enable the following command line options:
96  
97 % my_script.pl --verbose
98 % my_script.pl --noverbose
99  
100 =item I<Int>, I<Float>, I<Str>
101  
102 These type constraints are set up as properly typed options with
103 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
104  
105 =item I<ArrayRef>
106  
107 An I<ArrayRef> type constraint is set up as a multiple value option
108 in Getopt::Long. So that this attribute description:
109  
110 has 'include' => (
111 is => 'rw',
112 isa => 'ArrayRef',
113 default => sub { [] }
114 );
115  
116 would translate into C<includes=s@> as a Getopt::Long option descriptor,
117 which would enable the following command line options:
118  
119 % my_script.pl --include /usr/lib --include /usr/local/lib
120  
121 =item I<HashRef>
122  
123 A I<HashRef> type constraint is set up as a hash value option
124 in Getopt::Long. So that this attribute description:
125  
126 has 'define' => (
127 is => 'rw',
128 isa => 'HashRef',
129 default => sub { {} }
130 );
131  
132 would translate into C<define=s%> as a Getopt::Long option descriptor,
133 which would enable the following command line options:
134  
135 % my_script.pl --define os=linux --define vendor=debian
136  
137 =back
138  
139 =head2 Custom Type Constraints
140  
141 It is possible to create custom type constraint to option spec
142 mappings if you need them. The process is fairly simple (but a
143 little verbose maybe). First you create a custom subtype, like
144 so:
145  
146 subtype 'ArrayOfInts'
147 => as 'ArrayRef'
148 => where { scalar (grep { looks_like_number($_) } @$_) };
149  
150 Then you register the mapping, like so:
151  
152 MouseX::Getopt::OptionTypeMap->add_option_type_to_map(
153 'ArrayOfInts' => '=i@'
154 );
155  
156 Now any attribute declarations using this type constraint will
157 get the custom option spec. So that, this:
158  
159 has 'nums' => (
160 is => 'ro',
161 isa => 'ArrayOfInts',
162 default => sub { [0] }
163 );
164  
165 Will translate to the following on the command line:
166  
167 % my_script.pl --nums 5 --nums 88 --nums 199
168  
169 This example is fairly trivial, but more complex validations are
170 easily possible with a little creativity. The trick is balancing
171 the type constraint validations with the Getopt::Long validations.
172  
173 Better examples are certainly welcome :)
174  
175 =head2 Inferred Type Constraints
176  
177 If you define a custom subtype which is a subtype of one of the
178 standard L</Supported Type Constraints> above, and do not explicitly
179 provide custom support as in L</Custom Type Constraints> above,
180 MouseX::Getopt will treat it like the parent type for Getopt
181 purposes.
182  
183 For example, if you had the same custom C<ArrayOfInts> subtype
184 from the examples above, but did not add a new custom option
185 type for it to the C<OptionTypeMap>, it would be treated just
186 like a normal C<ArrayRef> type for Getopt purposes (that is,
187 C<=s@>).
188  
189 =over 4
190  
191 =item B<new_with_options (%params)>
192  
193 This method will take a set of default C<%params> and then collect
194 params from the command line (possibly overriding those in C<%params>)
195 and then return a newly constructed object.
196  
197 The special parameter C<argv>, if specified should point to an array
198 reference with an array to use instead of C<@ARGV>.
199  
200 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
201 C<new_with_options> will throw an exception.
202  
203 If L<Getopt::Long::Descriptive> is installed and any of the following
204 command line params are passed, the program will exit with usage
205 information (and the option's state will be stored in the help_flag
206 attribute). You can add descriptions for each option by including a
207 B<documentation> option for each attribute to document.
208  
209 --?
210 --help
211 --usage
212  
213 If you have L<Getopt::Long::Descriptive> the C<usage> param is also passed to
214 C<new> as the usage option.
215  
216 =item B<ARGV>
217  
218 This accessor contains a reference to a copy of the C<@ARGV> array
219 as it originally existed at the time of C<new_with_options>.
220  
221 =item B<extra_argv>
222  
223 This accessor contains an arrayref of leftover C<@ARGV> elements that
224 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
225 un-mangled.
226  
227 =item B<usage>
228  
229 This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
230 L<Getopt::Long::Descriptive> is used).
231  
232 =item B<help_flag>
233  
234 This accessor contains the boolean state of the --help, --usage and --?
235 options (true if any of these options were passed on the command line).
236  
237 =item B<meta>
238  
239 This returns the role meta object.
240  
241 =back
242  
243 =head1 AUTHORS
244  
245 =over 4
246  
247 =item NAKAGAWA Masaki <masaki@cpan.org>
248  
249 =item FUJI Goro <gfuji@cpan.org>
250  
251 =item Stevan Little <stevan@iinteractive.com>
252  
253 =item Brandon L. Black <blblack@gmail.com>
254  
255 =item Yuval Kogman <nothingmuch@woobling.org>
256  
257 =item Ryan D Johnson <ryan@innerfence.com>
258  
259 =item Drew Taylor <drew@drewtaylor.com>
260  
261 =item Tomas Doran <bobtfish@bobtfish.net>
262  
263 =item Florian Ragwitz <rafl@debian.org>
264  
265 =item Dagfinn Ilmari Mannsaker <ilmari@ilmari.org>
266  
267 =item Avar Arnfjord Bjarmason <avar@cpan.org>
268  
269 =item Chris Prather <perigrin@cpan.org>
270  
271 =item Mark Gardner <mjgardner@cpan.org>
272  
273 =item Tokuhiro Matsuno <tokuhirom@cpan.org>
274  
275 =back
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is copyright (c) 2012 by Infinity Interactive, Inc.
280  
281 This is free software; you can redistribute it and/or modify it under
282 the same terms as the Perl 5 programming language system itself.
283  
284 =cut
« Build ↑ Summary blib/lib/MouseX/Getopt/Meta/Attribute.pm »