blib/lib/Getopt/Complete/Options.pm

filter: Statementstmt 81.1% 138 / 170 Branchbran 55.8% 38 / 68 Conditioncond 50.0% 21 / 42 MC/DCmcdc 34.6% 9 / 26 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 9 totaltotal 67.2% 224 / 333 CC 51 SCAR 36.9
CC
51
Cov
70%
_init
86.7
parse_completion_request
28.3
handle_shell_completion
4.1
CRAP 40.2
« blib/lib/Getopt/Complete/Args.pm ↑ Summary blib/lib/Getopt/Complete.pm »
1 package Getopt::Complete::Options;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 our $VERSION = $Getopt::Complete::VERSION;
7  
8 13 ▶use IPC::Open2;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Getopt::Complete::LazyOptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 56 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 56 my $self = bless {
15 sub_commands => [],
16 option_specs => {},
17 completion_handlers => {},
18 parse_errors => undef,
19 }, $class;
20  
21 # process the params into normalized completion handlers
22 # if there are problems, the ->errors method will return a list.
23 56 $self->_init(@_);
24 56 return $self;
25 }
26  
27 sub sub_commands {
28 12 ▶ return @{ shift->{sub_commands} };
Subroutine sub_commands: called CC=1 SCAR=0.0
Pod undocumented
29 }
30  
31 sub option_names {
32 7 ▶ return keys %{ shift->{completion_handlers} };
Subroutine option_names: called CC=1 SCAR=0.0
Pod undocumented
33 }
34  
35 sub option_specs {
36 12 ▶ Carp::confess("Bad params") if @_ > 1;
Subroutine option_specs: called CC=4 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 12 12
37 12 my $self = shift;
38 12 my @specs;
39 12 for my $key (keys %{ $self->{option_specs} }) {
40 39 ▶ next if $key eq '<>';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "<>" 0 39 39
41 39 my $value = $self->{option_specs}{$key};
42 39 push @specs, $key . $value;
43 }
44 12 return @specs;
45 }
46  
47 sub option_spec {
48 43 ▶ my $self = shift;
Subroutine option_spec: called CC=2 SCAR=7.0
Pod undocumented
49 43 my $name = shift;
50 43 ▶ Carp::confess("Bad params") if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 43 43
51 43 return $self->{option_specs}{$name};
52 }
53  
54 sub has_option {
55 5 ▶ my $self = shift;
Subroutine has_option: called CC=1 SCAR=0.0
Pod undocumented
56 5 my $name = shift;
57 5 return exists $self->{completion_handlers}{$name};
58 }
59  
60 sub completion_handler {
61 99 ▶ my $self = shift;
Subroutine completion_handler: called CC=2 SCAR=7.0
Pod undocumented
62 99 my $name = shift;
63 99 ▶ Carp::confess("Bad params") if not defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 99 99
64 99 return $self->{completion_handlers}{$name};
65 }
66  
67 sub _init {
68 56 ▶ my $self = shift;
Subroutine _init: called CC=25 SCAR=44.6
69
70 56 my $completion_handlers = $self->{completion_handlers} = {};
71 56 my $option_specs = $self->{option_specs} = {};
72  
73 56 my @parse_errors;
74 56 ▶ while (my $key = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = shift @_) 177 56 233
75 177 my $handler = shift @_;
76
77 177 my ($name,$spec) = ($key =~ /^([\w|-|\>][\w|-]*|\<\>|)(\W.*|)/);
78 177 ▶ if (not defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 177 177
79 0 push @parse_errors, __PACKAGE__ . " is unable to parse '$key' from spec!";
80 0 next;
81 }
82 177 ▶ if ($handler and not ref $handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler and not ref $handler) 44 133 177
Condition100%3/3
$handler and not ref $handler
!ll&&!rl&&r
894444
MC/DC100%2/2
$handler and not ref $handler
$handler not ref $handler
Truth table100%3/3
A and not(B)
A= $handlerB= not ref $handler
ABresult
0X0
100
111
83 44 my $code;
84 44 ▶ if ($handler =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handler =~ /::/) { } 0 44 44
85 # fully qualified
86 0 eval {
87 0 $code = \&{ $handler };
88 };
89 0 ▶ unless (ref($code)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 0 0 0
90 0 push @parse_errors, __PACKAGE__ . " $key! references callback $handler which is not found! Did you use its module first?!";
91 }
92 }
93 else {
94 44 $code = Getopt::Complete::Compgen->can($handler);
95 44 ▶ unless (ref($code)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $code) 0 44 44
96 push @parse_errors, __PACKAGE__ . " $key! references builtin $handler which is not found! Select from:"
97 0 . join(", ", map { my $short = substr($_,0,1); "$_($short)" } @Getopt::Complete::Compgen::builtins);
98 }
99 }
100 44 ▶ if (ref($code)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code) 44 0 44
101 44 $handler = $code;
102 }
103 }
104 177 ▶ if (substr($name,0,1) eq '>') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) eq ">") 44 133 177
105 # a "sub-command": make a sub-options tree, which may happen recursively
106 44 my $word = substr($name,1);
107 44 ▶ if (ref($handler) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $handler eq 'ARRAY') { } 44 0 44
elsif (ref $handler eq 'CODE' or ref $handler eq 'SCALAR') { } 0 0 0
108 44 $handler = Getopt::Complete::Options->new(@$handler);
109 }
110 ▶ elsif (ref($handler) eq 'CODE' or ref($handler) eq 'SCALAR') {
Condition0%0/3
ref $handler eq "CODE" or ref $handler eq "SCALAR"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $handler eq "CODE" or ref $handler eq "SCALAR"
ref $handler eq "CODE" ref $handler eq "SCALAR"
Truth table0%0/3
A or B
A= ref $handler eq "CODE"B= ref $handler eq "SCALAR"
ABresult
1X1
011
000
111 # be lazy about actually resolving this
112 0 $handler = Getopt::Complete::LazyOptions->new($handler);
113 }
114 else {
115 0 die "expected arrayref or code for $name value!";
116 }
117 44 ▶ $handler->{command} = ($self->{command} || '') . " " . $word;
Condition50%1/2
$self->{'command'} || ''
l!l
044
MC/DC0%0/1
$self->{'command'} || ''
$self->{'command'}
Truth table50%1/2
A
A= $self->{'command'}
Aresult
11
00
118 44 $completion_handlers->{$name} = $handler;
119 44 push @{ $self->{sub_commands} }, $word;
120 44 next;
121 }
122  
123 133 $completion_handlers->{$name} = $handler;
124 133 ▶ if ($name eq '<>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "<>") 0 133 133
125 0 next;
126 }
127 133 ▶ if ($name eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "-") 0 133 133
128 0 ▶ if ($spec and $spec ne '!') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec and $spec ne "!") 0 0 0
Condition0%0/3
$spec and $spec ne "!"
!ll&&!rl&&r
000
MC/DC0%0/2
$spec and $spec ne "!"
$spec $spec ne "!"
Truth table0%0/3
A and B
A= $specB= $spec ne "!"
ABresult
0X0
100
111
129 0 push @parse_errors, __PACKAGE__ . " $key errors: $name is implicitly stand-alone!";
130 }
131 0 ▶ $spec ||= '!';
Condition0%0/2
$spec ||= "!"
l!l
00
MC/DC0%0/1
$spec ||= "!"
$spec
Truth table0%0/2
A
A= $spec
Aresult
11
00
132 }
133 133 ▶ $spec ||= '=s';
Condition50%1/2
$spec ||= "=s"
l!l
1330
MC/DC0%0/1
$spec ||= "=s"
$spec
Truth table50%1/2
A
A= $spec
Aresult
11
00
134 133 $option_specs->{$name} = $spec;
135 133 ▶ if ($spec =~ /[\!\+]/ and defined $completion_handlers->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /[\!\+]/ and defined $completion_handlers->{$key}) 0 133 133
Condition66%2/3
$spec =~ /[\!\+]/ and defined $completion_handlers->{$key}
!ll&&!rl&&r
66670
MC/DC0%0/2
$spec =~ /[\!\+]/ and defined $completion_handlers->{$key}
$spec =~ /[\!\+]/ defined $completion_handlers->{$key}
Truth table66%2/3
A and B
A= $spec =~ /[\!\+]/B= defined $completion_handlers->{$key}
ABresult
0X0
100
111
136 0 push @parse_errors, __PACKAGE__ . " error on option $key: ! and + expect an undef completion list, since they do not have values!";
137 0 next;
138 }
139 133 ▶ if (ref($completion_handlers->{$key}) eq 'ARRAY' and @{ $completion_handlers->{$key} } == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $completion_handlers->{$key} eq "ARRAY" and @{$completion_handlers->{$key};} == 0) 0 133 133
Condition33%1/3
ref $completion_handlers->{$key} eq "ARRAY" and @{$completion_handlers->{$key};} == 0
!ll&&!rl&&r
13300
MC/DC0%0/2
ref $completion_handlers->{$key} eq "ARRAY" and @{$completion_handlers->{$key};} == 0
ref $completion_handlers->{$key} eq "ARRAY" @{$completion_handlers->{$key};} == 0
Truth table33%1/3
A and B
A= ref $completion_handlers->{$key} eq "ARRAY"B= @{$completion_handlers->{$key};} == 0
ABresult
0X0
100
111
140 0 push @parse_errors, __PACKAGE__ . " error on option $key: an empty arrayref will never be valid!";
141 }
142 }
143
144 56 $self->{parse_errors} = \@parse_errors;
145
146 56 ▶ return (@parse_errors ? () : 1);
Branch: 1 branch
BranchTrueFalseTotal
@parse_errors ? : 0 56 56
147 }
148  
149 sub handle_shell_completion {
150 11 ▶ my $self = shift;
Subroutine handle_shell_completion: called CC=4 SCAR=14.2
Pod undocumented
151 11 ▶ if ($ENV{COMP_LINE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"COMP_LINE"}) 11 0 11
152 11 my ($command,$current,$previous,$other) = $self->parse_completion_request($ENV{COMP_LINE},$ENV{COMP_POINT});
153 11 ▶ unless ($command) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($command) 0 11 11
154 # parse error
155 # this typically only happens when there are mismatched quotes, which means something you can't complete anyway
156 # don't complete anything...
157 0 exit;
158 }
159 11 my $args = Getopt::Complete::Args->new(options => $self, argv => $other);
160 11 my @matches;
161 my @printable_matches;
162 11 ▶ unless ($args->errors) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->errors) 9 2 11
163 9 @matches = $args->resolve_possible_completions($command,$current,$previous);
164 9 @printable_matches = $args->translate_completions_for_shell_display($current, @matches);
165 }
166 11 print join("\n",@printable_matches),"\n";
167 11 exit;
168 }
169 0 return 1;
170 }
171  
172 sub _line_to_argv {
173 22 ▶ my $line = pop;
Subroutine _line_to_argv: called CC=2 SCAR=6.9
174 22 my $cmd = q{perl -e "use Data::Dumper; print Dumper(\@ARGV)" -- } . $line;
175 22 my ($reader,$writer);
176 22 my $pid = open2($reader,$writer,'bash 2>/dev/null');
177 22 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 22 22
178 22 print $writer $cmd;
179 22 close $writer;
180 22 my $result = join("",<$reader>);
181 13 ▶ no strict; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
182 22 my $array = eval $result;
183 22 my @array = @$array;
184  
185 # We don't want to expand ~ for user experience and to be consistent with
186 # Bash's behavior for tab completion (as opposed to expansion of ARGV).
187 22 my $home_dir = (getpwuid($<))[7];
188 22 @array = map { $_ =~ s/^$home_dir/\~/; $_ } @array;
189  
190 22 return @array;
191 }
192  
193 sub parse_completion_request {
194 11 ▶ my $self = shift;
Subroutine parse_completion_request: called CC=18 SCAR=33.4
Pod undocumented
195 11 my ($comp_line, $comp_point) = @_;
196  
197 11 my $left = substr($comp_line,0,$comp_point);
198 11 my @left = _line_to_argv($left);
199  
200 # find options for last sub-command if it has a completion handler
201 # skipping first command but old code didn't but it also never seemed to trigger before
202 11 my @sub_cmds = @left[1..$#left];
203 11 ▶ while (@sub_cmds and my $delegate = $self->completion_handler('>' . $sub_cmds[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sub_cmds and my $delegate = $self->completion_handler('>' . $sub_cmds[0])) 17 11 28
Condition100%3/3
@sub_cmds and my $delegate = $self->completion_handler('>' . $sub_cmds[0])
!ll&&!rl&&r
11017
MC/DC100%2/2
@sub_cmds and my $delegate = $self->completion_handler('>' . $sub_cmds[0])
@sub_cmds my $delegate = $self->completion_handler('>' . $sub_cmds[0])
Truth table100%3/3
A and B
A= @sub_cmdsB= my $delegate = $self->completion_handler('>' . $sub_cmds[0])
ABresult
0X0
100
111
204 17 shift @sub_cmds;
205 17 $self = $delegate;
206 }
207  
208 11 my $right = substr($comp_line,$comp_point);
209 11 my @right = _line_to_argv($right);
210
211 11 ▶ unless (@left) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@left) 0 11 11
212 # parse error
213 0 return;
214 }
215
216 11 my $command = shift @left;
217 11 my $current;
218 11 ▶ if (substr($left, -1) ne ' ' || substr($left, -2) eq '\ ') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($left, -1) ne ' ' or substr($left, -2) eq '\\ ') { } 6 5 11
Condition66%2/3
substr($left, -1) ne " " or substr($left, -2) eq "\\ "
l!l&&r!l&&!r
605
MC/DC50%1/2
substr($left, -1) ne " " or substr($left, -2) eq "\\ "
substr($left, -1) ne " " substr($left, -2) eq "\\ "
Truth table66%2/3
A or B
A= substr($left, -1) ne " "B= substr($left, -2) eq "\\ "
ABresult
1X1
011
000
219 # we're at the end of the final word in the @left list, and are trying to complete it
220 6 $current = pop @left;
221 }
222 else {
223 5 $current = '';
224 }
225 11 $left =~ s/\\ / /g;
226 11 ▶ my $previous = ( (@left and $left[-1] =~ /^-{1,2}/ and not $left[-1] =~ /^-{1,2}[\w\-]+\=/) ? (pop @left) : ()) ;
Branch: 1 branch
BranchTrueFalseTotal
@left && $left[-1] =~ /^-{1,2}/ && !($left[-1] =~ /^-{1,2}[\w\-]+\=/) ? : 3 8 11
Condition83%5/6
@left and $left[-1] =~ /^-{1,2}/ and not $left[-1] =~ /^-{1,2}[\w\-]+\=/
!ll&&!rl&&r
713
@left and $left[-1] =~ /^-{1,2}/
!ll&&!rl&&r
074
MC/DC66%2/3
@left and $left[-1] =~ /^-{1,2}/ and not $left[-1] =~ /^-{1,2}[\w\-]+\=/
@left $left[-1] =~ /^-{1,2}/ not $left[-1] =~ /^-{1,2}[\w\-]+\=/
Truth table75%3/4
A and B and not(C)
A= @leftB= $left[-1] =~ /^-{1,2}/C= not $left[-1] =~ /^-{1,2}[\w\-]+\=/
ABCresult
0XX0
10X0
1100
1111
227 # TODO: this might be a good spot to make sure we don't complete a new sub-command
228 11 my @other_options = (@left,@right);
229  
230 # it's hard to spot the case in which the previous word is "boolean", and has no value specified
231 11 ▶ if ($previous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($previous) 3 8 11
232 3 my ($name) = ($previous =~ /^-+(.*)/);
233 3 my $spec = $self->option_spec($name);
234 3 ▶ if ($spec and $spec =~ /[\!\+]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec and $spec =~ /[\!\+]/) { } 1 2 3
elsif ($name =~ /no-(.*)/) { } 0 2 2
Condition100%3/3
$spec and $spec =~ /[\!\+]/
!ll&&!rl&&r
111
MC/DC100%2/2
$spec and $spec =~ /[\!\+]/
$spec $spec =~ /[\!\+]/
Truth table100%3/3
A and B
A= $specB= $spec =~ /[\!\+]/
ABresult
0X0
100
111
235 1 push @other_options, $previous;
236 1 $previous = undef;
237 }
238 elsif ($name =~ /no-(.*)/) {
239 # Handle a case of an option which natively starts with "--no-"
240 # and is set to boolean. There is one of everything in this world.
241 0 $name =~ s/^no-//;
242 0 $spec = $self->option_spec($name);
243 0 ▶ if ($spec and $spec =~ /[\!\+]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec and $spec =~ /[\!\+]/) 0 0 0
Condition0%0/3
$spec and $spec =~ /[\!\+]/
!ll&&!rl&&r
000
MC/DC0%0/2
$spec and $spec =~ /[\!\+]/
$spec $spec =~ /[\!\+]/
Truth table0%0/3
A and B
A= $specB= $spec =~ /[\!\+]/
ABresult
0X0
100
111
244 0 push @other_options, $previous;
245 0 $previous = undef;
246 }
247 }
248
249 }
250  
251 11 my $quote;
252 11 ▶ if ($current =~ /^([\'\"])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current =~ /^([\'\"])/) 0 11 11
253 0 $quote = $1;
254 0 $current = substr($current,1);
255 0 ▶ if (substr($current,-1,1) eq $quote and not substr($current,-2,1) eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($current, -1, 1) eq $quote and not substr($current, -2, 1) eq "\\") 0 0 0
Condition0%0/3
substr($current, -1, 1) eq $quote and not substr($current, -2, 1) eq "\\"
!ll&&!rl&&r
000
MC/DC0%0/2
substr($current, -1, 1) eq $quote and not substr($current, -2, 1) eq "\\"
substr($current, -1, 1) eq $quote not substr($current, -2, 1) eq "\\"
Truth table0%0/3
A and not(B)
A= substr($current, -1, 1) eq $quoteB= not substr($current, -2, 1) eq "\\"
ABresult
0X0
100
111
256 0 $current = substr($current,0,length($current)-1);
257 };
258 }
259 11 return ($command,$current,$previous,\@other_options, $quote);
260 }
261  
262 13 1;
263  
264 =pod
265  
266 =head1 NAME
267  
268 Getopt::Complete::Options - a command-line options specification
269  
270 =head1 VERSION
271  
272 This document describes Getopt::Complete 0.25
273  
274 =head1 SYNOPSIS
275  
276 This is used internally by Getopt::Complete during compile.
277  
278 my $opts = Getopt::Complete::Options->new(
279 'myfile=s' => 'f',
280 'mydir=s@' => 'd',
281 '<>' => ['one','two','three']
282 );
283  
284 $opts->option_names;
285 # myfile mydir
286
287 $opts->option_spec("mydir")
288 # '=s@'
289
290 $opts->option_handler("myfile")
291 # 'f'
292
293 $opts->option_handler("<>")
294 # ['one','two','three'];
295  
296 $opts->handle_shell_completion;
297 # if it detects it is talking to the shell completer, it will respond and then exit
298  
299 # this method is used by the above, then makes a Getopt::Complete::Args.
300 ($text_typed,$option_name,$remainder_of_argv) = $self->parse_completion_request($comp_line,$comp_point);
301  
302 =head1 DESCRIPTION
303  
304 Objects of this class are used to construct a Getop::Complete::Args from a list of
305 command-line arguments. It specifies what options are available to the command
306 line, helping to direct the parser.
307  
308 It also specifies what values are valid for those options, and provides an API
309 for access by the shell to do tab-completion.
310  
311 The valid values list is also used by Getopt::Complete::Args to validate its
312 option values, and produce the error list it generates.
313  
314 =head1 SEE ALSO
315  
316 L<Getopt::Complete>, L<Getopt::Complete::Args>, L<Getopt::Complete;:Compgen>
317  
318 =head1 COPYRIGHT
319  
320 Copyright 2010 Scott Smith and Washington University School of Medicine
321  
322 =head1 AUTHORS
323  
324 Scott Smith (sakoht at cpan .org)
325  
326 =head1 LICENSE
327  
328 This program is free software; you can redistribute it and/or modify it under
329 the same terms as Perl itself.
330  
331 The full text of the license can be found in the LICENSE file included with this
332 module.
333  
334 =cut
335  
« blib/lib/Getopt/Complete/Args.pm ↑ Summary blib/lib/Getopt/Complete.pm »