add reference to MooseX::Getopt::Usage
[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
75434e78 6with 'MooseX::Getopt::GLD';
5dac17c3 7
669588e2 8no Moose::Role;
5dac17c3 9
669588e2 101;
5dac17c3 11
12=head1 SYNOPSIS
13
4e086633 14 ## In your class
5dac17c3 15 package My::App;
16 use Moose;
4e086633 17
5dac17c3 18 with 'MooseX::Getopt';
4e086633 19
5dac17c3 20 has 'out' => (is => 'rw', isa => 'Str', required => 1);
21 has 'in' => (is => 'rw', isa => 'Str', required => 1);
4e086633 22
5dac17c3 23 # ... rest of the class here
4e086633 24
5dac17c3 25 ## in your script
26 #!/usr/bin/perl
4e086633 27
5dac17c3 28 use My::App;
4e086633 29
5dac17c3 30 my $app = My::App->new_with_options();
31 # ... rest of the script here
4e086633 32
5dac17c3 33 ## on the command line
34 % perl my_app_script.pl -in file.input -out file.dump
35
36=head1 DESCRIPTION
37
4e086633 38This is a role which provides an alternate constructor for creating
39objects using parameters passed in from the command line.
8034a232 40
4e086633 41This module attempts to DWIM as much as possible with the command line
42params by introspecting your class's attributes. It will use the name
43of your attribute as the command line option, and if there is a type
8034a232 44constraint defined, it will configure Getopt::Long to handle the option
3899e5df 45accordingly.
46
2814de27 47You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
48attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
49commandline option names and aliases.
3899e5df 50
2814de27 51You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
52or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
0f8232b6 53to have C<MooseX::Getopt> ignore your attribute in the commandline options.
54
3899e5df 55By default, attributes which start with an underscore are not given
56commandline argument support, unless the attribute's metaclass is set
7f5f3d94 57to L<MooseX::Getopt::Meta::Attribute>. If you don't want your accessors
58to have the leading underscore in their name, you can do this:
3d9a716d 59
60 # for read/write attributes
61 has '_foo' => (accessor => 'foo', ...);
4e086633 62
3d9a716d 63 # or for read-only attributes
4e086633 64 has '_bar' => (reader => 'bar', ...);
3d9a716d 65
4e086633 66This will mean that Getopt will not handle a --foo param, but your
67code can still call the C<foo> method.
8034a232 68
ee69c4ba 69If your class also uses a configfile-loading role based on
70L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
71L<MooseX::Getopt>'s C<new_with_options> will load the configfile
b4a79051 72specified by the C<--configfile> option (or the default you've
73given for the configfile attribute) for you.
74
75Options specified in multiple places follow the following
fe01c3e8 76precedence order: commandline overrides configfile, which
b4a79051 77overrides explicit new_with_options parameters.
ee69c4ba 78
8034a232 79=head2 Supported Type Constraints
80
81=over 4
82
83=item I<Bool>
84
4e086633 85A I<Bool> type constraint is set up as a boolean option with
8034a232 86Getopt::Long. So that this attribute description:
87
88 has 'verbose' => (is => 'rw', isa => 'Bool');
89
4e086633 90would translate into C<verbose!> as a Getopt::Long option descriptor,
8034a232 91which would enable the following command line options:
92
93 % my_script.pl --verbose
4e086633 94 % my_script.pl --noverbose
95
8034a232 96=item I<Int>, I<Float>, I<Str>
97
4e086633 98These type constraints are set up as properly typed options with
8034a232 99Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
100
101=item I<ArrayRef>
102
103An I<ArrayRef> type constraint is set up as a multiple value option
104in Getopt::Long. So that this attribute description:
105
106 has 'include' => (
4e086633 107 is => 'rw',
108 isa => 'ArrayRef',
8034a232 109 default => sub { [] }
110 );
111
4e086633 112would translate into C<includes=s@> as a Getopt::Long option descriptor,
8034a232 113which would enable the following command line options:
114
115 % my_script.pl --include /usr/lib --include /usr/local/lib
116
117=item I<HashRef>
118
119A I<HashRef> type constraint is set up as a hash value option
120in Getopt::Long. So that this attribute description:
121
122 has 'define' => (
4e086633 123 is => 'rw',
124 isa => 'HashRef',
8034a232 125 default => sub { {} }
126 );
127
4e086633 128would translate into C<define=s%> as a Getopt::Long option descriptor,
8034a232 129which would enable the following command line options:
130
131 % my_script.pl --define os=linux --define vendor=debian
132
133=back
134
135=head2 Custom Type Constraints
136
4e086633 137It is possible to create custom type constraint to option spec
8034a232 138mappings if you need them. The process is fairly simple (but a
4e086633 139little verbose maybe). First you create a custom subtype, like
8034a232 140so:
141
142 subtype 'ArrayOfInts'
143 => as 'ArrayRef'
144 => where { scalar (grep { looks_like_number($_) } @$_) };
145
146Then you register the mapping, like so:
147
148 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
149 'ArrayOfInts' => '=i@'
150 );
151
4e086633 152Now any attribute declarations using this type constraint will
8034a232 153get the custom option spec. So that, this:
154
155 has 'nums' => (
156 is => 'ro',
157 isa => 'ArrayOfInts',
158 default => sub { [0] }
159 );
160
161Will translate to the following on the command line:
162
163 % my_script.pl --nums 5 --nums 88 --nums 199
164
4e086633 165This example is fairly trivial, but more complex validations are
8034a232 166easily possible with a little creativity. The trick is balancing
167the type constraint validations with the Getopt::Long validations.
168
169Better examples are certainly welcome :)
170
f63e6310 171=head2 Inferred Type Constraints
172
173If you define a custom subtype which is a subtype of one of the
174standard L</Supported Type Constraints> above, and do not explicitly
175provide custom support as in L</Custom Type Constraints> above,
176MooseX::Getopt will treat it like the parent type for Getopt
177purposes.
178
179For example, if you had the same custom C<ArrayOfInts> subtype
180from the examples above, but did not add a new custom option
181type for it to the C<OptionTypeMap>, it would be treated just
182like a normal C<ArrayRef> type for Getopt purposes (that is,
183C<=s@>).
184
669588e2 185=method B<new_with_options (%params)>
5dac17c3 186
4e086633 187This method will take a set of default C<%params> and then collect
8034a232 188params from the command line (possibly overriding those in C<%params>)
189and then return a newly constructed object.
190
035b1668 191The special parameter C<argv>, if specified should point to an array
0e3f178a 192reference with an array to use instead of C<@ARGV>.
193
f63e6310 194If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
195C<new_with_options> will throw an exception.
196
47a89a8d 197If L<Getopt::Long::Descriptive> is installed and any of the following
2557b526 198command line params are passed, the program will exit with usage
81b19ed8 199information (and the option's state will be stored in the help_flag
200attribute). You can add descriptions for each option by including a
47a89a8d 201B<documentation> option for each attribute to document.
202
fa8dcd69 203 -?
47a89a8d 204 --?
8d396d8a 205 -h
47a89a8d 206 --help
207 --usage
208
b766829d 209If you have L<Getopt::Long::Descriptive> the C<usage> param is also passed to
81b19ed8 210C<new> as the usage option.
fad5da09 211
669588e2 212=method B<ARGV>
3899e5df 213
214This accessor contains a reference to a copy of the C<@ARGV> array
f63e6310 215as it originally existed at the time of C<new_with_options>.
216
669588e2 217=method B<extra_argv>
f63e6310 218
219This accessor contains an arrayref of leftover C<@ARGV> elements that
220L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
221un-mangled.
3899e5df 222
faf7d951 223B<Important>: By default, L<Getopt::Long> will reject unrecognized I<options>
224(that is, options that do not correspond with attributes using the Getopt
edd79c6f 225trait). To disable this, and allow options to also be saved in C<extra_argv> (for example to pass along to another class's C<new_with_options>), you can either enable the
226C<pass_through> option of L<Getopt::Long> for your class: C<< use Getopt::Long
227qw(:config pass_through); >> or specify a value for for L<MooseX::Getopt::GLD>'s C<getopt_conf> parameter.
449f0087 228
81b19ed8 229=method B<usage>
230
231This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
232L<Getopt::Long::Descriptive> is used).
233
234=method B<help_flag>
235
236This accessor contains the boolean state of the --help, --usage and --?
237options (true if any of these options were passed on the command line).
238
83446b78 239=method B<print_usage_text>
240
241This method is called internally when the C<help_flag> state is true.
242It prints the text from the C<usage> object (see above) to stdout and then the
243program terminates normally. You can apply a method modification (see
244L<Moose::Manual::MethodModifiers>) if different behaviour is desired, for
245example to include additional text.
246
669588e2 247=method B<meta>
5dac17c3 248
8034a232 249This returns the role meta object.
250
f3615693 251=method B<process_argv (%params)>
252
253This does most of the work of C<new_with_options>, analyzing the parameters
254and argv, except for actually calling the constructor. It returns a
255L<MooseX::Getopt::ProcessedArgv> object. C<new_with_options> uses this
256method internally, so modifying this method via subclasses/roles will affect
257C<new_with_options>.
258
449f0087 259=head2 More Customization Options
260
772e216b 261See L<Getopt::Long/Configuring Getopt::Long> for many other customizations you
449f0087 262can make to how options are parsed. Simply C<use Getopt::Long qw(:config
263other_options...)> in your class to set these.
264
5dac17c3 265=cut
cdb3fe1f 266
986fb469 267=head1 SEE ALSO
268
269L<MooseX::Getopt::Usage>, an extension to generate man pages, with colour
270