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

filter: Statementstmt 86.2% 150 / 174 Branchbran 65.3% 51 / 78 Conditioncond 66.6% 10 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 23 / 23 Podpod 0.0% 0 / 5 totaltotal 77.5% 235 / 303 SCAR 43.9
CC
54
Cov
79%
_normalize
40.9
_checkOps
15.3
_invalidOptDef
9.0
CRAP 80.9
« blib/lib/Script/Toolbox/Util/Formatter.pm ↑ Summary blib/lib/Script/Toolbox/TableO.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 #$VERSION = '';
17  
18  
19 # Preloaded methods go here.
20  
21 # Autoload methods go after =cut, and are processed by the autosplit program.
22  
23 #------------------------------------------------------------------------------
24 # Create a new Opt object.
25 #------------------------------------------------------------------------------
26 sub new
27 {
28 20 ▶ my $classname = shift;
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
29 20 my $optDef = shift; # options definition
30 20 my $caller = shift; # may be omited
31  
32 20 ▶ $optDef = {} if( !defined $optDef );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $optDef 7 13 20
33 20 ▶ $optDef = {} if( ref $optDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $optDef ne "HASH" 0 20 20
34 20 ▶ $optDef = {} if( scalar keys %{$optDef} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$optDef;} == 0 7 13 20
35 20 _addDefaultOptions( \$optDef );
36 20 ▶ return undef if( _invalidOptDef( $optDef ));
Branch: 1 branch
BranchTrueFalseTotal
if &_invalidOptDef($optDef) 3 17 20
37 17 my $self = {};
38 17 bless( $self, $classname );
39 17 $self->_instCaller($caller);
40  
41 17 my $rc = $self->_init( $optDef, @_ );
42 17 ▶ exit $rc if( $rc != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $rc != 0 1 16 17
43  
44 16 return $self;
45 }
46  
47 #------------------------------------------------------------------------------
48 #------------------------------------------------------------------------------
49 sub _invalidOptDef($)
50 {
51 20 ▶ my ($optDef) = @_;
Subroutine _invalidOptDef: called CC=9 SCAR=22.0
52  
53 20 foreach my $val ( values %{$optDef} )
54 {
55 32 ▶ return 1 if( ref $val ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $val ne "HASH" 1 31 32
56 31 ▶ return 1 if( scalar keys %{$val} == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %{$val;} == 0 2 29 31
57 29 foreach my $key ( keys %{$val} )
58 {
59 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
60 $key ne 'mand' && $key ne 'default' );
61 }
62 }
63 17 return 0;
64 }
65  
66 #------------------------------------------------------------------------------
67 #------------------------------------------------------------------------------
68 sub _instCaller(@)
69 {
70 17 ▶ my ($self, $call) = @_;
Subroutine _instCaller: called CC=2 SCAR=6.9
71  
72 17 my @caller = caller();
73 17 ▶ $self->{'caller'} = defined $call ? $call : \@caller;
Branch: 1 branch
BranchTrueFalseTotal
defined $call ? : 16 1 17
74 }
75  
76 #------------------------------------------------------------------------------
77 # Get the options definition as input. Read the options from command line.
78 # Write usage message to STDERR if missing mandatory options.
79 #------------------------------------------------------------------------------
80 sub _init()
81 {
82 17 ▶ my ( $self, $ops, $addUsage ) = @_;
Subroutine _init: called CC=2 SCAR=7.0
83  
84 17 $self->{'opsDef'} = _normalize($ops);
85 17 ▶ $self->{'addUsage'} = defined $addUsage ? $addUsage :''; # additional usage text
Branch: 1 branch
BranchTrueFalseTotal
defined $addUsage ? : 0 17 17
86 17 return $self->_processCmdLine();
87 }
88  
89 #------------------------------------------------------------------------------
90 # Used for compatibility with old $ops format (array).
91 #------------------------------------------------------------------------------
92 sub _normalize($)
93 {
94 17 ▶ my ($ops) = @_;
Subroutine _normalize: called CC=7 SCAR=37.1
95  
96 17 ▶ return $ops if( ref $ops eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ops eq "HASH" 17 0 17
97  
98 0 my %o;
99 0 foreach my $old ( @{$ops} )
100 {
101 0 my $op = $old->{'op'};
102 0 my $mod= $op;
103 0 $mod=~ s/^[^:=]+//;
104 0 $op =~ s/[:=].*$//;
105 0 my %oo;
106 0 ▶ $oo{'mod'} = $mod if( $mod ne '' );
Branch: 1 branch
BranchTrueFalseTotal
if $mod ne "" 0 0 0
107 0 ▶ $oo{'desc'} = $old->{'desc'} if( defined $old->{'desc'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"desc"} 0 0 0
108 0 ▶ $oo{'mand'} = $old->{'mand'} if( defined $old->{'mand'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"mand"} 0 0 0
109 0 ▶ $oo{'default'} = $old->{'default'} if( defined $old->{'default'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $old->{"default"} 0 0 0
110  
111 0 $o{$op} = \%oo;
112 }
113 0 return \%o;
114 }
115  
116 #------------------------------------------------------------------------------
117 #------------------------------------------------------------------------------
118 sub _addDefaultOptions
119 {
120 20 ▶ my ($optDef) = @_;
Subroutine _addDefaultOptions: called CC=2 SCAR=7.1
121  
122 20 ▶ if( ! defined $$optDef->{'help'} )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$optDef->{"help"}) 20 0 20
123 {
124 20 $$optDef->{help} = {desc=>'Print online docu.'};
125 }
126 }
127  
128 #------------------------------------------------------------------------------
129 # Return the value of an option.
130 #------------------------------------------------------------------------------
131 sub get($)
132 {
133 73 ▶ my ( $self, $key ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
134  
135 73 return $self->{$key};
136 }
137  
138 #------------------------------------------------------------------------------
139 # Set the value of an option.
140 #------------------------------------------------------------------------------
141 sub set($)
142 {
143 1 ▶ my ( $self, $key, $val ) = @_;
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
144  
145 1 my $old = $self->{$key};
146  
147 1 $self->{$key} = $val;
148  
149 1 return $old;
150 }
151  
152 #------------------------------------------------------------------------------
153 # Read options from command line and start checking of the options.
154 # Exit with 1 if GetOptions found an internal error.
155 #------------------------------------------------------------------------------
156 sub _processCmdLine($)
157 {
158 17 ▶ my ( $self ) = @_;
Subroutine _processCmdLine: called CC=2 SCAR=7.0
159 17 my @opt = _mkOps( $self->{'opsDef'} );
160  
161 17 my $rc = GetOptions( $self, (@opt) );
162 17 ▶ $self->usage(), exit 1 if( ! $rc );
Branch: 1 branch
BranchTrueFalseTotal
unless $rc 0 17 17
163  
164 17 $rc = $self->_checkOps();
165 17 return $rc;
166 }
167  
168 #------------------------------------------------------------------------------
169 # Print usage message if missing any mandatory option and exit.
170 # Call perldoc of main programm if option -help is found.
171 # Exit with 2 if a mandatory option is missing.
172 #------------------------------------------------------------------------------
173 sub _checkOps($)
174 {
175 17 ▶ my ( $self ) = @_;
Subroutine _checkOps: called CC=8 SCAR=27.3
176  
177 17 my $rc=0;
178 17 my $errMsg;
179  
180 17 ▶ if( defined $self->{'help'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"help"}) 0 17 17
181 {
182 0 my $hasPerldoc = system("type perldoc >/dev/null 2>&1") / 256;
183 0 my $hasNroff = system("type nroff >/dev/null 2>&1") / 256;
184 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
185 {
186 0 my $fh = new IO::File "perldoc $0 |";
187 0 ▶ while( <$fh> ) { print STDERR $_; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 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 {
216 27 ▶ my ($self, $opt) = @_;
Subroutine setDefaults: called CC=5 SCAR=16.1
Pod undocumented
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 {
242 1 ▶ my ( $self, $addMsg ) = @_;
Subroutine usage: called CC=3 SCAR=11.0
Pod undocumented
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 {
269 1 ▶ my $line = `stty -a 2>/dev/null`;
Subroutine _getCols: called CC=3 SCAR=11.4
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 {
280 1 ▶ my ( $ops ) = @_;
Subroutine _calcForm: called CC=4 SCAR=13.9
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 ? : 1 1 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 {
301 1 ▶ my ( $ops ) = @_;
Subroutine _prepUsage: called CC=3 SCAR=11.0
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 {
322 2 ▶ my ( $op, $max, $cols ) = @_;
Subroutine _getOpDesc: called CC=2 SCAR=6.9
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 {
340 4 ▶ my ($op) = @_;
Subroutine _optionaly: called CC=4 SCAR=14.1
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 {
356 2 ▶ my ($op, $max, $cols) = @_;
Subroutine _insertNL: called CC=5 SCAR=17.1
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 {
382 17 ▶ my ( $ops ) = @_;
Subroutine _mkOps: called CC=3 SCAR=11.0
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/Toolbox/Util/Formatter.pm ↑ Summary blib/lib/Script/Toolbox/TableO.pm »