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