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');
37 my (@opts, %cmd_flags_to_names);
39 foreach my $attr (@attrs) {
40 my $name = $attr->name;
42 my ($flag, @aliases) = $getopt->_get_cmd_flags_for_attr($attr);
43 my $type = $getopt->_get_cmd_type_for_attr($attr);
45 $cmd_flags_to_names{$flag} = $name;
47 my $opt_string = join '|', $flag, @aliases;
48 $opt_string .= MooseX::Getopt::OptionTypeMap->get_option_type($type) if $type;
51 $doc = $attr->documentation if $attr->has_documentation;
52 $doc = ' ' unless $doc;
54 my $is_required = !exists $getopt->params->{$name}
56 && !$attr->has_default
57 && !$attr->has_builder;
62 ( $is_required ? ( required => $attr->is_required ) : () ),
70 local @ARGV = @{ $getopt->ARGV };
72 local $SIG{__WARN__} = sub {
73 return warn @_ if $_[0]=~/^\###/; # Smart::Comments
78 ($options, $usage) = Getopt::Long::Descriptive::describe_options(
79 $self->format, @opts, { getopt_conf => [ $self->config ] }
83 $warnings .= $e if $e;
85 my $extra_argv = \@ARGV;
86 $getopt->extra_argv( $extra_argv );
89 # Convert cmd_flags back to names in options hashref
90 $options = { map { $cmd_flags_to_names{$_} => $options->{$_} } keys %$options };
92 $getopt->options( $options );
94 die $warnings if $warnings;
108 MooseX::Getopt::Parser::Descriptive - A Getopt::Long::Descriptive parser for MooseX::Getopt
112 use MooseX::Getopt::Parser::Descriptive;
114 my $parser = MooseX::Getopt::Parser::Descriptive->new(
115 format => 'Usage: %c %o',
116 config => ['pass_through']
118 my $getopt = MooseX::Getopt::Session->new( parser => $parser );
119 my $app = My::App->new( getopt => $getopt );
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
131 =item B<build_options ($getopt, @attrs)>
133 This method parses the CLI options with L<Getopt::Long> and returns a hashref to options list.
135 The first argument have to be L<MooseX::Getopt::Session> object and
136 second argument is a list of attributes which contains options.
140 This accessor contains the arrayref to list with special configuration
141 keywords for L<Getopt::Long>.
145 This accessor contains the string with message printed by
146 L<Getopt::Long::Descriptive> if error is occured.
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
160 =item L<MooseX::Getopt::Parser>
162 =item L<MooseX::Getopt::Parser::Default>
164 =item L<MooseX::Getopt::Parser::Long>
166 =item L<Getopt::Long::Descriptive>
172 Piotr Roszatycki, E<lt>dexter@cpan.orgE<gt>
174 =head1 COPYRIGHT AND LICENSE
176 Copyright 2007-2008 by Infinity Interactive, Inc.
178 L<http://www.iinteractive.com>
180 This library is free software; you can redistribute it and/or modify
181 it under the same terms as Perl itself.