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