Commit | Line | Data |
c6c1f628 |
1 | |
2 | package MooseX::Getopt::Session; |
3 | |
4 | use Moose; |
5 | |
ac2073c8 |
6 | use MooseX::Getopt::Parser::Default; |
7 | |
8 | |
9 | use constant _default_getopt_parser => 'MooseX::Getopt::Parser::Default'; |
c6c1f628 |
10 | |
c6c1f628 |
11 | |
12 | # Pluggined MooseX::Getopt::Parser parser |
ac2073c8 |
13 | has 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 |
20 | has 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 |
27 | has params => ( |
053fa19e |
28 | is => 'rw', |
29 | isa => 'HashRef', |
30 | default => sub { {} }, |
31 | ); |
32 | |
c6c1f628 |
33 | # Original @ARGV values |
ac2073c8 |
34 | has ARGV => ( |
c6c1f628 |
35 | is => 'rw', |
36 | isa => 'ArrayRef[Str]', |
c6c1f628 |
37 | default => sub { [ @ARGV ] }, |
38 | ); |
39 | |
40 | # Unrecognized @ARGV values |
ac2073c8 |
41 | has extra_argv => ( |
c6c1f628 |
42 | is => 'rw', |
43 | isa => 'ArrayRef[Str]', |
c6c1f628 |
44 | default => sub { [] }, |
45 | ); |
46 | |
47 | # Hash with options parsed from argv |
ac2073c8 |
48 | has options => ( |
c6c1f628 |
49 | is => 'rw', |
50 | isa => 'HashRef', |
c6c1f628 |
51 | default => sub { {} }, |
52 | ); |
53 | |
c6c1f628 |
54 | |
c6c1f628 |
55 | sub BUILD { |
c6c1f628 |
56 | my ($self, $args) = @_; |
57 | |
58 | $self->build_options; |
59 | }; |
60 | |
61 | |
62 | sub 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 | |
71 | sub _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 |
82 | sub _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 | |
98 | sub _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 |
113 | 1; |
dd012666 |
114 | |
ac2073c8 |
115 | __END__ |
dd012666 |
116 | |
ac2073c8 |
117 | =pod |
dd012666 |
118 | |
ac2073c8 |
119 | =head1 NAME |
120 | |
121 | MooseX::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 | |
137 | This class handles CLI session for L<MooseX::Getopt>. The session can |
138 | be shared between more than one classes which does L<MooseX::Getopt>. |
139 | If C<getopt> parameter is missing, new session is created for one class. |
140 | |
141 | The L<MooseX::Getopt::Session> scans all classes and gets these which do |
142 | L<MooseX::Getopt>. These classes will make an option list for |
143 | L<MooseX::Getopt::Parser>. |
144 | |
145 | =head1 METHODS |
146 | |
147 | =over 4 |
148 | |
149 | =item B<new (%params)> |
150 | |
151 | This method creates new CLI session for L<MooseX::Getopt> and calls |
152 | C<build_options> method. |
153 | |
154 | =item B<build_options> |
155 | |
156 | This method creates the list of attributes which contains options and |
157 | calls C<build_options> method from L<MooseX::Getopt::Parser> with this |
158 | object and attributes list as parameters. |
159 | |
160 | =item B<parser> |
161 | |
162 | This accessor contains a parser object which does |
163 | L<MooseX::Getopt::Parser>. It can be set explicite, i.e. if you need to |
164 | modify the way parser works, or new object via |
165 | L<MooseX::Getopt::Parser::Default> factory will be created. |
166 | |
167 | =item B<classes_filter> |
168 | |
169 | This accessor contains a coderef with classes filter which is used for |
170 | searching proper classes to create options list. The filter passes any |
171 | class by default but L<MooseX::Getopt> will search the attributes only |
172 | in own class, if new session is created implicity. |
173 | |
174 | =item B<params> |
175 | |
176 | This accessor contains the parameters which will be included to each |
177 | L<MooseX::Getopt>->new_with_options call. |
178 | |
179 | =item B<ARGV> |
180 | |
181 | This accessor contains a reference to a copy of the C<@ARGV> array as it |
182 | originally existed at the time of C<new_with_options>. |
183 | |
184 | =item B<extra_argv> |
185 | |
186 | This accessor contains an arrayref of leftover C<@ARGV> elements that |
187 | L<Getopt::Long> did not parse. Note that the real C<@ARGV> is left |
188 | un-mangled. |
189 | |
190 | =item B<options> |
191 | |
192 | This accessor contains an arrayref of options parsed from command line |
193 | by L<MooseX::Getopt::Parser>. |
194 | |
195 | =item B<BUILD> |
196 | |
197 | This is a default L<Moose> constructor. |
198 | |
199 | =item B<meta> |
200 | |
201 | This 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 | |
217 | All complex software has bugs lurking in it, and this module is no |
218 | exception. If you find a bug please either email me, or add the bug |
219 | to cpan-RT. |
220 | |
221 | =head1 AUTHOR |
222 | |
223 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
224 | |
225 | Brandon L. Black, E<lt>blblack@gmail.comE<gt> |
226 | |
227 | Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt> |
228 | |
229 | =head1 CONTRIBUTORS |
230 | |
231 | Ryan D Johnson, E<lt>ryan@innerfence.comE<gt> |
232 | |
233 | Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt> |
234 | |
235 | =head1 COPYRIGHT AND LICENSE |
236 | |
237 | Copyright 2007-2008 by Infinity Interactive, Inc. |
238 | |
239 | L<http://www.iinteractive.com> |
240 | |
241 | This library is free software; you can redistribute it and/or modify |
242 | it under the same terms as Perl itself. |
243 | |
244 | =cut |