2 package MooseX::Getopt;
7 use MooseX::Getopt::OptionTypes;
8 use MooseX::Getopt::Meta::Attribute;
10 sub new_with_options {
11 my ($class, %params) = @_;
13 my (%options, %constructor_options);
14 foreach my $attr ($class->meta->compute_all_applicable_attributes) {
15 my $name = $attr->name;
17 if ($attr->isa('MooseX::Getopt::Meta::Attribute') && $attr->has_cmd_flag) {
18 $name = $attr->cmd_flag;
21 my $init_arg = $attr->init_arg;
23 # create a suitable default value
24 $constructor_options{$init_arg} = '';
26 if ($attr->has_type_constraint) {
27 my $type_name = $attr->type_constraint->name;
28 if (MooseX::Getopt::OptionTypes->has_option_type($type_name)) {
29 $name .= MooseX::Getopt::OptionTypes->get_option_type($type_name);
33 $options{$name} = \($constructor_options{$init_arg});
38 # filter out options which
39 # were not passed at all
40 %constructor_options = map {
41 $constructor_options{$_} ne ''
42 ? ($_ => $constructor_options{$_})
44 } keys %constructor_options;
46 $class->new(%params, %constructor_options);
65 with 'MooseX::Getopt';
67 has 'out' => (is => 'rw', isa => 'Str', required => 1);
68 has 'in' => (is => 'rw', isa => 'Str', required => 1);
70 # ... rest of the class here
77 my $app = My::App->new_with_options();
78 # ... rest of the script here
80 ## on the command line
81 % perl my_app_script.pl -in file.input -out file.dump
89 =item B<new_with_options (%params)>
97 All complex software has bugs lurking in it, and this module is no
98 exception. If you find a bug please either email me, or add the bug
103 Stevan Little E<lt>stevan@iinteractive.comE<gt>
105 =head1 COPYRIGHT AND LICENSE
107 Copyright 2007 by Infinity Interactive, Inc.
109 L<http://www.iinteractive.com>
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself.