From: Piotr Roszatycki Date: Fri, 28 Nov 2008 23:29:03 +0000 (+0000) Subject: * Perltidy. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c5c99e6b124174e5f9e56312058f6b302dd5369c;p=gitmo%2FMooseX-Getopt.git * Perltidy. * MooseX::Getopt::OptionTypeMap: Added new types: Bool|Int, Bool|Float, Bool|Str. --- diff --git a/ChangeLog b/ChangeLog index 488f283..dbcacda 100644 --- a/ChangeLog +++ b/ChangeLog @@ -25,6 +25,9 @@ Revision history for Perl extension MooseX-Getopt MooseX::Getopt::Session->parser->usage rather than passed via new_with_options( usage => $usage ) constructor. (dexter) + * MooseX::Getopt::OptionTypeMap + - Added new types: Bool|Int, Bool|Float, Bool|Str. (dexter) + 0.15 Sat. July 26 2008 * MooseX::Getopt::OptionTypeMap - Accept type constraint objects in the type mapping, not just names diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index 8eda924..ce091a6 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -23,10 +23,10 @@ use constant _default_getopt_session => 'MooseX::Getopt::Session'; has getopt => ( - is => 'rw', - isa => 'MooseX::Getopt::Session', + is => 'rw', + isa => 'MooseX::Getopt::Session', metaclass => 'NoGetopt', - handles => [ 'ARGV', 'extra_argv' ], + handles => [ 'ARGV', 'extra_argv' ], ); @@ -40,24 +40,28 @@ sub new_with_options { sub get_options_from_argv { my $class = shift; - Moose->throw_error("Single parameters to get_options_from_argv() must be a HASH ref") - if ref $_[0] and ref $_ ne 'HASH'; + Moose->throw_error( + "Single parameters to get_options_from_argv() must be a HASH ref" + ) if ref $_[0] and ref $_ ne 'HASH'; - my $options = { %{ $class->get_options_from_configfile }, @_ == 1 ? %{ $_[0] } : @_ }; + my $options = { + %{ $class->get_options_from_configfile }, + @_ == 1 ? %{ $_[0] } : @_ + }; my $getopt = defined $options->{getopt} - ? $options->{getopt} - : $class->_default_getopt_session->new( - classes_filter => sub { $_ eq $class }, - options => $options, - ); + ? $options->{getopt} + : $class->_default_getopt_session->new( + classes_filter => sub { $_ eq $class }, + options => $options, + ); # Call Getopt parser only once. $getopt->build_options if not $getopt->has_status; my $new_options = { - %{ $options }, # explicit options to ->new - %{ $getopt->options }, # options from CLI + %$options, # explicit options to ->new + %{ $getopt->options }, # options from CLI getopt => $getopt, }; @@ -70,19 +74,19 @@ sub get_options_from_configfile { my $options = {}; - if ($class->meta->does_role('MooseX::ConfigFromFile')) { + if ( $class->meta->does_role('MooseX::ConfigFromFile') ) { local @ARGV = @ARGV; my $configfile; - my $opt_parser = Getopt::Long::Parser->new( config => [ 'pass_through' ] ); + my $opt_parser = Getopt::Long::Parser->new( config => ['pass_through'] ); $opt_parser->getoptions( "configfile=s" => \$configfile ); - if (not defined $configfile) { + if ( not defined $configfile ) { my $cfmeta = $class->meta->find_attribute_by_name('configfile'); $configfile = $cfmeta->default if $cfmeta->has_default; }; - if (defined $configfile) { + if ( defined $configfile ) { $options = $class->get_config_from_file($configfile); }; }; @@ -96,8 +100,7 @@ sub _compute_getopt_attrs { return grep { $_->does('MooseX::Getopt::Meta::Attribute::Trait') - or - $_->name !~ /^_/ + or $_->name !~ /^_/ } grep { !$_->does('MooseX::Getopt::Meta::Attribute::Trait::NoGetopt') } $class->meta->compute_all_applicable_attributes; @@ -106,6 +109,7 @@ sub _compute_getopt_attrs { no Moose::Role; 1; + __END__ =pod @@ -203,6 +207,11 @@ which would enable the following command line options: These type constraints are set up as properly typed options with Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate. +=item I, I, I + +These type constaints are set up as properly typed options with +Getopt::Long, using the C<:i>, C<:f> and C<:s> modifiers as appropriate. + =item I An I type constraint is set up as a multiple value option @@ -402,12 +411,12 @@ Brandon L. Black, Eblblack@gmail.comE Yuval Kogman, Enothingmuch@woobling.orgE +Piotr Roszatycki, Edexter@cpan.orgE + =head1 CONTRIBUTORS Ryan D Johnson, Eryan@innerfence.comE -Piotr Roszatycki, Edexter@cpan.orgE - =head1 COPYRIGHT AND LICENSE Copyright 2007-2008 by Infinity Interactive, Inc. diff --git a/lib/MooseX/Getopt/OptionTypeMap.pm b/lib/MooseX/Getopt/OptionTypeMap.pm index 63bd3ba..449510c 100644 --- a/lib/MooseX/Getopt/OptionTypeMap.pm +++ b/lib/MooseX/Getopt/OptionTypeMap.pm @@ -7,72 +7,91 @@ use Moose::Util::TypeConstraints 'find_type_constraint'; our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; + my %option_type_map = ( - 'Bool' => '!', - 'Str' => '=s', - 'Int' => '=i', - 'Num' => '=f', - 'ArrayRef' => '=s@', - 'HashRef' => '=s%', + 'Bool' => '!', + 'Str' => '=s', + 'Int' => '=i', + 'Num' => '=f', + 'ArrayRef' => '=s@', + 'HashRef' => '=s%', + 'Bool|Str' => ':s', + 'Bool|Int' => ':i', + 'Bool|Num' => ':f', + 'Str|Bool' => ':s', + 'Int|Bool' => ':i', + 'Num|Bool' => ':f', ); + sub has_option_type { my (undef, $type_or_name) = @_; - return 1 if exists $option_type_map{blessed($type_or_name) ? $type_or_name->name : $type_or_name}; + return 1 if exists $option_type_map{ blessed $type_or_name + ? $type_or_name->name + : $type_or_name }; - my $current = blessed($type_or_name) ? $type_or_name : find_type_constraint($type_or_name); + my $current = blessed $type_or_name + ? $type_or_name + : find_type_constraint($type_or_name); - (defined $current) - || Moose->throw_error("Could not find the type constraint for '$type_or_name'"); + Moose->throw_error("Could not find the type constraint for '$type_or_name'") + unless defined $current; - while (my $parent = $current->parent) { - return 1 if exists $option_type_map{$parent->name}; + while ( my $parent = $current->parent ) { + return 1 if exists $option_type_map{ $parent->name }; $current = $parent; - } + }; + + return ''; +}; - return 0; -} sub get_option_type { my (undef, $type_or_name) = @_; - my $name = blessed($type_or_name) ? $type_or_name->name : $type_or_name; + my $name = blessed $type_or_name ? $type_or_name->name : $type_or_name; return $option_type_map{$name} if exists $option_type_map{$name}; - my $current = ref $type_or_name ? $type_or_name : find_type_constraint($type_or_name); + my $current = ref $type_or_name + ? $type_or_name + : find_type_constraint($type_or_name); - (defined $current) - || Moose->throw_error("Could not find the type constraint for '$type_or_name'"); + Moose->throw_error("Could not find the type constraint for '$type_or_name'") + unless defined $current; while ( $current = $current->parent ) { - return $option_type_map{$current->name} - if exists $option_type_map{$current->name}; - } + return $option_type_map{ $current->name } + if exists $option_type_map{ $current->name }; + }; return; -} +}; + sub add_option_type_to_map { my (undef, $type_name, $option_string) = @_; - (defined $type_name && defined $option_string) - || Moose->throw_error("You must supply both a type name and an option string"); - if ( blessed($type_name) ) { + Moose->throw_error("You must supply both a type name and an option string") + unless defined $type_name && defined $option_string; + + if (blessed $type_name) { $type_name = $type_name->name; - } else { - (find_type_constraint($type_name)) - || Moose->throw_error("The type constraint '$type_name' does not exist"); } + else { + Moose->throw_error("The type constraint '$type_name' does not exist") + unless find_type_constraint($type_name); + }; $option_type_map{$type_name} = $option_string; -} +}; + no Moose; no Moose::Util::TypeConstraints; 1; -__END__ +__END__ =pod diff --git a/lib/MooseX/Getopt/Parser.pm b/lib/MooseX/Getopt/Parser.pm index e066feb..4de40ce 100644 --- a/lib/MooseX/Getopt/Parser.pm +++ b/lib/MooseX/Getopt/Parser.pm @@ -4,8 +4,10 @@ package MooseX::Getopt::Parser; use Moose::Role; requires 'build_options'; + 1; + __END__ =pod diff --git a/lib/MooseX/Getopt/Parser/Default.pm b/lib/MooseX/Getopt/Parser/Default.pm index 8c460c4..f8342fc 100644 --- a/lib/MooseX/Getopt/Parser/Default.pm +++ b/lib/MooseX/Getopt/Parser/Default.pm @@ -10,13 +10,14 @@ use maybe 'MooseX::Getopt::Parser::Descriptive'; sub new { my $class = shift; return maybe::HAVE_MOOSEX_GETOPT_PARSER_DESCRIPTIVE - ? MooseX::Getopt::Parser::Descriptive->new(@_) - : MooseX::Getopt::Parser::Long->new(@_); + ? MooseX::Getopt::Parser::Descriptive->new(@_) + : MooseX::Getopt::Parser::Long->new(@_); }; 1; + __END__ =pod diff --git a/lib/MooseX/Getopt/Parser/Descriptive.pm b/lib/MooseX/Getopt/Parser/Descriptive.pm index f90e8e3..cf2a8f3 100644 --- a/lib/MooseX/Getopt/Parser/Descriptive.pm +++ b/lib/MooseX/Getopt/Parser/Descriptive.pm @@ -12,49 +12,50 @@ use Getopt::Long::Descriptive (); # Special configuration for parser has config => ( - is => 'rw', - isa => 'ArrayRef[Str]', - default => sub { [ 'default' ] }, + is => 'rw', + isa => 'ArrayRef[Str]', + default => sub { ['default'] }, ); # Format for usage description has format => ( - is => 'rw', - isa => 'Str', + is => 'rw', + isa => 'Str', default => 'usage: %c %o', ); # Usage object has usage => ( - is => 'rw', - isa => 'Maybe[Getopt::Long::Descriptive::Usage]', + is => 'rw', + isa => 'Maybe[Getopt::Long::Descriptive::Usage]', predicate => 'has_usage', ); sub build_options { my $self = shift; - my ($getopt, @attrs) = @_; + my ( $getopt, @attrs ) = @_; - Moose->throw_error('First argument is not a MooseX::Getopt::Session') + Moose->throw_error("First argument is not a MooseX::Getopt::Session") unless $getopt->isa('MooseX::Getopt::Session'); - my $options = $getopt->options; + my $options = $getopt->options; my $new_options = {}; my $usage; - my (@opts, %cmd_flags_to_names); + my ( @opts, %cmd_flags_to_names ); foreach my $attr (@attrs) { my $name = $attr->name; - my ($flag, @aliases) = $getopt->_get_cmd_flags_for_attr($attr); + my ( $flag, @aliases ) = $getopt->_get_cmd_flags_for_attr($attr); my $type = $getopt->_get_cmd_type_for_attr($attr); my $opt_string = join '|', $flag, @aliases; - $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) if $type; + $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) + if $type; - # opt_string is unmangled; parsed options key is mangled + # opt_string is unmangled; parsed options keys are mangled $flag =~ tr/-/_/; $cmd_flags_to_names{$flag} = $name; @@ -85,22 +86,27 @@ sub build_options { }; eval { - ($new_options, $usage) = Getopt::Long::Descriptive::describe_options( - $self->format, @opts, { getopt_conf => $self->config } - ); + ( $new_options, $usage ) = + Getopt::Long::Descriptive::describe_options( + $self->format, + @opts, + { getopt_conf => $self->config } + ); }; - my $e = $@; - $warnings .= $e if $e; + $warnings .= $@ if $@; my $extra_argv = \@ARGV; - $getopt->extra_argv( $extra_argv ); + $getopt->extra_argv($extra_argv); }; # Store usage object $self->usage( $usage ); # Convert cmd_flags back to names in options hashref - $new_options = { map { $cmd_flags_to_names{$_} => $new_options->{$_} } keys %$new_options }; + $new_options = { + map { $cmd_flags_to_names{$_} => $new_options->{$_} } + keys %$new_options + }; # Include old options $new_options = { %$options, %$new_options }; @@ -116,6 +122,7 @@ sub build_options { 1; + __END__ =pod diff --git a/lib/MooseX/Getopt/Parser/Long.pm b/lib/MooseX/Getopt/Parser/Long.pm index 70e5f34..f2b8471 100644 --- a/lib/MooseX/Getopt/Parser/Long.pm +++ b/lib/MooseX/Getopt/Parser/Long.pm @@ -12,21 +12,21 @@ use Getopt::Long (); # Special configuration for parser has config => ( - is => 'rw', - isa => 'ArrayRef[Str]', + is => 'rw', + isa => 'ArrayRef[Str]', auto_deref => 1, - default => sub { [ 'default' ] }, + default => sub { ['default'] }, ); sub build_options { my $self = shift; - my ($getopt, @attrs) = @_; + my ( $getopt, @attrs ) = @_; - Moose->throw_error('First argument is not a MooseX::Getopt::Session') + Moose->throw_error("First argument is not a MooseX::Getopt::Session") unless $getopt->isa('MooseX::Getopt::Session'); - my $options = $getopt->options; + my $options = $getopt->options; my $new_options = { %$options }; my @opts; @@ -34,11 +34,12 @@ sub build_options { foreach my $attr (@attrs) { my $name = $attr->name; - my ($flag, @aliases) = $getopt->_get_cmd_flags_for_attr($attr); + my ( $flag, @aliases ) = $getopt->_get_cmd_flags_for_attr($attr); my $type = $getopt->_get_cmd_type_for_attr($attr); my $opt_string = join '|', $flag, @aliases; - $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) if $type; + $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) + if $type; $new_options->{$name} = undef; push @opts, $opt_string => \$new_options->{$name}; @@ -63,7 +64,10 @@ sub build_options { }; # Filter not defined values in new_options hashref - $new_options = { map { $_ => $new_options->{$_} } grep { defined $new_options->{$_} } keys %$new_options }; + $new_options = { + map { $_ => $new_options->{$_} } + grep { defined $new_options->{$_} } keys %$new_options + }; $getopt->status( ! $warnings ); $getopt->options( $new_options ); @@ -76,6 +80,7 @@ sub build_options { 1; + __END__ =pod diff --git a/lib/MooseX/Getopt/Session.pm b/lib/MooseX/Getopt/Session.pm index dae6c01..e0bfff0 100644 --- a/lib/MooseX/Getopt/Session.pm +++ b/lib/MooseX/Getopt/Session.pm @@ -11,43 +11,43 @@ use constant _default_getopt_parser => 'MooseX::Getopt::Parser::Default'; # Pluggined MooseX::Getopt::Parser parser has parser => ( - is => 'rw', - does => 'MooseX::Getopt::Parser', + is => 'rw', + does => 'MooseX::Getopt::Parser', default => sub { $_[0]->_default_getopt_parser->new }, ); # Filter for classes which are searched for getopt trait has classes_filter => ( - is => 'rw', - isa => 'CodeRef', - default => sub { sub { 1 } }, + is => 'rw', + isa => 'CodeRef', + default => sub { sub { 1; } }, ); # Original @ARGV values has ARGV => ( - is => 'rw', - isa => 'ArrayRef[Str]', - default => sub { [ @ARGV ] }, + is => 'rw', + isa => 'ArrayRef[Str]', + default => sub { [@ARGV] }, ); # Unrecognized @ARGV values has extra_argv => ( - is => 'rw', - isa => 'ArrayRef[Str]', + is => 'rw', + isa => 'ArrayRef[Str]', default => sub { [] }, ); # Hash with options parsed from argv has options => ( - is => 'rw', - isa => 'HashRef', + is => 'rw', + isa => 'HashRef', default => sub { {} }, ); # Status returned by Getopt parser has status => ( - is => 'rw', - isa => 'Bool', + is => 'rw', + isa => 'Bool', predicate => 'has_status', ); @@ -58,7 +58,7 @@ sub build_options { my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes; return $self->parser->build_options( $self, @attrs ); -} +}; sub _compute_getopt_classes { @@ -73,29 +73,29 @@ sub _compute_getopt_classes { sub _get_cmd_flags_for_attr { - my ($self, $attr) = @_; + my ( $self, $attr ) = @_; my $flag = $attr->name; my @aliases; - if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) { - $flag = $attr->cmd_flag if $attr->has_cmd_flag; + if ( $attr->does('MooseX::Getopt::Meta::Attribute::Trait') ) { + $flag = $attr->cmd_flag if $attr->has_cmd_flag; @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; }; - return ($flag, @aliases); + return ( $flag, @aliases ); }; sub _get_cmd_type_for_attr { - my ($self, $attr) = @_; + my ( $self, $attr ) = @_; my $type; $type = $attr->type_constraint if $attr->has_type_constraint; - if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) { + if ( $attr->does('MooseX::Getopt::Meta::Attribute::Trait') ) { $type = $attr->cmd_type if $attr->has_cmd_type; }; @@ -105,6 +105,7 @@ sub _get_cmd_type_for_attr { 1; + __END__ =pod @@ -216,12 +217,12 @@ Brandon L. Black, Eblblack@gmail.comE Yuval Kogman, Enothingmuch@woobling.orgE +Piotr Roszatycki, Edexter@cpan.orgE + =head1 CONTRIBUTORS Ryan D Johnson, Eryan@innerfence.comE -Piotr Roszatycki, Edexter@cpan.orgE - =head1 COPYRIGHT AND LICENSE Copyright 2007-2008 by Infinity Interactive, Inc. diff --git a/lib/MooseX/Getopt/Strict.pm b/lib/MooseX/Getopt/Strict.pm index 266ebf4..bb5e096 100644 --- a/lib/MooseX/Getopt/Strict.pm +++ b/lib/MooseX/Getopt/Strict.pm @@ -13,8 +13,10 @@ around '_compute_getopt_attrs' => sub { } $class->$next(@args); }; + 1; + __END__ =pod