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