File Coverage

lib/Devel/Trepan/CmdProcessor/Command/Complete.pm
Criterion Covered Total %
statement 88 134 65.6
branch 5 36 13.8
condition 2 12 16.6
subroutine 26 28 92.8
pod 0 4 0.0
total 121 214 56.5


line stmt bran cond sub pod time code
1             # -*- coding: utf-8 -*-
2             # Copyright (C) 2011-2014 Rocky Bernstein <rocky@cpan.org>
3 12     12   73254 use warnings; no warnings 'redefine'; use utf8;
  12     12   32  
  12     12   375  
  12     2   86  
  12     2   24  
  12     2   354  
  12         555  
  12         38  
  12         102  
  2         14  
  2         4  
  2         56  
  2         9  
  2         4  
  2         54  
  2         23  
  2         4  
  2         26  
4              
5 12     12   306 use rlib '../../../..';
  12     2   33  
  12         72  
  2         51  
  2         4  
  2         9  
6              
7             package Devel::Trepan::CmdProcessor::Command::Complete;
8              
9 12     12   4872 use Getopt::Long qw(GetOptionsFromArray);
  12     2   10292  
  12         87  
  2         689  
  2         5  
  2         11  
10             use Devel::Trepan::Complete
11 12     12   1926 qw(complete_packages complete_subs complete_builtins);
  12     2   29  
  12         1605  
  2         227  
  2         5  
  2         222  
12              
13 12     12   649 use if !@ISA, Devel::Trepan::CmdProcessor::Command ;
  12     2   41  
  12         70  
  2         11  
  2         3  
  2         10  
14              
15             unless (@ISA) {
16 12     12   80 eval <<'EOE';
  12     12   28  
  12     12   680  
  12     12   75  
  12         27  
  12         588  
  12         75  
  12         36  
  12         604  
  12         71  
  12         31  
  12         527  
17             use constant CATEGORY => 'support';
18             use constant SHORT_HELP => 'List the completions for the rest of the line as a command';
19             use constant MAX_ARGS => undef; # Need at most this many -
20             # undef -> unlimited
21             use constant NEED_STACK => 0;
22             EOE
23             }
24              
25 12     12   17085 use strict;
  12     2   25  
  12         305  
  2         123  
  2         47  
  2         57  
26 12     12   65 use vars qw(@ISA);
  12     2   24  
  12         708  
  2         10  
  2         5  
  2         111  
27             @ISA = @CMD_ISA;
28 12     12   68 use vars @CMD_VARS; # Value inherited from parent
  12     2   23  
  12         20902  
  2         11  
  2         4  
  2         1828  
