* MooseX::Getopt: Reimplemented MooseX::ConfigFromFile support.
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt.pm
index c8504df..27c17c8 100644 (file)
@@ -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;