blib/lib/Util/CommandLine.pm

filter: Statementstmt 51.4% 36 / 70 Branchbran 9.0% 2 / 22 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 76.9% 10 / 13 Podpod 100% 3 / 3 totaltotal 38.0% 51 / 134 CC 20 SCAR 43.8
CC
20
Cov
35%
options
182.0
singleton
30.0
import
5.5
CRAP 79.7
↑ Summary
1 package Util::CommandLine;
2 # ABSTRACT: Command-line interface helper utility
3  
4 2 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Getopt::Long 'GetOptions';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Pod::Usage 'pod2usage';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Proc::PID::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Term::ReadKey 'ReadMode';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '1.10'; # VERSION
14  
15 2 ▶use constant EXPORT_OK => [ qw( options pod2usage singleton readmode ) ];
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶sub import {
Subroutine import: called CC=4 SCAR=17.1
18 1 my $self = shift;
19  
20 # determine the caller and setup the exports hash
21 1 my $callpkg = caller();
22 0 my %exports = map { $_ => 1 } grep {
23 0 my $x = $_;
24 0 grep { $_ eq $x } @{(EXPORT_OK)};
25 } @_;
26  
27 # method injection as appropriate
28 {
29 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 0 *{"$callpkg\::$_"} = \&{"$self\::$_"} for ( keys %exports );
31 }
32  
33 0 ▶ singleton() if ( grep { $_ eq 'singleton' } @_ );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "singleton";} @_ 0 1 1
34 0 ▶ options() if ( grep { $_ eq 'podhelp' } @_ );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "podhelp";} @_ 0 1 1
35  
36 1 return;
37 }
38  
39 0 ▶sub singleton {
Subroutine singleton: not called CC=5 SCAR=34.0
Pod documented
40 my @dirs = (
41 '/var/run',
42 '/tmp',
43 $ENV{HOME},
44 0 '.',
45 '/',
46 );
47  
48 0 my $singleton;
49 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
not $singleton and @dirs and do { eval { do { if ("Proc::PID::File"->running({"dir", shift @dirs})) { warn "Running as singleton; forcing exit of $0\n"; exit 1; } ; $singleton = 1 } } } 0 0 0
Condition0%0/3
not $singleton and @dirs
!ll&&!rl&&r
000
MC/DC0%0/2
not $singleton and @dirs
not $singleton @dirs
Truth table0%0/3
not(A) and B
A= not $singletonB= @dirs
ABresult
0X0
100
111
50 0 ▶ if ( Proc::PID::File->running({ dir => shift @dirs }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Proc::PID::File"->running({"dir", shift @dirs})) 0 0 0
51 0 warn "Running as singleton; forcing exit of $0\n";
52 0 exit 1;
53 }
54 0 $singleton = 1;
55 } while ( not $singleton and @dirs );
56  
57 0 ▶ die "Unable to establish PID file for singleton functionality\n" unless ($singleton);
Branch: 1 branch
BranchTrueFalseTotal
unless $singleton 0 0 0
58  
59 0 return;
60 }
61  
62 0 ▶sub options {
Subroutine options: not called CC=13 SCAR=52.0
Pod documented
63 0 ▶ shift if ( index( ( $_[0] || '' ), '::' ) != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($_[0] || "", "::") != -1) 0 0 0
Condition0%0/2
$_[0] || ''
l!l
00
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
64  
65 0 my $settings = {};
66 GetOptions(
67 map {
68 0 ▶ if (/\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\{/u) { } 0 0 0
69 0 $settings->{ ( split(/[|=]/) )[0] } = [];
70 0 $_ => $settings->{ ( split(/[|=]/) )[0] };
71 }
72 else {
73 0 $_ => \$settings->{ ( split(/[|=]/) )[0] };
74 }
75 0 ▶ } map { split(/\s+/) } @_, qw( help man )
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(map({if (/\{/u) { $$settings{(split(/[|=]/u, $_, 0))[0]} = []; $_, $$settings{(split(/[|=]/u, $_, 0))[0]}; } else { $_, \$$settings{(split(/[|=]/u, $_, 0))[0]}; } ;} map({split(/\s+/u, $_, 0);} @_, ("help", "man")))) or pod2usage(0) 0 0 0
76 ) || pod2usage(0);
77  
78 0 for ( keys %$settings ) {
79 delete $settings->{$_} if (
80 not defined $settings->{$_} or
81 (
82 ref $settings->{$_} eq 'ARRAY' and (
83 0 ▶ not @{ $settings->{$_} } or
Condition0%0/3
@{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
@{$settings->{$_};} == 1 $settings->{$_}[0] eq ""
Truth table0%0/3
A and B
A= @{$settings->{$_};} == 1B= $settings->{$_}[0] eq ""
ABresult
0X0
100
111
84 (
85 0 @{ $settings->{$_} } == 1 and
86 0 ▶ $settings->{$_}[0] eq ''
Branch: 1 branch
BranchTrueFalseTotal
if not defined $settings->{$_} or ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq "" 0 0 0
Condition0%0/9
not defined $settings->{$_} or ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq ""
l!l&&r!l&&!r
000
ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq ""
!ll&&!rl&&r
000
not @{$settings->{$_};} or @{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
l!l&&r!l&&!r
000
MC/DC0%0/4
not defined $settings->{$_} or ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq ""
not defined $settings->{$_} ref $settings->{$_} eq "ARRAY" not @{$settings->{$_};} @{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
Truth table0%0/5
not(A) or B and not(C) or D
A= not defined $settings->{$_}B= ref $settings->{$_} eq "ARRAY"C= not @{$settings->{$_};}D= @{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
ABCDresult
1XXX1
011X1
01011
00XX0
01000
87 )
88 )
89 )
90 );
91 }
92  
93 0 ▶ pod2usage( '-exitstatus' => 1, '-verbose' => 1 ) if ( $settings->{'help'} );
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"help"} 0 0 0
94 0 ▶ pod2usage( '-exitstatus' => 0, '-verbose' => 2 ) if ( $settings->{'man'} );
Branch: 1 branch
BranchTrueFalseTotal
if $settings->{"man"} 0 0 0
95  
96 0 return $settings;
97 }
98  
99 0 ▶sub readmode {
Subroutine readmode: not called CC=1 SCAR=6.9
Pod documented
100 0 return ReadMode(@_);
101 }
102  
103 2 1;
104  
105 __END__
↑ Summary