Changeset 27994 for lang/perl/MouseX-Getopt/trunk
- Timestamp:
- 01/06/09 13:49:59 (4 years ago)
- Location:
- lang/perl/MouseX-Getopt/trunk
- Files:
-
- 2 added
- 3 modified
-
. (modified) (1 prop)
-
MANIFEST (modified) (1 diff)
-
lib/MouseX/Getopt (added)
-
lib/MouseX/Getopt.pm (modified) (4 diffs)
-
lib/MouseX/Getopt/OptionTypeMap.pm (added)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/MouseX-Getopt/trunk
-
Property
svn:ignore set
to
Makefile
Makefile.old
blib
inc
pm_to_blib
META.yml
MANIFEST.bak
-
Property
svn:ignore set
to
-
lang/perl/MouseX-Getopt/trunk/MANIFEST
r27960 r27994 59 59 inc/Test/More.pm 60 60 lib/MouseX/Getopt.pm 61 lib/MouseX/Getopt/OptionTypeMap.pm 61 62 Makefile.PL 62 63 MANIFEST This list of files -
lang/perl/MouseX-Getopt/trunk/lib/MouseX/Getopt.pm
r27960 r27994 3 3 use 5.8.1; 4 4 use Mouse::Role; 5 use MouseX::Getopt::OptionTypeMap; 5 6 use Getopt::Long (); 6 7 … … 10 11 my ($class, %params) = @_; 11 12 13 my %processed = $class->_parse_argv( 14 specs => $class->_attrs_to_specs, 15 params => \%params, 16 ); 17 12 18 return $class->new( 13 %params, # explicit params to new14 $class->_parse_argv(%params), #"params from CLI19 %params, # explicit params to new 20 %processed, # params from CLI 15 21 ); 16 22 } … … 19 25 my ($class, %params) = @_; 20 26 21 my $options = $class->_attrs_to_options;22 my %init_arg = map { $_->{name} => $_->{init_arg} } @$options;27 local @ARGV = @{ $params{argv} || \@ARGV }; 28 my $specs = $params{specs}; 23 29 24 local @ARGV = @{ $params{argv} || \@ARGV }; 25 26 my @err; 27 my $parsed_options = eval { 28 local $SIG{__WARN__} = sub { push @err, @_ }; 29 Getopt::Long::GetOptions(\my %options, map { $_->{spec} } @$options); 30 my @warn; 31 my $options = eval { 32 local $SIG{__WARN__} = sub { push @warn, @_ }; 33 Getopt::Long::GetOptions(\my %options, map { $_->{spec} } values %$specs); 30 34 \%options; 31 35 }; 32 if (@ error $@) {33 die join '', grep { defined } @ err, $@;36 if (@warn or $@) { 37 die join '', grep { defined } @warn, $@; 34 38 } 35 39 36 my %args = map { $ init_arg{$_} => $parsed_options->{$_} } keys %$parsed_options;40 my %args = map { $specs->{$_}->{name} => $options->{$_} } keys %$options; 37 41 return %args; 38 42 } 39 43 40 sub _attrs_to_ options {44 sub _attrs_to_specs { 41 45 my $class = shift; 42 46 43 my @options;47 my $specs = {}; 44 48 for my $attr ($class->meta->compute_all_applicable_attributes) { 45 49 my $name = $attr->name; … … 49 53 if ($attr->has_type_constraint) { 50 54 my $type = $attr->type_constraint; 51 if ( $class->_has_option_type($type)) {52 $spec .= $class->_get_option_type($type);55 if (MouseX::Getopt::OptionTypeMap->has_option_type($type)) { 56 $spec .= MouseX::Getopt::OptionTypeMap->get_option_type($type); 53 57 } 54 58 } 55 59 56 60 $name =~ s/\W/_/g; 57 push @options, { 58 name => $name, 59 spec => $spec, 60 init_arg => $attr->init_arg, 61 }; 61 $specs->{$name} = { spec => $spec, name => $attr->init_arg }; 62 62 } 63 63 64 \@options; 65 } 66 67 { 68 my %typemap = ( 69 'Bool' => '!', 70 'Str' => '=s', 71 'Int' => '=i', 72 'Num' => '=f', 73 'ArrayRef' => '=s@', 74 'HashRef' => '=s%', 75 ); 76 77 sub _has_option_type { exists $typemap{$_[1]} } 78 sub _get_option_type { $typemap{$_[1]} } 64 $specs; 79 65 } 80 66
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)