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

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since f80ada8 was 82a6e8b, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Add a generic function for completing a command with options. Implement some minimal tests, too.
  • Property mode set to 100644
File size: 1.1 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    while($idx < $ctx->word) {
22        my $word = $ctx->words->[$idx];
23        BarnOwl::debug("[completing] idx=$idx word=$word ctx->word=@{[$ctx->word]}");
24        if($flag) {
25            undef $flag;
26        } elsif($word =~ m{^--}) {
27            last if $word eq '--';
28            $flag = $word if(exists $args->{$word});
29        } elsif ($word =~ m{^-}) {
30            $word = "-" . substr($word, -1);
31            $flag = $word if(exists $args->{$word});
32        }
33        $idx++;
34    }
35
36    if($flag) {
37        BarnOwl::debug("END: flag=$flag");
38        my $c = $args->{$flag};
39        if($c) {
40            return $c->($ctx);
41        }
42        return;
43    } else {
44        return (@$no_args,
45                keys %$args,
46                $default ? ($default->($ctx)) : ());
47    }
48}
Note: See TracBrowser for help on using the repository browser.