blib/lib/Getopt/Guided.pm

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