source: perl/lib/BarnOwl/Completion/Util.pm @ e19eb97

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since e19eb97 was 4fbc7f5, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
complete_flags: Pass the completer the argument number being completed This is the index of the argument (not flag or flag option) being completed, for convenience.
  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[82a6e8b]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
[4fbc7f5]21    my $argct = 0;
[e711ca7]22    my $optsdone = 0;
23
[82a6e8b]24    while($idx < $ctx->word) {
25        my $word = $ctx->words->[$idx];
26        if($flag) {
27            undef $flag;
28        } elsif($word =~ m{^--}) {
[e711ca7]29            if($word eq '--') {
30                $optsdone = 1;
31                last;
32            }
[82a6e8b]33            $flag = $word if(exists $args->{$word});
34        } elsif ($word =~ m{^-}) {
35            $word = "-" . substr($word, -1);
36            $flag = $word if(exists $args->{$word});
[4fbc7f5]37        } else {
38            $argct++;
[82a6e8b]39        }
40        $idx++;
41    }
42
43    if($flag) {
44        my $c = $args->{$flag};
45        if($c) {
46            return $c->($ctx);
47        }
48        return;
49    } else {
[e711ca7]50        return ($optsdone ? () : (@$no_args, keys %$args),
[4fbc7f5]51                $default ? ($default->($ctx, $argct)) : ());
[82a6e8b]52    }
53}
Note: See TracBrowser for help on using the repository browser.