X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FGetopt%2FBasic.pm;fp=lib%2FMooseX%2FGetopt%2FBasic.pm;h=98fb72f80e50c4c25c49bca7c181502a01a85655;hb=3d232413c803b36aef16a22eada0726d376514d5;hp=316b15cb1adb5641c64214601758ad4a72365546;hpb=c56feb6de4e53f87dce0cb79d81e21e15a350870;p=gitmo%2FMooseX-Getopt.git diff --git a/lib/MooseX/Getopt/Basic.pm b/lib/MooseX/Getopt/Basic.pm index 316b15c..98fb72f 100644 --- a/lib/MooseX/Getopt/Basic.pm +++ b/lib/MooseX/Getopt/Basic.pm @@ -12,16 +12,27 @@ use Getopt::Long (); has ARGV => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); has extra_argv => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt"); +sub _get_configfile_from_cli { + my $class = shift; + my $configfile; + my $opt_parser = Getopt::Long::Parser->new( config => [ qw( pass_through ) ] ); + $opt_parser->getoptions( "configfile=s" => \$configfile ); + return $configfile; +} + sub new_with_options { my ($class, @params) = @_; - my $config_from_file; + my $constructor_params = ( @params == 1 ? $params[0] : {@params} ); + + my ($config_from_file, $configfile); if($class->meta->does_role('MooseX::ConfigFromFile')) { local @ARGV = @ARGV; - my $configfile; - my $opt_parser = Getopt::Long::Parser->new( config => [ qw( pass_through ) ] ); - $opt_parser->getoptions( "configfile=s" => \$configfile ); + $configfile = $class->_get_configfile_from_cli; + if(!defined $configfile) { + $configfile = $constructor_params->{configfile}; + } if(!defined $configfile) { my $cfmeta = $class->meta->find_attribute_by_name('configfile'); @@ -45,8 +56,6 @@ sub new_with_options { } } - 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'; @@ -57,7 +66,7 @@ sub new_with_options { params => $constructor_params, ); - my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params}; + my $params = $config_from_file ? { configfile => $configfile, %$config_from_file, %{$processed{params}} } : $processed{params}; # did the user request usage information? if ( $processed{usage} && ($params->{'?'} or $params->{help} or $params->{usage}) )