2 package MooseX::Getopt::Session;
6 use MooseX::Getopt::Parser::Default;
9 use constant _default_getopt_parser => 'MooseX::Getopt::Parser::Default';
12 # Pluggined MooseX::Getopt::Parser parser
15 does => 'MooseX::Getopt::Parser',
16 default => sub { $_[0]->_default_getopt_parser->new },
19 # Filter for classes which are searched for getopt trait
20 has classes_filter => (
23 default => sub { sub { 1 } },
26 # Original @ARGV values
29 isa => 'ArrayRef[Str]',
30 default => sub { [ @ARGV ] },
33 # Unrecognized @ARGV values
36 isa => 'ArrayRef[Str]',
37 default => sub { [] },
40 # Hash with options parsed from argv
44 default => sub { {} },
49 my ($self, $args) = @_;
58 my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes;
60 return $self->parser->build_options( $self, @attrs );
64 sub _compute_getopt_classes {
68 $self->classes_filter->()
70 $_->isa('Moose::Object') && $_->does('MooseX::Getopt')
71 } Class::MOP->get_all_metaclasses;
75 sub _get_cmd_flags_for_attr {
76 my ($self, $attr) = @_;
78 my $flag = $attr->name;
82 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
83 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
84 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
87 return ($flag, @aliases);
91 sub _get_cmd_type_for_attr {
92 my ($self, $attr) = @_;
96 $type = $attr->type_constraint if $attr->has_type_constraint;
98 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
99 $type = $attr->cmd_type if $attr->has_cmd_type;
114 MooseX::Getopt::Session - A CLI session for MooseX::Getopt
123 my $getopt = MooseX::Getopt::Session->new;
125 my $app = My::App->new_with_options( getopt => $getopt );
126 my $cmd = My::App::Cmd->new_with_options( getopt => $getopt );
130 This class handles CLI session for L<MooseX::Getopt>. The session can
131 be shared between more than one classes which does L<MooseX::Getopt>.
132 If C<getopt> parameter is missing, new session is created for one class.
134 The L<MooseX::Getopt::Session> scans all classes and gets these which do
135 L<MooseX::Getopt>. These classes will make an option list for
136 L<MooseX::Getopt::Parser>.
142 =item B<new (%params)>
144 This method creates new CLI session for L<MooseX::Getopt> and calls
145 C<build_options> method.
147 =item B<build_options>
149 This method creates the list of attributes which contains options and
150 calls C<build_options> method from L<MooseX::Getopt::Parser> with this
151 object and attributes list as parameters.
155 This accessor contains a parser object which does
156 L<MooseX::Getopt::Parser>. It can be set explicite, i.e. if you need to
157 modify the way parser works, or new object via
158 L<MooseX::Getopt::Parser::Default> factory will be created.
160 =item B<classes_filter>
162 This accessor contains a coderef with classes filter which is used for
163 searching proper classes to create options list. The filter passes any
164 class by default but L<MooseX::Getopt> will search the attributes only
165 in own class, if new session is created implicity.
169 This accessor contains a reference to a copy of the C<@ARGV> array as it
170 originally existed at the time of C<new_with_options>.
174 This accessor contains an arrayref of leftover C<@ARGV> elements that
175 L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
180 This accessor contains an arrayref of options parsed from command line by
181 L<MooseX::Getopt::Parser>. If the options list are not empty before parsing
182 the command line, the old list will be included to new list.
186 This is a default L<Moose> constructor.
190 This returns the role meta object.
198 =item L<MooseX::Getopt>
200 =item L<MooseX::Getopt::Parser>
206 All complex software has bugs lurking in it, and this module is no
207 exception. If you find a bug please either email me, or add the bug
212 Stevan Little E<lt>stevan@iinteractive.comE<gt>
214 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
216 Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
220 Ryan D Johnson, E<lt>ryan@innerfence.comE<gt>
222 Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
224 =head1 COPYRIGHT AND LICENSE
226 Copyright 2007-2008 by Infinity Interactive, Inc.
228 L<http://www.iinteractive.com>
230 This library is free software; you can redistribute it and/or modify
231 it under the same terms as Perl itself.