From: Piotr Roszatycki Date: Tue, 11 Nov 2008 01:40:27 +0000 (+0000) Subject: * cmd_flag parameter now works correctly. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5b582f22bdea267ebe6bd70c6cb664fdac30d20e;p=gitmo%2FMooseX-Getopt.git * cmd_flag parameter now works correctly. * MooseX::Getopt::Strict now works correctly. --- diff --git a/lib/MooseX/Getopt/Parser/Long.pm b/lib/MooseX/Getopt/Parser/Long.pm index a6a2800..70ad5a1 100644 --- a/lib/MooseX/Getopt/Parser/Long.pm +++ b/lib/MooseX/Getopt/Parser/Long.pm @@ -27,28 +27,16 @@ sub build_options { unless $getopt->isa('MooseX::Getopt::Session'); my %options; - my @opts; foreach my $attr (@attrs) { my $name = $attr->name; - my $is_cmd = $attr->does('MooseX::Getopt::Meta::Attribute::Trait'); - - my $opt_string = $is_cmd && $attr->has_cmd_flag - ? $attr->cmd_flag - : $name; - - if ($is_cmd && $attr->has_cmd_aliases && scalar @{ $attr->cmd_aliases }) { - $opt_string .= '|' . join '|', @{ $attr->cmd_aliases }; - }; + my ($flag, @aliases) = $getopt->_get_cmd_flags_for_attr($attr); + my $type = $getopt->_get_cmd_type_for_attr($attr); - if ($is_cmd && $attr->has_cmd_type || $attr->has_type_constraint) { - my $type = $is_cmd && $attr->has_cmd_type ? $attr->cmd_type : $attr->type_constraint; - if (MooseX::Getopt::OptionTypeMap->has_option_type($type)) { - $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) - }; - }; + my $opt_string = join '|', $flag, @aliases; + $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type); $options{$name} = undef; push @opts, $opt_string => \$options{$name}; diff --git a/lib/MooseX/Getopt/Session.pm b/lib/MooseX/Getopt/Session.pm index c75c34e..57b5220 100644 --- a/lib/MooseX/Getopt/Session.pm +++ b/lib/MooseX/Getopt/Session.pm @@ -100,4 +100,35 @@ sub _compute_getopt_classes { }; +sub _get_cmd_flags_for_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; + @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; + }; + + return ($flag, @aliases); +}; + + +sub _get_cmd_type_for_attr { + my ($self, $attr) = @_; + + my $type; + + $type = $attr->type_constraint if $attr->has_type_constraint; + + if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) { + $type = $attr->cmd_type if $attr->has_cmd_type; + }; + + return $type; +}; + + 1; diff --git a/lib/MooseX/Getopt/Strict.pm b/lib/MooseX/Getopt/Strict.pm index f845d69..3c9ef39 100644 --- a/lib/MooseX/Getopt/Strict.pm +++ b/lib/MooseX/Getopt/Strict.pm @@ -6,9 +6,10 @@ with 'MooseX::Getopt'; around '_compute_getopt_attrs' => sub { my $next = shift; - my ( $class, @args ) = @_; - grep { - $_->isa("MooseX::Getopt::Meta::Attribute") + my ($class, @args) = @_; + + return grep { + $_->does('MooseX::Getopt::Meta::Attribute::Trait') } $class->$next(@args); };