source:
perl/lib/BarnOwl/Completion/Util.pm
@
94ef58c
Last change on this file since 94ef58c was 94ef58c, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago | |
---|---|
|
|
File size: 1.4 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 | ||
[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.