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' ],
);
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
);
};
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
=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>
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
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.