blib/lib/Script/Toolbox/Util/Opt.pm

filter: Statementstmt 87.2% 150 / 172 Branchbran 65.7% 50 / 76 Conditioncond 66.6% 10 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 5 totaltotal 78.2% 234 / 299 CC 53 SCAR 21.2
CC
53
Cov
80%
_normalize
40.9
_checkOps
10.5
_invalidOptDef
9.0
CRAP 8.3
« blib/lib/Script/mytest.pl ↑ Summary blib/lib/Script/Toolbox/Util/Formatter.pm »
1 package Script::Toolbox::Util::Opt;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 require Exporter;
9  
10 21 @ISA = qw(Exporter );
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14 21 @EXPORT = qw(
15 );
16  
17  
18 # Preloaded methods go here.
19  
20 # Autoload methods go after =cut, and are processed by the autosplit program.
21  
22 #------------------------------------------------------------------------------
23 # Create a new Opt object.
24 #------------------------------------------------------------------------------
25 sub new
26 20 ▶{
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
27 20 my $classname = shift;
28 20 my $optDef = shift; # options definition
29 20 my $caller = shift; # may be omited
30  
31 20 ▶ $optDef = {} if( !defined $optDef );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $optDef 7 13 20
32 20 ▶ $optDef = {} if( ref $optDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $optDef ne "HASH" 0 20 20
33 20 ▶ $optDef = {} if( scalar keys %{$optDef} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$optDef;} == 0 7 13 20
34 20 _addDefaultOptions( \$optDef );
35 20 ▶ return undef if( _invalidOptDef( $optDef ));
Branch: 1 branch
BranchTrueFalseTotal
if &_invalidOptDef($optDef) 3 17 20
36 17 my $self = {};
37 17 bless( $self, $classname );
38 17 $self->_instCaller($caller);
39  
40 17 my $rc = $self->_init( $optDef, @_ );
41 17 ▶ exit $rc if( $rc != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc != 0 1 16 17
42  
43 16 return $self;
44 }
45  
46 #------------------------------------------------------------------------------
47 #------------------------------------------------------------------------------
48 sub _invalidOptDef($)
49 20 ▶{
Subroutine _invalidOptDef: called CC=9 SCAR=22.0
50 20 my ($optDef) = @_;
51  
52 20 foreach my $val ( values %{$optDef} )
53 {
54 32 ▶ return 1 if( ref $val ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $val ne "HASH" 1 31 32
55 31 ▶ return 1 if( scalar keys %{$val} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$val;} == 0 2 29 31
56 29 foreach my $key ( keys %{$val} )
57 {
58 52 ▶ return 1 if( $key ne 'mod' && $key ne 'desc'&&
Branch: 1 branch
BranchTrueFalseTotal
if $key ne "mod" and $key ne "desc" and $key ne "mand" and $key ne "default" 0 52 52
Condition88%8/9
$key ne "mod" and $key ne "desc" and $key ne "mand" and $key ne "default"
!ll&&!rl&&r
4660
$key ne "mod" and $key ne "desc" and $key ne "mand"
!ll&&!rl&&r
36106
$key ne "mod" and $key ne "desc"
!ll&&!rl&&r
102616
MC/DC0%0/4
$key ne "mod" and $key ne "desc" and $key ne "mand" and $key ne "default"
$key ne "mod" $key ne "desc" $key ne "mand" $key ne "default"
Truth table80%4/5
A and B and C and D
A= $key ne "mod"B= $key ne "desc"C= $key ne "mand"D= $key ne "default"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
59 $key ne 'mand' && $key ne 'default' );
60 }
61 }
62 17 return 0;
63 }
64  
65 #------------------------------------------------------------------------------
66 #------------------------------------------------------------------------------
67 sub _instCaller(@)
68 17 ▶{
Subroutine _instCaller: called CC=2 SCAR=6.9
69 17 my ($self, $call) = @_;
70  
71 17 my @caller = caller();
72 17 ▶ $self->{'caller'} = defined $call ? $call : \@caller;
Branch: 1 branch
BranchTrueFalseTotal
defined $call ? : 16 1 17
73 }
74  
75 #------------------------------------------------------------------------------
76 # Get the options definition as input. Read the options from command line.
77 # Write usage message to STDERR if missing mandatory options.
78 #------------------------------------------------------------------------------
79 sub _init()
80 17 ▶{
Subroutine _init: called CC=2 SCAR=7.0
81 17 my ( $self, $ops, $addUsage ) = @_;
82  
83 17 $self->{'opsDef'} = _normalize($ops);
84 17 ▶ $self->{'addUsage'} = defined $addUsage ? $addUsage :''; # additional usage text
Branch: 1 branch
BranchTrueFalseTotal
defined $addUsage ? : 0 17 17
85 17 return $self->_processCmdLine();
86 }
87  
88 #------------------------------------------------------------------------------
89 # Used for compatibility with old $ops format (array).
90 #------------------------------------------------------------------------------
91 sub _normalize($)
92 17 ▶{
Subroutine _normalize: called CC=7 SCAR=37.1
93 17 my ($ops) = @_;
94  
95 17 ▶ return $ops if( ref $ops eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ops eq "HASH" 17 0 17
96  
97 0 my %o;
98 0 foreach my $old ( @{$ops} )
99 {
100 0 my $op = $old->{'op'};
101 0 my $mod= $op;
102 0 $mod=~ s/^[^:=]+//;
103 0 $op =~ s/[:=].*$//;
104 0 my %oo;
105 0 ▶ $oo{'mod'} = $mod if( $mod ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $mod ne "" 0 0 0
106 0 ▶ $oo{'desc'} = $old->{'desc'} if( defined $old->{'desc'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"desc"} 0 0 0
107 0 ▶ $oo{'mand'} = $old->{'mand'} if( defined $old->{'mand'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"mand"} 0 0 0
108 0 ▶ $oo{'default'} = $old->{'default'} if( defined $old->{'default'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"default"} 0 0 0
109  
110 0 $o{$op} = \%oo;
111 }
112 0 return \%o;
113 }
114  
115 #------------------------------------------------------------------------------
116 #------------------------------------------------------------------------------
117 sub _addDefaultOptions
118 20 ▶{
Subroutine _addDefaultOptions: called CC=2 SCAR=7.1
119 20 my ($optDef) = @_;
120  
121 20 ▶ if( ! defined $$optDef->{'help'} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$optDef->{"help"}) 20 0 20
122 {
123 20 $$optDef->{help} = {desc=>'Print online docu.'};
124 }
125 }
126  
127 #------------------------------------------------------------------------------
128 # Return the value of an option.
129 #------------------------------------------------------------------------------
130 sub get($)
131 73 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
132 73 my ( $self, $key ) = @_;
133  
134 73 return $self->{$key};
135 }
136  
137 #------------------------------------------------------------------------------
138 # Set the value of an option.
139 #------------------------------------------------------------------------------
140 sub set($)
141 1 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
142 1 my ( $self, $key, $val ) = @_;
143  
144 1 my $old = $self->{$key};
145  
146 1 $self->{$key} = $val;
147  
148 1 return $old;
149 }
150  
151 #------------------------------------------------------------------------------
152 # Read options from command line and start checking of the options.
153 # Exit with 1 if GetOptions found an internal error.
154 #------------------------------------------------------------------------------
155 sub _processCmdLine($)
156 17 ▶{
Subroutine _processCmdLine: called CC=2 SCAR=7.0
157 17 my ( $self ) = @_;
158 17 my @opt = _mkOps( $self->{'opsDef'} );
159  
160 17 my $rc = GetOptions( $self, (@opt) );
161 17 ▶ $self->usage(), exit 1 if( ! $rc );
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 0 17 17
162  
163 17 $rc = $self->_checkOps();
164 17 return $rc;
165 }
166  
167 #------------------------------------------------------------------------------
168 # Print usage message if missing any mandatory option and exit.
169 # Call perldoc of main programm if option -help is found.
170 # Exit with 2 if a mandatory option is missing.
171 #------------------------------------------------------------------------------
172 sub _checkOps($)
173 17 ▶{
Subroutine _checkOps: called CC=7 SCAR=23.5
174 17 my ( $self ) = @_;
175  
176 17 my $rc=0;
177 17 my $errMsg;
178  
179 17 ▶ if( defined $self->{'help'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"help"}) 0 17 17
180 {
181 0 my $hasPerldoc = system("type perldoc >/dev/null 2>&1") / 256;
182 0 my $hasNroff = system("type nroff >/dev/null 2>&1") / 256;
183 0 ▶ if( $hasPerldoc == 0 && $hasNroff == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($hasPerldoc == 0 and $hasNroff == 0) { } 0 0 0
Condition0%0/3
$hasPerldoc == 0 and $hasNroff == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$hasPerldoc == 0 and $hasNroff == 0
$hasPerldoc == 0 $hasNroff == 0
Truth table0%0/3
A and B
A= $hasPerldoc == 0B= $hasNroff == 0
ABresult
0X0
100
111
184 {
185 #my $fh = new IO::File "perldoc $0 |";
186 #while( <$fh> ) { print STDERR $_; }
187 0 system("perldoc $0");
188 0 $rc = 1;
189 }else{
190 0 $errMsg .= "Can't display online manual. Missing nroff and/or perldoc.\n";
191 }
192 }
193  
194 17 foreach ( keys %{$self->{'opsDef'}} )
195 {
196 27 ▶ $errMsg .= "Missing mandatory option '$_'.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->setDefaults($_) 1 26 27
197 if( ! $self->setDefaults( $_ ));
198 }
199  
200 17 ▶ if( defined $errMsg )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $errMsg) 1 16 17
201 {
202 1 print STDERR $errMsg;
203 1 $self->usage();
204 1 $rc = 2;
205 }
206 17 return $rc;
207 }
208  
209 #------------------------------------------------------------------------------
210 # Set option to default value if option is missing and default value defined.
211 # Return 0 if option is mandatory, not set on comand line and no default value
212 # available. Otherwise return 1;
213 #------------------------------------------------------------------------------
214 sub setDefaults($$)
215 27 ▶{
Subroutine setDefaults: called CC=5 SCAR=16.1
Pod undocumented
216 27 my ($self, $opt) = @_;
217  
218 # We are happy, found the option on command line
219 27 ▶ return 1 if( defined $self->{$opt} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$opt} 2 25 27
220  
221 # Nothing to do, option not found on command line but not mandatory
222 25 ▶ return 1 if(! $self->{'opsDef'}{$opt}{'mand'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"opsDef"}{$opt}{"mand"} 18 7 25
223  
224 # WOW!! We found an error!
225 # Option not on comand line, mandatory and no default defined
226 return 0 if(!defined $self->{'opsDef'}{$opt}{'default'} &&
227 7 ▶ $self->{'opsDef'}{$opt}{'mand'} );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"opsDef"}{$opt}{"default"} and $self->{"opsDef"}{$opt}{"mand"} 1 6 7
Condition66%2/3
not defined $self->{"opsDef"}{$opt}{"default"} and $self->{"opsDef"}{$opt}{"mand"}
!ll&&!rl&&r
601
MC/DC50%1/2
not defined $self->{"opsDef"}{$opt}{"default"} and $self->{"opsDef"}{$opt}{"mand"}
not defined $self->{"opsDef"}{$opt}{"default"} $self->{"opsDef"}{$opt}{"mand"}
Truth table66%2/3
not(A) and B
A= not defined $self->{"opsDef"}{$opt}{"default"}B= $self->{"opsDef"}{$opt}{"mand"}
ABresult
0X0
100
111
228  
229 # Option not on comand line, mandatory and default defined
230 # -> so we can use the default value
231 6 $self->{$opt} = $self->{'opsDef'}{$opt}{'default'};
232 6 return 1;
233  
234  
235 }
236  
237 #------------------------------------------------------------------------------
238 # Print an usage message to STDERR.
239 #------------------------------------------------------------------------------
240 sub usage($$)
241 1 ▶{
Subroutine usage: called CC=3 SCAR=11.0
Pod undocumented
242 1 my ( $self, $addMsg ) = @_;
243  
244 1 my $call = $self->{'caller'}->[1];
245 1 $call =~ s/^.*\///;
246  
247 1 my $cols = _getCols(); my $col2 = $cols/2-6;
248 printf STDERR "\nUsage: %s <Options> %s\n%s %s %s\n",
249 $call,
250 1 $self->{'addUsage'},
251 '-' x $col2, 'Options', '-' x $col2;
252  
253 1 my ($form, $max) = _calcForm( $self->{'opsDef'} );
254  
255 1 foreach my $key ( sort keys %{$self->{'opsDef'}} )
256 {
257 2 my $val = $self->{'opsDef'}{$key};
258 2 printf STDERR "$form\n", _getOpDesc( $val, $max, $cols );
259 }
260 1 ▶ printf STDERR "%s\n%s\n", '-' x ($cols-3),
Branch: 1 branch
BranchTrueFalseTotal
defined $addMsg ? : 0 1 1
261 defined $addMsg ? "$addMsg\n" : "\n";
262 }
263  
264 #-----------------------------------------------------------------------------
265 # Compute the number of columns of then contoling terminal.
266 #-----------------------------------------------------------------------------
267 sub _getCols
268 1 ▶{
Subroutine _getCols: called CC=3 SCAR=11.4
269 1 my $line = `stty -a 2>/dev/null`;
270 1 ▶ return 80 if( !defined $line );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 1 1
271 1 $line =~ /(.*columns[^0-9]+)([0-9]+)/;
272 1 ▶ return defined $2 ? $2 : 80;
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 1 1
273 }
274  
275 #------------------------------------------------------------------------------
276 # Calculate format template for usage message.
277 #------------------------------------------------------------------------------
278 sub _calcForm($)
279 1 ▶{
Subroutine _calcForm: called CC=4 SCAR=13.9
280 1 my ( $ops ) = @_;
281  
282 1 _prepUsage( $ops );
283 1 my ($form, $max) = ( '', 0 );
284 1 foreach my $op ( values %{$ops} )
285 {
286 2 my $ln = length $op->{'usage'};
287 2 ▶ my $ad = _optionaly($op) ? 2 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&_optionaly($op) ? : 1 1 2
288  
289 2 ▶ $max = $ln+$ad > $max ? $ln+$ad : $max;
Branch: 1 branch
BranchTrueFalseTotal
$ln + $ad > $max ? : 2 0 2
290 }
291  
292 1 $form = "%${max}s - %s";
293 1 return ($form, $max+3);
294 }
295  
296 #------------------------------------------------------------------------------
297 # Prepare usage message using [] for optional options and <> for input values.
298 #------------------------------------------------------------------------------
299 sub _prepUsage($)
300 1 ▶{
Subroutine _prepUsage: called CC=3 SCAR=11.0
301 1 my ( $ops ) = @_;
302  
303 1 foreach my $op ( keys %{$ops} )
304 {
305 2 my $o = $ops->{$op}{'mod'};
306 2 ▶ $o = '' if( !defined $o );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 1 1 2
307 2 $o =~ s/=s.*/ <name>/;
308 2 $o =~ s/:s.*/ [<name>]/;
309 2 $o =~ s/=i.*/ <number>/;
310 2 $o =~ s/:i.*/ [<number>]/;
311 2 $o =~ s/=f.*/ <float>/;
312 2 $o =~ s/:f.*/ [<float>]/;
313  
314 2 $ops->{$op}{'usage'} = "-$op$o";
315 }
316 }
317 #------------------------------------------------------------------------------
318 # Build the description of an option.
319 #------------------------------------------------------------------------------
320 sub _getOpDesc($)
321 2 ▶{
Subroutine _getOpDesc: called CC=2 SCAR=6.9
322 2 my ( $op, $max, $cols ) = @_;
323  
324 2 my $rc;
325 2 ▶ if( _optionaly($op) )
Branch: 1 branch
BranchTrueFalseTotal
if (&_optionaly($op)) { } 1 1 2
326 {
327 1 $rc = '[' . $op->{'usage'} . ']';
328 }else{
329 1 $rc = $op->{'usage'};
330 }
331 2 my $desc = _insertNL( $op, $max, $cols );
332 2 return ( $rc, $desc );
333 }
334 #-----------------------------------------------------------------------------
335 # Return false if the option is madatory and has no default value.
336 # Return true otherwise.
337 #-----------------------------------------------------------------------------
338 sub _optionaly($)
339 4 ▶{
Subroutine _optionaly: called CC=4 SCAR=14.1
340 4 my ($op) = @_;
341  
342 4 ▶ if( defined $op->{'mand'})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $op->{"mand"}) 2 2 4
343 {
344 2 ▶ return 1 if( ! $op->{'mand'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $op->{"mand"} 0 2 2
345 2 ▶ return 0 if( ! defined $op->{'default'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op->{"default"} 2 0 2
346 }
347 2 return 1
348 }
349  
350  
351 #-----------------------------------------------------------------------------
352 # Fold line into two lines if line length exceeds number of columns .
353 #-----------------------------------------------------------------------------
354 sub _insertNL
355 2 ▶{
Subroutine _insertNL: called CC=5 SCAR=17.1
356 2 my ($op, $max, $cols) = @_;
357  
358 2 my $l='';
359 2 my $line='';
360 2 ▶ $op->{'desc'} = '--no description--' if( !defined $op->{'desc'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op->{"desc"} 0 2 2
361  
362 2 foreach my $x ( split /\s+/, $op->{'desc'} )
363 {
364 6 ▶ if( length($l) + length($x) + $max >= $cols )
Branch: 1 branch
BranchTrueFalseTotal
if (length($l) + length($x) + $max >= $cols) 0 6 6
365 {
366 0 $line .= sprintf "%s\n%s", $l, ' ' x $max;
367 0 $l = '';
368 }
369 6 $l .= "$x ";
370 }
371 2 $line .= $l;
372 $line .= sprintf "\n%s(default=%s)", ' ' x $max,$op->{'default'}
373 2 ▶ if( defined $op->{'default'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $op->{"default"} 0 2 2
374 2 return $line;
375 }
376  
377 #------------------------------------------------------------------------------
378 # Prepare the option hash for Getopt::Long::GetOptions().
379 #------------------------------------------------------------------------------
380 sub _mkOps()
381 17 ▶{
Subroutine _mkOps: called CC=3 SCAR=11.0
382 17 my ( $ops ) = @_;
383 17 my @OPS;
384  
385 my $mod;
386 17 foreach my $opt ( keys %{$ops} )
387 {
388 27 ▶ $mod = defined $ops->{$opt}{'mod'} ? $ops->{$opt}{'mod'} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $ops->{$opt}{'mod'} ? : 10 17 27
389 27 push @OPS, $opt . $mod;
390 }
391 17 return @OPS;
392 }
393 ##############################################################################
394 21 1;
395 __END__
« blib/lib/Script/mytest.pl ↑ Summary blib/lib/Script/Toolbox/Util/Formatter.pm »