blib/lib/CBSSports/Getopt.pm

filter: Statementstmt 66.3% 67 / 101 Branchbran 42.1% 16 / 38 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 70.5% 12 / 17 Podpod 100% 3 / 3 totaltotal 58.1% 100 / 172 CC 27 SCAR 23.6
CC
27
Cov
58%
_filter_options
20.3
Usage
20.0
_merge_config_file_options
17.8
CRAP 10.6
↑ Summary
1 package CBSSports::Getopt;
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Getopt::Long qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 our @EXPORT = qw(GetOptions Usage Configure);
8 3 our $VERSION = '1.1';
9  
10 3 our $PRESET_OPTIONS = {
11 h => 'help',
12 help => 'help',
13 v => 'verbose',
14 verbose => 'verbose',
15 H => 'man',
16 man => 'man',
17 version => 'version',
18 };
19  
20 3 our $ALLOW_PRESET_OVERRIDE = 0;
21  
22 6 ▶sub GetOptions {
Subroutine GetOptions: called CC=4 SCAR=14.0
Pod documented
23 6 my (@option_args) = @_;
24 6 my %opts = ();
25 6 _merge_config_file_options();
26 6 Getopt::Long::Configure( 'no_auto_abbrev', 'no_ignore_case', 'bundling' );
27 6 ▶ Getopt::Long::GetOptions( _filter_options( \%opts, \@option_args ) ) || Usage( verbose => 0 );
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptions(_filter_options(\%opts, \@option_args)) or Usage("verbose", 0) 6 0 6
28 6 ▶ _print_version() if $opts{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"version"} 0 6 6
29 6 _clean_options( \%opts );
30 6 ▶ return wantarray ? %opts : \%opts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 5 6
31 }
32  
33 0 ▶sub Usage {
Subroutine Usage: not called CC=4 SCAR=30.0
Pod documented
34 0 my %args = @_;
35 0 ▶ print $args{message}, "\n" if $args{message};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"message"} 0 0 0
36 require Pod::Usage && Pod::Usage::pod2usage(
37 0 ▶ '-verbose' => $args{verbose} || 99,
Branch: 1 branch
BranchTrueFalseTotal
require Pod::Usage and Pod::Usage::pod2usage("-verbose", $args{"verbose"} || 99, "-sections", "(?i:(Usage|Options))", "-exitval", 0) 0 0 0
Condition0%0/2
$args{'verbose'} || 99
l!l
00
MC/DC0%0/1
$args{'verbose'} || 99
$args{'verbose'}
Truth table0%0/2
A
A= $args{'verbose'}
Aresult
11
00
38 '-sections' => '(?i:(Usage|Options))',
39 '-exitval' => 0,
40 );
41 0 return;
42 }
43  
44 0 ▶sub Configure {
Subroutine Configure: not called CC=1 SCAR=6.9
Pod documented
45 0 my @config = @_;
46 0 $ALLOW_PRESET_OVERRIDE = scalar grep { $_ eq 'allow_preset_override' } @_;
47 0 return Getopt::Long::Configure( grep { $_ ne 'allow_preset_override' } @_ );
48 }
49  
50 6 ▶sub _clean_options {
Subroutine _clean_options: called CC=3 SCAR=11.0
51 6 my ( $opts ) = @_;
52 6 for my $option ( keys %$opts ) {
53 15 ▶ delete $opts->{$option} unless defined $opts->{$option}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{$option} 12 3 15
54 }
55 6 return;
56 }
57  
58 0 ▶sub _print_version {
Subroutine _print_version: not called CC=2 SCAR=17.9
59 0 my $script = basename($0);
60 0 ▶ my $version = $main::VERSION ? ( 'v' . $main::VERSION ) : '(unknown version)';
Branch: 1 branch
BranchTrueFalseTotal
$main::VERSION ? : 0 0 0
61 0 print "$script $version\n";
62 0 exit 1;
63 }
64  
65 6 ▶sub _merge_config_file_options {
Subroutine _merge_config_file_options: called CC=5 SCAR=28.8
66 6 my $config_file = _default_config();
67 6 ▶ if ( -e $config_file && open( my $fh, '<', $config_file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $config_file and open my $fh, "<", $config_file) 0 6 6
Condition33%1/3
-e $config_file and open my $fh, "<", $config_file
!ll&&!rl&&r
600
MC/DC0%0/2
-e $config_file and open my $fh, "<", $config_file
-e $config_file open my $fh, "<", $config_file
Truth table33%1/3
A and B
A= -e $config_fileB= open my $fh, "<", $config_file
ABresult
0X0
100
111
68 0 my @options = ();
69 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
70 0 $line =~ s/\#.+$//;
71 0 $line =~ s/^\s+//;
72 0 $line =~ s/\s+$//;
73 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
74 0 push @options, split( /\s+/, $line );
75 }
76 0 close $fh;
77 0 unshift @ARGV, @options;
78 }
79 }
80  
81 6 ▶sub _default_config {
Subroutine _default_config: called CC=2 SCAR=8.4
82 6 my $script = basename($0);
83 6 $script =~ s/\.pl$//;
84 6 ▶ require File::HomeDir && return File::HomeDir->my_home . '/.' . $script . 'rc';
Branch: 1 branch
BranchTrueFalseTotal
require File::HomeDir and return "File::HomeDir"->my_home . '/.' . $script . 'rc' 6 0 6
85 0 warn q|Can't load File::HomeDir|;
86 0 return;
87 }
88  
89 6 ▶sub _filter_options {
Subroutine _filter_options: called CC=11 SCAR=30.1
90 6 my ( $opts, $option_args ) = @_;
91 6 my $getopt_long_options = {};
92  
93 6 my $found_presets = {};
94 6 my $illegal_preset_count = 0;
95 6 for my $opt (@$option_args) {
96 3 my ( $option_lookup, $hash_key ) = _cleanup_options($opt);
97 3 for my $option ( keys %$option_lookup ) {
98 6 ▶ if ( $PRESET_OPTIONS->{$option} && !$ALLOW_PRESET_OVERRIDE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($CBSSports::Getopt::PRESET_OPTIONS->{$option} and not $CBSSports::Getopt::ALLOW_PRESET_OVERRIDE) { } 0 6 6
elsif ($CBSSports::Getopt::PRESET_OPTIONS->{$option}) { } 0 6 6
Condition33%1/3
$CBSSports::Getopt::PRESET_OPTIONS->{$option} and not $CBSSports::Getopt::ALLOW_PRESET_OVERRIDE
!ll&&!rl&&r
600
MC/DC0%0/2
$CBSSports::Getopt::PRESET_OPTIONS->{$option} and not $CBSSports::Getopt::ALLOW_PRESET_OVERRIDE
$CBSSports::Getopt::PRESET_OPTIONS->{$option} not $CBSSports::Getopt::ALLOW_PRESET_OVERRIDE
Truth table33%1/3
A and not(B)
A= $CBSSports::Getopt::PRESET_OPTIONS->{$option}B= not $CBSSports::Getopt::ALLOW_PRESET_OVERRIDE
ABresult
0X0
100
111
99 0 print "By default, you may not override preset option '$option'\n"
100 . "To enable preset overriding use \"Configure( 'allow_preset_override' );\"\n";
101 0 $illegal_preset_count++;
102 0 next;
103 }
104 elsif ( $PRESET_OPTIONS->{$option} ) {
105 0 $found_presets->{ $PRESET_OPTIONS->{$option} }++;
106 }
107 6 $getopt_long_options->{$opt} = \$opts->{$hash_key};
108 }
109 }
110  
111 6 ▶ if ( $illegal_preset_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($illegal_preset_count) 0 6 6
112 0 print "\n";
113 0 exit;
114 }
115  
116 0 ▶ $getopt_long_options->{'h|help'} = sub { Usage( verbose => 0 ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 6 ▶ unless $found_presets->{help};
Branch: 1 branch
BranchTrueFalseTotal
unless $found_presets->{"help"} 6 0 6
118 0 ▶ $getopt_long_options->{'H|man'} = sub { Usage( verbose => 2 ) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 6 ▶ unless $found_presets->{man};
Branch: 1 branch
BranchTrueFalseTotal
unless $found_presets->{"man"} 6 0 6
120 6 ▶ $getopt_long_options->{'v|verbose+'} = \$opts->{verbose} unless $found_presets->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $found_presets->{"verbose"} 6 0 6
121 6 ▶ $getopt_long_options->{'version'} = \$opts->{version} unless $found_presets->{version};
Branch: 1 branch
BranchTrueFalseTotal
unless $found_presets->{"version"} 6 0 6
122  
123 6 return %$getopt_long_options;
124 }
125  
126 3 ▶sub _cleanup_options {
Subroutine _cleanup_options: called CC=3 SCAR=11.3
127 3 my ( $opt ) = @_;
128 3 my $option_lookup = {};
129 3 my ( $option, @modifier ) = split( /([\!\+\=\:])/, $opt );
130 3 my $modifier = join( '', grep { $_ } @modifier );
131  
132 3 ▶ if ( length( $option ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $option == 1) 0 3 3
133 0 print "Short option definition '$option' is invalid.\n"
134 . "A descriptive long option is required when defining short option (ie 'h|help')\n\n";
135 0 exit;
136 }
137  
138 3 my @alt_options = split /\|/, $option;
139 3 $option_lookup->{$_} = 1 for @alt_options;
140  
141 3 return ( $option_lookup, _determine_key( \@alt_options ) );
142 }
143  
144 3 ▶sub _determine_key {
Subroutine _determine_key: called CC=1 SCAR=0.0
145 3 my ( $alt_options ) = @_;
146 3 my @keys = sort { length($b) <=> length($a) } @$alt_options;
147 3 my $key = $keys[0];
148 3 $key =~ s/\-/\_/g;
149 3 return $key;
150 }
151  
152 3 1;
153  
154 __END__
↑ Summary