File Coverage

blib/lib/MooseX/Getopt/GLD.pm
Criterion Covered Total %
statement 25 25 100.0
branch 2 2 100.0
condition 5 5 100.0
subroutine 6 6 100.0
pod n/a
total 38 38 100.0


line stmt bran cond sub pod time code
1             package MooseX::Getopt::GLD;
2             # ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive
3              
4             our $VERSION = '0.75';
5              
6 26     46   17811 use strict;
  26         62  
  26         870  
7 26     26   139 use warnings;
  26         60  
  26         1006  
8 26     26   12333 use MooseX::Role::Parameterized 1.01;
  26         2040844  
  26         194  
9 26     26   1029552 use Getopt::Long::Descriptive 0.088;
  26         573051  
  26         180  
10             with 'MooseX::Getopt::Basic';
11 26     26   9348 use namespace::autoclean;
  26         62  
  26         252  
12              
13             parameter getopt_conf => (
14             isa => 'ArrayRef[Str]',
15             default => sub { [] },
16             );
17              
18             role {
19              
20             my $p = shift;
21             my $getopt_conf = $p->getopt_conf;
22              
23             has usage => (
24             is => 'rw', isa => 'Getopt::Long::Descriptive::Usage',
25             traits => ['NoGetopt'],
26             );
27              
28             # captures the options: --help --usage --? -? -h
29             has help_flag => (
30             is => 'ro',
31             traits => ['Getopt'],
32             cmd_flag => 'help',
33             cmd_aliases => [ qw(usage ? h) ],
34             documentation => 'Prints this usage information.',
35             );
36              
37             around _getopt_spec => sub {
38             shift;
39             shift->_gld_spec(@_);
40             };
41              
42             around _getopt_get_options => sub {
43             shift;
44             my ($class, $params, $opt_spec) = @_;
45             # Check if a special args hash were already passed, or create a new one
46             my $args = ref($opt_spec->[-1]) eq 'HASH' ? pop @$opt_spec : {};
47             unshift @{$args->{getopt_conf}}, @$getopt_conf;
48             push @$opt_spec, $args;
49             return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec);
50             };
51              
52             method _gld_spec => sub {
53 98     98   350 my ( $class, %params ) = @_;
54              
55 98         216 my ( @options, %name_to_init_arg );
56              
57 98         216 my $constructor_params = $params{params};
58              
59 98         184 foreach my $opt ( @{ $params{options} } ) {
  98         290  
60             push @options, [
61             $opt->{opt_string},
62             $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack
63             {
64 484 100 100     2271 ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ),
      100        
65             # NOTE:
66             # remove this 'feature' because it didn't work
67             # all the time, and so is better to not bother
68             # since Moose will handle the defaults just
69             # fine anyway.
70             # - SL
71             #( exists $opt->{default} ? (default => $opt->{default}) : () ),
72             },
73             ];
74              
75 484         884 my $identifier = lc($opt->{name});
76 484         801 $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names
77              
78 484         1083 $name_to_init_arg{$identifier} = $opt->{init_arg};
79             }
80              
81 98         465 return ( \@options, \%name_to_init_arg );
82             }
83             };
84              
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =encoding UTF-8
93              
94             =head1 NAME
95              
96             MooseX::Getopt::GLD - A Moose role for processing command line options with Getopt::Long::Descriptive
97              
98             =head1 VERSION
99              
100             version 0.75
101              
102             =head1 SYNOPSIS
103              
104             ## In your class
105             package My::App;
106             use Moose;
107              
108             with 'MooseX::Getopt::GLD';
109              
110             # or
111              
112             with 'MooseX::Getopt::GLD' => { getopt_conf => [ 'pass_through', ... ] };
113              
114             has 'out' => (is => 'rw', isa => 'Str', required => 1);
115             has 'in' => (is => 'rw', isa => 'Str', required => 1);
116              
117             # ... rest of the class here
118              
119             ## in your script
120             #!/usr/bin/perl
121              
122             use My::App;
123              
124             my $app = My::App->new_with_options();
125             # ... rest of the script here
126              
127             ## on the command line
128             % perl my_app_script.pl -in file.input -out file.dump
129              
130             =head1 OPTIONS
131              
132             This role is a parameterized role. It accepts one configuration parameter,
133             C<getopt_conf>. This parameter is an ArrayRef of strings, which are
134             L<Getopt::Long> configuration options (see "Configuring Getopt::Long" in
135             L<Getopt::Long>)
136              
137             =head1 SUPPORT
138              
139             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Getopt>
140             (or L<bug-MooseX-Getopt@rt.cpan.org|mailto:bug-MooseX-Getopt@rt.cpan.org>).
141              
142             There is also a mailing list available for users of this distribution, at
143             L<http://lists.perl.org/list/moose.html>.
144              
145             There is also an irc channel available for users of this distribution, at
146             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
147              
148             =head1 AUTHOR
149              
150             Stevan Little <stevan@iinteractive.com>
151              
152             =head1 COPYRIGHT AND LICENSE
153              
154             This software is copyright (c) 2007 by Infinity Interactive, Inc.
155              
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158              
159             =cut