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