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