From: Piotr Roszatycki Date: Sun, 30 Nov 2008 17:06:35 +0000 (+0000) Subject: * MooseX::Getopt: Combine new options also from old options. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fgetopt-more-flexible;p=gitmo%2FMooseX-Getopt.git * MooseX::Getopt: Combine new options also from old options. * MooseX::Getopt: get_options_from_configfile and get_options_from_argv return scalar or list. --- 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; };