blib/lib/Getopt/Guided.pm

filter: Statementstmt 100% 137 / 137 Branchbran 97.6% 84 / 86 Conditioncond 88.0% 22 / 25 MC/DCmcdc 75.0% 12 / 16 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 7 totaltotal 94.4% 270 / 286 CC 56 SCAR 25.8
CC
56
Cov
98%
getopts
22.0
processopts
17.0
readopts
8.0
CRAP 13.2
↑ Summary Makefile.PL »
1 # Prefer numeric version for backwards compatibility
2 16 ▶BEGIN { require 5.010000 }; ## no critic ( RequireUseStrict, RequireUseWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #<<<
7 package Getopt::Guided;
8 BEGIN {
9 16 ▶our $VERSION = 'v3.3.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 }
11 #>>>
12  
13 # Options Delimiter constant
14 sub OD () { '-' }
15 # End Of Options Delimiter constant
16 sub EOOD () { '--' }
17 # Flag Indicator Character Class constant
18 sub FICC () { '[!+]' }
19 # Option-Argument Indicator Character Class constant
20 sub OAICC () { '[,:=]' }
21 # Perl boolean true constant ( IV == 1 )
22 sub TRUE () { !!1 }
23 # Perl boolean false constant
24 sub FALSE () { !!0 }
25 # Common exit status constants
26 sub EXIT_SUCCESS () { 0 }
27 sub EXIT_FAILURE () { 1 }
28 sub EXIT_USAGE () { 2 }
29  
30 16 @Getopt::Guided::EXPORT_OK =
31 qw( EOOD EXIT_SUCCESS EXIT_FAILURE EXIT_USAGE getopts processopts readopts print_version_info );
32  
33 sub program_name () {
34 18 ▶ require File::Basename;
Subroutine program_name: called CC=1 SCAR=0.0
Pod undocumented
35 18 File::Basename::basename( $0 )
36 }
37  
38 sub croakf ( $@ ) {
39 13 ▶ @_ = ( ( @_ == 1 ? shift : sprintf shift, @_ ) . ', stopped' );
Subroutine croakf: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 12 13
40 13 require Carp;
41 13 goto &Carp::croak
42 }
43  
44 sub import {
45 16 ▶ my $module = shift;
Subroutine import: called CC=3 SCAR=11.0
46  
47 16 our @EXPORT_OK;
48 16 my $target = caller;
49 16 for my $function ( @_ ) {
50 croakf "%s: '%s' is not exported", $module, $function
51 41 ▶ unless grep { $function eq $_ } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$function eq $_;} @Getopt::Guided::EXPORT_OK 1 40 41
52 16 ▶ no strict 'refs'; ## no critic ( ProhibitNoStrict )
Subroutine BEGIN: called CC=1 SCAR=0.0
53 40 *{ "$target\::$function" } = $module->can( $function )
54 }
55 }
56  
57 # Implementation is based on m//gc with \G
58 sub parse_spec ( $;\%$ ) {
59 79 ▶ my ( $spec, $spec_as_hash, $spec_length_expected ) = @_;
Subroutine parse_spec: called CC=8 SCAR=20.8
Pod undocumented
60  
61 79 ▶ $spec_as_hash = {} unless defined $spec_as_hash;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec_as_hash 42 37 79
62  
63 79 my $spec_length_got;
64 16 ▶ no warnings qw( uninitialized ); ## no critic ( ProhibitNoWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
65 79 ▶ while ( $spec =~ m/\G ( [[:alnum:]] ) ( ${ \( FICC ) } | ${ \( OAICC ) } | )/gcox ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\G ( [[:alnum:]] ) ( ${\"[!+]";} | ${\"[,:=]";} | )/cgox) 147 78 225
66 147 my ( $name, $indicator ) = ( $1, $2 );
67 croakf "%s parameter contains option '%s' multiple times", '$spec', $name
68 147 ▶ if exists $spec_as_hash->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
if exists $spec_as_hash->{$name} 1 146 147
69 146 $spec_as_hash->{ $name } = $indicator;
70 146 ++$spec_length_got
71 }
72 78 my $offset = pos $spec;
73 78 ▶ croakf "%s parameter isn't a non-empty string of alphanumeric characters", '$spec'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset and $offset == length $spec 6 72 78
Condition100%3/3
defined $offset and $offset == length $spec
!ll&&!rl&&r
5172
MC/DC100%2/2
defined $offset and $offset == length $spec
defined $offset $offset == length $spec
Truth table100%3/3
A and B
A= defined $offsetB= $offset == length $spec
ABresult
0X0
100
111
74 unless defined $offset and $offset == length $spec;
75 72 ▶ croakf '%s parameter specifies %d options (expected: %d)', '$spec', $spec_length_got, $spec_length_expected
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec_length_expected and $spec_length_got != $spec_length_expected 1 71 72
Condition100%3/3
defined $spec_length_expected and $spec_length_got != $spec_length_expected
!ll&&!rl&&r
36351
MC/DC100%2/2
defined $spec_length_expected and $spec_length_got != $spec_length_expected
defined $spec_length_expected $spec_length_got != $spec_length_expected
Truth table100%3/3
A and B
A= defined $spec_length_expectedB= $spec_length_got != $spec_length_expected
ABresult
0X0
100
111
76 if defined $spec_length_expected and $spec_length_got != $spec_length_expected;
77  
78 71 $spec_as_hash
79 }
80  
81 # https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html#tag_12_02>
82 sub getopts ( $\%;\@ ) {
83 61 ▶ my ( $spec, $opts, $argv ) = @_;
Subroutine getopts: called CC=22 SCAR=30.9
Pod undocumented
84  
85 61 ▶ my $spec_as_hash = ref $spec eq 'HASH' ? $spec : parse_spec $spec;
Branch: 1 branch
BranchTrueFalseTotal
ref $spec eq 'HASH' ? : 19 42 61
86 55 ▶ croakf "%s parameter isn't an empty hash", '%$opts'
Branch: 1 branch
BranchTrueFalseTotal
if %$opts 1 54 55
87 if %$opts;
88 54 ▶ $argv = \@ARGV unless defined $argv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argv 33 21 54
89  
90 54 my @argv_backup = @$argv;
91 54 my @error;
92 # Guideline 4, Guideline 9
93 54 ▶ while ( @$argv and my ( $name, $rest ) = ( $argv->[ 0 ] =~ m/\A ${ \( OD ) } (.) (.*)/ox ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$argv and () = $argv->[0] =~ /\A ${\"-";} (.) (.*)/ox) 171 45 216
Condition100%3/3
@$argv and () = $argv->[0] =~ /\A ${\"-";} (.) (.*)/ox
!ll&&!rl&&r
396171
MC/DC100%2/2
@$argv and () = $argv->[0] =~ /\A ${\"-";} (.) (.*)/ox
@$argv () = $argv->[0] =~ /\A ${\"-";} (.) (.*)/ox
Truth table100%3/3
A and B
A= @$argvB= () = $argv->[0] =~ /\A ${\"-";} (.) (.*)/ox
ABresult
0X0
100
111
94 # Guideline 10
95 171 ▶ shift @$argv, last
Branch: 1 branch
BranchTrueFalseTotal
if $argv->[0] eq "--" 1 170 171
96 if $argv->[ 0 ] eq EOOD;
97 @error = ( 'illegal option', $name ), last
98 170 ▶ unless exists $spec_as_hash->{ $name }; ## no critic ( ProhibitNegativeExpressionsInUnlessAndUntilConditions )
Branch: 1 branch
BranchTrueFalseTotal
unless exists $spec_as_hash->{$name} 4 166 170
99  
100 166 my $indicator = $spec_as_hash->{ $name };
101 166 ▶ if ( $indicator =~ m/\A ${ \( OAICC ) } \z/ox ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indicator =~ /\A ${\'[,:=]';} \z/ox) { } 89 77 166
102 # Case: Option has an option-argument
103 # Shift delimeted option name
104 89 shift @$argv;
105 # Extract option-argument value
106 89 my $value;
107 89 ▶ if ( $rest ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest ne '') { } 14 75 89
108 14 $value = $rest;
109 } else {
110 # Guideline 7
111 75 ▶ @error = ( 'option requires an argument', $name ), last
Branch: 1 branch
BranchTrueFalseTotal
unless @$argv 2 73 75
112 unless @$argv;
113 # Guideline 6, Guideline 8
114 73 ▶ @error = ( 'option requires an argument', $name ), last
Branch: 1 branch
BranchTrueFalseTotal
unless defined($value = shift @$argv) 1 72 73
115 unless defined( $value = shift @$argv );
116 }
117 # Store option-argument value
118 86 ▶ if ( $indicator eq ':' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($indicator eq ':') { } 38 48 86
elsif ($indicator eq ',') { } 25 23 48
119 # Standard behaviour: Overwrite option-argument
120 38 $opts->{ $name } = $value
121 } elsif ( $indicator eq ',' ) {
122 # Create and fill list of option-arguments
123 25 ▶ $opts->{ $name } = [] unless exists $opts->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$name} 12 13 25
124 25 push @{ $opts->{ $name } }, $value
125 } else {
126 # Create and fill list of option-arguments ( $indicator eq '=' )
127 23 ▶ $opts->{ $name } = {} unless exists $opts->{ $name };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$name} 13 10 23
128 23 ▶ if ( $value =~ m/\A ([^=]*) = (.*) \z/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\A ([^=]*) = (.*) \z/x) { } 22 1 23
129 22 $opts->{ $name }->{ $1 } = $2
130 } else {
131 1 @error = ( 'option requires a key=value argument', $name ), last
132 }
133 }
134 } else {
135 # Case: Option is a flag
136 77 ▶ if ( not exists $opts->{ $name } ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not exists $opts->{$name}) { } 55 22 77
elsif ($indicator eq '!') { } 1 21 22
elsif ($indicator eq '+') { } 18 3 21
137 # Initialisation
138 55 $opts->{ $name } = TRUE
139 } elsif ( $indicator eq '!' ) {
140 # Negate logically
141 1 $opts->{ $name } = not $opts->{ $name }
142 } elsif ( $indicator eq '+' ) {
143 # Increment
144 18 ++$opts->{ $name }
145 }
146 # Guideline 5
147 77 ▶ if ( $rest eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq '') { } 60 17 77
148 # Shift delimeted option name
149 60 shift @$argv
150 } else {
151 17 $argv->[ 0 ] = OD . $rest ## no critic ( RequireLocalizedPunctuationVars )
152 }
153 }
154  
155 }
156  
157 54 ▶ if ( @error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@error) 8 46 54
158 # Restore to avoid side effects
159 8 @$argv = @argv_backup; ## no critic ( RequireLocalizedPunctuationVars )
160 8 %$opts = ();
161 # Prepare and print warning message:
162 # Program name, type of error, and invalid option character
163 8 warn sprintf( "%s: %s -- %s\n", program_name(), @error ) ## no critic ( RequireCarping )
164 }
165  
166 54 @error == 0
167 }
168  
169 # Prepare and print version info message:
170 # - Program name and version (first line)
171 # - Interpreter name ("perl") and version (second line)
172 sub print_version_info {
173 # call frame 0: processopts() (the caller) calls print_version_info()
174 # the caller's package name is Getopt::Guided
175 # call frame 1: a run() function/method (the caller) usually calls processopts()
176 # the caller's package name is whatever it is (could be "main")
177 3 ▶ printf STDOUT "%s %s\nperl v%vd\n", program_name(), ( caller( 2 ) // 'main' )->VERSION, $^V;
Subroutine print_version_info: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
caller 2 // "main"
l!l
21
MC/DC100%1/1
caller 2 // "main"
caller 2
Truth table100%2/2
A
A= caller 2
Aresult
11
00
178 3 EOOD
179 }
180  
181 sub processopts ( \@@ ) {
182 21 ▶ my $argv = shift;
Subroutine processopts: called CC=17 SCAR=28.3
Pod undocumented
183 21 my $spec_as_array = do { my $t = 0; [ grep $t ^= 1, @_ ] }; ## no critic ( RequireBlockGrep )
184  
185 # Check each option specification individually (1)
186 21 my $spec_as_hash;
187 21 parse_spec $_, %$spec_as_hash, 1 for @$spec_as_array;
188  
189 19 my @argv_backup = @$argv;
190 19 ▶ return FALSE unless getopts $spec_as_hash, my %opts, @$argv;
Branch: 1 branch
BranchTrueFalseTotal
unless &getopts($spec_as_hash, \my %opts, \@$argv) 2 17 19
191  
192 17 my @error;
193 # This ordered processing is a feature!
194 17 ▶ for ( my $i = 0 ; $i < @_ and not @error ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_ and not @error) 26 12 38
Condition66%2/3
$i < @_ and not @error
!ll&&!rl&&r
12026
MC/DC50%1/2
$i < @_ and not @error
$i < @_ not @error
Truth table66%2/3
A and not(B)
A= $i < @_B= not @error
ABresult
0X0
100
111
195 # If $_[ $i ] refers to a flag with no indicator, the split still returns
196 # the empty string (not undef!) as the value for the indicator
197 26 my ( $name, $indicator ) = split //, $_[ $i ];
198 26 ▶ if ( exists $opts{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$name}) 24 2 26
199 24 my $value = delete $opts{ $name };
200 24 my $dest = $_[ $i + 1 ];
201 # Do not use $_ on purpose
202 # https://dev.to/matthewpersico/yet-another-perl-switch-statement-54h3
203 24 for my $dest_ref_type ( ref $dest ) {
204 24 ▶ $dest_ref_type eq 'SCALAR' and ${ $dest } = $value, last;
Branch: 1 branch
BranchTrueFalseTotal
$dest_ref_type eq "SCALAR" and ${$dest;} = $value, last 7 17 24
205 17 ▶ $dest_ref_type eq 'ARRAY' and $indicator eq ',' and @{ $dest } = @$value, last;
Branch: 1 branch
BranchTrueFalseTotal
$dest_ref_type eq "ARRAY" and $indicator eq "," and @{$dest;} = @$value, last 1 16 17
Condition100%3/3
$dest_ref_type eq "ARRAY" and $indicator eq ","
!ll&&!rl&&r
1511
MC/DC100%2/2
$dest_ref_type eq "ARRAY" and $indicator eq ","
$dest_ref_type eq "ARRAY" $indicator eq ","
Truth table100%3/3
A and B
A= $dest_ref_type eq "ARRAY"B= $indicator eq ","
ABresult
0X0
100
111
206 16 ▶ $dest_ref_type eq 'HASH' and $indicator eq '=' and %{ $dest } = %$value, last;
Branch: 1 branch
BranchTrueFalseTotal
$dest_ref_type eq "HASH" and $indicator eq "=" and %{$dest;} = %$value, last 1 15 16
Condition66%2/3
$dest_ref_type eq "HASH" and $indicator eq "="
!ll&&!rl&&r
1501
MC/DC50%1/2
$dest_ref_type eq "HASH" and $indicator eq "="
$dest_ref_type eq "HASH" $indicator eq "="
Truth table66%2/3
A and B
A= $dest_ref_type eq "HASH"B= $indicator eq "="
ABresult
0X0
100
111
207 15 ▶ if ( $dest_ref_type eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dest_ref_type eq "CODE") 14 1 15
208 14 local $@; ## no critic ( RequireInitializationForLocalVars )
209 14 ▶ my $rv = eval { $dest->( $value, $name, $indicator ) // '' };
Condition100%2/2
&$dest($value, $name, $indicator) // ""
l!l
121
MC/DC100%1/1
&$dest($value, $name, $indicator) // ""
&$dest($value, $name, $indicator)
Truth table100%2/2
A
A= &$dest($value, $name, $indicator)
Aresult
11
00
210 14 ▶ @error = ( 'option callback destination throws an exception', $name, $@ ), last
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 1 13 14
211 unless defined $rv;
212 # Callbacks are called in scalar context
213 # If EOOD is the return value of the callback,
214 # processopts() will terminate early. The return value will be
215 # a special TRUE value. Using $name is not sufficient because 0 and 1
216 # are posible values for $name.
217 13 ▶ $rv eq EOOD ? return ( OD . $name ) : last
Branch: 1 branch
BranchTrueFalseTotal
$rv eq '--' ? : 4 9 13
218 }
219 1 croakf "'%s' is an unsupported destination reference type for the '%s' indicator", $dest_ref_type, $indicator
220 }
221 }
222 }
223  
224 12 ▶ if ( @error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@error) 1 11 12
225 1 chomp @error;
226 # Restore to avoid side effects
227 1 @$argv = @argv_backup; ## no critic ( RequireLocalizedPunctuationVars )
228 # Prepare and print warning message:
229 # Program name, eval error, and option character
230 1 warn sprintf( "%s: %s -- %s\n%s\n", program_name(), @error ) ## no critic ( RequireCarping )
231 }
232  
233 12 @error == 0
234 }
235  
236 sub readopts ( \@;@ ) {
237 6 ▶ my $argv = shift;
Subroutine readopts: called CC=8 SCAR=20.8
Pod undocumented
238  
239 6 require File::Spec::Functions;
240 my $file =
241 6 ▶ File::Spec::Functions::catfile( $ENV{ XDG_CONFIG_HOME } // File::Spec::Functions::catdir( $ENV{ HOME }, '.config' ),
Condition66%2/3
$ENV{"XDG_CONFIG_HOME"} // File::Spec::Functions::catdir($ENV{"HOME"}, ".config")
l!l&&r!l&&!r
510
MC/DC0%0/2
$ENV{"XDG_CONFIG_HOME"} // File::Spec::Functions::catdir($ENV{"HOME"}, ".config")
$ENV{"XDG_CONFIG_HOME"} File::Spec::Functions::catdir($ENV{"HOME"}, ".config")
Truth table66%2/3
A // B
A= $ENV{"XDG_CONFIG_HOME"}B= File::Spec::Functions::catdir($ENV{"HOME"}, ".config")
ABresult
1X1
011
000
242 program_name() . 'rc' );
243 6 ▶ unless ( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 2 4 6
244 # Prepend hard defaults
245 2 unshift @$argv, @_;
246 return
247 2 }
248  
249 4 ▶ open my $fh, '<:encoding(UTF-8)', $file ## no critic ( RequireBriefOpen )
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or &croakf("Cannot open file '%s' for reading (%s)", $file, $!) 0 4 4
250 or croakf "Cannot open file '%s' for reading (%s)", $file, $!;
251  
252 4 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 7 3 10
253 7 chomp;
254 7 ▶ next if m/\A (?: \#.* | ) \z/x;
Branch: 1 branch
BranchTrueFalseTotal
if /\A (?: \#.* | ) \z/x 0 7 7
255 7 ▶ if ( m/\A ( [[:alnum:]] ) (?: \ ( .+ ) )? \z/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\A ( [[:alnum:]] ) (?: \ ( .+ ) )? \z/x) 6 1 7
256 6 ▶ unshift @$argv, ( OD . $1, defined $2 ? $2 : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 3 3 6
257 next
258 6 }
259 1 croakf "File '%s' contains the invalid line '%s'", $file, $_
260 }
261 # Prepend hard defaults
262 3 unshift @$argv, @_;
263  
264 return
265 3 }
266  
267 16 1
↑ Summary Makefile.PL »