X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMooseX%2FGetopt%2FGLD.pm;fp=lib%2FMooseX%2FGetopt%2FGLD.pm;h=97298e09d712ce4b5248e33c0d3ca286986fa44e;hb=69fbe1aad4dafb04a8dc357b7c15402c1e6e49be;hp=5edb9375df3dc85f9a32785aeccab16f7d892f2c;hpb=6caf9a1a817181c24efde52738ca326e2c17e409;p=gitmo%2FMooseX-Getopt.git diff --git a/lib/MooseX/Getopt/GLD.pm b/lib/MooseX/Getopt/GLD.pm index 5edb937..97298e0 100644 --- a/lib/MooseX/Getopt/GLD.pm +++ b/lib/MooseX/Getopt/GLD.pm @@ -1,70 +1,84 @@ package MooseX::Getopt::GLD; # ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive -use Moose::Role; +use MooseX::Role::Parameterized; use Getopt::Long::Descriptive 0.081; with 'MooseX::Getopt::Basic'; -has usage => ( - is => 'rw', isa => 'Getopt::Long::Descriptive::Usage', - traits => ['NoGetopt'], +parameter getopt_conf => ( + isa => 'ArrayRef[Str]', + default => sub { [] }, ); -# captures the options: --help --usage --? -has help_flag => ( - is => 'ro', isa => 'Bool', - traits => ['Getopt'], - cmd_flag => 'help', - cmd_aliases => [ qw(usage ?) ], - documentation => 'Prints this usage information.', -); - -around _getopt_spec => sub { - shift; - shift->_gld_spec(@_); -}; - -around _getopt_get_options => sub { - shift; - my ($class, $params, $opt_spec) = @_; - return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec); -}; - -sub _gld_spec { - my ( $class, %params ) = @_; - - my ( @options, %name_to_init_arg ); - - my $constructor_params = $params{params}; - - foreach my $opt ( @{ $params{options} } ) { - push @options, [ - $opt->{opt_string}, - $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack - { - ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ), - # NOTE: - # remove this 'feature' because it didn't work - # all the time, and so is better to not bother - # since Moose will handle the defaults just - # fine anyway. - # - SL - #( exists $opt->{default} ? (default => $opt->{default}) : () ), - }, - ]; - - my $identifier = lc($opt->{name}); - $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names - - $name_to_init_arg{$identifier} = $opt->{init_arg}; +role { + + my $p = shift; + my $getopt_conf = $p->getopt_conf; + + has usage => ( + is => 'rw', isa => 'Getopt::Long::Descriptive::Usage', + traits => ['NoGetopt'], + ); + + # captures the options: --help --usage --? + has help_flag => ( + is => 'ro', isa => 'Bool', + traits => ['Getopt'], + cmd_flag => 'help', + cmd_aliases => [ qw(usage ?) ], + documentation => 'Prints this usage information.', + ); + + around _getopt_spec => sub { + shift; + shift->_gld_spec(@_); + }; + + around _getopt_get_options => sub { + shift; + my ($class, $params, $opt_spec) = @_; + # Check if a special args hash were already passed, or create a new one + my $args = ref($opt_spec->[-1]) eq 'HASH' ? pop @$opt_spec : {}; + unshift @{$args->{getopt_conf}}, @$getopt_conf; + push @$opt_spec, $args; + return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec); + }; + + method _gld_spec => sub { + my ( $class, %params ) = @_; + + my ( @options, %name_to_init_arg ); + + my $constructor_params = $params{params}; + + foreach my $opt ( @{ $params{options} } ) { + push @options, [ + $opt->{opt_string}, + $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack + { + ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ), + # NOTE: + # remove this 'feature' because it didn't work + # all the time, and so is better to not bother + # since Moose will handle the defaults just + # fine anyway. + # - SL + #( exists $opt->{default} ? (default => $opt->{default}) : () ), + }, + ]; + + my $identifier = lc($opt->{name}); + $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names + + $name_to_init_arg{$identifier} = $opt->{init_arg}; + } + + return ( \@options, \%name_to_init_arg ); } +}; - return ( \@options, \%name_to_init_arg ); -} - -no Moose::Role; 1;