| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | options() if ( grep { $_ eq 'podhelp' } @_ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
not $singleton and @dirs
MC/DC0%0/2
not $singleton and @dirs
not $singleton
@dirs
Truth table0%0/3
not(A) and B
A= not $singletonB= @dirs
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( Proc::PID::File->running({ dir => shift @dirs }) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$_[0] || ''
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table0%0/2
A
A= $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $settings = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | GetOptions( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if (/\{/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | @{ $settings->{$_} } == 1 and | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $settings->{$_}[0] eq '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
not defined $settings->{$_} or ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq ""
ref $settings->{$_} eq "ARRAY" and !@{$settings->{$_};} || @{$settings->{$_};} == 1 && $settings->{$_}[0] eq ""
not @{$settings->{$_};} or @{$settings->{$_};} == 1 and $settings->{$_}[0] eq ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | pod2usage( '-exitstatus' => 1, '-verbose' => 1 ) if ( $settings->{'help'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | pod2usage( '-exitstatus' => 0, '-verbose' => 2 ) if ( $settings->{'man'} ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||