From: Piotr Roszatycki Date: Thu, 13 Nov 2008 16:37:43 +0000 (+0000) Subject: * MooseX::Getopt: Reimplemented MooseX::ConfigFromFile support. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d09046450c54b37640fed28a06a17b7d040ba81a;p=gitmo%2FMooseX-Getopt.git * MooseX::Getopt: Reimplemented MooseX::ConfigFromFile support. --- diff --git a/ChangeLog b/ChangeLog index d9976f4..39d85bf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -20,9 +20,6 @@ Revision history for Perl extension MooseX-Getopt - Getopt parser is pluggined. (dexter) - * TODO: - - MooseX::ConfigFromFile should be restored? - 0.15 Sat. July 26 2008 * MooseX::Getopt::OptionTypeMap - Accept type constraint objects in the type mapping, not just names diff --git a/lib/MooseX/Getopt.pm b/lib/MooseX/Getopt.pm index c8504df..27c17c8 100644 --- a/lib/MooseX/Getopt.pm +++ b/lib/MooseX/Getopt.pm @@ -40,7 +40,7 @@ sub get_options_from_argv { Moose->throw_error("Single parameters to get_options_from_argv() must be a HASH ref") if ref $_[0] and ref $_ ne 'HASH'; - my %params = ( @_ == 1 ? %{ $_[0] } : @_ ); + my %params = ( $class->_get_options_from_configfile, @_ == 1 ? %{ $_[0] } : @_ ); my $getopt = defined $params{getopt} ? $params{getopt} @@ -60,6 +60,32 @@ sub get_options_from_argv { }; +sub _get_options_from_configfile { + my $class = shift; + + my %params = (); + + if ($class->meta->does_role('MooseX::ConfigFromFile')) { + local @ARGV = @ARGV; + + my $configfile; + my $opt_parser = Getopt::Long::Parser->new( config => [ 'pass_through' ] ); + $opt_parser->getoptions( "configfile=s" => \$configfile ); + + if (not defined $configfile) { + my $cfmeta = $class->meta->find_attribute_by_name('configfile'); + $configfile = $cfmeta->default if $cfmeta->has_default; + }; + + if (defined $configfile) { + %params = %{ $class->get_config_from_file($configfile) }; + }; + }; + + return %params; +}; + + sub _compute_getopt_attrs { my $class = shift;