2 package MooseX::Getopt;
5 use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive };
7 my @roles = ('MooseX::Getopt::Basic');
8 if (HAVE_GLD()) { push @roles, 'MooseX::Getopt::GLD' }
20 MooseX::Getopt - A Moose role for processing command line options
28 with 'MooseX::Getopt';
30 has 'out' => (is => 'rw', isa => 'Str', required => 1);
31 has 'in' => (is => 'rw', isa => 'Str', required => 1);
33 # ... rest of the class here
40 my $app = My::App->new_with_options();
41 # ... rest of the script here
43 ## on the command line
44 % perl my_app_script.pl -in file.input -out file.dump
48 This is a role which provides an alternate constructor for creating
49 objects using parameters passed in from the command line.
51 This module attempts to DWIM as much as possible with the command line
52 params by introspecting your class's attributes. It will use the name
53 of your attribute as the command line option, and if there is a type
54 constraint defined, it will configure Getopt::Long to handle the option
57 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
58 attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
59 commandline option names and aliases.
61 You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
62 or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
63 to have C<MooseX::Getopt> ignore your attribute in the commandline options.
65 By default, attributes which start with an underscore are not given
66 commandline argument support, unless the attribute's metaclass is set
67 to L<MooseX::Getopt::Meta::Attribute>. If you don't want you accessors
68 to have the leading underscore in thier name, you can do this:
70 # for read/write attributes
71 has '_foo' => (accessor => 'foo', ...);
73 # or for read-only attributes
74 has '_bar' => (reader => 'bar', ...);
76 This will mean that Getopt will not handle a --foo param, but your
77 code can still call the C<foo> method.
79 If your class also uses a configfile-loading role based on
80 L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
81 L<MooseX::Getopt>'s C<new_with_options> will load the configfile
82 specified by the C<--configfile> option (or the default you've
83 given for the configfile attribute) for you.
85 Options specified in multiple places follow the following
86 precendence order: commandline overrides configfile, which
87 overrides explicit new_with_options parameters.
89 =head2 Supported Type Constraints
95 A I<Bool> type constraint is set up as a boolean option with
96 Getopt::Long. So that this attribute description:
98 has 'verbose' => (is => 'rw', isa => 'Bool');
100 would translate into C<verbose!> as a Getopt::Long option descriptor,
101 which would enable the following command line options:
103 % my_script.pl --verbose
104 % my_script.pl --noverbose
106 =item I<Int>, I<Float>, I<Str>
108 These type constraints are set up as properly typed options with
109 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
113 An I<ArrayRef> type constraint is set up as a multiple value option
114 in Getopt::Long. So that this attribute description:
119 default => sub { [] }
122 would translate into C<includes=s@> as a Getopt::Long option descriptor,
123 which would enable the following command line options:
125 % my_script.pl --include /usr/lib --include /usr/local/lib
129 A I<HashRef> type constraint is set up as a hash value option
130 in Getopt::Long. So that this attribute description:
135 default => sub { {} }
138 would translate into C<define=s%> as a Getopt::Long option descriptor,
139 which would enable the following command line options:
141 % my_script.pl --define os=linux --define vendor=debian
145 =head2 Custom Type Constraints
147 It is possible to create custom type constraint to option spec
148 mappings if you need them. The process is fairly simple (but a
149 little verbose maybe). First you create a custom subtype, like
152 subtype 'ArrayOfInts'
154 => where { scalar (grep { looks_like_number($_) } @$_) };
156 Then you register the mapping, like so:
158 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
159 'ArrayOfInts' => '=i@'
162 Now any attribute declarations using this type constraint will
163 get the custom option spec. So that, this:
167 isa => 'ArrayOfInts',
168 default => sub { [0] }
171 Will translate to the following on the command line:
173 % my_script.pl --nums 5 --nums 88 --nums 199
175 This example is fairly trivial, but more complex validations are
176 easily possible with a little creativity. The trick is balancing
177 the type constraint validations with the Getopt::Long validations.
179 Better examples are certainly welcome :)
181 =head2 Inferred Type Constraints
183 If you define a custom subtype which is a subtype of one of the
184 standard L</Supported Type Constraints> above, and do not explicitly
185 provide custom support as in L</Custom Type Constraints> above,
186 MooseX::Getopt will treat it like the parent type for Getopt
189 For example, if you had the same custom C<ArrayOfInts> subtype
190 from the examples above, but did not add a new custom option
191 type for it to the C<OptionTypeMap>, it would be treated just
192 like a normal C<ArrayRef> type for Getopt purposes (that is,
199 =item B<new_with_options (%params)>
201 This method will take a set of default C<%params> and then collect
202 params from the command line (possibly overriding those in C<%params>)
203 and then return a newly constructed object.
205 The special parameter C<argv>, if specified should point to an array
206 reference with an array to use instead of C<@ARGV>.
208 The paramater C<disable_gld>, if specified and a true value will disable
209 the use of L<Getopt::Long::Descriptive> .
211 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
212 C<new_with_options> will throw an exception.
214 If L<Getopt::Long::Descriptive> is installed and any of the following
215 command line params are passed, the program will exit with usage
216 information. You can add descriptions for each option by including a
217 B<documentation> option for each attribute to document.
223 If you have L<Getopt::Long::Descriptive> a the C<usage> param is also passed to
228 This accessor contains a reference to a copy of the C<@ARGV> array
229 as it originally existed at the time of C<new_with_options>.
233 This accessor contains an arrayref of leftover C<@ARGV> elements that
234 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
239 This returns the role meta object.
243 A constant for internal use.
249 All complex software has bugs lurking in it, and this module is no
250 exception. If you find a bug please either email me, or add the bug
255 Stevan Little E<lt>stevan@iinteractive.comE<gt>
257 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
259 Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
263 Ryan D Johnson, E<lt>ryan@innerfence.comE<gt>
265 Drew Taylor, E<lt>drew@drewtaylor.comE<gt>
267 =head1 COPYRIGHT AND LICENSE
269 Copyright 2007-2008 by Infinity Interactive, Inc.
271 L<http://www.iinteractive.com>
273 This library is free software; you can redistribute it and/or modify
274 it under the same terms as Perl itself.