source:
perl/lib/BarnOwl/Completion/Util.pm
@
f1a2736
Last change on this file since f1a2736 was 69c27e6, checked in by David Benjamin <davidben@mit.edu>, 15 years ago | |
---|---|
|
|
File size: 1.6 KB |
Rev | Line | |
---|---|---|
[82a6e8b] | 1 | use strict; |
2 | use warnings; | |
3 | ||
4 | package BarnOwl::Completion::Util; | |
5 | ||
6 | use base qw(Exporter); | |
7 | our @EXPORT_OK = qw(complete_flags); | |
8 | ||
9 | use Getopt::Long; | |
10 | ||
11 | sub 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 | ||
[69c27e6] | 26 | my %flags_seen; |
27 | ||
[82a6e8b] | 28 | while($idx < $ctx->word) { |
29 | my $word = $ctx->words->[$idx]; | |
30 | if($flag) { | |
31 | undef $flag; | |
32 | } elsif($word =~ m{^--}) { | |
[e711ca7] | 33 | if($word eq '--') { |
34 | $optsdone = 1; | |
[6c144b3] | 35 | $idx++; |
[e711ca7] | 36 | last; |
37 | } | |
[82a6e8b] | 38 | $flag = $word if(exists $args->{$word}); |
39 | } elsif ($word =~ m{^-}) { | |
40 | $word = "-" . substr($word, -1); | |
[69c27e6] | 41 | $flags_seen{$word} = 1; # record flag |
[82a6e8b] | 42 | $flag = $word if(exists $args->{$word}); |
[4fbc7f5] | 43 | } else { |
44 | $argct++; | |
[94ef58c] | 45 | if ($options{stop_at_nonflag}) { |
46 | $optsdone = 1; | |
47 | $idx++; | |
48 | last; | |
49 | } | |
[82a6e8b] | 50 | } |
51 | $idx++; | |
52 | } | |
[6c144b3] | 53 | # Account for any words we skipped |
54 | $argct += $ctx->word - $idx; | |
[82a6e8b] | 55 | |
56 | if($flag) { | |
57 | my $c = $args->{$flag}; | |
58 | if($c) { | |
59 | return $c->($ctx); | |
60 | } | |
61 | return; | |
62 | } else { | |
[69c27e6] | 63 | my @opts = $optsdone ? () : (@$no_args, keys %$args); |
64 | # filter out flags we've seen if needbe | |
65 | @opts = grep {!$flags_seen{$_}} @opts unless $options{repeat_flags}; | |
66 | return (@opts, $default ? ($default->($ctx, $argct)) : ()); | |
[82a6e8b] | 67 | } |
68 | } |
Note: See TracBrowser
for help on using the repository browser.