Implement functionality of MooseX::Getopt::Basic .
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt.pm
CommitLineData
5dac17c3 1
2package MooseX::Getopt;
3use Moose::Role;
4
75a6449b 5use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive };
6
ef47fe44 7my @roles = ('MooseX::Getopt::Basic');
30ed85f7 8if (HAVE_GLD()) { push @roles, 'MooseX::Getopt::GLD' }
ef47fe44 9
10with @roles;
11
8034a232 12no Moose::Role; 1;
5dac17c3 13
14__END__
15
16=pod
17
18=head1 NAME
19
8034a232 20MooseX::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 48This is a role which provides an alternate constructor for creating
49objects using parameters passed in from the command line.
8034a232 50
4e086633 51This module attempts to DWIM as much as possible with the command line
52params by introspecting your class's attributes. It will use the name
53of your attribute as the command line option, and if there is a type
8034a232 54constraint defined, it will configure Getopt::Long to handle the option
3899e5df 55accordingly.
56
2814de27 57You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
58attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
59commandline option names and aliases.
3899e5df 60
2814de27 61You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
62or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
0f8232b6 63to have C<MooseX::Getopt> ignore your attribute in the commandline options.
64
3899e5df 65By default, attributes which start with an underscore are not given
66commandline argument support, unless the attribute's metaclass is set
3d9a716d 67to L<MooseX::Getopt::Meta::Attribute>. If you don't want you accessors
68to 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 76This will mean that Getopt will not handle a --foo param, but your
77code can still call the C<foo> method.
8034a232 78
ee69c4ba 79If your class also uses a configfile-loading role based on
80L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
81L<MooseX::Getopt>'s C<new_with_options> will load the configfile
b4a79051 82specified by the C<--configfile> option (or the default you've
83given for the configfile attribute) for you.
84
85Options specified in multiple places follow the following
86precendence order: commandline overrides configfile, which
87overrides explicit new_with_options parameters.
ee69c4ba 88
8034a232 89=head2 Supported Type Constraints
90
91=over 4
92
93=item I<Bool>
94
4e086633 95A I<Bool> type constraint is set up as a boolean option with
8034a232 96Getopt::Long. So that this attribute description:
97
98 has 'verbose' => (is => 'rw', isa => 'Bool');
99
4e086633 100would translate into C<verbose!> as a Getopt::Long option descriptor,
8034a232 101which 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 108These type constraints are set up as properly typed options with
8034a232 109Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
110
111=item I<ArrayRef>
112
113An I<ArrayRef> type constraint is set up as a multiple value option
114in 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 122would translate into C<includes=s@> as a Getopt::Long option descriptor,
8034a232 123which 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
129A I<HashRef> type constraint is set up as a hash value option
130in 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 138would translate into C<define=s%> as a Getopt::Long option descriptor,
8034a232 139which 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 147It is possible to create custom type constraint to option spec
8034a232 148mappings if you need them. The process is fairly simple (but a
4e086633 149little verbose maybe). First you create a custom subtype, like
8034a232 150so:
151
152 subtype 'ArrayOfInts'
153 => as 'ArrayRef'
154 => where { scalar (grep { looks_like_number($_) } @$_) };
155
156Then you register the mapping, like so:
157
158 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
159 'ArrayOfInts' => '=i@'
160 );
161
4e086633 162Now any attribute declarations using this type constraint will
8034a232 163get the custom option spec. So that, this:
164
165 has 'nums' => (
166 is => 'ro',
167 isa => 'ArrayOfInts',
168 default => sub { [0] }
169 );
170
171Will translate to the following on the command line:
172
173 % my_script.pl --nums 5 --nums 88 --nums 199
174
4e086633 175This example is fairly trivial, but more complex validations are
8034a232 176easily possible with a little creativity. The trick is balancing
177the type constraint validations with the Getopt::Long validations.
178
179Better examples are certainly welcome :)
180
f63e6310 181=head2 Inferred Type Constraints
182
183If you define a custom subtype which is a subtype of one of the
184standard L</Supported Type Constraints> above, and do not explicitly
185provide custom support as in L</Custom Type Constraints> above,
186MooseX::Getopt will treat it like the parent type for Getopt
187purposes.
188
189For example, if you had the same custom C<ArrayOfInts> subtype
190from the examples above, but did not add a new custom option
191type for it to the C<OptionTypeMap>, it would be treated just
192like a normal C<ArrayRef> type for Getopt purposes (that is,
193C<=s@>).
194
5dac17c3 195=head1 METHODS
196
197=over 4
198
199=item B<new_with_options (%params)>
200
4e086633 201This method will take a set of default C<%params> and then collect
8034a232 202params from the command line (possibly overriding those in C<%params>)
203and then return a newly constructed object.
204
0e3f178a 205The special parameter C<argv>, if specified should point to an array
206reference with an array to use instead of C<@ARGV>.
207
6140ec5c 208The paramater C<disable_gld>, if specified and a true value will disable
209the use of L<Getopt::Long::Descriptive> .
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