blib/lib/Getopt/Complete/Args.pm

filter: Statementstmt 71.7% 173 / 241 Branchbran 57.1% 72 / 126 Conditioncond 42.0% 21 / 50 MC/DCmcdc 21.4% 6 / 28 Subroutinesub 82.6% 19 / 23 Podpod 45.4% 5 / 11 totaltotal 61.8% 296 / 479 CC 88 SCAR 40.8
CC
88
Cov
64%
_validate_values
170.5
resolve_possible_completions
67.8
_init
15.6
CRAP 59.3
« blib/lib/Getopt/Complete/Cache.pm ↑ Summary blib/lib/Getopt/Complete/Options.pm »
1 package Getopt::Complete::Args;
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 Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub new {
12 12 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=26.3
Pod undocumented
13 12 my $self = bless {
14 'options' => undef,
15 'values' => {},
16 'errors' => [],
17 'argv' => undef,
18 @_,
19 }, $class;
20  
21 12 ▶ unless ($self->{argv}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"argv"}) 0 12 12
22 0 die "No argv passed to " . __PACKAGE__ . " constructor!";
23 }
24
25 12 my $options = $self->{options};
26  
27 12 ▶ unless ($options) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options) 0 12 12
28 0 die "No options passed to " . __PACKAGE__ . " constructor!";
29 }
30  
31 12 my $type = ref($options);
32 12 ▶ if (not $type) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $type) { } 0 12 12
elsif ($type eq 'ARRAY') { } 0 12 12
elsif ($type eq 'HASH') { } 0 12 12
elsif (builtin::blessed($options)) { } 12 0 12
33 0 die "Expected Getopt::Complete::Options, or a constructor ARRAY/HASH for ''options''. Got: $type $options.";
34 }
35 elsif ($type eq 'ARRAY') {
36 0 $self->{options} = Getopt::Complete::Options(@$options);
37 }
38 elsif ($type eq 'HASH') {
39 0 $self->{options} = Getopt::Complete::Options(%$options);
40 }
41 elsif (Scalar::Util::blessed($options)) {
42 12 ▶ if (not $options->isa("Getopt::Complete::Options")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->isa("Getopt::Complete::Options")) 0 12 12
43 0 die "Expected Getopt::Complete::Options, or a constructor ARRAY/HASH for ''options''. Got: $options.";
44 }
45 }
46 else {
47 0 die "Expected Getopt::Complete::Options, or a constructor ARRAY/HASH for ''options''. Got reference $options.";
48 }
49
50 12 $self->_init();
51  
52 12 return $self;
53 }
54  
55 sub options {
56 160 ▶ shift->{options};
Subroutine options: called CC=1 SCAR=0.0
Pod documented
57 }
58  
59 sub argv {
60 0 ▶ @{ shift->{argv} };
Subroutine argv: not called CC=1 SCAR=6.9
Pod documented
61 }
62  
63 sub errors {
64 12 ▶ @{ shift->{errors} }
Subroutine errors: called CC=1 SCAR=0.0
Pod undocumented
65 }
66  
67 13 for my $method (qw/sub_commands option_names option_specs option_spec completion_handler/) {
68 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
69 65 *{$method} = sub {
70 64 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
71 64 my $options = $self->options;
72 64 return $options->$method(@_);
73 }
74 65 }
75  
76 sub has_value {
77 8 ▶ my $self = shift;
Subroutine has_value: called CC=1 SCAR=0.0
Pod undocumented
78 8 my $name = shift;
79 8 return exists $self->{'values'}{$name};
80 }
81  
82 sub value {
83 34 ▶ my $self = shift;
Subroutine value: called CC=1 SCAR=0.0
Pod documented
84 34 my $name = shift;
85 34 my $value = $self->{'values'}{$name};
86 34 return $value;
87 }
88  
89 sub bare_args {
90 0 ▶ my $self = shift;
Subroutine bare_args: not called CC=1 SCAR=6.9
Pod documented
91 0 my $name = shift;
92 0 my $value = $self->{'values'}{'<>'};
93 0 return $value;
94 }
95  
96 sub parent_sub_commands {
97 0 ▶ my $self = shift;
Subroutine parent_sub_commands: not called CC=1 SCAR=6.9
Pod documented
98 0 my $name = shift;
99 0 my $value = $self->{'values'}{'>'};
100 0 return $value;
101 }
102  
103 sub _init {
104 12 ▶ my $self = shift;
Subroutine _init: called CC=14 SCAR=27.5
105
106 # as long as the first word is a valid sub-command, drill down to the subordinate options list,
107 # and also shift the args into a special buffer
108 # (if you have sub-commands AND bare arguments, and the arg is a valid sub-command ...don't do that
109 12 local @ARGV = @{ $self->{argv} };
110 12 my @sub_command_path;
111 12 ▶ while (@ARGV and my $delegate = $self->options->completion_handler('>' . $ARGV[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV and my $delegate = $self->options->completion_handler('>' . $ARGV[0])) 17 12 29
Condition100%3/3
@ARGV and my $delegate = $self->options->completion_handler('>' . $ARGV[0])
!ll&&!rl&&r
6617
MC/DC100%2/2
@ARGV and my $delegate = $self->options->completion_handler('>' . $ARGV[0])
@ARGV my $delegate = $self->options->completion_handler('>' . $ARGV[0])
Truth table100%3/3
A and B
A= @ARGVB= my $delegate = $self->options->completion_handler('>' . $ARGV[0])
ABresult
0X0
100
111
112 17 push @sub_command_path, shift @ARGV;
113 17 $self->{options} = $delegate;
114 }
115  
116 12 my %values;
117 my @errors;
118  
119 12 do {
120 12 ▶ local $SIG{__WARN__} = sub { push @errors, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
121 12 my $retval = Getopt::Long::GetOptions(\%values,$self->options->option_specs);
122 12 ▶ if (!$retval and @errors == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retval and @errors == 0) 0 12 12
Condition66%2/3
not $retval and @errors == 0
!ll&&!rl&&r
1110
MC/DC0%0/2
not $retval and @errors == 0
not $retval @errors == 0
Truth table66%2/3
not(A) and B
A= not $retvalB= @errors == 0
ABresult
0X0
100
111
123 0 push @errors, "unknown error processing arguments!";
124 }
125 12 ▶ if ($ENV{COMP_LINE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"COMP_LINE"}) 11 1 12
126 # we want to allow unknown option if the user puts them in, we just
127 # didn't help complete it
128 11 @errors = grep { $_ !~ /^Unknown option:/ } @errors;
129 }
130 };
131  
132 12 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 2 10 12
133 2 ▶ if ($self->options->has_option('<>')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->options->has_option('<>')) { } 0 2 2
134 0 ▶ my $a = $values{'<>'} ||= [];
Condition0%0/2
$values{"<>"} ||= []
l!l
00
MC/DC0%0/1
$values{"<>"} ||= []
$values{"<>"}
Truth table0%0/2
A
A= $values{"<>"}
Aresult
11
00
135 0 push @$a, @ARGV;
136 }
137 else {
138 # in order to allow bare-args we only block unexpected arguments
139 # for commands with sub-commands
140 2 ▶ if ( $self->sub_commands ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sub_commands) 2 0 2
141 2 for my $arg (@ARGV) {
142 2 push @errors, "unexpected sub-command: $arg";
143 }
144 }
145 }
146 }
147  
148 12 ▶ if (@sub_command_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sub_command_path) 10 2 12
149 10 $values{'>'} = \@sub_command_path;
150 }
151  
152 12 %{ $self->{'values'} } = %values;
153
154 12 ▶ if (my @more_errors = $self->_validate_values()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@more_errors) = $self->_validate_values) 0 12 12
155 0 push @errors, @more_errors;
156 }
157  
158 12 @{ $self->{'errors'} } = @errors;
159  
160 12 ▶ return (@errors ? () : 1);
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 3 9 12
161 }
162  
163  
164 sub _validate_values {
165 12 ▶ my $self = shift;
Subroutine _validate_values: called CC=22 SCAR=51.4
166  
167 12 my @failed;
168 12 for my $key (keys %{ $self->options->{completion_handlers} }) {
169 44 my $completion_handler= $self->options->completion_handler($key);
170 44 my $completions;
171 44 ▶ if (ref($completion_handler) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $completion_handler eq 'CODE') { } 14 30 44
elsif (ref $completion_handler eq 'ARRAY') { } 0 30 30
172 # defer setting $completions
173 }
174 elsif (ref($completion_handler) eq 'ARRAY') {
175 0 $completions = $completion_handler;
176 0 $completion_handler = undef;
177 }
178 else {
179 #warn "unexpected completion specification for $key: $completion_handler???";
180 30 next;
181 }
182  
183 14 my ($name,$spec) = ($key =~ /^([\w|-|\>][\w|-]*|\<\>|)(\W.*|)/);
184 #my ($dashes,$name,$spec) = ($key =~ /^(\-*?)([\w|-]*|\<\>|)(\W.*|)/);
185 14 ▶ if (not defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 14 14
186 0 print STDERR "key $key is unparsable in " . __PACKAGE__ . " spec inside of $0 !!!";
187 0 next;
188 }
189 14 ▶ if ($name eq '<>' and not $spec) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "<>" and not $spec) 0 14 14
Condition33%1/3
$name eq "<>" and not $spec
!ll&&!rl&&r
1400
MC/DC0%0/2
$name eq "<>" and not $spec
$name eq "<>" not $spec
Truth table33%1/3
A and not(B)
A= $name eq "<>"B= not $spec
ABresult
0X0
100
111
190 0 $spec = '=s@';
191 }
192  
193 14 my $value_returned = $self->value($name);
194 14 ▶ my @values = (ref($value_returned) ? @$value_returned : $value_returned);
Branch: 1 branch
BranchTrueFalseTotal
ref $value_returned ? : 0 14 14
195
196 14 my $all_valid_values;
197 14 for my $value (@values) {
198 14 ▶ next if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 12 2 14
199 2 ▶ next if not defined $completions;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $completions 2 0 2
200 0 my @valid_values_shown_in_message;
201 0 ▶ if ($completion_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if ($completion_handler) 0 0 0
202 # we pass in the value as the "completeme" word, so that the callback
203 # can be as optimal as possible in determining if that value is acceptable.
204 0 $completions = $completion_handler->(undef,$value,$key,$self->{'values'});
205 0 ▶ if (not defined $completions or not ref($completions) eq 'ARRAY' or @$completions == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $completions && ref $completions eq "ARRAY" or @$completions == 0) 0 0 0
Condition0%0/6
not defined $completions && ref $completions eq "ARRAY" or @$completions == 0
l!l&&r!l&&!r
000
defined $completions && ref $completions eq 'ARRAY'
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $completions && ref $completions eq "ARRAY" or @$completions == 0
defined $completions ref $completions eq 'ARRAY' @$completions == 0
Truth table0%0/4
not(A && B) or C
A= defined $completionsB= ref $completions eq 'ARRAY'C= @$completions == 0
ABCresult
0XX1
10X1
1111
1100
206 # if not, we give it the chance to give us the full list of options
207 0 $completions = $completion_handler->(undef,undef,$key,$self->{'values'});
208 }
209 0 ▶ unless (ref($completions) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $completions eq "ARRAY") 0 0 0
210 0 warn "unexpected completion specification for $key: $completions???";
211 0 next;
212 }
213 }
214 0 my @valid_values = @$completions;
215 0 @valid_values_shown_in_message = @valid_values;
216
217 0 ▶ if (ref($valid_values[-1]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid_values[-1] eq "ARRAY") 0 0 0
218 0 push @valid_values, @{ pop(@valid_values) };
219 0 pop @valid_values_shown_in_message;
220 }
221 0 ▶ unless (grep { $_ eq $value } map { /(.*)\t$/ ? $1 : $_ } @valid_values) {
Branch: 2 branches
BranchTrueFalseTotal
unless (grep {$_ eq $value;} map({/(.*)\t$/ ? $1 : $_;} @valid_values)) 0 0 0
/(.*)\t$/ ? : 0 0 0
222 0 ▶ my $msg = ($key eq '<>' ? "invalid argument $value." : "$key has invalid value $value.");
Branch: 1 branch
BranchTrueFalseTotal
$key eq '<>' ? : 0 0 0
223 0 ▶ if (@valid_values_shown_in_message) {
Branch: 1 branch
BranchTrueFalseTotal
if (@valid_values_shown_in_message) 0 0 0
224 0 ▶ $msg .= " Select from: " . join(", ", map { /^(.+)\t$/ ? $1 : $_ } @valid_values_shown_in_message);
Branch: 1 branch
BranchTrueFalseTotal
/^(.+)\t$/ ? : 0 0 0
225 }
226 0 push @failed, $msg;
227 }
228 }
229 }
230 12 return @failed;
231 }
232  
233 sub resolve_possible_completions {
234 9 ▶ my ($self, $command, $current, $previous) = @_;
Subroutine resolve_possible_completions: called CC=33 SCAR=42.2
Pod undocumented
235  
236 9 my $all = $self->{values};
237  
238 9 ▶ $previous = '' if not defined $previous;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $previous 7 2 9
239  
240 9 my @possibilities;
241  
242 9 my ($dashes,$resolve_values_for_option_name) = ($previous =~ /^(-{1,2})(.*)/);
243 9 my $is_option_name = 0;
244 9 ▶ if (not length $previous) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $previous) 7 2 9
245 # no specific option is before this: a sub-command, a bare argument, or an option name
246 7 ▶ if ($current =~ /^(-+)/
Branch: 1 branch
BranchTrueFalseTotal
if ($current =~ /^(-+)/ or $current eq '' and not $self->sub_commands and not $self->options->has_option('<>')) { } 4 3 7
Condition77%7/9
$current =~ /^(-+)/ or $current eq "" and not $self->sub_commands and not $self->options->has_option("<>")
l!l&&r!l&&!r
133
$current eq "" and not $self->sub_commands and not $self->options->has_option("<>")
!ll&&!rl&&r
303
$current eq "" and not $self->sub_commands
!ll&&!rl&&r
303
MC/DC50%2/4
$current =~ /^(-+)/ or $current eq "" and not $self->sub_commands and not $self->options->has_option("<>")
$current =~ /^(-+)/ $current eq "" not $self->sub_commands not $self->options->has_option("<>")
Truth table60%3/5
A or B and not(C) and not(D)
A= $current =~ /^(-+)/B= $current eq ""C= not $self->sub_commandsD= not $self->options->has_option("<>")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
247 or (
248 $current eq ''
249 and not ($self->sub_commands)
250 and not ($self->options->has_option('<>'))
251 )
252 ) {
253 # the incomplete word is an option name
254 4 $is_option_name = 1;
255  
256 4 my @args = $self->option_names;
257
258 # We only show the negative version of boolean options
259 # when the user already has "--no-" on the line.
260 # Otherwise, we just include --no- as a possible (partial) completion
261 13 ▶ no warnings; #########
Subroutine BEGIN: called CC=1 SCAR=0.0
262 my %boolean =
263 8 map { $_ => 1 }
264 8 grep { not $self->has_value($_) }
265 20 grep { $self->option_spec($_) =~ /\!/ }
266 4 ▶ grep { $_ ne '<>' and substr($_,0,1) ne '>' }
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "<>" and substr($_, 0, 1) ne ">" 20 0 20
267 @args;
268  
269 4 ▶ my $show_negative_booleans = ($current =~ /^--no-/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$current =~ /^--no-/ ? : 0 4 4
270 @possibilities =
271 18 ▶ map { length($_) ? ('--' . $_) : ('-') }
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 18 0 18
272 map {
273 18 ▶ ($self->option_spec($_) =~ /\=/ ? "$_=\t" : $_ )
Branch: 1 branch
BranchTrueFalseTotal
$self->option_spec($_) =~ /\=/ ? : 10 8 18
274 }
275 map {
276 18 ▶ ($show_negative_booleans and $boolean{$_} and not substr($_,0,3) eq 'no-')
Branch: 1 branch
BranchTrueFalseTotal
$show_negative_booleans && $boolean{$_} && !(substr($_, 0, 3) eq 'no-') ? : 0 18 18
Condition33%2/6
$show_negative_booleans and $boolean{$_} and not substr($_, 0, 3) eq "no-"
!ll&&!rl&&r
1800
$show_negative_booleans and $boolean{$_}
!ll&&!rl&&r
1800
MC/DC0%0/3
$show_negative_booleans and $boolean{$_} and not substr($_, 0, 3) eq "no-"
$show_negative_booleans $boolean{$_} not substr($_, 0, 3) eq "no-"
Truth table25%1/4
A and B and not(C)
A= $show_negative_booleansB= $boolean{$_}C= not substr($_, 0, 3) eq "no-"
ABCresult
0XX0
10X0
1100
1111
277 ? ($_, 'no-' . $_)
278 : $_
279 }
280 grep {
281 20 ▶ not (defined $self->value($_) and not $self->option_spec($_) =~ /@/)
Condition66%2/3
defined $self->value($_) && !($self->option_spec($_) =~ /@/)
!ll&&!rl&&r
1802
MC/DC50%1/2
defined $self->value($_) && !($self->option_spec($_) =~ /@/)
defined $self->value($_) !($self->option_spec($_) =~ /@/)
Truth table66%2/3
A && not(B)
A= defined $self->value($_)B= !($self->option_spec($_) =~ /@/)
ABresult
0X0
100
111
282 }
283 4 ▶ grep { $_ ne '<>' and substr($_,0,1) ne '>' }
Branch: 1 branch
BranchTrueFalseTotal
$_ ne "<>" and substr($_, 0, 1) ne ">" 20 0 20
284 @args;
285 4 ▶ if (%boolean and not $show_negative_booleans) {
Branch: 1 branch
BranchTrueFalseTotal
if (%boolean and not $show_negative_booleans) 4 0 4
Condition33%1/3
%boolean and not $show_negative_booleans
!ll&&!rl&&r
004
MC/DC0%0/2
%boolean and not $show_negative_booleans
%boolean not $show_negative_booleans
Truth table33%1/3
A and not(B)
A= %booleanB= not $show_negative_booleans
ABresult
0X0
100
111
286 # a partial completion for negating booleans when we're NOT
287 # already showing the complete list
288 4 push @possibilities, "--no-\t";
289 }
290 4 ▶ if ($current =~ /-{1,2}(.+?)=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current =~ /-{1,2}(.+?)=(.*)/) 0 4 4
291 # using the --key=value syntax..
292 0 my ($option,$value) = ($1,$2);
293 0 @possibilities = $self->reduce_possibilities_for_current_word('--' . $option, @possibilities);
294 0 ▶ if (!@possibilities || @possibilities == 1 and length($current) >= $possibilities[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (!@possibilities || @possibilities == 1 and length $current >= $possibilities[0]) 0 0 0
Condition0%0/6
!@possibilities || @possibilities == 1 and length $current >= $possibilities[0]
!ll&&!rl&&r
000
not @possibilities or @possibilities == 1
l!l&&r!l&&!r
000
MC/DC0%0/3
!@possibilities || @possibilities == 1 and length $current >= $possibilities[0]
not @possibilities @possibilities == 1 length $current >= $possibilities[0]
Truth table0%0/5
not(A) or B and C
A= not @possibilitiesB= @possibilities == 1C= length $current >= $possibilities[0]
ABCresult
00X0
1X00
0100
1X11
0111
295 # the key portion is complete
296 # continue below as though were were doing a regular value completion
297 0 $resolve_values_for_option_name = $option;
298 0 ▶ $current = ($value eq "\t" ? '' : $value);
Branch: 1 branch
BranchTrueFalseTotal
$value eq "\t" ? : 0 0 0
299 0 @possibilities = ();
300 }
301 }
302 }
303 else {
304 # bare argument or sub-command
305 3 $resolve_values_for_option_name = '<>';
306 }
307 }
308
309 9 ▶ if ($resolve_values_for_option_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve_values_for_option_name) 5 4 9
310 # either a value for a named option, or a bare argument.
311 5 ▶ if (my $handler = $self->completion_handler($resolve_values_for_option_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $handler = $self->completion_handler($resolve_values_for_option_name)) { } 1 4 5
elsif ($resolve_values_for_option_name and not $self->sub_commands) { } 2 2 4
312 # the incomplete word is a value for some option (possible the option '<>' for bare args)
313 1 ▶ if (defined($handler) and not ref($handler) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handler and not ref $handler eq "ARRAY") 1 0 1
Condition33%1/3
defined $handler and not ref $handler eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $handler and not ref $handler eq "ARRAY"
defined $handler not ref $handler eq "ARRAY"
Truth table33%1/3
A and not(B)
A= defined $handlerB= not ref $handler eq "ARRAY"
ABresult
0X0
100
111
314 1 $handler = $handler->($command,$current,$previous,$all);
315 }
316 1 ▶ unless (ref($handler) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $handler eq "ARRAY") 0 1 1
317 0 die "values for $previous must be an arrayref! got $handler\n";
318 }
319 1 @possibilities = @$handler;
320 }
321 ▶ elsif ($resolve_values_for_option_name && !$self->sub_commands) {
Condition66%2/3
$resolve_values_for_option_name and not $self->sub_commands
!ll&&!rl&&r
022
MC/DC50%1/2
$resolve_values_for_option_name and not $self->sub_commands
$resolve_values_for_option_name not $self->sub_commands
Truth table66%2/3
A and not(B)
A= $resolve_values_for_option_nameB= not $self->sub_commands
ABresult
0X0
100
111
322 2 my $handler = Getopt::Complete::files->($command,$current,$previous,$all);
323 2 @possibilities = @$handler;
324 }
325 else {
326 # no possibilities
327 2 @possibilities = ();
328 }
329  
330 5 ▶ if ($resolve_values_for_option_name eq '<>') {
Branch: 1 branch
BranchTrueFalseTotal
if ($resolve_values_for_option_name eq "<>") 3 2 5
331 3 push @possibilities, $self->sub_commands;
332 3 ▶ if (grep { $_ ne '<>' and substr($_,0,1) ne '>' } $self->option_names) {
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$_ ne "<>" and substr($_, 0, 1) ne ">";} $self->option_names) 3 0 3
$_ ne "<>" and substr($_, 0, 1) ne ">" 9 0 9
333 # do a partial completion on dashes if there are any non-bare (option) arguments
334 #push @possibilities, "--\t"
335 }
336 }
337 }
338  
339 9 my @matches = $self->reduce_possibilities_for_current_word($current,@possibilities);
340 9 return @matches;
341 }
342  
343 sub reduce_possibilities_for_current_word {
344 9 ▶ my ($self, $current, @possibilities) = @_;
Subroutine reduce_possibilities_for_current_word: called CC=4 SCAR=13.9
Pod undocumented
345
346 9 ▶ my $uncompletable_valid_possibilities = pop @possibilities if ref($possibilities[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $possibilities[-1] 3 6 9
347
348 # Determine which possibilities will actually match the current word
349 # The shell does this for us, but we need to do it to predict a few things
350 # and to adjust what we show the shell.
351 # This loop also determines which options should complete with a space afterward,
352 # and which options can be abbreviated when showing a list for the user.
353 9 my @matches;
354 my @nospace;
355 9 my @abbreviated_matches;
356 9 for my $p (@possibilities) {
357 34 my $i =index($p,$current);
358 34 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) 27 7 34
359 27 push @matches, $p;
360 }
361 }
362 9 return @matches;
363 }
364  
365 sub translate_completions_for_shell_display {
366 9 ▶ my ($self, $current, @matches) = @_;
Subroutine translate_completions_for_shell_display: called CC=12 SCAR=26.3
Pod undocumented
367  
368 9 ▶ my $uncompletable_valid_matches = pop @matches if ref($matches[-1]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $matches[-1] 0 9 9
369
370 # Determine which matches will actually match the current word
371 # The shell does this for us, but we need to do it to predict a few things
372 # and to adjust what we show the shell.
373 # This loop also determines which options should complete with a space afterward,
374 # and which options can be abbreviated when showing a list for the user.
375 9 my @printable;
376 my @nospace;
377 9 my @abbreviated_printable;
378 9 for my $p (@matches) {
379 27 my $m;
380 27 ▶ if (substr($p,length($p)-1,1) eq "\t") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($p, length($p) - 1, 1) eq "\t") { } 14 13 27
381 # a partial match: no space at the end so the user can "drill down"
382 14 $m = substr($p,0,length($p)-1);
383 14 $nospace[$#printable+1] = 1;
384 }
385 else {
386 13 $m = $p;
387 13 $nospace[$#printable+1] = 0;
388 }
389 27 ▶ if (substr($m,0,1) eq "\t") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($m, 0, 1) eq "\t") { } 0 27 27
390 # abbreviatable...
391 # (nothing does this currently, and the code below which uses it does not work yet)
392 0 my ($prefix,$abbreviation) = ($m =~ /^\t(.*)\t(.*)$/);
393 0 push @printable, $prefix . $abbreviation;
394 0 push @abbreviated_printable, $abbreviation;
395 }
396 else {
397 27 push @printable, $m;
398 27 push @abbreviated_printable, $m;
399 }
400 }
401  
402 9 ▶ if (@printable == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@printable == 1) { } 1 8 9
403 # there is one match
404 # the shell will complete it if it is not already complete, and put a space at the end
405 1 ▶ if ($nospace[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nospace[0]) { } 0 1 1
406 # We don't want a space, and there is no way to tell bash that, so we trick it.
407 0 ▶ if ($printable[0] eq $current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($printable[0] eq $current) { } 0 0 0
408 # It IS done completing the word: return nothing so it doesn't stride forward with a space
409 # It will think it has a bad completion, effectively.
410 0 @printable = ();
411 }
412 else {
413 # It is NOT done completing the word.
414 # We return 2 items which start with the real value, but have an arbitrary ending.
415 # It will show everything but that ending, and then stop.
416 0 push @printable, $printable[0];
417 0 $printable[0] .= 'A';
418 0 $printable[1] .= 'B';
419 }
420 }
421 else {
422 # we do want a space, so just let this go normally
423 }
424 }
425 else {
426 # There are multiple printable to the text already typed.
427 # If all of them have a prefix in common, the shell will complete that much.
428 # If not, it will show a list.
429 # We may not want to show the complete text of each word, but a shortened version,
430 8 my $first_mismatch = eval {
431 8 my $pos;
432 13 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
433 8 ▶ for ($pos=0; $pos < length($printable[0]); $pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $printable[0]) 15 3 18
434 15 my $expected = substr($printable[0],$pos,1);
435 15 for my $match (@printable[1..$#printable]) {
436 53 ▶ if (substr($match,$pos,1) ne $expected) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($match, $pos, 1) ne $expected) 5 48 53
437 5 return $pos;
438 }
439 }
440 }
441 3 return $pos;
442 };
443
444  
445 # NOTE: nothing does this currently, and the code below does not work.
446 # Enable to get file/directory completions to be short, like is default in the shell.
447 8 if (0) {
448 my $current_length = length($current);
449 if (@printable and ($first_mismatch == $current_length)) {
450 # No partial completion will occur: the shell will show a list now.
451 # Attempt abbreviation of the displayed options:
452  
453 my @printable = @abbreviated_printable;
454  
455 #my $cut = $current;
456 #$cut =~ s/[^\/]+$//;
457 #my $cut_length = length($cut);
458 #my @printable =
459 # map { substr($_,$cut_length) }
460 # @printable;
461  
462 # If there are > 1 abbreviated items starting with the same character
463 # the shell won't realize they're abbreviated, and will do completion
464 # instead of listing options. We force some variation into the list
465 # to prevent this.
466 my $first_c = substr($printable[0],0,1);
467 my @distinct_firstchar = grep { substr($_,0,1) ne $first_c } @printable[1,$#printable];
468 unless (@distinct_firstchar) {
469 # this puts an ugly space at the beginning of the completion set :(
470 push @printable,' ';
471 }
472 }
473 else {
474 # some partial completion will occur, continue passing the list so it can do that
475 }
476 }
477 }
478  
479 9 for (@printable) {
480 27 s/ /\\ /g;
481 }
482  
483 9 return @printable;
484 }
485  
486 sub __install_as_default__ {
487 0 ▶ my $self = shift;
Subroutine __install_as_default__: not called CC=1 SCAR=6.9
488 0 *Getopt::Complete::ARGS = \$self;
489 0 *Getopt::Complete::ARGS = \%{ $self->{values} };
490 }
491  
492 13 1;
493  
494 =pod
495  
496 =head1 NAME
497  
498 Getopt::Complete::Args - a set of option/value pairs
499  
500 =head1 VERSION
501  
502 This document describes Getopt::Complete::Args 0.25.
503  
504 =head1 SYNOPSIS
505  
506 This is used internally by Getopt::Complete during compile.
507  
508 A hand-built implementation might use the objects directly, and
509 look like this:
510  
511 # process @ARGV...
512
513 my $args = Getopt::Complete::Args->new(
514 options => [ # or pass a Getopt::Complete::Options directly
515 'myfiles=s@' => 'f',
516 'name' => 'u',
517 'age=n' => undef,
518 'fast!' => undef,
519 'color' => ['red','blue','yellow'],
520 ]
521 argv => \@ARGV
522 );
523  
524 $args->options->handle_shell_completion; # support 'complete -C myprogram myprogram'
525  
526 if (my @e = $args->errors) {
527 for my $e (@e) {
528 warn $e;
529 }
530 exit 1;
531 }
532  
533 # on to normal running of the program...
534  
535 for my $name ($args->option_names) {
536 my $spec = $args->option_spec($name);
537 my $value = $args->value($name);
538 print "option $name has specification $spec and value $value\n";
539 }
540  
541 =head1 DESCRIPTION
542  
543 An object of this class describes a set of option/value pairs, built from a L<Getopt::Complete::Options>
544 object and a list of command-line arguments (@ARGV).
545  
546 This is the class of the $Getopt::Complete::ARGS object, and $ARGS alias created at compile time.
547 It is also the source of the %ARGS hash injected into both of those namepaces at compile time.
548  
549 =head1 METHODS
550  
551 =over 4
552  
553 =item argv
554  
555 Returns the list of original command-line arguments.
556  
557 =item options
558  
559 Returns the L<Getopt::Complete::Options> object which was used to parse the command-line.
560  
561 =item value($option_name)
562  
563 Returns the value for a given option name after parsing.
564  
565 =item bare_args
566  
567 Returns the bare arguments. The same as ->value('<>')
568  
569 =item parent_sub_commands
570  
571 When using a tree of sub-commands, gives the list of sub-commands selected, in order to
572 get to this point. The options and option/value pairs apply to just this particular sub-command.
573  
574 The same as ->value('>').
575  
576 Distinct from ->sub_commands(), which returns the list of next possible choices when
577 drilling down.
578  
579 =item option_spec($name)
580  
581 Returns the GetOptions specification for the parameter in question.
582  
583 =item completion_handler($name)
584  
585 Returns the arrayref or code ref which handles resolving valid completions.
586  
587 =item sub_commands
588  
589 The list of sub-commands which are options at this level of a command tree.
590  
591 This is distinct from sub_command_path, which are the sub-commands which were chosen
592 to get to this level in the tree.
593  
594 =back
595  
596 =head1 SEE ALSO
597  
598 L<Getopt::Complete>, L<Getopt::Complete::Options>, L<Getopt::Complete::Compgen>
599  
600 =head1 COPYRIGHT
601  
602 Copyright 2010 Scott Smith and Washington University School of Medicine
603  
604 =head1 AUTHORS
605  
606 Scott Smith (sakoht at cpan .org)
607  
608 =head1 LICENSE
609  
610 This program is free software; you can redistribute it and/or modify it under
611 the same terms as Perl itself.
612  
613 The full text of the license can be found in the LICENSE file included with this
614 module.
615  
616 =cut
617  
« blib/lib/Getopt/Complete/Cache.pm ↑ Summary blib/lib/Getopt/Complete/Options.pm »