really commit
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt.pm
index 2356ef8..d463fd4 100644 (file)
@@ -11,7 +11,7 @@ use Carp ();
 use Getopt::Long (); # GLD uses it anyway, doesn't hurt
 use constant HAVE_GLD => not not eval { require Getopt::Long::Descriptive };
 
-our $VERSION   = '0.18';
+our $VERSION   = '0.20';
 our $AUTHORITY = 'cpan:STEVAN';
 
 has ARGV       => (is => 'rw', isa => 'ArrayRef', metaclass => "NoGetopt");
@@ -52,6 +52,12 @@ sub new_with_options {
 
     my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
 
+    # did the user request usage information?
+    if ( $processed{usage} && ($params->{'?'} or $params->{help} or $params->{usage}) )
+    {
+        $processed{usage}->die();
+    }
+
     $class->new(
         ARGV       => $processed{argv_copy},
         extra_argv => $processed{argv},
@@ -60,6 +66,58 @@ sub new_with_options {
     );
 }
 
+sub BUILDARGS {
+
+    my ($class, @params) = @_;
+
+    my $config_from_file;
+    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 );
+
+        if(!defined $configfile) {
+            my $cfmeta = $class->meta->find_attribute_by_name('configfile');
+            $configfile = $cfmeta->default if $cfmeta->has_default;
+        }
+
+        if(defined $configfile) {
+            $config_from_file = $class->get_config_from_file($configfile);
+        }
+    }
+
+    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';
+
+    my %processed = $class->_parse_argv(
+        options => [
+            $class->_attrs_to_options( $config_from_file )
+        ],
+        params => $constructor_params,
+    );
+
+    my $params = $config_from_file ? { %$config_from_file, %{$processed{params}} } : $processed{params};
+
+    # did the user request usage information?
+    if ( $processed{usage} && ($params->{'?'} or $params->{help} or $params->{usage}) )
+    {
+        $processed{usage}->die();
+    }
+
+       # BUILDALL needs to return a hash ref of args used to build
+       return {
+        ARGV       => $processed{argv_copy},
+        extra_argv => $processed{argv},
+        %$constructor_params, # explicit params to ->new
+        %$params, # params from CLI
+       }
+}
+
+
 sub _parse_argv {
     my ( $class, %params ) = @_;
 
@@ -420,6 +478,15 @@ and then return a newly constructed object.
 If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
 C<new_with_options> will throw an exception.
 
+If L<Getopt::Long::Descriptive> is installed and any of the following
+command line params are passed, the program will exit with usage 
+information. You can add descriptions for each option by including a
+B<documentation> option for each attribute to document.
+
+  --?
+  --help
+  --usage
+
 If you have L<Getopt::Long::Descriptive> a the C<usage> param is also passed to
 C<new>.
 
@@ -458,6 +525,8 @@ Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
 
 Ryan D Johnson, E<lt>ryan@innerfence.comE<gt>
 
+Drew Taylor, E<lt>drew@drewtaylor.comE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright 2007-2008 by Infinity Interactive, Inc.