From: Yuval Kogman Date: Wed, 1 Aug 2007 01:06:55 +0000 (+0000) Subject: foo X-Git-Tag: 0_06~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Getopt.git;a=commitdiff_plain;h=bff3807bb402a84be10c48d2e4d1be0628fde911 foo --- diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index 1333e7f..cdd78e7 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -14,18 +14,18 @@ has ARGV => (is => 'rw', isa => 'ArrayRef'); has extra_argv => (is => 'rw', isa => 'ArrayRef'); sub new_with_options { - my ($class, %params) = @_; + my ($class, @params) = @_; my %processed = $class->_parse_argv( options => [ - $class->_attrs_to_options( %params ) + $class->_attrs_to_options( @params ) ] ); $class->new( ARGV => $processed{argv_copy}, extra_argv => $processed{argv}, - %params, # explicit params to ->new + @params, # explicit params to ->new %{ $processed{params} }, # params from CLI ); } @@ -64,12 +64,24 @@ sub _parse_argv { ); } +sub _compute_getopt_attrs { + my $class = shift; + + grep { + $_->isa("MooseX::Getopt::Meta::Attribute") + or + $_->name !~ /^_/ + && + !$_->isa('MooseX::Getopt::Meta::NoGetopt') + } $class->meta->compute_all_applicable_attributes +} + sub _attrs_to_options { my $class = shift; my @options; - foreach my $attr ($class->meta->compute_all_applicable_attributes) { + foreach my $attr ($class->_compute_getopt_attrs) { my $name = $attr->name; my $aliases; @@ -78,10 +90,6 @@ sub _attrs_to_options { $name = $attr->cmd_flag if $attr->has_cmd_flag; $aliases = $attr->cmd_aliases if $attr->has_cmd_aliases; } - else { - next if $name =~ /^_/; - next if $attr->isa('MooseX::Getopt::Meta::NoGetopt'); - } my $opt_string = $aliases ? join(q{|}, $name, @$aliases) diff --git a/lib/MooseX/Getopt/Strict.pm b/lib/MooseX/Getopt/Strict.pm new file mode 100644 index 0000000..d5cb38a --- /dev/null +++ b/lib/MooseX/Getopt/Strict.pm @@ -0,0 +1,40 @@ +#!/usr/bin/perl + +package MooseX::Getopt::Strict; +use Moose::Role; + +with qw/MooseX::Getopt/; + +sub _compute_getopt_attrs { + my ( $class, @args ) = @_; + + grep { $_->isa("MooseX::Getopt::Meta::Attribute") } $class->MooseX::Getopt::_compute_getopt_attrs(@args); +} + +__PACKAGE__; + +__END__ + +=pod + +=head1 NAME + +MooseX::Getopt::Strict - only make options for attrs with the Getopt metaclass + +=head1 SYNOPSIS + + # see MooseX::Getopt + +=over 4 + +=item meta + +Is a section devoted to making the #!#%^ stupid pod coverage test pass. Stevan, I do +hope you're actually reading this. + +Love, +Yuval + +=back + +=cut diff --git a/t/004_nogetop.t b/t/004_nogetop.t index 03b09db..b0fa16d 100644 --- a/t/004_nogetop.t +++ b/t/004_nogetop.t @@ -3,7 +3,8 @@ use strict; use warnings; -use Test::More tests => 8; +use Test::More tests => 9; +use Test::Exception; BEGIN { use_ok('MooseX::Getopt'); @@ -94,3 +95,9 @@ BEGIN { is_deeply( $app->details, {}, '... details is {} as expected' ); is($app->private_stuff, 713, '... private stuff is 713 as expected'); } + +{ + local @ARGV = (qw/--private_stuff 317/); + + throws_ok { App->new_with_options } qr/Unknown option: private_stuff/; +} diff --git a/t/005_strict.t b/t/005_strict.t new file mode 100644 index 0000000..096ace9 --- /dev/null +++ b/t/005_strict.t @@ -0,0 +1,101 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Exception; + +BEGIN { + use_ok('MooseX::Getopt'); +} + +{ + + package App; + use Moose; + + with 'MooseX::Getopt::Strict'; + + has 'data' => ( + metaclass => 'MooseX::Getopt::Meta::Attribute', + is => 'ro', + isa => 'Str', + default => 'file.dat', + cmd_flag => 'f', + ); + + has 'cow' => ( + metaclass => 'Getopt', + is => 'ro', + isa => 'Str', + default => 'moo', + cmd_aliases => [qw/ moocow m c /], + ); + + has 'horse' => ( + metaclass => 'MooseX::Getopt::Meta::Attribute', + is => 'ro', + isa => 'Str', + default => 'bray', + cmd_flag => 'horsey', + cmd_aliases => 'x', + ); + + has 'length' => ( + is => 'ro', + isa => 'Int', + default => 24 + ); + + has 'verbose' => ( + is => 'ro', + isa => 'Bool', + ); + + has 'libs' => ( + is => 'ro', + isa => 'ArrayRef', + default => sub { [] }, + ); + + has 'details' => ( + is => 'ro', + isa => 'HashRef', + default => sub { {} }, + ); + + has 'private_stuff' => ( + is => 'ro', + isa => 'Int', + default => 713 + ); + + has '_private_stuff_cmdline' => ( + is => 'ro', + isa => 'Int', + default => 832, + cmd_flag => 'p', + ); + +} + +{ + local @ARGV = (); + + my $app = App->new_with_options; + isa_ok( $app, 'App' ); + + ok( !$app->verbose, '... verbosity is off as expected' ); + is( $app->length, 24, '... length is 24 as expected' ); + is( $app->data, 'file.dat', '... data is file.dat as expected' ); + is_deeply( $app->libs, [], '... libs is [] as expected' ); + is_deeply( $app->details, {}, '... details is {} as expected' ); + is($app->private_stuff, 713, '... private stuff is 713 as expected'); +} + +{ + local @ARGV = (qw/--private_stuff 317/); + + throws_ok { App->new_with_options } qr/Unknown option: private_stuff/; +}