Whitespace pickyness
[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
81b19ed8 10has usage => (
11 is => 'rw', isa => 'Getopt::Long::Descriptive::Usage',
12 traits => ['NoGetopt'],
13);
14
15# captures the options: --help --usage --?
16has help_flag => (
17 is => 'ro', isa => 'Bool',
18 traits => ['Getopt'],
19 cmd_flag => 'help',
20 cmd_aliases => [ qw(usage ?) ],
21 documentation => 'Prints this usage information.',
22);
23
33edcaa4 24around _getopt_spec => sub {
25 shift;
26 shift->_gld_spec(@_);
ef47fe44 27};
28
33edcaa4 29around _getopt_get_options => sub {
30 shift;
31 my ($class, $params, $opt_spec) = @_;
32 return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec);
33};
a2099669 34
35sub _gld_spec {
36 my ( $class, %params ) = @_;
37
38 my ( @options, %name_to_init_arg );
39
40 my $constructor_params = $params{params};
41
42 foreach my $opt ( @{ $params{options} } ) {
43 push @options, [
44 $opt->{opt_string},
45 $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
46 {
47 ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
48 # NOTE:
2557b526 49 # remove this 'feature' because it didn't work
a2099669 50 # all the time, and so is better to not bother
2557b526 51 # since Moose will handle the defaults just
a2099669 52 # fine anyway.
53 # - SL
54 #( exists $opt->{default} ? (default => $opt->{default}) : () ),
55 },
56 ];
57
33edcaa4 58 my $identifier = lc($opt->{name});
a2099669 59 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
60
61 $name_to_init_arg{$identifier} = $opt->{init_arg};
62 }
63
64 return ( \@options, \%name_to_init_arg );
65}
66
669588e2 67no Moose::Role;
ef47fe44 68
669588e2 691;
ef47fe44 70
71=head1 SYNOPSIS
ef47fe44 72
33edcaa4 73 ## In your class
74 package My::App;
75 use Moose;
ef47fe44 76
33edcaa4 77 with 'MooseX::Getopt::GLD';
ef47fe44 78
33edcaa4 79 has 'out' => (is => 'rw', isa => 'Str', required => 1);
80 has 'in' => (is => 'rw', isa => 'Str', required => 1);
ef47fe44 81
33edcaa4 82 # ... rest of the class here
ef47fe44 83
33edcaa4 84 ## in your script
85 #!/usr/bin/perl
ef47fe44 86
33edcaa4 87 use My::App;
ef47fe44 88
33edcaa4 89 my $app = My::App->new_with_options();
90 # ... rest of the script here
ef47fe44 91
33edcaa4 92 ## on the command line
93 % perl my_app_script.pl -in file.input -out file.dump
ef47fe44 94
ef47fe44 95=cut