blib/lib/Getopt/Complete/Compgen.pm

filter: Statementstmt 100% 38 / 38 Branchbran 75.0% 6 / 8 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 80.9% 51 / 63 CC 8 SCAR 18.7
CC
8
Cov
87%
__ANON__
8.6
BEGIN
1.0
BEGIN
1.0
CRAP 6.5
« blib/lib/Getopt/Complete/LazyOptions.pm ↑ Summary Build »
1 package Getopt::Complete::Compgen;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 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 13 for my $subname (qw/
15 files
16 directories
17 commands
18 users
19 groups
20 environment
21 services
22 aliases
23 builtins
24 /) {
25 117 my $option = substr($subname,0,1);
26 my $code = sub {
27 3 ▶ my ($command,$value,$key,$args) = @_;
Subroutine __ANON__: called CC=8 SCAR=21.5
28 3 ▶ $value ||= '';
Condition50%1/2
$value ||= ""
l!l
30
MC/DC0%0/1
$value ||= ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
29 3 $value =~ s/\\/\\\\/;
30 3 $value =~ s/\'/\\'/;
31 3 my @f = grep { $_ !~/^\s+$/ } `bash -c "compgen -$option -- '$value'"`;
32 3 chomp @f;
33 3 ▶ if ($option eq 'f' or $option eq 'd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($option eq "f" or $option eq "d") 3 0 3
Condition33%1/3
$option eq "f" or $option eq "d"
l!l&&r!l&&!r
300
MC/DC0%0/2
$option eq "f" or $option eq "d"
$option eq "f" $option eq "d"
Truth table33%1/3
A or B
A= $option eq "f"B= $option eq "d"
ABresult
1X1
011
000
34 # bash is fine with ~/ paths but perl is not, need to translate
35 3 my $home_dir = (getpwuid($<))[7];
36 3 ▶ for (my $i = 0; $i < @f; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @f) 10 3 13
37 10 my $perl_path = $f[$i];
38 10 $perl_path =~ s/^~/$home_dir/;
39 10 ▶ if ( -d $perl_path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $perl_path) 4 6 10
40 4 $f[$i] .= "/\t";
41 }
42 }
43  
44 3 my @not_shown = ($value);
45 3 push @f, \@not_shown;
46 3 ▶ push @not_shown, '-' if $Getopt::Complete::LONE_DASH_SUPPORT and $option eq 'f';
Branch: 1 branch
BranchTrueFalseTotal
if $Getopt::Complete::LONE_DASH_SUPPORT and $option eq "f" 0 3 3
Condition33%1/3
$Getopt::Complete::LONE_DASH_SUPPORT and $option eq "f"
!ll&&!rl&&r
300
MC/DC0%0/2
$Getopt::Complete::LONE_DASH_SUPPORT and $option eq "f"
$Getopt::Complete::LONE_DASH_SUPPORT $option eq "f"
Truth table33%1/3
A and B
A= $Getopt::Complete::LONE_DASH_SUPPORTB= $option eq "f"
ABresult
0X0
100
111
47 }
48 3 return \@f;
49 117 };
50 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
51 117 *$subname = $code;
52 117 *$option = $code;
53 117 *{ 'Getopt::Complete::' . $subname } = $code;
54 117 *{ 'Getopt::Complete::' . $option } = $code;
55 }
56  
57 13 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<TAB>",
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<Getopt::Complete>
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  
« blib/lib/Getopt/Complete/LazyOptions.pm ↑ Summary Build »