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