29              
30             our $NAME = set_name();
31             our $HELP = <<"HELP";
32             =pod
33              
34             B<complete> [I<options>] I<prefix>
35              
36             options:
37              
38             -b | --builtins
39             -f | --files
40             -p | --packages
41             -s | --subs
42              
43              
44             List the command completions of I<prefix>.
45              
46             =head2 Examples:
47              
48             complete se # => set server
49             complete -p Tie::H # => Tie::Hash (probably)
50             complete -s Tie::Hash::n
51             # => Tie::Hash::new
52              
53             =cut
54             HELP
55              
56             my $DEFAULT_OPTIONS = {
57             lexicals => 0,
58             files => 0,
59             'my' => 0,
60             'our' => 0,
61             packages => 0,
62             subs => 0,
63             };
64              
65             sub parse_options($$)
66             {
67 7     7 0 11 my ($self, $args) = @_;
  0     0 0 0  
68 7         30 my %opts = %$DEFAULT_OPTIONS;
  0         0  
69             my $result = &GetOptionsFromArray
70             ($args,
71             '-b' => \$opts{builtins},
72             '--builtins' => \$opts{builtins},
73             '-f' => \$opts{files},
74             '--files' => \$opts{files},
75             '-p' => \$opts{packages},
76             '--packages' => \$opts{packages},
77             '-s' => \$opts{subs},
78             '--subs' => \$opts{subs}
79 7         34 );
  0         0  
80              
81 7         2932 \%opts;
  0         0  
82              
83             }
84              
85             # This method runs the command
86             sub run($$) {
87 0     0 0 0 my ($self, $args) = @_;
  7     7 0 6164  
88 0         0 my @args = @{$args}; shift @args; # remove "complete".
  0         0  
  0         0  
  7         14  
  7         15  
  7         10  
89 0         0 my $opts = parse_options($self, \@args);
  7         19  
90              
91 0         0 my $proc = $self->{proc};
  7         14  
92              
93 0 0 0     0 if ($opts->{files}) {
  7 0 0     53  
    50 33        
    50 33        
94 0 0       0 if (scalar @args != 1) {
  0 0       0  
95 0         0 $proc->errmsg('Expecting only a single argument after options');
  0         0  
96 0         0 return;
  0         0  
97             }
98 0         0 foreach my $file ($proc->filename_complete($args[0])) {
  0         0  
99 0         0 $proc->msg($file);
  0         0  
100             }
101             } elsif ($opts->{builtins}||$opts->{packages}||$opts->{subs}) {
102 0 0       0 if (scalar @args != 1) {
  0 0       0  
103 0         0 $proc->errmsg('Expecting only a single argument after options');
  0         0  
104 0         0 return;
  0         0  
105             }
106 0         0 my $prefix = $args[0];
  0         0  
107 0         0 my @matches = ();
  0         0  
108 0 0       0 push @matches, complete_builtins($prefix) if ($opts->{builtins});
  0 0       0  
109 0 0       0 push @matches, complete_packages($prefix) if ($opts->{packages});
  0 0       0  
110 0 0       0 push @matches, complete_subs($prefix) if ($opts->{subs});
  0 0       0  
111 0         0 for my $match (@matches) {
  0         0  
112 0         0 $proc->msg($match);
  0         0  
113             }
114             } else {
115 0         0 my $cmd_argstr = $proc->{cmd_argstr};
  7         14  
116 0 0       0 my $last_arg = (' ' eq substr($cmd_argstr, -1)) ? '' : $args[-1];
  7 100       18  
117 0 0       0 $last_arg = '' unless defined $last_arg;
  7 50       16  
118 0         0 for my $match ($proc->complete($cmd_argstr, $cmd_argstr,
  7         33  
119             0, length($cmd_argstr))) {
120 0         0 $proc->msg($match);
  23         83  
121             }
122             }
123             }
124              
125             unless (caller) {
126             require Devel::Trepan::CmdProcessor;
127             my $proc = Devel::Trepan::CmdProcessor->new;
128             my $cmd = __PACKAGE__->new($proc);
129             for my $prefix (qw(d b bt)) {
130             $cmd->{proc}{cmd_argstr} = $prefix;
131             $cmd->run([$cmd->name, $prefix]);
132             print '=' x 40, "\n";
133             }
134             for my $prefix ('set a') {
135             $cmd->{proc}{cmd_argstr} = $prefix;
136             $cmd->run([$cmd->name, $prefix]);
137             print '=' x 40, "\n";
138             }
139             for my $prefix ('help syntax c') {
140             $cmd->{proc}{cmd_argstr} = $prefix;
141             $cmd->run([$cmd->name, $prefix]);
142             print '=' x 40, "\n";
143             }
144              
145             %DB::sub = (__PACKAGE__ . '::run', 1);
146             for my $tuple (['-b', 'call'], ['-p', __PACKAGE__],
147             ['-s', __PACKAGE__ . '::r']) {
148             my ($opt, $prefix) = @$tuple;
149             $cmd->{proc}{cmd_argstr} = $prefix;
150             $cmd->run([$cmd->name, $opt, $prefix]);
151             print '=' x 40, "\n";
152             }
153             # $cmd->run([$cmd->name, 'fdafsasfda']);
154             }
155              
156             1;