Commit | Line | Data |
5dac17c3 |
1 | |
2 | package MooseX::Getopt; |
3 | use Moose::Role; |
4 | |
75a6449b |
5 | use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive }; |
6 | |
ef47fe44 |
7 | my @roles = ('MooseX::Getopt::Basic'); |
30ed85f7 |
8 | if (HAVE_GLD()) { push @roles, 'MooseX::Getopt::GLD' } |
ef47fe44 |
9 | |
10 | with @roles; |
11 | |
8034a232 |
12 | no Moose::Role; 1; |
5dac17c3 |
13 | |
14 | __END__ |
15 | |
16 | =pod |
17 | |
18 | =head1 NAME |
19 | |
8034a232 |
20 | MooseX::Getopt - A Moose role for processing command line options |
5dac17c3 |
21 | |
22 | =head1 SYNOPSIS |
23 | |
4e086633 |
24 | ## In your class |
5dac17c3 |
25 | package My::App; |
26 | use Moose; |
4e086633 |
27 | |
5dac17c3 |
28 | with 'MooseX::Getopt'; |
4e086633 |
29 | |
5dac17c3 |
30 | has 'out' => (is => 'rw', isa => 'Str', required => 1); |
31 | has 'in' => (is => 'rw', isa => 'Str', required => 1); |
4e086633 |
32 | |
5dac17c3 |
33 | # ... rest of the class here |
4e086633 |
34 | |
5dac17c3 |
35 | ## in your script |
36 | #!/usr/bin/perl |
4e086633 |
37 | |
5dac17c3 |
38 | use My::App; |
4e086633 |
39 | |
5dac17c3 |
40 | my $app = My::App->new_with_options(); |
41 | # ... rest of the script here |
4e086633 |
42 | |
5dac17c3 |
43 | ## on the command line |
44 | % perl my_app_script.pl -in file.input -out file.dump |
45 | |
46 | =head1 DESCRIPTION |
47 | |
4e086633 |
48 | This is a role which provides an alternate constructor for creating |
49 | objects using parameters passed in from the command line. |
8034a232 |
50 | |
4e086633 |
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 |
8034a232 |
54 | constraint defined, it will configure Getopt::Long to handle the option |
3899e5df |
55 | accordingly. |
56 | |
2814de27 |
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. |
3899e5df |
60 | |
2814de27 |
61 | You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt> |
62 | or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt> |
0f8232b6 |
63 | to have C<MooseX::Getopt> ignore your attribute in the commandline options. |
64 | |
3899e5df |
65 | By default, attributes which start with an underscore are not given |
66 | commandline argument support, unless the attribute's metaclass is set |
3d9a716d |
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', ...); |
4e086633 |
72 | |
3d9a716d |
73 | # or for read-only attributes |
4e086633 |
74 | has '_bar' => (reader => 'bar', ...); |
3d9a716d |
75 | |
4e086633 |
76 | This will mean that Getopt will not handle a --foo param, but your |
77 | code can still call the C<foo> method. |
8034a232 |
78 | |
ee69c4ba |
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 |
b4a79051 |
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. |
ee69c4ba |
88 | |
8034a232 |
89 | =head2 Supported Type Constraints |
90 | |
91 | =over 4 |
92 | |
93 | =item I<Bool> |
94 | |
4e086633 |
95 | A I<Bool> type constraint is set up as a boolean option with |
8034a232 |
96 | Getopt::Long. So that this attribute description: |
97 | |
98 | has 'verbose' => (is => 'rw', isa => 'Bool'); |
99 | |
4e086633 |
100 | would translate into C<verbose!> as a Getopt::Long option descriptor, |
8034a232 |
101 | which would enable the following command line options: |
102 | |
103 | % my_script.pl --verbose |
4e086633 |
104 | % my_script.pl --noverbose |
105 | |
8034a232 |
106 | =item I<Int>, I<Float>, I<Str> |
107 | |
4e086633 |
108 | These type constraints are set up as properly typed options with |
8034a232 |
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' => ( |
4e086633 |
117 | is => 'rw', |
118 | isa => 'ArrayRef', |
8034a232 |
119 | default => sub { [] } |
120 | ); |
121 | |
4e086633 |
122 | would translate into C<includes=s@> as a Getopt::Long option descriptor, |
8034a232 |
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' => ( |
4e086633 |
133 | is => 'rw', |
134 | isa => 'HashRef', |
8034a232 |
135 | default => sub { {} } |
136 | ); |
137 | |
4e086633 |
138 | would translate into C<define=s%> as a Getopt::Long option descriptor, |
8034a232 |
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 | |
4e086633 |
147 | It is possible to create custom type constraint to option spec |
8034a232 |
148 | mappings if you need them. The process is fairly simple (but a |
4e086633 |
149 | little verbose maybe). First you create a custom subtype, like |
8034a232 |
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 | |
4e086633 |
162 | Now any attribute declarations using this type constraint will |
8034a232 |
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 | |
4e086633 |
175 | This example is fairly trivial, but more complex validations are |
8034a232 |
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 | |
f63e6310 |
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 | |
5dac17c3 |
195 | =head1 METHODS |
196 | |
197 | =over 4 |
198 | |
199 | =item B<new_with_options (%params)> |
200 | |
4e086633 |
201 | This method will take a set of default C<%params> and then collect |
8034a232 |
202 | params from the command line (possibly overriding those in C<%params>) |
203 | and then return a newly constructed object. |
204 | |
0e3f178a |
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 | |
6140ec5c |
208 | The paramater C<disable_gld>, if specified and a true value will disable |
209 | the use of L<Getopt::Long::Descriptive> . |
210 | |
f63e6310 |
211 | If L<Getopt::Long/GetOptions> fails (due to invalid arguments), |
212 | C<new_with_options> will throw an exception. |
213 | |
47a89a8d |
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 | |
fad5da09 |
223 | If you have L<Getopt::Long::Descriptive> a the C<usage> param is also passed to |
224 | C<new>. |
225 | |
3899e5df |
226 | =item B<ARGV> |
227 | |
228 | This accessor contains a reference to a copy of the C<@ARGV> array |
f63e6310 |
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. |
3899e5df |
236 | |
5dac17c3 |
237 | =item B<meta> |
238 | |
8034a232 |
239 | This returns the role meta object. |
240 | |
ef47fe44 |
241 | =item B<HAVE_GLD> |
242 | |
243 | A constant for internal use. |
244 | |
5dac17c3 |
245 | =back |
246 | |
247 | =head1 BUGS |
248 | |
4e086633 |
249 | All complex software has bugs lurking in it, and this module is no |
5dac17c3 |
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 | |
e2911e34 |
257 | Brandon L. Black, E<lt>blblack@gmail.comE<gt> |
258 | |
630657d5 |
259 | Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt> |
260 | |
78a71ae5 |
261 | =head1 CONTRIBUTORS |
262 | |
263 | Ryan D Johnson, E<lt>ryan@innerfence.comE<gt> |
264 | |
47a89a8d |
265 | Drew Taylor, E<lt>drew@drewtaylor.comE<gt> |
266 | |
5dac17c3 |
267 | =head1 COPYRIGHT AND LICENSE |
268 | |
adbe3e57 |
269 | Copyright 2007-2008 by Infinity Interactive, Inc. |
5dac17c3 |
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 |