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