App/BashComplete.pm

filter: Statementstmt 54.7% 23 / 42 Branchbran 8.3% 1 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 63.6% 7 / 11 Podpod 100% 6 / 6 totaltotal 52.1% 37 / 71 CC 8 SCAR 22.7
CC
8
Cov
44%
run
20.0
complete
20.0
_parse_options
2.0
CRAP 9.7
« Bash/Completion/Plugins/Perldoc.pm ↑ Summary Bash/Completion/Utils.pm »
1 package App::BashComplete;
2 {
3 1 $App::BashComplete::VERSION = '0.008';
4 }
5  
6 # ABSTRACT: command line interface to Bash::Complete
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Bash::Completion;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ############
14 # Attributes
15  
16  
17 1 ▶sub opts { return $_[0]->{opts} }
Subroutine opts: called CC=1 SCAR=0.0
Pod documented
18  
19  
20 0 ▶sub cmd_line { return $_[0]->{cmd_line} }
Subroutine cmd_line: not called CC=1 SCAR=6.9
Pod documented
21  
22  
23 #########
24 # Methods
25  
26  
27 1 ▶sub new { return bless {opts => {}, cmd_line => []}, shift }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28  
29  
30  
31 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
32 0 my $self = shift;
33  
34 # TODO: move commands to a plugin system
35  
36 # TODO: proper usage message
37 0 ▶ return 1 unless my $cmd = $self->_parse_options(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless my $cmd = $self->_parse_options(@_) 0 0 0
38  
39 0 ▶ return $self->setup if $cmd eq 'setup';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "setup" 0 0 0
40 0 ▶ return $self->complete if $cmd eq 'complete';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "complete" 0 0 0
41  
42 # TODO: proper unknown command message
43 0 return 1;
44 }
45  
46  
47 #########
48 # Actions
49  
50  
51 0 ▶sub complete {
Subroutine complete: not called CC=4 SCAR=30.0
Pod documented
52 0 my ($self) = @_;
53 0 my $cmd_line = $self->cmd_line;
54 0 my $plugin = shift @$cmd_line;
55  
56 ## TODO: need a plugin
57 0 ▶ return 1 unless $plugin;
Branch: 1 branch
BranchTrueFalseTotal
unless $plugin 0 0 0
58  
59 0 my $bc = Bash::Completion->new;
60 0 my $req = $bc->complete($plugin, $cmd_line);
61  
62 0 ▶ return 1 unless $req;
Branch: 1 branch
BranchTrueFalseTotal
unless $req 0 0 0
63  
64 0 print "$_\n" for $req->candidates;
65 0 return 0;
66 }
67  
68  
69  
70 0 ▶sub setup {
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
71 0 my ($self) = @_;
72  
73 0 my $bc = Bash::Completion->new;
74 0 print $bc->setup;
75  
76 0 return 0;
77 }
78  
79  
80 #######
81 # Utils
82  
83 1 ▶sub _parse_options {
Subroutine _parse_options: called CC=2 SCAR=7.0
84 1 my $self = shift;
85  
86 1 my $cmd_line = $self->{cmd_line} = [@_];
87 1 my $opts = $self->{opts} = {};
88  
89 1 my $ok = GetOptionsFromArray($cmd_line, $opts, 'help');
90  
91 # TODO: deal with !$ok
92 1 ▶ return unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
93  
94 1 return shift(@$cmd_line);
95 }
96  
97 1 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 <melo@cpan.org>
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__
« Bash/Completion/Plugins/Perldoc.pm ↑ Summary Bash/Completion/Utils.pm »