line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Util::CommandLine; |
2
|
|
|
|
|
|
|
# ABSTRACT: Command-line interface helper utility |
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
129513
|
use 5.008; |
|
1
|
|
|
|
|
12
|
|
5
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
780
|
use Getopt::Long 'GetOptions'; |
|
1
|
|
|
|
|
10810
|
|
|
1
|
|
|
|
|
4
|
|
9
|
1
|
|
|
1
|
|
894
|
use Pod::Usage 'pod2usage'; |
|
1
|
|
|
|
|
50870
|
|
|
1
|
|
|
|
|
81
|
|
10
|
1
|
|
|
1
|
|
511
|
use Proc::PID::File; |
|
1
|
|
|
|
|
2242
|
|
|
1
|
|
|
|
|
64
|
|
11
|
1
|
|
|
1
|
|
534
|
use Term::ReadKey 'ReadMode'; |
|
1
|
|
|
|
|
2141
|
|
|
1
|
|
|
|
|
77
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '1.05'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
7
|
use constant EXPORT_OK => [ qw( options pod2usage singleton readmode ) ]; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
181
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub import { |
18
|
1
|
|
|
1
|
|
13
|
my $self = shift; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# determine the caller and setup the exports hash |
21
|
1
|
|
|
|
|
3
|
my $callpkg = caller(); |
22
|
0
|
|
|
|
|
0
|
my %exports = map { $_ => 1 } grep { |
23
|
1
|
|
|
|
|
21
|
my $x = $_; |
|
0
|
|
|
|
|
0
|
|
24
|
0
|
|
|
|
|
0
|
grep { $_ eq $x } @{(EXPORT_OK)}; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
25
|
|
|
|
|
|
|
} @_; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# method injection as appropriate |
28
|
|
|
|
|
|
|
{ |
29
|
1
|
|
|
1
|
|
10
|
no strict 'refs'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
657
|
|
|
1
|
|
|
|
|
3
|
|
30
|
1
|
|
|
|
|
4
|
*{"$callpkg\::$_"} = \&{"$self\::$_"} for ( keys %exports ); |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
1
|
50
|
|
|
|
8
|
singleton() if ( grep { $_ eq 'singleton' } @_ ); |
|
0
|
|
|
|
|
0
|
|
34
|
1
|
50
|
|
|
|
5
|
options() if ( grep { $_ eq 'podhelp' } @_ ); |
|
0
|
|
|
|
|
0
|
|
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
12
|
return; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub singleton { |
40
|
|
|
|
|
|
|
my @dirs = ( |
41
|
|
|
|
|
|
|
'/var/run', |
42
|
|
|
|
|
|
|
'/tmp', |
43
|
|
|
|
|
|
|
$ENV{HOME}, |
44
|
0
|
|
|
0
|
1
|
|
'.', |
45
|
|
|
|
|
|
|
'/', |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
my $singleton; |
49
|
0
|
|
0
|
|
|
|
eval { |
50
|
0
|
0
|
|
|
|
|
if ( Proc::PID::File->running({ dir => shift @dirs }) ) { |
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
|
0
|
|
|
|
|
die "Unable to establish PID file for singleton functionality\n" unless ($singleton); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
return; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub options { |
63
|
0
|
0
|
0
|
0
|
1
|
|
shift if ( index( ( $_[0] || '' ), '::' ) != -1 ); |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my $settings = {}; |
66
|
|
|
|
|
|
|
GetOptions( |
67
|
|
|
|
|
|
|
map { |
68
|
0
|
0
|
|
|
|
|
if (/\{/) { |
69
|
0
|
|
|
|
|
|
$settings->{ ( split(/[|=]/) )[0] } = []; |
70
|
0
|
|
|
|
|
|
$_ => $settings->{ ( split(/[|=]/) )[0] }; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
0
|
|
|
|
|
|
$_ => \$settings->{ ( split(/[|=]/) )[0] }; |
74
|
|
|
|
|
|
|
} |
75
|
0
|
0
|
|
|
|
|
} map { split(/\s+/) } @_, qw( help man ) |
|
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
|
|
|
|
|
|
|
not @{ $settings->{$_} } or |
84
|
|
|
|
|
|
|
( |
85
|
|
|
|
|
|
|
@{ $settings->{$_} } == 1 and |
86
|
0
|
0
|
0
|
|
|
|
$settings->{$_}[0] eq '' |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
87
|
|
|
|
|
|
|
) |
88
|
|
|
|
|
|
|
) |
89
|
|
|
|
|
|
|
) |
90
|
|
|
|
|
|
|
); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
pod2usage( '-exitstatus' => 1, '-verbose' => 1 ) if ( $settings->{'help'} ); |
94
|
0
|
0
|
|
|
|
|
pod2usage( '-exitstatus' => 0, '-verbose' => 2 ) if ( $settings->{'man'} ); |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $settings; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub readmode { |
100
|
0
|
|
|
0
|
1
|
|
return ReadMode(@_); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |