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