source:
perl/lib/BarnOwl/Completion/Util.pm
@
6c144b3
Last change on this file since 6c144b3 was 6c144b3, checked in by Nelson Elhage <nelhage@mit.edu>, 15 years ago | |
---|---|
|
|
File size: 1.2 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 | ||
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; | |
[6c144b3] | 31 | $idx++; |
[e711ca7] | 32 | last; |
33 | } | |
[82a6e8b] | 34 | $flag = $word if(exists $args->{$word}); |
35 | } elsif ($word =~ m{^-}) { | |
36 | $word = "-" . substr($word, -1); | |
37 | $flag = $word if(exists $args->{$word}); | |
[4fbc7f5] | 38 | } else { |
39 | $argct++; | |
[82a6e8b] | 40 | } |
41 | $idx++; | |
42 | } | |
[6c144b3] | 43 | # Account for any words we skipped |
44 | $argct += $ctx->word - $idx; | |
[82a6e8b] | 45 | |
46 | if($flag) { | |
47 | my $c = $args->{$flag}; | |
48 | if($c) { | |
49 | return $c->($ctx); | |
50 | } | |
51 | return; | |
52 | } else { | |
[e711ca7] | 53 | return ($optsdone ? () : (@$no_args, keys %$args), |
[4fbc7f5] | 54 | $default ? ($default->($ctx, $argct)) : ()); |
[82a6e8b] | 55 | } |
56 | } |
Note: See TracBrowser
for help on using the repository browser.