File Coverage

blib/lib/App/BashComplete.pm
Criterion Covered Total %
statement 20 39 51.2
branch 1 12 8.3
condition n/a
subroutine 7 11 63.6
pod 6 6 100.0
total 34 68 50.0


line stmt bran cond sub pod time code
1             package App::BashComplete;
2             {
3             $App::BashComplete::VERSION = '0.008';
4             }
5              
6             # ABSTRACT: command line interface to Bash::Complete
7              
8 1     1   989 use strict;
  1         2  
  1         50  
9 1     1   7 use warnings;
  1         2  
  1         38  
10 1     1   658 use Bash::Completion;
  1         4  
  1         42  
11 1     1   1481 use Getopt::Long qw(GetOptionsFromArray);
  1         37441  
  1         8  
12              
13             ############
14             # Attributes
15              
16              
17 1     1 1 7 sub opts { return $_[0]->{opts} }
18              
19              
20 0     0 1 0 sub cmd_line { return $_[0]->{cmd_line} }
21              
22              
23             #########
24             # Methods
25              
26              
27 1     1 1 475 sub new { return bless {opts => {}, cmd_line => []}, shift }
28              
29              
30              
31             sub run {
32 0     0 1 0 my $self = shift;
33              
34             # TODO: move commands to a plugin system
35              
36             # TODO: proper usage message
37 0 0       0 return 1 unless my $cmd = $self->_parse_options(@_);
38              
39 0 0       0 return $self->setup if $cmd eq 'setup';
40 0 0       0 return $self->complete if $cmd eq 'complete';
41              
42             # TODO: proper unknown command message
43 0         0 return 1;
44             }
45              
46              
47             #########
48             # Actions
49              
50              
51             sub complete {
52 0     0 1 0 my ($self) = @_;
53 0         0 my $cmd_line = $self->cmd_line;
54 0         0 my $plugin = shift @$cmd_line;
55              
56             ## TODO: need a plugin
57 0 0       0 return 1 unless $plugin;
58              
59 0         0 my $bc = Bash::Completion->new;
60 0         0 my $req = $bc->complete($plugin, $cmd_line);
61              
62 0 0       0 return 1 unless $req;
63              
64 0         0 print "$_\n" for $req->candidates;
65 0         0 return 0;
66             }
67              
68              
69              
70             sub setup {
71 0     0 1 0 my ($self) = @_;
72              
73 0         0 my $bc = Bash::Completion->new;
74 0         0 print $bc->setup;
75              
76 0         0 return 0;
77             }
78              
79              
80             #######
81             # Utils
82              
83             sub _parse_options {
84 1     1   675 my $self = shift;
85              
86 1         6 my $cmd_line = $self->{cmd_line} = [@_];
87 1         3 my $opts = $self->{opts} = {};
88              
89 1         6 my $ok = GetOptionsFromArray($cmd_line, $opts, 'help');
90              
91             # TODO: deal with !$ok
92 1 50       327 return unless $ok;
93              
94 1         7 return shift(@$cmd_line);
95             }
96              
97             1;
98              
99              
100              
101              
102             =pod
103              
104             =head1 NAME
105              
106             App::BashComplete - command line interface to Bash::Complete
107              
108             =head1 VERSION
109              
110             version 0.008
111              
112             =head1 ATTRIBUTES
113              
114             =head2 opts
115              
116             Returns an HashRef with all the command line options used.
117              
118             =head2 cmd_line
119              
120             Returns a ArrayRef with the parts of the command line that could not be parsed as options.
121              
122             =head1 METHODS
123              
124             =head2 new
125              
126             Creates a new empty instance.
127              
128             =head2 run
129              
130             Processes options, using both command line and arguments to run(), and
131             executes the proper action.
132              
133             =head2 complete
134              
135             =head2 setup
136              
137             Collects all plugins, decides which ones should be activated, and generates the bash complete command lines for each one.
138              
139             This allows you to setup your bash completion with only this:
140              
141             # Stick this into your .bashrc
142             eval $( bash-complete setup )
143              
144             The system will adjust to new plugins that you install via CPAN.
145              
146             =head1 AUTHOR
147              
148             Pedro Melo
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is Copyright (c) 2011 by Pedro Melo.
153              
154             This is free software, licensed under:
155              
156             The Artistic License 2.0 (GPL Compatible)
157              
158             =cut
159              
160              
161             __END__