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