source: perl/lib/BarnOwl/Completion/Util.pm @ 4ccd92c

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4ccd92c was 94ef58c, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago
Add keyword arg to complete_flags: stop_at_nonflag Signed-off-by: David Benjamin <davidben@mit.edu>
  • Property mode set to 100644
File size: 1.4 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
[94ef58c]17    my %options = ();
18    %options = @_ if @_;
[82a6e8b]19
20    my $idx = 1;
21    my $flag = undef;
22
[4fbc7f5]23    my $argct = 0;
[e711ca7]24    my $optsdone = 0;
25
[82a6e8b]26    while($idx < $ctx->word) {
27        my $word = $ctx->words->[$idx];
28        if($flag) {
29            undef $flag;
30        } elsif($word =~ m{^--}) {
[e711ca7]31            if($word eq '--') {
32                $optsdone = 1;
[6c144b3]33                $idx++;
[e711ca7]34                last;
35            }
[82a6e8b]36            $flag = $word if(exists $args->{$word});
37        } elsif ($word =~ m{^-}) {
38            $word = "-" . substr($word, -1);
39            $flag = $word if(exists $args->{$word});
[4fbc7f5]40        } else {
41            $argct++;
[94ef58c]42            if ($options{stop_at_nonflag}) {
43                $optsdone = 1;
44                $idx++;
45                last;
46            }
[82a6e8b]47        }
48        $idx++;
49    }
[6c144b3]50    # Account for any words we skipped
51    $argct += $ctx->word - $idx;
[82a6e8b]52
53    if($flag) {
54        my $c = $args->{$flag};
55        if($c) {
56            return $c->($ctx);
57        }
58        return;
59    } else {
[e711ca7]60        return ($optsdone ? () : (@$no_args, keys %$args),
[4fbc7f5]61                $default ? ($default->($ctx, $argct)) : ());
[82a6e8b]62    }
63}
Note: See TracBrowser for help on using the repository browser.