blib/lib/Getopt/Yagow.pm

filter: Statementstmt 63.2% 81 / 128 Branchbran 68.5% 37 / 54 Conditioncond 53.3% 8 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 52.9% 9 / 17 Podpod 69.2% 9 / 13 totaltotal 62.1% 146 / 235 CC 38 SCAR 28.2
CC
38
Cov
64%
DEBUG
56.0
parse_cmd_line
19.2
load_options
11.2
CRAP 16.8
↑ Summary
1 package Getopt::Yagow;
2 # -*-Perl-*-
3 #
4 # DESCRIPTION
5 # Loads and parses command line options. See pod doc.
6 #
7 # AUTHOR AND COPYRIGHT
8 # Enrique Castilla Contreras <ecastillacontreras@yahoo.es>
9 # Copyright (C) 2004-2007 Enrique Castilla.
10 #
11 #-----------------------------------------------------------------------
12 # $Id: Yagow.pm,v 1.1 2004/02/10 12:58:02 ecastilla Exp $
13 #-----------------------------------------------------------------------
14
15 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 4 $VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
19
20 4 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
22
23 # Usage: same as load_options.
24 #
25 sub new
26 8 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
27 8 my $class = shift;
28
29 8 my $this = bless {}, $class;
30 8 $this->load_options( @_ );
31
32 8 return $this;
33 }
34
35 # Usage:
36 # $opt = Getopt::Yagow->load_options; or
37 # $opt = Getopt::Yagow->load_options( 'option_spec' => default_value, ...) or
38 # $opt = Getopt::Yagow->load_options( { 'option_spec' => default_value, ... } ) or
39 # $opt = Getopt::Yagow->load_options( { ... }, ['pass_through', ... ] );
40 #
41 # Sample:
42 #
43 # $p->load_options(
44 # 'config=s' => '',
45 # 'srcdir:s' => '',
46 # 'tardir:s' => '',
47 # # 'help' => '', # Se aƱade por defecto.
48 # 'css:s' => 'style.css',
49 # 'file_icon|f_icon:s' => 'c:\Perl\site\lib\tk\file.xbm',
50 # 'dir_icon|d_icon:s' => 'c:\Perl\site\lib\tk\folder.xbm'
51 # );
52 #
53 sub load_options
54 12 ▶{
Subroutine load_options: called CC=11 SCAR=24.1
Pod documented
55 12 my $this = shift;
56
57 12 my( $options_spec, $config);
58 12 ▶ if( @_ == 2 && ref $_[0] eq 'HASH' && ref $_[1] eq 'ARRAY' )
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 2 and ref $_[0] eq 'HASH' and ref $_[1] eq 'ARRAY') { } 2 10 12
elsif (@_ == 1 and ref $_[0] eq 'HASH') { } 3 7 10
elsif (@_ >= 1) { } 1 6 7
Condition66%4/6
@_ == 2 and ref $_[0] eq "HASH" and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
1002
@_ == 2 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1002
MC/DC33%1/3
@_ == 2 and ref $_[0] eq "HASH" and ref $_[1] eq "ARRAY"
@_ == 2 ref $_[0] eq "HASH" ref $_[1] eq "ARRAY"
Truth table50%2/4
A and B and C
A= @_ == 2B= ref $_[0] eq "HASH"C= ref $_[1] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
59 {
60 2 $options_spec = $_[0];
61 2 $config = $_[1];
62 }
63 ▶ elsif( @_ == 1 && ref $_[0] eq 'HASH' )
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
703
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
64 {
65 3 $options_spec = $_[0];
66 }
67 elsif( @_ >= 1 )
68 {
69 # Hash
70 1 ▶ $options_spec = { @_ } if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
71 }
72
73 12 ▶ if( defined $config )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config) 2 10 12
74 {
75 2 $this->{configuration} = $config;
76 2 Getopt::Long::Configure( @$config );
77 }
78
79 12 ▶ return $this if ! defined $options_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options_spec 6 6 12
80
81 6 ▶ while( my ($opt_spec,$default) = each %$options_spec )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$options_spec) 32 6 38
82 {
83 32 $this->add_option( $opt_spec,$default );
84 }
85
86 6 return $this;
87 }
88
89 sub add_option
90 32 ▶{
Subroutine add_option: called CC=4 SCAR=13.9
Pod undocumented
91 32 my ($this, $opt_spec, $default) = @_;
92
93 32 (my $opt_name = $opt_spec) =~ s/([|=:!+]).*//;
94 # croak "Can't add_option '$opt_name', already defined"
95 # if exists $this->{$opt_name};
96
97 32 $this->{options}->{$opt_name} = $opt_spec;
98
99 32 ▶ $this->{default}->{$opt_name} = $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 26 6 32
100 32 ▶ if( ! defined $default )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $default) 6 26 32
101 {
102 6 ▶ $this->{mandatory} = [] if ! exists $this->{mandatory};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"mandatory"} 6 0 6
103 6 push @{$this->{mandatory}}, $opt_name ;
104 }
105 }
106
107 sub usage
108 36 ▶{
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
109 36 my $this = shift;
110 36 my $usage_opts = shift;
111
112 36 pod2usage( $usage_opts );
113 }
114
115 # Usage:
116 # $opt->parse_cmd_line( '--opt1 val1', '--opt2 val2', ... );
117 # $opt->parse_cmd_line( ... , '--optx valx',
118 # { -msg=>$msg_help,-verbose=>1 },
119 # { -msg=>$msg_wrong_syntax,-verbose=>0 } );
120 # $opt->parse_cmd_line( ... , '--optx valx',
121 # { -msg=>$msg_help,-verbose=>1 } );
122 # $opt->parse_cmd_line( ... , '--optx valx',
123 # undef,
124 # { -msg=>$msg_wrong_syntax,-verbose=>0 } );
125 #
126 sub parse_cmd_line
127 28 ▶{
Subroutine parse_cmd_line: called CC=18 SCAR=29.6
Pod documented
128 28 my $this = shift;
129
130 28 my $default_msg = "!! Incorrect syntax. Use --h for help !!.\n";
131
132 28 my ($i,$arg,$help_usage,$wrong_syntax);
133
134 # Cases:
135 # 1.- ... , {...}, {...}
136 # 3.- ... , undef, undef
137 # 4.- ... , {...}, undef
138 # 5.- ... , undef, {...}
139 # 6.- ... , undef
140 # 7.- ... , {...}
141 # 2.- {...}, {...}
142 # 8.- undef, undef
143 # 9.- undef, {...}
144 # 10.- {...}, undef
145 # 11.- {...}
146 # 12.- undef
147 #
148 28 my $hash_no = 1;
149 28 ▶ for( $i = 0; $i < @_; $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 56 28 84
150 {
151 56 $arg = $_[$i];
152 56 ▶ if( (defined $arg && ref $arg eq 'HASH') || !defined $arg )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg and ref $arg eq "HASH" or not defined $arg) 56 0 56
Condition33%2/6
defined $arg and ref $arg eq "HASH" or not defined $arg
l!l&&r!l&&!r
5600
defined $arg and ref $arg eq "HASH"
!ll&&!rl&&r
0056
MC/DC0%0/3
defined $arg and ref $arg eq "HASH" or not defined $arg
defined $arg ref $arg eq "HASH" not defined $arg
Truth table20%1/5
A and B or not(C)
A= defined $argB= ref $arg eq "HASH"C= not defined $arg
ABCresult
11X1
0X11
1011
0X00
1000
153 {
154 56 ▶ if( $hash_no == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_no == 1) 28 28 56
155 {
156 28 ▶ $help_usage = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 28 0 28
157 28 $hash_no = 2;
158 }
159
160 56 ▶ if( $hash_no == 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hash_no == 2) 28 28 56
161 {
162 28 ▶ $wrong_syntax = $arg if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 28 0 28
163 28 $hash_no = 3;
164 }
165
166 56 splice @_,$i,1; # Supress $arg from argument list.
167 56 $i--;
168 }
169 }
170
171 28 ▶ $wrong_syntax = {-msg=>$default_msg,-verbose=>0} if ! defined $wrong_syntax;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wrong_syntax 0 28 28
172 28 ▶ $help_usage = { -verbose => 1 } if ! defined $help_usage;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $help_usage 0 28 28
173
174 28 ▶ my @args = @_ ? @_ : @ARGV;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 28 28
175 28 local (@ARGV) = @args;
176
177 #
178 # Handle command line parameters
179 #
180 28 my @options = values %{ $this->{options} };
181 28 my %used = ();
182 28 ▶ unless( GetOptions(\%used, 'help|h|?!', @options))
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions(\%used, "help|h|?!", @options)) 15 13 28
183 {
184 15 $this->usage( $wrong_syntax );
185 }
186
187 28 ▶ if( exists $used{help} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $used{"help"}) 9 19 28
188 {
189 9 $this->usage( $help_usage );
190 }
191
192 # If execution reaches this, syntax is correct from the point of view of
193 # GetOptions, but we require that options with 'undef' default value be
194 # specified in command line.
195 #
196 28 foreach my $mandatory_opt ( @{$this->{mandatory}} )
197 {
198 19 ▶ if( ! exists $used{$mandatory_opt} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $used{$mandatory_opt}) 12 7 19
199 {
200 12 warn "There is/are mandatory argument(s)";
201 12 $this->usage( $wrong_syntax );
202 }
203 }
204 # Also, options with default values, but used in command line, must be
205 # deleted.
206 28 foreach ( keys %{$this->{default}} )
207 {
208 19 ▶ delete $this->{default}->{$_} if exists $used{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $used{$_} 0 19 19
209 }
210 28 $this->{used} = \%used;
211
212 # Debug:
213 # print "# Getopt::Yagow. \@ARGV: ",join(',',@ARGV),"\n";
214
215 28 $this->{unhandled_options} = [];
216 28 push @{$this->{unhandled_options}}, @ARGV;
217
218 28 return $this;
219 }
220
221 sub get_configuration
222 0 ▶{
Subroutine get_configuration: not called CC=2 SCAR=17.9
Pod documented
223 0 my $this = shift;
224
225 0 ▶ return ( exists $this->{configuration} ? $this->{configuration} : [] );
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'configuration'} ? : 0 0 0
226 }
227
228 sub get_options
229 0 ▶{
Subroutine get_options: not called CC=1 SCAR=6.9
Pod undocumented
230 0 my $this = shift;
231
232 0 return $this->{options};
233 }
234
235 sub get_default
236 0 ▶{
Subroutine get_default: not called CC=1 SCAR=6.9
Pod documented
237 0 my $this = shift;
238
239 0 return $this->{default};
240 }
241
242 sub get_mandatory
243 0 ▶{
Subroutine get_mandatory: not called CC=1 SCAR=6.9
Pod documented
244 0 my $this = shift;
245
246 0 return $this->{mandatory};
247 }
248
249 sub get_used
250 0 ▶{
Subroutine get_used: not called CC=1 SCAR=6.9
Pod documented
251 0 my $this = shift;
252
253 0 return $this->{used};
254 }
255
256 sub get_unhandled
257 0 ▶{
Subroutine get_unhandled: not called CC=1 SCAR=6.9
Pod documented
258 0 my $this = shift;
259
260 0 return $this->{unhandled_options};
261 }
262
263 sub get_options_values
264 0 ▶{
Subroutine get_options_values: not called CC=1 SCAR=6.9
Pod documented
265 0 my $this = shift;
266
267 0 return { %{$this->{default}}, %{$this->{used}} };
268 }
269
270 sub DEBUG
271 0 ▶{
Subroutine DEBUG: not called CC=7 SCAR=40.3
Pod undocumented
272 0 my $this = shift;
273
274 0 print STDERR "configuration:\n";
275 0 foreach my $key ( @{$this->{configuration}} )
276 {
277 0 print STDERR "$key\n";
278 }
279 0 print STDERR "\n";
280
281 0 print STDERR "options:\n";
282 0 ▶ while( my($key,$val) = each %{$this->{options}} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$this{"options"};}) 0 0 0
283 {
284 0 print STDERR "$key => $val\n";
285 }
286 0 print STDERR "\n";
287
288 0 print STDERR "default:\n";
289 0 ▶ while( my($key,$val) = each %{$this->{default}} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$this{"default"};}) 0 0 0
290 {
291 0 print STDERR "$key => $val\n";
292 }
293 0 print STDERR "\n";
294
295 0 print STDERR "mandatory:\n";
296 0 foreach my $key ( @{$this->{mandatory}} )
297 {
298 0 print STDERR "$key\n";
299 }
300 0 print STDERR "\n";
301
302 0 print STDERR "used:\n";
303 0 ▶ while( my($key,$val) = each %{$this->{used}} )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$this{"used"};}) 0 0 0
304 {
305 0 print STDERR "$key => $val\n";
306 }
307 0 print STDERR "\n";
308
309 0 print STDERR "unhandled:\n";
310 0 foreach my $key ( @{$this->{unhandled_options}} )
311 {
312 0 print STDERR "$key\n";
313 }
314 0 print STDERR "\n";
315
316 }
317
318 __END__
↑ Summary