* Add current $VERSION to all files.
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / Parser / Long.pm
CommitLineData
550da402 1
2package MooseX::Getopt::Parser::Long;
3
96df119b 4our $VERSION = '0.150001';
5
550da402 6use Moose;
7
8with 'MooseX::Getopt::Parser';
9
c6c1f628 10use MooseX::Getopt::OptionTypeMap;
11
2a4bd49b 12use Getopt::Long ();
13
c6c1f628 14
15# Special configuration for parser
ac2073c8 16has config => (
c5c99e6b 17 is => 'rw',
18 isa => 'ArrayRef[Str]',
c6c1f628 19 auto_deref => 1,
c5c99e6b 20 default => sub { ['default'] },
c6c1f628 21);
22
23
24sub build_options {
25 my $self = shift;
c5c99e6b 26 my ( $getopt, @attrs ) = @_;
c6c1f628 27
c5c99e6b 28 Moose->throw_error("First argument is not a MooseX::Getopt::Session")
c6c1f628 29 unless $getopt->isa('MooseX::Getopt::Session');
550da402 30
c5c99e6b 31 my $options = $getopt->options;
354c610a 32 my $new_options = { %$options };
33
c6c1f628 34 my @opts;
35
36 foreach my $attr (@attrs) {
37 my $name = $attr->name;
38
c5c99e6b 39 my ( $flag, @aliases ) = $getopt->_get_cmd_flags_for_attr($attr);
5b582f22 40 my $type = $getopt->_get_cmd_type_for_attr($attr);
41
42 my $opt_string = join '|', $flag, @aliases;
c5c99e6b 43 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type)
44 if $type;
c6c1f628 45
354c610a 46 $new_options->{$name} = undef;
47 push @opts, $opt_string => \$new_options->{$name};
c6c1f628 48 };
49
ac2073c8 50 my $warnings = '';
c6c1f628 51
52 GETOPT: {
53 my $parser = new Getopt::Long::Parser;
54 $parser->configure( $self->config );
55
ac2073c8 56 local @ARGV = @{ $getopt->ARGV };
c6c1f628 57
58 local $SIG{__WARN__} = sub {
ac2073c8 59 $warnings .= $_[0];
c6c1f628 60 };
550da402 61
ac2073c8 62 $parser->getoptions( @opts );
550da402 63
c6c1f628 64 my $extra_argv = \@ARGV;
65 $getopt->extra_argv( $extra_argv );
66 };
550da402 67
354c610a 68 # Filter not defined values in new_options hashref
c5c99e6b 69 $new_options = {
70 map { $_ => $new_options->{$_} }
71 grep { defined $new_options->{$_} } keys %$new_options
72 };
550da402 73
41dd8ab3 74 $getopt->status( ! $warnings );
354c610a 75 $getopt->options( $new_options );
550da402 76
ac2073c8 77 die $warnings if $warnings;
78
354c610a 79 return $new_options;
c6c1f628 80};
550da402 81
550da402 82
831;
ac2073c8 84
c5c99e6b 85
ac2073c8 86__END__
87
88=pod
89
90=head1 NAME
91
92MooseX::Getopt::Parser::Long - A Getopt::Long parser for MooseX::Getopt
93
94=head1 SYNOPSIS
95
96 use MooseX::Getopt::Parser::Long;
97
98 my $parser = MooseX::Getopt::Parser::Long->new( config => ['pass_through'] );
99 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
100 my $app = My::App->new( getopt => $getopt );
101
102=head1 DESCRIPTION
103
104This class does L<MooseX::Getopt::Parser> for L<MooseX::Getopt>. This
105class is used by default if L<Getopt::Long::Descriptive> module is
106missing.
107
108=head1 METHODS
109
110=over 4
111
112=item B<build_options ($getopt, @attrs)>
113
114This method parses the CLI options with L<Getopt::Long> and returns a hashref to options list.
115
116The first argument have to be L<MooseX::Getopt::Session> object and
117second argument is a list of attributes which contains options.
118
119=item B<config>
120
121This accessor contains the arrayref to list with special configuration
122keywords for L<Getopt::Long>.
123
124=back
125
126=head1 BUGS
127
128All complex software has bugs lurking in it, and this module is no
129exception. If you find a bug please either email me, or add the bug
130to cpan-RT.
131
132=head1 SEE ALSO
133
134=over 4
135
136=item L<MooseX::Getopt::Parser>
137
138=item L<MooseX::Getopt::Parser::Default>
139
140=item L<MooseX::Getopt::Parser::Descriptive>
141
142=item L<Getopt::Long>
143
144=back
145
146=head1 AUTHOR
147
148Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
149
150=head1 COPYRIGHT AND LICENSE
151
152Copyright 2007-2008 by Infinity Interactive, Inc.
153
154L<http://www.iinteractive.com>
155
156This library is free software; you can redistribute it and/or modify
157it under the same terms as Perl itself.
158
159=cut