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