foo
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt.pm
1
2 package MooseX::Getopt;
3 use Moose::Role;
4
5 use Getopt::Long ();
6
7 use MooseX::Getopt::OptionTypeMap;
8 use MooseX::Getopt::Meta::Attribute;
9
10 our $VERSION   = '0.06';
11 our $AUTHORITY = 'cpan:STEVAN';
12
13 has ARGV       => (is => 'rw', isa => 'ArrayRef');
14 has extra_argv => (is => 'rw', isa => 'ArrayRef');
15
16 sub new_with_options {
17     my ($class, @params) = @_;
18
19     my %processed = $class->_parse_argv( 
20         options => [ 
21             $class->_attrs_to_options( @params ) 
22         ] 
23     );
24
25     $class->new(
26         ARGV       => $processed{argv_copy},
27         extra_argv => $processed{argv},
28         @params, # explicit params to ->new
29         %{ $processed{params} }, # params from CLI
30     );
31 }
32
33 sub _parse_argv {
34     my ( $class, %params ) = @_;
35
36     local @ARGV = @{ $params{argv} || \@ARGV };
37
38     my ( @options, %name_to_init_arg, %options );
39
40     foreach my $opt ( @{ $params{options} } ) {
41         push @options, $opt->{opt_string};
42         $name_to_init_arg{ $opt->{name} } = $opt->{init_arg};
43     }
44
45     # Get a clean copy of the original @ARGV
46     my $argv_copy = [ @ARGV ];
47
48     {
49         local $SIG{__WARN__} = sub { die $_[0] };
50         Getopt::Long::GetOptions(\%options, @options);
51     }
52
53     # Get a copy of the Getopt::Long-mangled @ARGV
54     my $argv_mangled = [ @ARGV ];
55
56     return (
57         argv_copy => $argv_copy,
58         argv      => $argv_mangled,
59         params    => {
60             map { 
61                 $name_to_init_arg{$_} => $options{$_} 
62             } keys %options,   
63         }
64     );
65 }
66
67 sub _compute_getopt_attrs {
68     my $class = shift;
69
70     grep {
71         $_->isa("MooseX::Getopt::Meta::Attribute")
72             or
73         $_->name !~ /^_/
74             &&
75         !$_->isa('MooseX::Getopt::Meta::NoGetopt')
76     } $class->meta->compute_all_applicable_attributes
77 }
78
79 sub _attrs_to_options {
80     my $class = shift;
81
82     my @options;
83
84     foreach my $attr ($class->_compute_getopt_attrs) {
85         my $name = $attr->name;
86
87         my $aliases;
88
89         if ($attr->isa('MooseX::Getopt::Meta::Attribute')) {
90             $name = $attr->cmd_flag if $attr->has_cmd_flag;
91             $aliases = $attr->cmd_aliases if $attr->has_cmd_aliases;
92         }
93
94         my $opt_string = $aliases
95             ? join(q{|}, $name, @$aliases)
96             : $name;
97
98         if ($attr->has_type_constraint) {
99             my $type_name = $attr->type_constraint->name;
100             if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {                   
101                 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name);
102             }
103         }
104
105         push @options, {
106             name       => $name,
107             init_arg   => $attr->init_arg,
108             opt_string => $opt_string,
109             required   => $attr->is_required,
110             ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
111         }
112     }
113
114     return @options;
115 }
116
117 no Moose::Role; 1;
118
119 __END__
120
121 =pod
122
123 =head1 NAME
124
125 MooseX::Getopt - A Moose role for processing command line options
126
127 =head1 SYNOPSIS
128
129   ## In your class 
130   package My::App;
131   use Moose;
132   
133   with 'MooseX::Getopt';
134   
135   has 'out' => (is => 'rw', isa => 'Str', required => 1);
136   has 'in'  => (is => 'rw', isa => 'Str', required => 1);
137   
138   # ... rest of the class here
139   
140   ## in your script
141   #!/usr/bin/perl
142   
143   use My::App;
144   
145   my $app = My::App->new_with_options();
146   # ... rest of the script here
147   
148   ## on the command line
149   % perl my_app_script.pl -in file.input -out file.dump
150
151 =head1 DESCRIPTION
152
153 This is a role which provides an alternate constructor for creating 
154 objects using parameters passed in from the command line. 
155
156 This module attempts to DWIM as much as possible with the command line 
157 params by introspecting your class's attributes. It will use the name 
158 of your attribute as the command line option, and if there is a type 
159 constraint defined, it will configure Getopt::Long to handle the option
160 accordingly.
161
162 You can use the attribute metaclass L<MooseX::Getopt::Meta::Attribute>
163 to get non-default commandline option names and aliases.
164
165 By default, attributes which start with an underscore are not given
166 commandline argument support, unless the attribute's metaclass is set
167 to L<MooseX::Getopt::Meta::Attribute>. If you don't want you accessors
168 to have the leading underscore in thier name, you can do this:
169
170   # for read/write attributes
171   has '_foo' => (accessor => 'foo', ...);
172   
173   # or for read-only attributes
174   has '_bar' => (reader => 'bar', ...);  
175
176 This will mean that Getopt will not handle a --foo param, but your 
177 code can still call the C<foo> method. 
178
179 =head2 Supported Type Constraints
180
181 =over 4
182
183 =item I<Bool>
184
185 A I<Bool> type constraint is set up as a boolean option with 
186 Getopt::Long. So that this attribute description:
187
188   has 'verbose' => (is => 'rw', isa => 'Bool');
189
190 would translate into C<verbose!> as a Getopt::Long option descriptor, 
191 which would enable the following command line options:
192
193   % my_script.pl --verbose
194   % my_script.pl --noverbose  
195   
196 =item I<Int>, I<Float>, I<Str>
197
198 These type constraints are set up as properly typed options with 
199 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
200
201 =item I<ArrayRef>
202
203 An I<ArrayRef> type constraint is set up as a multiple value option
204 in Getopt::Long. So that this attribute description:
205
206   has 'include' => (
207       is      => 'rw', 
208       isa     => 'ArrayRef', 
209       default => sub { [] }
210   );
211
212 would translate into C<includes=s@> as a Getopt::Long option descriptor, 
213 which would enable the following command line options:
214
215   % my_script.pl --include /usr/lib --include /usr/local/lib
216
217 =item I<HashRef>
218
219 A I<HashRef> type constraint is set up as a hash value option
220 in Getopt::Long. So that this attribute description:
221
222   has 'define' => (
223       is      => 'rw', 
224       isa     => 'HashRef', 
225       default => sub { {} }
226   );
227
228 would translate into C<define=s%> as a Getopt::Long option descriptor, 
229 which would enable the following command line options:
230
231   % my_script.pl --define os=linux --define vendor=debian
232
233 =back
234
235 =head2 Custom Type Constraints
236
237 It is possible to create custom type constraint to option spec 
238 mappings if you need them. The process is fairly simple (but a
239 little verbose maybe). First you create a custom subtype, like 
240 so:
241
242   subtype 'ArrayOfInts'
243       => as 'ArrayRef'
244       => where { scalar (grep { looks_like_number($_) } @$_)  };
245
246 Then you register the mapping, like so:
247
248   MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
249       'ArrayOfInts' => '=i@'
250   );
251
252 Now any attribute declarations using this type constraint will 
253 get the custom option spec. So that, this:
254
255   has 'nums' => (
256       is      => 'ro',
257       isa     => 'ArrayOfInts',
258       default => sub { [0] }
259   );
260
261 Will translate to the following on the command line:
262
263   % my_script.pl --nums 5 --nums 88 --nums 199
264
265 This example is fairly trivial, but more complex validations are 
266 easily possible with a little creativity. The trick is balancing
267 the type constraint validations with the Getopt::Long validations.
268
269 Better examples are certainly welcome :)
270
271 =head2 Inferred Type Constraints
272
273 If you define a custom subtype which is a subtype of one of the
274 standard L</Supported Type Constraints> above, and do not explicitly
275 provide custom support as in L</Custom Type Constraints> above,
276 MooseX::Getopt will treat it like the parent type for Getopt
277 purposes.
278
279 For example, if you had the same custom C<ArrayOfInts> subtype
280 from the examples above, but did not add a new custom option
281 type for it to the C<OptionTypeMap>, it would be treated just
282 like a normal C<ArrayRef> type for Getopt purposes (that is,
283 C<=s@>).
284
285 =head1 METHODS
286
287 =over 4
288
289 =item B<new_with_options (%params)>
290
291 This method will take a set of default C<%params> and then collect 
292 params from the command line (possibly overriding those in C<%params>)
293 and then return a newly constructed object.
294
295 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
296 C<new_with_options> will throw an exception.
297
298 =item B<ARGV>
299
300 This accessor contains a reference to a copy of the C<@ARGV> array
301 as it originally existed at the time of C<new_with_options>.
302
303 =item B<extra_argv>
304
305 This accessor contains an arrayref of leftover C<@ARGV> elements that
306 L<Getopt::Long> did not parse.  Note that the real C<@ARGV> is left
307 un-mangled.
308
309 =item B<meta>
310
311 This returns the role meta object.
312
313 =back
314
315 =head1 BUGS
316
317 All complex software has bugs lurking in it, and this module is no 
318 exception. If you find a bug please either email me, or add the bug
319 to cpan-RT.
320
321 =head1 AUTHOR
322
323 Stevan Little E<lt>stevan@iinteractive.comE<gt>
324
325 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
326
327 =head1 COPYRIGHT AND LICENSE
328
329 Copyright 2007 by Infinity Interactive, Inc.
330
331 L<http://www.iinteractive.com>
332
333 This library is free software; you can redistribute it and/or modify
334 it under the same terms as Perl itself.
335
336 =cut