X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FGetopt.pm;fp=lib%2FMooseX%2FGetopt.pm;h=30eb306fbb3d3f1e162bead02158089aa95e6769;hb=37eb9f8a099afb890e2ad2d8703f23187850e9e0;hp=49ac04895c93b81f508c10c69a273812e9bbe495;hpb=96df119bc747eb5823ba0490cf7d71d2dc457f6a;p=gitmo%2FMooseX-Getopt.git diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index 49ac048..30eb306 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -44,35 +44,40 @@ sub get_options_from_argv { "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 $getopt = defined $options->{getopt} - ? $options->{getopt} - : $class->_default_getopt_session->new( - classes_filter => sub { $_ eq $class }, - options => $options, - ); + my %args = @_ == 1 ? %{ $_[0] } : @_; + + my $getopt = defined $args{getopt} + ? $args{getopt} + : $class->_default_getopt_session->new( + classes_filter => sub { $_ eq $class }, + ); + + # Combine explicit options and resend to session object + my %options = ( + %{ $getopt->options }, # options from getopt session + $class->get_options_from_configfile, # options from --configfile + %args, # options from @_ + ); + $getopt->options( { %options } ); # Call Getopt parser only once. $getopt->build_options if not $getopt->has_status; - my $new_options = { - %$options, # explicit options to ->new + # Combine final options + my %new_options = ( + %options, # explicit options %{ $getopt->options }, # options from CLI - getopt => $getopt, - }; + getopt => $getopt, # session object itself + ); - return $new_options; + return wantarray ? %new_options : \%new_options; }; sub get_options_from_configfile { my $class = shift; - my $options = {}; + my %options; if ( $class->meta->does_role('MooseX::ConfigFromFile') ) { local @ARGV = @ARGV; @@ -87,11 +92,11 @@ sub get_options_from_configfile { }; if ( defined $configfile ) { - $options = $class->get_config_from_file($configfile); + %options = %{ $class->get_config_from_file($configfile) }; }; }; - return $options; + return wantarray ? %options : \%options; };