* MooseX::Getopt: ARGV and extra_argv are deletaged from MooseX::Getopt::Session.
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt.pm
index 48ea79e..8bc9399 100644 (file)
@@ -19,22 +19,11 @@ our $AUTHORITY = 'cpan:STEVAN';
 use constant _default_getopt_session => 'MooseX::Getopt::Session';
 
 
-has ARGV => (
-    is => 'rw',
-    isa => 'ArrayRef',
-    metaclass => 'NoGetopt',
-);
-
-has extra_argv => (
-    is => 'rw',
-    isa => 'ArrayRef',
-    metaclass => 'NoGetopt',
-);
-
 has getopt => (
     is => 'rw',
     isa => 'MooseX::Getopt::Session',
     metaclass => 'NoGetopt',
+    handles => [ 'ARGV', 'extra_argv' ],
 );
 
 
@@ -49,19 +38,15 @@ sub new_with_options {
     my $getopt = defined $params{getopt}
                  ? $params{getopt}
                  : $class->_default_getopt_session->new(
-                      classes_filter => sub { $_ eq $class },
-                      params => \%params,
-                  );
-
-    my %options = $getopt->options;
+                       classes_filter => sub { $_ eq $class },
+                       params => \%params,
+                   );
 
     $class->new(
-        ARGV       => [ $getopt->argv ],        # backward compatibility
-        extra_argv => [ $getopt->extra_argv ],  # backward compatibility
         getopt     => $getopt,
         %{ $getopt->params },                   # params from session object
         %params,                                # explicit params to ->new
-        %options,                               # params from CLI
+        %{ $getopt->options },                  # params from CLI
     );
 };
 
@@ -262,6 +247,42 @@ type for it to the C<OptionTypeMap>, it would be treated just
 like a normal C<ArrayRef> type for Getopt purposes (that is,
 C<=s@>).
 
+=head2 Session
+
+L<MooseX::Getopt> can handle more than one class which contain
+attributes filled from CLI.  In this case, you need to use explicite
+L<MooseX::Getopt::Session> object and then the Getopt attributes will be
+searched in any class which does L<MooseX::Getopt>.
+
+  package My::App;
+  use Moose;
+  with 'MooseX::Getopt';
+  has 'send' => (is => 'rw', predicate => 'has_send');
+
+  package My::App::Send;
+  use Moose;
+  with 'MooseX::Getopt';
+  has 'to' => (is => 'rw', isa => 'Str', default => 'localhost');
+  sub send { my $self = shift; warn "Sending mail to ", $self->to; }
+
+  # ... rest of the class here
+
+  ## in your script
+  #!/usr/bin/perl
+
+  my $getopt = MooseX::Getopt::Session->new;
+
+  my $app = My::App->new_with_options( getopt => $getopt );
+  if ($app->has_send) {
+      # Use the same command line
+      my $sender = My::App::Send->new_with_options( getopt => $getopt );
+      $sender->send;
+  }
+  # ... rest of the script here
+
+  ## on the command line
+  % perl my_app_script.pl --send --to server.example.net
+
 =head1 METHODS
 
 =over 4
@@ -280,8 +301,10 @@ C<new>.
 
 =item B<ARGV>
 
-This accessor contains a reference to a copy of the C<@ARGV> array
-as it originally existed at the time of C<new_with_options>.
+This accessor contains a reference to a copy of the C<@ARGV> array as it
+originally existed at the time of C<new_with_options>.
+
+The C<ARGV> is delegated from L<MooseX::Getopt::Session> object.
 
 =item B<extra_argv>
 
@@ -289,12 +312,32 @@ This accessor contains an arrayref of leftover C<@ARGV> elements that
 L<Getopt::Long> did not parse.  Note that the real C<@ARGV> is left
 un-mangled.
 
+The C<extra_argv> is delegated from L<MooseX::Getopt::Session> object.
+
+=item B<getopt>
+
+This accessor contains a L<MooseX::Getopt::Session> object.  This object can
+be shared between more than one class which does L<MooseX::Getopt>.  The new
+object is created by default.
+
 =item B<meta>
 
 This returns the role meta object.
 
 =back
 
+=head1 SEE ALSO
+
+=over 4
+
+=item L<MooseX::Getopt::Strict>
+
+=item L<MooseX::Getopt::Session>
+
+=item L<MooseX::Getopt::Parser>
+
+=back
+
 =head1 BUGS
 
 All complex software has bugs lurking in it, and this module is no
@@ -313,6 +356,8 @@ Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
 
 Ryan D Johnson, E<lt>ryan@innerfence.comE<gt>
 
+Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
 Copyright 2007-2008 by Infinity Interactive, Inc.