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