File Coverage

blib/lib/Wetware/CLI.pm
Criterion Covered Total %
statement 12 42 28.5
branch 0 12 0.0
condition n/a
subroutine 4 12 33.3
pod 8 8 100.0
total 24 74 32.4


line stmt bran cond sub pod time code
1             #-------------------------------------------------------------------------------
2             # $URL$
3             # $Date$
4             # $Author$
5             # $Revision$
6             #-------------------------------------------------------------------------------
7             package Wetware::CLI;
8              
9 2     2   81721 use warnings;
  2         629  
  2         78  
10 2     2   12 use strict;
  2         4  
  2         73  
11 2     2   3605 use Getopt::Long;
  2         36231  
  2         11  
12 2     2   3587 use Pod::Usage qw(pod2usage); # it will export it, I want this annotated.
  2         129577  
  2         1130  
13              
14             our $VERSION = 0.06;
15              
16             #-------------------------------------------------------------------------------
17              
18             sub get_options {
19 0     0 1   my ($self, @argv) = @_;
20            
21 0           my $options_ref = $self->option_defaults();
22 0           my @option_spec = $self->option_specifications();
23              
24             # Because GetOptions will consume ARGV - and we want to
25             # test this code with what ever @argv was passed in.
26 0           local @ARGV = @argv;
27            
28 0 0         GetOptions( $options_ref, @option_spec )
29             or return pod2usage(
30             -message => 'Error Parsing GetOptions',
31             -exitval => 2
32             );
33              
34             # if it is just the help or pod option, then exit there.
35 0           $self->help_or_pod($options_ref);
36            
37 0           $self->remaining_argv($options_ref,@ARGV);
38            
39 0           return $self->verify_required_options($options_ref);
40             }
41              
42             sub help_or_pod {
43 0     0 1   my ($self, $options) = @_;
44              
45 0 0         pod2usage(1) if ( $options->{'help'} );
46 0 0         pod2usage( -verbose => 2 ) if ( $options->{'pod'} );
47            
48 0           return $self;
49             }
50              
51             #-------------------------------------------------------------------------------
52             # the Plain Vanilla form
53             sub new {
54 0     0 1   my ($class, %params) = @_;
55 0           my $self = bless {}, $class;
56 0           return $self;
57             }
58              
59             sub option_defaults {
60 0     0 1   return {};
61             }
62              
63             sub option_specifications {
64 0     0 1   return qw(
65             verbose
66             help
67             pod
68             );
69             }
70              
71             sub remaining_argv {
72 0     0 1   my ($self,$options_ref,@argv) = @_;
73 0 0         return unless @argv;
74 0           $options_ref->{'remaining_argv'} = [@argv];
75 0           return $self;
76             }
77              
78             sub required_settings {
79 0     0 1   return qw() ;
80             }
81              
82             sub verify_required_options {
83 0     0 1   my ($self,$options_ref) = @_;
84              
85 0           my @missing_settings = ();
86 0           foreach my $setting ( $self->required_settings() ) {
87 0 0         if ( !$options_ref->{$setting} ) {
88 0           push @missing_settings, $setting;
89             }
90             }
91 0 0         if (@missing_settings) {
92 0           pod2usage(
93             -message => "Missing settings: @missing_settings",
94             -exitval => 2,
95             );
96             }
97 0           return $options_ref;
98             }
99              
100             #-------------------------------------------------------------------------------
101              
102             1;
103              
104             __END__
105              
106             =pod
107              
108             =head1 NAME
109              
110             Wetware::CLI - A base class wrapper on Getopt::Long::GetOptions()
111              
112             =head1 SYNOPSIS
113              
114             use Wetware::CLI;
115              
116             my $cli = Wetware::CLI->new();
117             my $options_hash = $cli->get_options();
118              
119             =head1 DESCRIPTION
120              
121             I looked around, and there is no simple wrapper on GetOptions().
122              
123             So rather than have to keep cutting and pasting the same basic set
124             of semi private methods. I have opted to create a CLI Object, that
125             will do all of the work for me.
126              
127             I will discuss the question of subclassing later on.
128              
129             The list of Semi Private Methods explain basically how to make
130             your own CLI sub class.
131              
132             =head1 METHODS
133              
134             =head2 new()
135              
136             Takes no arguments, and creates a simple blessed has.
137              
138             =head2 get_options()
139              
140             This wraps the Getopt::Long function.
141              
142             =head1 SEMI_PRIVATE_METHODS
143              
144             If you are not planning to subclass this, do not worry about this.
145              
146             =head2 help_or_pod($options_hash)
147              
148             If the help or pod option is set, then this will invoke the appropriate
149             pod2usage() command.
150              
151             =head2 option_defaults()
152              
153             Returns the hash reference of option defaults. As implemented
154             this is an empty hash reference.
155              
156             =head2 option_specifications()
157              
158             Returns the list of option specificans. As implemented this
159             is merely the list help, pod, verbose.
160              
161             =head2 remaining_argv($opts, @argv)
162              
163             This is called after the C<help_or_pod()>. As implemented this
164             will add the 'remaining_argv' attributes to the $opts hash ref,
165             if @ARGV is not empty.
166              
167             This should be overridden if the sub class will want to
168             have a named value.
169              
170             It returns self, if there were any remaining values.
171             Otherwise it returns undef.
172              
173             =head2 required_settings()
174              
175             returns the list of required settings.
176              
177             =head2 verify_required_options($options_hash)
178              
179             Check that all of the required options are set.
180              
181             =head1 AUTHOR
182              
183             "drieux", C<< <"drieux [AT] at wetware.com"> >>
184              
185             =head1 BUGS
186              
187             Please report any bugs or feature requests to C<bug-wetware-cli at rt.cpan.org>, or through
188             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Wetware-CLI>. I will be notified, and then you'll
189             automatically be notified of progress on your bug as I make changes.
190              
191             =head1 SEE ALSO
192              
193             Getopt::Long;
194              
195             use Pod::Usage;
196              
197             =head1 SUPPORT
198              
199             At present I do not have any support solutions.
200              
201             =head1 ACKNOWLEDGEMENTS
202              
203             =head1 COPYRIGHT & LICENSE
204              
205             Copyright 2009 "drieux", all rights reserved.
206              
207             This program is free software; you can redistribute it and/or modify it
208             under the same terms as Perl itself.
209              
210             =cut
211              
212             # End of Wetware::CLI