2 package MooseX::Getopt;
7 use MooseX::Getopt::OptionTypeMap;
8 use MooseX::Getopt::Meta::Attribute;
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 sub new_with_options {
14 my ($class, %params) = @_;
16 my (@options, %name_to_init_arg);
17 foreach my $attr ($class->meta->compute_all_applicable_attributes) {
18 my $name = $attr->name;
20 if ($attr->isa('MooseX::Getopt::Meta::Attribute') && $attr->has_cmd_flag) {
21 $name = $attr->cmd_flag;
24 $name_to_init_arg{$name} = $attr->init_arg;
26 if ($attr->has_type_constraint) {
27 my $type_name = $attr->type_constraint->name;
28 if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {
29 $name .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name);
33 push @options => $name;
38 GetOptions(\%options, @options);
41 #warn Dumper \@options;
42 #warn Dumper \%name_to_init_arg;
43 #warn Dumper \%options;
48 $name_to_init_arg{$_} => $options{$_}
61 MooseX::Getopt - A Moose role for processing command line options
69 with 'MooseX::Getopt';
71 has 'out' => (is => 'rw', isa => 'Str', required => 1);
72 has 'in' => (is => 'rw', isa => 'Str', required => 1);
74 # ... rest of the class here
81 my $app = My::App->new_with_options();
82 # ... rest of the script here
84 ## on the command line
85 % perl my_app_script.pl -in file.input -out file.dump
89 This is a role which provides an alternate constructor for creating
90 objects using parameters passed in from the command line.
92 This module attempts to DWIM as much as possible with the command line
93 params by introspecting your class's attributes. It will use the name
94 of your attribute as the command line option, and if there is a type
95 constraint defined, it will configure Getopt::Long to handle the option
98 =head2 Supported Type Constraints
104 A I<Bool> type constraint is set up as a boolean option with
105 Getopt::Long. So that this attribute description:
107 has 'verbose' => (is => 'rw', isa => 'Bool');
109 would translate into C<verbose!> as a Getopt::Long option descriptor,
110 which would enable the following command line options:
112 % my_script.pl --verbose
113 % my_script.pl --noverbose
115 =item I<Int>, I<Float>, I<Str>
117 These type constraints are set up as properly typed options with
118 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
122 An I<ArrayRef> type constraint is set up as a multiple value option
123 in Getopt::Long. So that this attribute description:
128 default => sub { [] }
131 would translate into C<includes=s@> as a Getopt::Long option descriptor,
132 which would enable the following command line options:
134 % my_script.pl --include /usr/lib --include /usr/local/lib
138 A I<HashRef> type constraint is set up as a hash value option
139 in Getopt::Long. So that this attribute description:
144 default => sub { {} }
147 would translate into C<define=s%> as a Getopt::Long option descriptor,
148 which would enable the following command line options:
150 % my_script.pl --define os=linux --define vendor=debian
154 =head2 Custom Type Constraints
156 It is possible to create custom type constraint to option spec
157 mappings if you need them. The process is fairly simple (but a
158 little verbose maybe). First you create a custom subtype, like
161 subtype 'ArrayOfInts'
163 => where { scalar (grep { looks_like_number($_) } @$_) };
165 Then you register the mapping, like so:
167 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
168 'ArrayOfInts' => '=i@'
171 Now any attribute declarations using this type constraint will
172 get the custom option spec. So that, this:
176 isa => 'ArrayOfInts',
177 default => sub { [0] }
180 Will translate to the following on the command line:
182 % my_script.pl --nums 5 --nums 88 --nums 199
184 This example is fairly trivial, but more complex validations are
185 easily possible with a little creativity. The trick is balancing
186 the type constraint validations with the Getopt::Long validations.
188 Better examples are certainly welcome :)
194 =item B<new_with_options (%params)>
196 This method will take a set of default C<%params> and then collect
197 params from the command line (possibly overriding those in C<%params>)
198 and then return a newly constructed object.
202 This returns the role meta object.
208 All complex software has bugs lurking in it, and this module is no
209 exception. If you find a bug please either email me, or add the bug
214 Stevan Little E<lt>stevan@iinteractive.comE<gt>
216 =head1 COPYRIGHT AND LICENSE
218 Copyright 2007 by Infinity Interactive, Inc.
220 L<http://www.iinteractive.com>
222 This library is free software; you can redistribute it and/or modify
223 it under the same terms as Perl itself.