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