File Coverage

blib/lib/Getopt/Complete/Compgen.pm
Criterion Covered Total %
statement 9 27 33.3
branch 0 6 0.0
condition 0 8 0.0
subroutine 3 4 75.0
pod n/a
total 12 45 26.6


line stmt bran cond sub pod time code
1             package Getopt::Complete::Compgen;
2              
3 2     2   12 use strict;
  2         3  
  2         49  
4 2     2   8 use warnings;
  2         3  
  2         537  
5              
6             our $VERSION = $Getopt::Complete::VERSION;
7              
8             # Support the shell-builtin completions.
9             # Some hackery seems to be required to replicate regular file completion.
10             # Case 1: you want to selectively not put a space after some options (incomplete directories)
11             # Case 2: you want to show only part of the completion value (the last dir in a long path)
12              
13             # Manufacture the long and short sub-names on the fly.
14             for my $subname (qw/
15             files
16             directories
17             commands
18             users
19             groups
20             environment
21             services
22             aliases
23             builtins
24             /) {
25             my $option = substr($subname,0,1);
26             my $code = sub {
27 0     0     my ($command,$value,$key,$args) = @_;
28 0   0       $value ||= '';
29 0           $value =~ s/\\/\\\\/;
30 0           $value =~ s/\'/\\'/;
31 0           my @f = grep { $_ !~/^\s+$/ } `bash -c "compgen -$option -- '$value'"`;
  0            
32 0           chomp @f;
33 0 0 0       if ($option eq 'f' or $option eq 'd') {
34             # bash is fine with ~/ paths but perl is not, need to translate
35 0           my $home_dir = (getpwuid($<))[7];
36 0           for (my $i = 0; $i < @f; $i++) {
37 0           my $perl_path = $f[$i];
38 0           $perl_path =~ s/^~/$home_dir/;
39 0 0         if ( -d $perl_path ) {
40 0           $f[$i] .= "/\t";
41             }
42             }
43              
44 0           my @not_shown = ($value);
45 0           push @f, \@not_shown;
46 0 0 0       push @not_shown, '-' if $Getopt::Complete::LONE_DASH_SUPPORT and $option eq 'f';
47             }
48 0           return \@f;
49             };
50 2     2   13 no strict 'refs';
  2         3  
  2         148  
51             *$subname = $code;
52             *$option = $code;
53             *{ 'Getopt::Complete::' . $subname } = $code;
54             *{ 'Getopt::Complete::' . $option } = $code;
55             }
56              
57             1;
58              
59             =pod
60              
61             =head1 NAME
62              
63             Getopt::Complete::Compgen - standard tab-completion callbacks
64              
65             =head1 VERSION
66              
67             This document describes Getopt::Complete::Compgen 0.25.
68              
69             =head1 SYNOPSIS
70              
71             # A completion spec can use any of the following, specified by a single
72             # word, or a single character, automatically:
73            
74             use Getopt::Complete(
75             'myfile' => 'files', # or 'f'
76             'mydir' => 'directories', # or 'd'
77             'mycommand' => 'commands', # or 'c'
78             'myuser' => 'users', # or 'u'
79             'mygroup' => 'groups', # or 'd'
80             'myenv' => 'environment', # or 'e'
81             'myservice' => 'services', # or 's'
82             'myalias' => 'aliases', # or 'a'
83             'mybuiltin' => 'builtins' # or 'b'
84             );
85              
86            
87              
88             =head1 DESCRIPTION
89              
90             This module contains subroutines which can be used as callbacks with Getopt::Complete,
91             and which implement all of the standard completions supported by the bash "compgen" builtin.
92              
93             Running "compgen -o files abc" will produce the completion list as though the user typed "abc",
94             with the presumption the user is attempting to complete file names.
95              
96             This module provides a subroutine names "files", with an alias named "f", which returns the same list.
97              
98             The subroutine is suitable for use in a callback in a Getopt::Complete competion specification.
99              
100             It does the equivalent for directories, executable commands, users, groups, environment variables, services,
101             aliases and shell builtins.
102              
103             These are the same:
104              
105             @matches = Getopt::Complete::Compgen::files("whatevercommand","abc","whateverparam",$whatever_other_args);
106            
107             @same = `bash -c "compgen -f sometext"`;
108             chomp @same;
109              
110             These are equivalent in any spec:
111              
112             \&Getopt::Complete::Compgen::files
113             \&Getopt;:Complete::Compgen::f
114             'Getopt::Complete::Compgen::files'
115             'Getopt;:Complete::Compgen::f'
116             'files'
117             'f'
118              
119             =head1 SEE ALSO
120              
121             L
122              
123             The manual page for bash details the bash built-in command "compgen", which this wraps.
124              
125             =head1 COPYRIGHT
126              
127             Copyright 2010 Scott Smith and Washington University School of Medicine
128              
129             =head1 AUTHORS
130              
131             Scott Smith (sakoht at cpan .org)
132              
133             =head1 LICENSE
134              
135             This program is free software; you can redistribute it and/or modify it under
136             the same terms as Perl itself.
137              
138             The full text of the license can be found in the LICENSE file included with this
139             module.
140              
141             =cut
142