2 package MooseX::Getopt;
5 use Getopt::Long::Descriptive ();
7 use MooseX::Getopt::OptionTypeMap;
8 use MooseX::Getopt::Meta::Attribute;
9 use MooseX::Getopt::Meta::Attribute::NoGetopt;
11 our $VERSION = '0.08';
12 our $AUTHORITY = 'cpan:STEVAN';
14 has ARGV => (is => 'rw', isa => 'ArrayRef', documentation => "hidden");
15 has extra_argv => (is => 'rw', isa => 'ArrayRef', documentation => "hidden");
17 sub new_with_options {
18 my ($class, @params) = @_;
20 my %processed = $class->_parse_argv(
22 $class->_attrs_to_options( @params )
26 my $params = $processed{params};
28 if($class->meta->does_role('MooseX::ConfigFromFile')
29 && defined $params->{configfile}) {
31 %{$class->get_config_from_file($params->{configfile})},
37 ARGV => $processed{argv_copy},
38 extra_argv => $processed{argv},
39 @params, # explicit params to ->new
40 %$params, # params from CLI
45 my ( $class, %params ) = @_;
47 local @ARGV = @{ $params{argv} || \@ARGV };
49 my ( $opt_spec, $name_to_init_arg ) = $class->_gld_spec(%params);
51 # Get a clean copy of the original @ARGV
52 my $argv_copy = [ @ARGV ];
56 my ( $parsed_options, $usage ) = eval {
57 local $SIG{__WARN__} = sub { push @err, @_ };
58 Getopt::Long::Descriptive::describe_options($class->_usage_format(%params), @$opt_spec)
61 die join "", grep { defined } @err, $@ if @err or $@;
63 # Get a copy of the Getopt::Long-mangled @ARGV
64 my $argv_mangled = [ @ARGV ];
66 my %constructor_args = (
68 $name_to_init_arg->{$_} => $parsed_options->{$_}
69 } keys %$parsed_options,
73 params => \%constructor_args,
74 argv_copy => $argv_copy,
75 argv => $argv_mangled,
81 return "usage: %c %o";
85 my ( $class, %params ) = @_;
87 my ( @options, %name_to_init_arg );
89 foreach my $opt ( @{ $params{options} } ) {
92 $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
94 ( $opt->{required} ? (required => $opt->{required}) : () ),
95 ( exists $opt->{default} ? (default => $opt->{default}) : () ),
99 $name_to_init_arg{ $opt->{name} } = $opt->{init_arg};
102 return ( \@options, \%name_to_init_arg );
105 sub _compute_getopt_attrs {
108 $_->isa("MooseX::Getopt::Meta::Attribute")
112 !$_->isa('MooseX::Getopt::Meta::Attribute::NoGetopt')
113 } $class->meta->compute_all_applicable_attributes
116 sub _attrs_to_options {
121 foreach my $attr ($class->_compute_getopt_attrs) {
122 my $name = $attr->name;
126 if ($attr->isa('MooseX::Getopt::Meta::Attribute')) {
127 $name = $attr->cmd_flag if $attr->has_cmd_flag;
128 $aliases = $attr->cmd_aliases if $attr->has_cmd_aliases;
131 my $opt_string = $aliases
132 ? join(q{|}, $name, @$aliases)
135 if ($attr->has_type_constraint) {
136 my $type_name = $attr->type_constraint->name;
137 if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {
138 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name)
144 init_arg => $attr->init_arg,
145 opt_string => $opt_string,
146 required => $attr->is_required,
147 ( ( $attr->has_default && ( $attr->is_default_a_coderef xor $attr->is_lazy ) ) ? ( default => $attr->default({}) ) : () ),
148 ( $attr->has_documentation ? ( doc => $attr->documentation ) : () ),
163 MooseX::Getopt - A Moose role for processing command line options
171 with 'MooseX::Getopt';
173 has 'out' => (is => 'rw', isa => 'Str', required => 1);
174 has 'in' => (is => 'rw', isa => 'Str', required => 1);
176 # ... rest of the class here
183 my $app = My::App->new_with_options();
184 # ... rest of the script here
186 ## on the command line
187 % perl my_app_script.pl -in file.input -out file.dump
191 This is a role which provides an alternate constructor for creating
192 objects using parameters passed in from the command line.
194 This module attempts to DWIM as much as possible with the command line
195 params by introspecting your class's attributes. It will use the name
196 of your attribute as the command line option, and if there is a type
197 constraint defined, it will configure Getopt::Long to handle the option
200 You can use the attribute metaclass L<MooseX::Getopt::Meta::Attribute>
201 to get non-default commandline option names and aliases.
203 You can use the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetOpt>
204 to have C<MooseX::Getopt> ignore your attribute in the commandline options.
206 By default, attributes which start with an underscore are not given
207 commandline argument support, unless the attribute's metaclass is set
208 to L<MooseX::Getopt::Meta::Attribute>. If you don't want you accessors
209 to have the leading underscore in thier name, you can do this:
211 # for read/write attributes
212 has '_foo' => (accessor => 'foo', ...);
214 # or for read-only attributes
215 has '_bar' => (reader => 'bar', ...);
217 This will mean that Getopt will not handle a --foo param, but your
218 code can still call the C<foo> method.
220 If your class also uses a configfile-loading role based on
221 L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
222 L<MooseX::Getopt>'s C<new_with_options> will load the configfile
223 specified by the C<--configfile> option for you.
225 =head2 Supported Type Constraints
231 A I<Bool> type constraint is set up as a boolean option with
232 Getopt::Long. So that this attribute description:
234 has 'verbose' => (is => 'rw', isa => 'Bool');
236 would translate into C<verbose!> as a Getopt::Long option descriptor,
237 which would enable the following command line options:
239 % my_script.pl --verbose
240 % my_script.pl --noverbose
242 =item I<Int>, I<Float>, I<Str>
244 These type constraints are set up as properly typed options with
245 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
249 An I<ArrayRef> type constraint is set up as a multiple value option
250 in Getopt::Long. So that this attribute description:
255 default => sub { [] }
258 would translate into C<includes=s@> as a Getopt::Long option descriptor,
259 which would enable the following command line options:
261 % my_script.pl --include /usr/lib --include /usr/local/lib
265 A I<HashRef> type constraint is set up as a hash value option
266 in Getopt::Long. So that this attribute description:
271 default => sub { {} }
274 would translate into C<define=s%> as a Getopt::Long option descriptor,
275 which would enable the following command line options:
277 % my_script.pl --define os=linux --define vendor=debian
281 =head2 Custom Type Constraints
283 It is possible to create custom type constraint to option spec
284 mappings if you need them. The process is fairly simple (but a
285 little verbose maybe). First you create a custom subtype, like
288 subtype 'ArrayOfInts'
290 => where { scalar (grep { looks_like_number($_) } @$_) };
292 Then you register the mapping, like so:
294 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
295 'ArrayOfInts' => '=i@'
298 Now any attribute declarations using this type constraint will
299 get the custom option spec. So that, this:
303 isa => 'ArrayOfInts',
304 default => sub { [0] }
307 Will translate to the following on the command line:
309 % my_script.pl --nums 5 --nums 88 --nums 199
311 This example is fairly trivial, but more complex validations are
312 easily possible with a little creativity. The trick is balancing
313 the type constraint validations with the Getopt::Long validations.
315 Better examples are certainly welcome :)
317 =head2 Inferred Type Constraints
319 If you define a custom subtype which is a subtype of one of the
320 standard L</Supported Type Constraints> above, and do not explicitly
321 provide custom support as in L</Custom Type Constraints> above,
322 MooseX::Getopt will treat it like the parent type for Getopt
325 For example, if you had the same custom C<ArrayOfInts> subtype
326 from the examples above, but did not add a new custom option
327 type for it to the C<OptionTypeMap>, it would be treated just
328 like a normal C<ArrayRef> type for Getopt purposes (that is,
335 =item B<new_with_options (%params)>
337 This method will take a set of default C<%params> and then collect
338 params from the command line (possibly overriding those in C<%params>)
339 and then return a newly constructed object.
341 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
342 C<new_with_options> will throw an exception.
346 This accessor contains a reference to a copy of the C<@ARGV> array
347 as it originally existed at the time of C<new_with_options>.
351 This accessor contains an arrayref of leftover C<@ARGV> elements that
352 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
357 This returns the role meta object.
363 All complex software has bugs lurking in it, and this module is no
364 exception. If you find a bug please either email me, or add the bug
369 Stevan Little E<lt>stevan@iinteractive.comE<gt>
371 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
373 =head1 COPYRIGHT AND LICENSE
375 Copyright 2007 by Infinity Interactive, Inc.
377 L<http://www.iinteractive.com>
379 This library is free software; you can redistribute it and/or modify
380 it under the same terms as Perl itself.