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;
21 if ($attr->isa('MooseX::Getopt::Meta::Attribute')) {
22 $name = $attr->cmd_flag if $attr->has_cmd_flag;
23 $aliases = $attr->cmd_aliases if $attr->has_cmd_aliases;
26 $name_to_init_arg{$name} = $attr->init_arg;
28 my $opt_string = $aliases
29 ? join(q{|}, $name, @$aliases)
32 if ($attr->has_type_constraint) {
33 my $type_name = $attr->type_constraint->name;
34 if (MooseX::Getopt::OptionTypeMap->has_option_type($type_name)) {
35 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type_name);
39 push @options => $opt_string;
44 GetOptions(\%options, @options);
47 #warn Dumper \@options;
48 #warn Dumper \%name_to_init_arg;
49 #warn Dumper \%options;
54 $name_to_init_arg{$_} => $options{$_}
67 MooseX::Getopt - A Moose role for processing command line options
75 with 'MooseX::Getopt';
77 has 'out' => (is => 'rw', isa => 'Str', required => 1);
78 has 'in' => (is => 'rw', isa => 'Str', required => 1);
80 # ... rest of the class here
87 my $app = My::App->new_with_options();
88 # ... rest of the script here
90 ## on the command line
91 % perl my_app_script.pl -in file.input -out file.dump
95 This is a role which provides an alternate constructor for creating
96 objects using parameters passed in from the command line.
98 This module attempts to DWIM as much as possible with the command line
99 params by introspecting your class's attributes. It will use the name
100 of your attribute as the command line option, and if there is a type
101 constraint defined, it will configure Getopt::Long to handle the option
104 =head2 Supported Type Constraints
110 A I<Bool> type constraint is set up as a boolean option with
111 Getopt::Long. So that this attribute description:
113 has 'verbose' => (is => 'rw', isa => 'Bool');
115 would translate into C<verbose!> as a Getopt::Long option descriptor,
116 which would enable the following command line options:
118 % my_script.pl --verbose
119 % my_script.pl --noverbose
121 =item I<Int>, I<Float>, I<Str>
123 These type constraints are set up as properly typed options with
124 Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
128 An I<ArrayRef> type constraint is set up as a multiple value option
129 in Getopt::Long. So that this attribute description:
134 default => sub { [] }
137 would translate into C<includes=s@> as a Getopt::Long option descriptor,
138 which would enable the following command line options:
140 % my_script.pl --include /usr/lib --include /usr/local/lib
144 A I<HashRef> type constraint is set up as a hash value option
145 in Getopt::Long. So that this attribute description:
150 default => sub { {} }
153 would translate into C<define=s%> as a Getopt::Long option descriptor,
154 which would enable the following command line options:
156 % my_script.pl --define os=linux --define vendor=debian
160 =head2 Custom Type Constraints
162 It is possible to create custom type constraint to option spec
163 mappings if you need them. The process is fairly simple (but a
164 little verbose maybe). First you create a custom subtype, like
167 subtype 'ArrayOfInts'
169 => where { scalar (grep { looks_like_number($_) } @$_) };
171 Then you register the mapping, like so:
173 MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
174 'ArrayOfInts' => '=i@'
177 Now any attribute declarations using this type constraint will
178 get the custom option spec. So that, this:
182 isa => 'ArrayOfInts',
183 default => sub { [0] }
186 Will translate to the following on the command line:
188 % my_script.pl --nums 5 --nums 88 --nums 199
190 This example is fairly trivial, but more complex validations are
191 easily possible with a little creativity. The trick is balancing
192 the type constraint validations with the Getopt::Long validations.
194 Better examples are certainly welcome :)
200 =item B<new_with_options (%params)>
202 This method will take a set of default C<%params> and then collect
203 params from the command line (possibly overriding those in C<%params>)
204 and then return a newly constructed object.
208 This returns the role meta object.
214 All complex software has bugs lurking in it, and this module is no
215 exception. If you find a bug please either email me, or add the bug
220 Stevan Little E<lt>stevan@iinteractive.comE<gt>
222 =head1 COPYRIGHT AND LICENSE
224 Copyright 2007 by Infinity Interactive, Inc.
226 L<http://www.iinteractive.com>
228 This library is free software; you can redistribute it and/or modify
229 it under the same terms as Perl itself.