Implements feature suggestion RT#58715 by storing the Usage object, fixes
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / GLD.pm
index 806d738..5edb937 100644 (file)
@@ -1,25 +1,36 @@
 package MooseX::Getopt::GLD;
+# ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive
 
 use Moose::Role;
 
-around '_getopt_spec' => sub {
-    my $orig = shift;
-    my $self = shift;
+use Getopt::Long::Descriptive 0.081;
 
-    return $self->_gld_spec(@_);
-    # Ignore $orig, code for _gld_spec here
-};
+with 'MooseX::Getopt::Basic';
+
+has usage => (
+    is => 'rw', isa => 'Getopt::Long::Descriptive::Usage',
+    traits => ['NoGetopt'],
+);
 
-around '_get_options' => sub {
-    my $orig = shift;
-    my $class = shift;
+# captures the options: --help --usage --?
+has help_flag => (
+    is => 'ro', isa => 'Bool',
+    traits => ['Getopt'],
+    cmd_flag => 'help',
+    cmd_aliases => [ qw(usage ?) ],
+    documentation => 'Prints this usage information.',
+);
 
-    my ($params, $opt_spec) = @_;
-    return Getopt::Long::Descriptive::describe_options(
-        $class->_usage_format(%$params), @$opt_spec
-    );
+around _getopt_spec => sub {
+    shift;
+    shift->_gld_spec(@_);
 };
 
+around _getopt_get_options => sub {
+    shift;
+    my ($class, $params, $opt_spec) = @_;
+    return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec);
+};
 
 sub _gld_spec {
     my ( $class, %params ) = @_;
@@ -35,16 +46,16 @@ sub _gld_spec {
             {
                 ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
                 # NOTE:
-                # remove this 'feature' because it didn't work 
+                # remove this 'feature' because it didn't work
                 # all the time, and so is better to not bother
-                # since Moose will handle the defaults just 
+                # since Moose will handle the defaults just
                 # fine anyway.
                 # - SL
                 #( exists $opt->{default}  ? (default  => $opt->{default})  : () ),
             },
         ];
 
-        my $identifier = $opt->{name};
+        my $identifier = lc($opt->{name});
         $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
 
         $name_to_init_arg{$identifier} = $opt->{init_arg};
@@ -53,55 +64,32 @@ sub _gld_spec {
     return ( \@options, \%name_to_init_arg );
 }
 
-1;
-
-__END__
-
-=pod
+no Moose::Role;
 
-=head1 NAME
-
-MooseX::Getopt::GLD - role to implement specific functionality for 
-L<Getopt::Long::Descriptive>
+1;
 
 =head1 SYNOPSIS
-    
-For internal use.
-
-=head1 DESCRIPTION
-
-This is a role for C<MooseX::Getopt>.
-
-=head1 METHODS
-
-=over 4
-
-=item meta
-
-=back
-
-=head1 BUGS
-
-All complex software has bugs lurking in it, and this module is no
-exception. If you find a bug please either email me, or add the bug
-to cpan-RT.
 
-=head1 AUTHOR
+  ## In your class
+  package My::App;
+  use Moose;
 
-Dagfinn Ilmari MannsE<aring>ker E<lt>ilmari@ilmari.orgE<gt>
+  with 'MooseX::Getopt::GLD';
 
-Stevan Little E<lt>stevan@iinteractive.comE<gt>
+  has 'out' => (is => 'rw', isa => 'Str', required => 1);
+  has 'in'  => (is => 'rw', isa => 'Str', required => 1);
 
-Yuval Kogman  C<< <nuffin@cpan.org> >>
+  # ... rest of the class here
 
-=head1 COPYRIGHT AND LICENSE
+  ## in your script
+  #!/usr/bin/perl
 
-Copyright 2007-2008 by Infinity Interactive, Inc.
+  use My::App;
 
-L<http://www.iinteractive.com>
+  my $app = My::App->new_with_options();
+  # ... rest of the script here
 
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+  ## on the command line
+  % perl my_app_script.pl -in file.input -out file.dump
 
 =cut
-=head1