blib/lib/Getopt/Long/Subcommand.pm

filter: Statementstmt 61.6% 95 / 154 Branchbran 47.0% 32 / 68 Conditioncond 53.5% 15 / 28 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 57.1% 160 / 280 CC 49 SCAR 38.1
CC
49
Cov
57%
GetOptions
115.4
__ANON__
42.0
_GetOptions
37.8
CRAP 45.0
↑ Summary
1 package Getopt::Long::Subcommand;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Log::ger;
7  
8 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 2 our $DATE = '2025-02-21'; # DATE
12 2 our $DIST = 'Getopt-Long-Subcommand'; # DIST
13 2 our $VERSION = '0.105'; # VERSION
14  
15 ## no critic (Modules::ProhibitAutomaticExportation)
16 2 our @EXPORT = qw(
17 GetOptions
18 );
19 ## use critic
20  
21 # XXX completion & configure are actually only allowed at the top-level
22 2 my @known_cmdspec_keys = qw(
23 options
24 subcommands
25 default_subcommand
26 summary description
27 completion
28 configure
29 );
30  
31 40 ▶sub _cmdspec_opts_to_gl_ospec {
Subroutine _cmdspec_opts_to_gl_ospec: called CC=4 SCAR=14.2
32 40 my ($cmdspec_opts, $is_completion, $res) = @_;
33 return { map {
34 40 ▶ if ($is_completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_completion) { } 0 62 62
35 # we don't want side-effects during completion (handler printing or
36 # existing, etc), so we set an empty coderef for all handlers.
37 0 ▶ ($_ => sub{});
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 } else {
39 62 my $k = $_;
40 62 my $v = $cmdspec_opts->{$k};
41 62 ▶ my $handler = ref($v) eq 'HASH' ? $v->{handler} : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'HASH' ? : 62 0 62
42 62 ▶ if (ref($handler) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 62 0 62
43 62 my $orig_handler = $handler;
44 7 ▶ $handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
45 7 my ($cb, $val) = @_;
46 7 $orig_handler->($cb, $val, $res);
47 62 };
48 }
49 62 ($k => $handler);
50 }
51 } keys %$cmdspec_opts };
52 }
53  
54 40 ▶sub _gl_getoptions {
Subroutine _gl_getoptions: called CC=5 SCAR=16.1
55 40 require Getopt::Long;
56  
57 40 my ($ospec, $configure, $pass_through, $res) = @_;
58  
59 my @configure = @{
60 40 ▶ $configure //
Condition100%2/2
$configure // ["no_ignore_case", "no_getopt_compat", "gnu_compat", "bundling"]
l!l
139
MC/DC100%1/1
$configure // ["no_ignore_case", "no_getopt_compat", "gnu_compat", "bundling"]
$configure
Truth table100%2/2
A
A= $configure
Aresult
11
00
61 ['no_ignore_case', 'no_getopt_compat', 'gnu_compat', 'bundling']
62 };
63 40 ▶ if ($pass_through) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass_through) { } 34 6 40
64 push @configure, 'pass_through'
65 34 ▶ unless grep { $_ eq 'pass_through' } @configure;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq "pass_through";} @configure 34 0 34
66 } else {
67 6 @configure = grep { $_ ne 'pass_through' } @configure;
68 }
69 #log_trace('[comp][glsubc] Performing Getopt::Long::GetOptions (configure: %s)',
70 # $pass_through, \@configure);
71  
72 40 my $old_conf = Getopt::Long::Configure(@configure);
73 40 ▶ local $SIG{__WARN__} = sub {} if $pass_through;
Subroutine __ANON__: not called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $pass_through 34 6 40
74  
75 # ugh, this is ugly. the problem we're trying to solve: in the case of 'subc
76 # --help', 'subc' is consumed first by Getopt::Long and thus removed from
77 # @ARGV. when --help handler wants to find out the subcommand name ('subc'),
78 # it doesn't have anywhere to look for. so we give it in $res which is
79 # passed as the third argument to the handler.
80 40 local $res->{_non_options_argv} = [];
81  
82 #log_trace('[comp][glsubc] @ARGV before Getopt::Long::GetOptions: %s', \@ARGV);
83 #log_trace('[comp][glsubc] spec for Getopt::Long::GetOptions: %s', $ospec);
84 my $gl_res = Getopt::Long::GetOptions(
85 %$ospec,
86 59 ▶ '<>' => sub { push @{ $res->{_non_options_argv} }, $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 40 );
88 40 @ARGV = @{ $res->{_non_options_argv} };
89  
90 #log_trace('[comp][glsubc] @ARGV after Getopt::Long::GetOptions: %s', \@ARGV);
91 40 Getopt::Long::Configure($old_conf);
92 40 $gl_res;
93 }
94  
95 40 ▶sub _GetOptions {
Subroutine _GetOptions: called CC=25 SCAR=36.3
96 40 my ($cmdspec, $is_completion, $res, $stash) = @_;
97  
98 40 ▶ $res //= {success=>undef};
Condition100%2/2
$res //= {"success", undef}
l!l
2218
MC/DC100%1/1
$res //= {"success", undef}
$res
Truth table100%2/2
A
A= $res
Aresult
11
00
99 40 ▶ $stash //= {
Condition100%2/2
$stash //= {"path", "", "level", 0}
l!l
2218
MC/DC100%1/1
$stash //= {"path", "", "level", 0}
$stash
Truth table100%2/2
A
A= $stash
Aresult
11
00
100 path => '', # for displaying error message
101 level => 0,
102 };
103  
104 # check command spec
105 {
106 #log_trace("[comp][glsubc] Checking cmdspec keys: %s", [keys %$cmdspec]);
107 40 for my $k (keys %$cmdspec) {
108 679 ▶ (grep { $_ eq $k } @known_cmdspec_keys)
Branch: 1 branch
BranchTrueFalseTotal
$stash->{'path'} ? : 0 0 0
109 or die "Unknown command specification key '$k'" .
110 97 ▶ ($stash->{path} ? " (under $stash->{path})" : "") . "\n";
Branch: 1 branch
BranchTrueFalseTotal
grep {$_ eq $k;} @known_cmdspec_keys or die "Unknown command specification key '${k}'" . ($stash->{"path"} ? qq[ (under $stash->{"path"})] : "") . "\n" 97 0 97
111 }
112 }
113  
114 my $has_subcommands = $cmdspec->{subcommands} &&
115 40 ▶ keys(%{$cmdspec->{subcommands}});
Condition66%2/3
$cmdspec->{'subcommands'} && keys %{$$cmdspec{'subcommands'};}
!ll&&!rl&&r
6034
MC/DC50%1/2
$cmdspec->{'subcommands'} && keys %{$$cmdspec{'subcommands'};}
$cmdspec->{'subcommands'} keys %{$$cmdspec{'subcommands'};}
Truth table66%2/3
A && B
A= $cmdspec->{'subcommands'}B= keys %{$$cmdspec{'subcommands'};}
ABresult
0X0
100
111
116 #log_trace("TMP:has_subcommands=%s", $has_subcommands);
117 40 ▶ my $pass_through = $has_subcommands || $is_completion;
Condition66%2/3
$has_subcommands || $is_completion
l!l&&r!l&&!r
3406
MC/DC50%1/2
$has_subcommands || $is_completion
$has_subcommands $is_completion
Truth table66%2/3
A || B
A= $has_subcommandsB= $is_completion
ABresult
1X1
011
000
118  
119 my $ospec = _cmdspec_opts_to_gl_ospec(
120 40 $cmdspec->{options}, $is_completion, $res);
121 40 ▶ unless (_gl_getoptions(
Branch: 1 branch
BranchTrueFalseTotal
unless (_gl_getoptions($ospec, $cmdspec->{"configure"}, $pass_through, $res)) 1 39 40
122 $ospec, $cmdspec->{configure}, $pass_through, $res)) {
123 1 $res->{success} = 0;
124 1 return $res;
125 }
126  
127 # for doing completion
128 39 ▶ if ($is_completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_completion) 0 39 39
129 0 ▶ $res->{comp_ospec} //= {};
Condition0%0/2
$res->{"comp_ospec"} //= {}
l!l
00
MC/DC0%0/1
$res->{"comp_ospec"} //= {}
$res->{"comp_ospec"}
Truth table0%0/2
A
A= $res->{"comp_ospec"}
Aresult
11
00
130 0 for (keys %$ospec) {
131 0 $res->{comp_ospec}{$_} = $ospec->{$_};
132 }
133 }
134  
135 39 ▶ if ($has_subcommands) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_subcommands) 34 5 39
136 # for doing completion of subcommand names
137 34 ▶ if ($is_completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_completion) 0 34 34
138 my $scnames = $res->{comp_subcommand_names}[$stash->{level}] =
139 0 [sort keys %{$cmdspec->{subcommands}}];
140 $res->{comp_subcommand_summaries}[$stash->{level}] =
141 0 [map {$cmdspec->{subcommands}{$_}{summary}} @$scnames];
142 }
143  
144 34 ▶ $res->{subcommand} //= [];
Condition100%2/2
$res->{"subcommand"} //= []
l!l
1618
MC/DC100%1/1
$res->{"subcommand"} //= []
$res->{"subcommand"}
Truth table100%2/2
A
A= $res->{"subcommand"}
Aresult
11
00
145  
146 34 my $push;
147 my $sc_name;
148  
149 34 ▶ if (defined $res->{subcommand}[ $stash->{level} ]) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $res->{'subcommand'}[$stash->{'level'}]) { } 0 34 34
elsif (@ARGV) { } 29 5 34
elsif (defined $cmdspec->{'default_subcommand'}) { } 0 5 5
150 # subcommand has been set, e.g. by option handler
151 0 $sc_name = $res->{subcommand}[ $stash->{level} ];
152 } elsif (@ARGV) {
153 29 $sc_name = shift @ARGV;
154 29 $push++; # we need to push to $res->{subcommand} later
155 } elsif (defined $cmdspec->{default_subcommand}) {
156 0 $sc_name = $cmdspec->{default_subcommand};
157 0 $push++;
158 } else {
159 # no subcommand
160 5 $res->{success} = 1;
161 5 return $res;
162 }
163  
164 # for doing completion of subcommand names
165 29 ▶ if ($is_completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_completion) 0 29 29
166 0 push @{ $res->{comp_subcommand_name} }, $sc_name;
167 }
168  
169 29 my $sc_spec = $cmdspec->{subcommands}{$sc_name};
170 29 ▶ unless ($sc_spec) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sc_spec) 7 22 29
171 warn "Unknown subcommand '$sc_name'".
172 7 ▶ ($stash->{path} ? " for $stash->{path}":"")."\n"
Branch: 2 branches
BranchTrueFalseTotal
unless $is_completion 7 0 7
$stash->{'path'} ? : 3 4 7
173 unless $is_completion;
174 7 $res->{success} = 0;
175 7 return $res;
176 };
177 22 ▶ push @{ $res->{subcommand} }, $sc_name if $push;
Branch: 1 branch
BranchTrueFalseTotal
if $push 22 0 22
178 22 ▶ local $stash->{path} = ($stash->{path} ? "/" : "") . $sc_name;
Branch: 1 branch
BranchTrueFalseTotal
$stash->{'path'} ? : 9 13 22
179 22 local $stash->{level} = $stash->{level}+1;
180 22 _GetOptions($sc_spec, $is_completion, $res, $stash);
181 }
182 27 ▶ $res->{success} //= 1;
Condition100%2/2
$res->{"success"} //= 1
l!l
225
MC/DC100%1/1
$res->{"success"} //= 1
$res->{"success"}
Truth table100%2/2
A
A= $res->{"success"}
Aresult
11
00
183  
184 #log_trace('[comp][glsubc] Final @ARGV: %s', \@ARGV) unless $stash->{path};
185 #log_trace('[comp][glsubc] TMP: stash=%s', $stash);
186 #log_trace('[comp][glsubc] TMP: res=%s', $res);
187 27 $res;
188 }
189  
190 18 ▶sub GetOptions {
Subroutine GetOptions: called CC=13 SCAR=47.5
Pod documented
191 18 my %cmdspec = @_;
192  
193 # figure out if we run in completion mode
194 18 my ($is_completion, $shell, $words, $cword);
195 CHECK_COMPLETION:
196 {
197 18 ▶ if ($ENV{COMP_SHELL}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'COMP_SHELL'}) { } 0 18 18
elsif ($ENV{'COMMAND_LINE'}) { } 0 18 18
198 0 ($shell = $ENV{COMP_SHELL}) =~ s!.+/!!;
199 } elsif ($ENV{COMMAND_LINE}) {
200 0 $shell = 'tcsh';
201 } else {
202 18 $shell = 'bash';
203 }
204  
205 18 ▶ if ($ENV{COMP_LINE} || $ENV{COMMAND_LINE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"COMP_LINE"} or $ENV{"COMMAND_LINE"}) 0 18 18
Condition33%1/3
$ENV{"COMP_LINE"} or $ENV{"COMMAND_LINE"}
l!l&&r!l&&!r
0018
MC/DC0%0/2
$ENV{"COMP_LINE"} or $ENV{"COMMAND_LINE"}
$ENV{"COMP_LINE"} $ENV{"COMMAND_LINE"}
Truth table33%1/3
A or B
A= $ENV{"COMP_LINE"}B= $ENV{"COMMAND_LINE"}
ABresult
1X1
011
000
206 0 ▶ if ($ENV{COMP_LINE}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ENV{'COMP_LINE'}) { } 0 0 0
elsif ($ENV{'COMMAND_LINE'}) { } 0 0 0
207 0 $is_completion++;
208 0 require Complete::Bash;
209 0 ($words, $cword) = @{ Complete::Bash::parse_cmdline(
210 undef, undef, {truncate_current_word=>1}) };
211 0 ($words, $cword) = @{ Complete::Bash::join_wordbreak_words(
212 $words, $cword) };
213 } elsif ($ENV{COMMAND_LINE}) {
214 0 $is_completion++;
215 0 require Complete::Tcsh;
216 0 $shell = 'tcsh';
217 0 ($words, $cword) = @{ Complete::Tcsh::parse_cmdline() };
218 } else {
219 0 last CHECK_COMPLETION;
220 }
221  
222 0 shift @$words; $cword--; # strip program name
223 0 @ARGV = @$words;
224 }
225 }
226  
227 18 my $res = _GetOptions(\%cmdspec, $is_completion);
228  
229 18 ▶ if ($is_completion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_completion) 0 18 18
230 0 my $ospec = $res->{comp_ospec};
231 0 require Complete::Getopt::Long;
232 my $compres = Complete::Getopt::Long::complete_cli_arg(
233 words => $words, cword => $cword, getopt_spec=>$ospec,
234 extras => {
235 stash => $res->{stash},
236 },
237 bundling => do {
238 0 ▶ if (!$cmdspec{configure}) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $cmdspec{'configure'}) { } 0 0 0
elsif (grep {$_ eq 'bundling';} @{$cmdspec{'configure'};}) { } 0 0 0
elsif (grep {$_ eq 'no_bundling';} @{$cmdspec{'configure'};}) { } 0 0 0
239 0 1;
240 0 } elsif (grep { $_ eq 'bundling' } @{ $cmdspec{configure} }) {
241 0 1;
242 0 } elsif (grep { $_ eq 'no_bundling' } @{ $cmdspec{configure} }) {
243 0 0;
244 } else {
245 0 0;
246 }
247 },
248 0 ▶ completion => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
249 0 my %args = @_;
250  
251 0 ▶ my $word = $args{word} // '';
Condition0%0/2
$args{"word"} // ""
l!l
00
MC/DC0%0/1
$args{"word"} // ""
$args{"word"}
Truth table0%0/2
A
A= $args{"word"}
Aresult
11
00
252 0 my $type = $args{type};
253 0 my $stash = $args{stash};
254  
255 # complete subcommand names
256 0 ▶ if ($type eq 'arg' &&
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "arg" and $args{"argpos"} < @{$res->{"comp_subcommand_names"} // [];}) 0 0 0
Condition0%0/3
$type eq "arg" and $args{"argpos"} < @{$res->{"comp_subcommand_names"} // [];}
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "arg" and $args{"argpos"} < @{$res->{"comp_subcommand_names"} // [];}
$type eq "arg" $args{"argpos"} < @{$res->{"comp_subcommand_names"} // [];}
Truth table0%0/3
A and B
A= $type eq "arg"B= $args{"argpos"} < @{$res->{"comp_subcommand_names"} // [];}
ABresult
0X0
100
111
257 0 ▶ $args{argpos} < @{$res->{comp_subcommand_names}//[]}) {
Condition0%0/2
$res->{"comp_subcommand_names"} // []
l!l
00
MC/DC0%0/1
$res->{"comp_subcommand_names"} // []
$res->{"comp_subcommand_names"}
Truth table0%0/2
A
A= $res->{"comp_subcommand_names"}
Aresult
11
00
258 0 require Complete::Util;
259 return Complete::Util::complete_array_elem(
260 word => $res->{comp_subcommand_name}[$args{argpos}],
261 array => $res->{comp_subcommand_names}[$args{argpos}],
262 0 summaries => $res->{comp_subcommand_summaries}[$args{argpos}]
263 );
264 }
265  
266 0 $args{getopt_res} = $res;
267 0 $args{subcommand} = $res->{comp_subcommand_name};
268 0 ▶ $cmdspec{completion}->(%args) if $cmdspec{completion};
Branch: 1 branch
BranchTrueFalseTotal
if $cmdspec{"completion"} 0 0 0
269 },
270 0 );
271  
272 0 ▶ if ($shell eq 'bash') {
Branch: 2 branches
BranchTrueFalseTotal
if ($shell eq 'bash') { } 0 0 0
elsif ($shell eq 'tcsh') { } 0 0 0
273 0 print Complete::Bash::format_completion($compres);
274 } elsif ($shell eq 'tcsh') {
275 0 print Complete::Tcsh::format_completion($compres);
276 } else {
277 0 die "Unknown shell '$shell'";
278 }
279  
280 0 exit 0;
281 }
282  
283 # cleanup unneeded details
284 18 $res;
285 }
286  
287 2 1;
288 # ABSTRACT: Process command-line options, with subcommands and completion
289  
290 __END__
↑ Summary