From: Karen Etheridge Date: Fri, 11 May 2012 17:41:03 +0000 (-0700) Subject: respect new_with_options(configfile => "foo") X-Git-Tag: v0.44~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0a386894d3dec0e948fc40d9f0019a4d2752edf6;p=gitmo%2FMooseX-Getopt.git respect new_with_options(configfile => "foo") --- diff --git a/Changes b/Changes index 10855f6..e93be2e 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for Perl extension MooseX-Getopt {{$NEXT}} + - now respecting value of configfile passed to the constructor, when used in + conjunction with MooseX::ConfigFromFile. (Karen Etheridge) 0.43 2012-04-29 09:35:52 PDT-0700 - re-release as non-trial. diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index 6b4d40e..808ff18 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -17,18 +17,28 @@ has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); sub process_argv { my ($class, @params) = @_; + my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); + my $config_from_file; if($class->meta->does_role('MooseX::ConfigFromFile')) { local @ARGV = @ARGV; - # just get the configfile arg now; the rest of the args will be - # fetched later + # just get the configfile arg now out of @ARGV; the rest of the args + # will be fetched later my $configfile; my $opt_parser = Getopt::Long::Parser->new( config => [ qw( no_auto_help pass_through no_auto_version ) ] ); $opt_parser->getoptions( "configfile=s" => \$configfile ); + my $cfmeta = $class->meta->find_attribute_by_name('configfile'); + + # was it passed to the constructor? + if (!defined $configfile) + { + my $key = $cfmeta->init_arg; + $configfile = $constructor_params->{$key} if $key; + } + if(!defined $configfile) { - my $cfmeta = $class->meta->find_attribute_by_name('configfile'); $configfile = $cfmeta->default if $cfmeta->has_default; if (ref $configfile eq 'CODE') { # not sure theres a lot you can do with the class and may break some assumptions @@ -49,8 +59,6 @@ sub process_argv { } } - my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); - Carp::croak("Single parameters to new_with_options() must be a HASH ref") unless ref($constructor_params) eq 'HASH'; diff --git a/t/112_configfile_constructor_arg.t b/t/112_configfile_constructor_arg.t index 7b55a83..746e62a 100644 --- a/t/112_configfile_constructor_arg.t +++ b/t/112_configfile_constructor_arg.t @@ -19,10 +19,7 @@ use Test::More tests => 2; ); } -TODO: { - local $TODO = "doh, this doesn't work!"; - my $configfile = 't/112_configfile_constructor_arg.yml'; my $obj = Foo->new_with_options(configfile => $configfile);