release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change
on this file since de3f641 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
|
Line | |
---|
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 | my %options = (); |
---|
18 | %options = @_ if @_; |
---|
19 | |
---|
20 | my $idx = 1; |
---|
21 | my $flag = undef; |
---|
22 | |
---|
23 | my $argct = 0; |
---|
24 | my $optsdone = 0; |
---|
25 | |
---|
26 | while($idx < $ctx->word) { |
---|
27 | my $word = $ctx->words->[$idx]; |
---|
28 | if($flag) { |
---|
29 | undef $flag; |
---|
30 | } elsif($word =~ m{^--}) { |
---|
31 | if($word eq '--') { |
---|
32 | $optsdone = 1; |
---|
33 | $idx++; |
---|
34 | last; |
---|
35 | } |
---|
36 | $flag = $word if(exists $args->{$word}); |
---|
37 | } elsif ($word =~ m{^-}) { |
---|
38 | $word = "-" . substr($word, -1); |
---|
39 | $flag = $word if(exists $args->{$word}); |
---|
40 | } else { |
---|
41 | $argct++; |
---|
42 | if ($options{stop_at_nonflag}) { |
---|
43 | $optsdone = 1; |
---|
44 | $idx++; |
---|
45 | last; |
---|
46 | } |
---|
47 | } |
---|
48 | $idx++; |
---|
49 | } |
---|
50 | # Account for any words we skipped |
---|
51 | $argct += $ctx->word - $idx; |
---|
52 | |
---|
53 | if($flag) { |
---|
54 | my $c = $args->{$flag}; |
---|
55 | if($c) { |
---|
56 | return $c->($ctx); |
---|
57 | } |
---|
58 | return; |
---|
59 | } else { |
---|
60 | return ($optsdone ? () : (@$no_args, keys %$args), |
---|
61 | $default ? ($default->($ctx, $argct)) : ()); |
---|
62 | } |
---|
63 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.