source: perl/lib/BarnOwl/Completion/Util.pm @ 6c144b3

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6c144b3 was 6c144b3, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Fix $argct when given -- in complete_flags $argct is updated in the loop, but the loop is prematurely exited when it finds --. Signed-off-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 1.2 KB
Line 
1use strict;
2use warnings;
3
4package BarnOwl::Completion::Util;
5
6use base qw(Exporter);
7our @EXPORT_OK = qw(complete_flags);
8
9use Getopt::Long;
10
11sub complete_flags {
12    my $ctx     = shift;
13    my $no_args = shift;
14    my $args    = shift;
15    my $default = shift;
16
17
18    my $idx = 1;
19    my $flag = undef;
20
21    my $argct = 0;
22    my $optsdone = 0;
23
24    while($idx < $ctx->word) {
25        my $word = $ctx->words->[$idx];
26        if($flag) {
27            undef $flag;
28        } elsif($word =~ m{^--}) {
29            if($word eq '--') {
30                $optsdone = 1;
31                $idx++;
32                last;
33            }
34            $flag = $word if(exists $args->{$word});
35        } elsif ($word =~ m{^-}) {
36            $word = "-" . substr($word, -1);
37            $flag = $word if(exists $args->{$word});
38        } else {
39            $argct++;
40        }
41        $idx++;
42    }
43    # Account for any words we skipped
44    $argct += $ctx->word - $idx;
45
46    if($flag) {
47        my $c = $args->{$flag};
48        if($c) {
49            return $c->($ctx);
50        }
51        return;
52    } else {
53        return ($optsdone ? () : (@$no_args, keys %$args),
54                $default ? ($default->($ctx, $argct)) : ());
55    }
56}
Note: See TracBrowser for help on using the repository browser.