Remove some more trailing whitespace
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / GLD.pm
CommitLineData
33edcaa4 1package MooseX::Getopt::GLD;
669588e2 2# ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive
3
ef47fe44 4use Moose::Role;
5
669588e2 6use Getopt::Long::Descriptive 0.081;
ef47fe44 7
33edcaa4 8with 'MooseX::Getopt::Basic';
ef47fe44 9
33edcaa4 10around _getopt_spec => sub {
11 shift;
12 shift->_gld_spec(@_);
ef47fe44 13};
14
33edcaa4 15around _getopt_get_options => sub {
16 shift;
17 my ($class, $params, $opt_spec) = @_;
18 return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec);
19};
a2099669 20
21sub _gld_spec {
22 my ( $class, %params ) = @_;
23
24 my ( @options, %name_to_init_arg );
25
26 my $constructor_params = $params{params};
27
28 foreach my $opt ( @{ $params{options} } ) {
29 push @options, [
30 $opt->{opt_string},
31 $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
32 {
33 ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
34 # NOTE:
2557b526 35 # remove this 'feature' because it didn't work
a2099669 36 # all the time, and so is better to not bother
2557b526 37 # since Moose will handle the defaults just
a2099669 38 # fine anyway.
39 # - SL
40 #( exists $opt->{default} ? (default => $opt->{default}) : () ),
41 },
42 ];
43
33edcaa4 44 my $identifier = lc($opt->{name});
a2099669 45 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
46
47 $name_to_init_arg{$identifier} = $opt->{init_arg};
48 }
49
50 return ( \@options, \%name_to_init_arg );
51}
52
669588e2 53no Moose::Role;
ef47fe44 54
669588e2 551;
ef47fe44 56
57=head1 SYNOPSIS
ef47fe44 58
33edcaa4 59 ## In your class
60 package My::App;
61 use Moose;
ef47fe44 62
33edcaa4 63 with 'MooseX::Getopt::GLD';
ef47fe44 64
33edcaa4 65 has 'out' => (is => 'rw', isa => 'Str', required => 1);
66 has 'in' => (is => 'rw', isa => 'Str', required => 1);
ef47fe44 67
33edcaa4 68 # ... rest of the class here
ef47fe44 69
33edcaa4 70 ## in your script
71 #!/usr/bin/perl
ef47fe44 72
33edcaa4 73 use My::App;
ef47fe44 74
33edcaa4 75 my $app = My::App->new_with_options();
76 # ... rest of the script here
ef47fe44 77
33edcaa4 78 ## on the command line
79 % perl my_app_script.pl -in file.input -out file.dump
ef47fe44 80
ef47fe44 81=cut