* Perltidy.
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / Session.pm
CommitLineData
c6c1f628 1
2package MooseX::Getopt::Session;
3
4use Moose;
5
ac2073c8 6use MooseX::Getopt::Parser::Default;
7
8
9use constant _default_getopt_parser => 'MooseX::Getopt::Parser::Default';
c6c1f628 10
c6c1f628 11
12# Pluggined MooseX::Getopt::Parser parser
ac2073c8 13has parser => (
c5c99e6b 14 is => 'rw',
15 does => 'MooseX::Getopt::Parser',
ac2073c8 16 default => sub { $_[0]->_default_getopt_parser->new },
c6c1f628 17);
18
19# Filter for classes which are searched for getopt trait
ac2073c8 20has classes_filter => (
c5c99e6b 21 is => 'rw',
22 isa => 'CodeRef',
23 default => sub { sub { 1; } },
c6c1f628 24);
25
26# Original @ARGV values
ac2073c8 27has ARGV => (
c5c99e6b 28 is => 'rw',
29 isa => 'ArrayRef[Str]',
30 default => sub { [@ARGV] },
c6c1f628 31);
32
33# Unrecognized @ARGV values
ac2073c8 34has extra_argv => (
c5c99e6b 35 is => 'rw',
36 isa => 'ArrayRef[Str]',
c6c1f628 37 default => sub { [] },
38);
39
40# Hash with options parsed from argv
ac2073c8 41has options => (
c5c99e6b 42 is => 'rw',
43 isa => 'HashRef',
c6c1f628 44 default => sub { {} },
45);
46
8a29a50d 47# Status returned by Getopt parser
48has status => (
c5c99e6b 49 is => 'rw',
50 isa => 'Bool',
8a29a50d 51 predicate => 'has_status',
52);
c6c1f628 53
54
55sub build_options {
56 my ($self) = @_;
57
58 my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes;
c6c1f628 59
19b87ede 60 return $self->parser->build_options( $self, @attrs );
c5c99e6b 61};
c6c1f628 62
63
64sub _compute_getopt_classes {
65 my $self = shift;
66
67 return grep {
68 $self->classes_filter->()
69 } grep {
70 $_->isa('Moose::Object') && $_->does('MooseX::Getopt')
71 } Class::MOP->get_all_metaclasses;
72};
73
74
5b582f22 75sub _get_cmd_flags_for_attr {
c5c99e6b 76 my ( $self, $attr ) = @_;
5b582f22 77
78 my $flag = $attr->name;
79
80 my @aliases;
81
c5c99e6b 82 if ( $attr->does('MooseX::Getopt::Meta::Attribute::Trait') ) {
83 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
5b582f22 84 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
85 };
86
c5c99e6b 87 return ( $flag, @aliases );
5b582f22 88};
89
90
91sub _get_cmd_type_for_attr {
c5c99e6b 92 my ( $self, $attr ) = @_;
5b582f22 93
94 my $type;
ac2073c8 95
5b582f22 96 $type = $attr->type_constraint if $attr->has_type_constraint;
97
c5c99e6b 98 if ( $attr->does('MooseX::Getopt::Meta::Attribute::Trait') ) {
5b582f22 99 $type = $attr->cmd_type if $attr->has_cmd_type;
100 };
101
102 return $type;
103};
104
105
ac2073c8 1061;
dd012666 107
c5c99e6b 108
ac2073c8 109__END__
dd012666 110
ac2073c8 111=pod
dd012666 112
ac2073c8 113=head1 NAME
114
115MooseX::Getopt::Session - A CLI session for MooseX::Getopt
116
117=head1 SYNOPSIS
118
119 ## in your script
120 #!/usr/bin/perl
121
122 use My::App;
123
124 my $getopt = MooseX::Getopt::Session->new;
125
126 my $app = My::App->new_with_options( getopt => $getopt );
127 my $cmd = My::App::Cmd->new_with_options( getopt => $getopt );
128
129=head1 DESCRIPTION
130
131This class handles CLI session for L<MooseX::Getopt>. The session can
132be shared between more than one classes which does L<MooseX::Getopt>.
133If C<getopt> parameter is missing, new session is created for one class.
134
135The L<MooseX::Getopt::Session> scans all classes and gets these which do
136L<MooseX::Getopt>. These classes will make an option list for
137L<MooseX::Getopt::Parser>.
138
139=head1 METHODS
140
141=over 4
142
143=item B<new (%params)>
144
8a29a50d 145This method creates new CLI session object for L<MooseX::Getopt>. The
146session should be started with C<build_options> method.
ac2073c8 147
148=item B<build_options>
149
150This method creates the list of attributes which contains options and
151calls C<build_options> method from L<MooseX::Getopt::Parser> with this
152object and attributes list as parameters.
153
154=item B<parser>
155
156This accessor contains a parser object which does
157L<MooseX::Getopt::Parser>. It can be set explicite, i.e. if you need to
158modify the way parser works, or new object via
159L<MooseX::Getopt::Parser::Default> factory will be created.
160
161=item B<classes_filter>
162
163This accessor contains a coderef with classes filter which is used for
164searching proper classes to create options list. The filter passes any
165class by default but L<MooseX::Getopt> will search the attributes only
166in own class, if new session is created implicity.
167
ac2073c8 168=item B<ARGV>
169
170This accessor contains a reference to a copy of the C<@ARGV> array as it
171originally existed at the time of C<new_with_options>.
172
173=item B<extra_argv>
174
175This accessor contains an arrayref of leftover C<@ARGV> elements that
176L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left
177un-mangled.
178
179=item B<options>
180
10ed52cb 181This accessor contains an arrayref of options parsed from command line by
182L<MooseX::Getopt::Parser>. If the options list are not empty before parsing
183the command line, the old list will be included to new list.
ac2073c8 184
8a29a50d 185=item B<status>
ac2073c8 186
8a29a50d 187This accessor contains a true value if L<MooseX::Getopt::Parser> was
188called via C<build_options> method and it succeeded.
ac2073c8 189
190=item B<meta>
191
192This returns the role meta object.
193
194=back
195
196=head1 SEE ALSO
197
198=over 4
199
200=item L<MooseX::Getopt>
201
202=item L<MooseX::Getopt::Parser>
203
204=back
205
206=head1 BUGS
207
208All complex software has bugs lurking in it, and this module is no
209exception. If you find a bug please either email me, or add the bug
210to cpan-RT.
211
212=head1 AUTHOR
213
214Stevan Little E<lt>stevan@iinteractive.comE<gt>
215
216Brandon L. Black, E<lt>blblack@gmail.comE<gt>
217
218Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt>
219
c5c99e6b 220Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
221
ac2073c8 222=head1 CONTRIBUTORS
223
224Ryan D Johnson, E<lt>ryan@innerfence.comE<gt>
225
ac2073c8 226=head1 COPYRIGHT AND LICENSE
227
228Copyright 2007-2008 by Infinity Interactive, Inc.
229
230L<http://www.iinteractive.com>
231
232This library is free software; you can redistribute it and/or modify
233it under the same terms as Perl itself.
234
235=cut