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