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