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