2 package MooseX::Getopt::Parser::Descriptive;
6 with 'MooseX::Getopt::Parser';
8 use Getopt::Long::Descriptive;
9 use MooseX::Getopt::OptionTypeMap;
12 # Special configuration for parser
15 isa => 'ArrayRef[Str]',
17 default => sub { [] },
20 # Format for usage description
24 default => 'usage: %c %o',
30 my ($getopt, @attrs) = @_;
32 Moose->throw_error('First argument is not a MooseX::Getopt::Session')
33 unless $getopt->isa('MooseX::Getopt::Session');
35 my $options = $getopt->options;
39 my (@opts, %cmd_flags_to_names);
41 foreach my $attr (@attrs) {
42 my $name = $attr->name;
44 my ($flag, @aliases) = $getopt->_get_cmd_flags_for_attr($attr);
45 my $type = $getopt->_get_cmd_type_for_attr($attr);
47 $cmd_flags_to_names{$flag} = $name;
49 my $opt_string = join '|', $flag, @aliases;
50 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) if $type;
53 $doc = $attr->documentation if $attr->has_documentation;
54 $doc = ' ' unless $doc;
56 my $is_required = !exists $options->{$name}
58 && !$attr->has_default
59 && !$attr->has_builder;
64 ( $is_required ? ( required => $attr->is_required ) : () ),
72 local @ARGV = @{ $getopt->ARGV };
74 local $SIG{__WARN__} = sub {
75 return warn @_ if $_[0]=~/^\###/; # Smart::Comments
80 ($new_options, $usage) = Getopt::Long::Descriptive::describe_options(
81 $self->format, @opts, { getopt_conf => [ $self->config ] }
85 $warnings .= $e if $e;
87 my $extra_argv = \@ARGV;
88 $getopt->extra_argv( $extra_argv );
91 # Convert cmd_flags back to names in options hashref
92 $new_options = { map { $cmd_flags_to_names{$_} => $new_options->{$_} } keys %$new_options };
94 # Include old options and usage object
95 $new_options = { usage => $usage, %$options, %$new_options };
97 $getopt->options( $new_options );
99 die $warnings if $warnings;
113 MooseX::Getopt::Parser::Descriptive - A Getopt::Long::Descriptive parser for MooseX::Getopt
117 use MooseX::Getopt::Parser::Descriptive;
119 my $parser = MooseX::Getopt::Parser::Descriptive->new(
120 format => 'Usage: %c %o',
121 config => ['pass_through']
123 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
124 my $app = My::App->new( getopt => $getopt );
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
136 =item B<build_options ($getopt, @attrs)>
138 This method parses the CLI options with L<Getopt::Long> and returns a hashref to options list.
140 The first argument have to be L<MooseX::Getopt::Session> object and
141 second argument is a list of attributes which contains options.
145 This accessor contains the arrayref to list with special configuration
146 keywords for L<Getopt::Long>.
150 This accessor contains the string with message printed by
151 L<Getopt::Long::Descriptive> if error is occured.
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
165 =item L<MooseX::Getopt::Parser>
167 =item L<MooseX::Getopt::Parser::Default>
169 =item L<MooseX::Getopt::Parser::Long>
171 =item L<Getopt::Long::Descriptive>
177 Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
179 =head1 COPYRIGHT AND LICENSE
181 Copyright 2007-2008 by Infinity Interactive, Inc.
183 L<http://www.iinteractive.com>
185 This library is free software; you can redistribute it and/or modify
186 it under the same terms as Perl itself.