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 | |
26 | # Original @ARGV values |
ac2073c8 |
27 | has ARGV => ( |
c6c1f628 |
28 | is => 'rw', |
29 | isa => 'ArrayRef[Str]', |
c6c1f628 |
30 | default => sub { [ @ARGV ] }, |
31 | ); |
32 | |
33 | # Unrecognized @ARGV values |
ac2073c8 |
34 | has extra_argv => ( |
c6c1f628 |
35 | is => 'rw', |
36 | isa => 'ArrayRef[Str]', |
c6c1f628 |
37 | default => sub { [] }, |
38 | ); |
39 | |
40 | # Hash with options parsed from argv |
ac2073c8 |
41 | has options => ( |
c6c1f628 |
42 | is => 'rw', |
43 | isa => 'HashRef', |
c6c1f628 |
44 | default => sub { {} }, |
45 | ); |
46 | |
c6c1f628 |
47 | |
c6c1f628 |
48 | sub BUILD { |
c6c1f628 |
49 | my ($self, $args) = @_; |
50 | |
51 | $self->build_options; |
52 | }; |
53 | |
54 | |
55 | sub 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 | |
64 | sub _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 |
75 | sub _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 | |
91 | sub _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 |
106 | 1; |
dd012666 |
107 | |
ac2073c8 |
108 | __END__ |
dd012666 |
109 | |
ac2073c8 |
110 | =pod |
dd012666 |
111 | |
ac2073c8 |
112 | =head1 NAME |
113 | |
114 | MooseX::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 | |
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. |
133 | |
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>. |
137 | |
138 | =head1 METHODS |
139 | |
140 | =over 4 |
141 | |
142 | =item B<new (%params)> |
143 | |
144 | This method creates new CLI session for L<MooseX::Getopt> and calls |
145 | C<build_options> method. |
146 | |
147 | =item B<build_options> |
148 | |
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. |
152 | |
153 | =item B<parser> |
154 | |
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. |
159 | |
160 | =item B<classes_filter> |
161 | |
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. |
166 | |
ac2073c8 |
167 | =item B<ARGV> |
168 | |
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>. |
171 | |
172 | =item B<extra_argv> |
173 | |
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 |
176 | un-mangled. |
177 | |
178 | =item B<options> |
179 | |
10ed52cb |
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. |
ac2073c8 |
183 | |
184 | =item B<BUILD> |
185 | |
186 | This is a default L<Moose> constructor. |
187 | |
188 | =item B<meta> |
189 | |
190 | This returns the role meta object. |
191 | |
192 | =back |
193 | |
194 | =head1 SEE ALSO |
195 | |
196 | =over 4 |
197 | |
198 | =item L<MooseX::Getopt> |
199 | |
200 | =item L<MooseX::Getopt::Parser> |
201 | |
202 | =back |
203 | |
204 | =head1 BUGS |
205 | |
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 |
208 | to cpan-RT. |
209 | |
210 | =head1 AUTHOR |
211 | |
212 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
213 | |
214 | Brandon L. Black, E<lt>blblack@gmail.comE<gt> |
215 | |
216 | Yuval Kogman, E<lt>nothingmuch@woobling.orgE<gt> |
217 | |
218 | =head1 CONTRIBUTORS |
219 | |
220 | Ryan D Johnson, E<lt>ryan@innerfence.comE<gt> |
221 | |
222 | Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt> |
223 | |
224 | =head1 COPYRIGHT AND LICENSE |
225 | |
226 | Copyright 2007-2008 by Infinity Interactive, Inc. |
227 | |
228 | L<http://www.iinteractive.com> |
229 | |
230 | This library is free software; you can redistribute it and/or modify |
231 | it under the same terms as Perl itself. |
232 | |
233 | =cut |