CLI/Argument/Filename.pm

filter: Statementstmt 75.8% 47 / 62 Branchbran 65.3% 17 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 75.7% 75 / 99 CC 15 SCAR 22.7
CC
15
Cov
73%
glob_complete
13.9
complete
12.2
BEGIN
1.0
CRAP 9.7
« CLI/Command.pm ↑ Summary CLI.pm »
1 #=============================================================================
2 #
3 # Module: Term::CLI::Argument::Filename
4 #
5 # Description: Class for file name arguments in Term::CLI
6 #
7 # Author: Steven Bakker (SBAKKER), <sbakker@cpan.org>
8 # Created: 23/01/18
9 #
10 # Copyright (c) 2022 Steven Bakker
11 #
12 # This module is free software; you can redistribute it and/or modify
13 # it under the same terms as Perl itself. See "perldoc perlartistic."
14 #
15 # This software is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 #
19 #=============================================================================
20  
21 package Term::CLI::Argument::Filename 0.061000;
22  
23 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶use Moo 1.000001;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 7 ▶use namespace::clean 0.25;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 7 extends 'Term::CLI::Argument';
29  
30 7 ▶use File::Glob 'bsd_glob';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 7 ▶use Fcntl ':mode';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub complete {
36 5 ▶ my ( $self, $text ) = @_;
Subroutine complete: called CC=4 SCAR=25.0
Pod documented
37  
38 my $func_ref = $self->term->Attribs->{filename_completion_function}
39 5 ▶ or return $self->glob_complete($text);
Branch: 1 branch
BranchTrueFalseTotal
my $func_ref = $self->term->Attribs->{"filename_completion_function"} or return $self->glob_complete($text) 5 0 5
40  
41 0 ▶ if ($func_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func_ref) 0 0 0
42 0 my $state = 0;
43 0 my @list;
44 0 ▶ while ( my $f = $func_ref->( $text, $state ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = &$func_ref($text, $state)) 0 0 0
45 0 push @list, $f;
46 0 $state = 1;
47 }
48 0 return @list;
49 }
50 }
51  
52 sub glob_complete {
53 10 ▶ my ( $self, $text ) = @_;
Subroutine glob_complete: called CC=12 SCAR=26.3
Pod documented
54 10 my @list = bsd_glob("$text*");
55  
56 10 ▶ return if @list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 0 2 8 10
57  
58 8 ▶ if (@list == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list == 1) 4 4 8
59 4 ▶ if (-d $list[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $list[0]) 2 2 4
60 # Dumb trick to get readline to expand a directory
61 # with a trailing "/", but *not* add a space.
62 # Simulates the way GNU readline does it.
63 2 return ("$list[0]/", "$list[0]//");
64 }
65 2 return @list;
66 }
67  
68 # Add filetype suffixes if there is more than one possible completion.
69 4 foreach (@list) {
70 14 lstat;
71 14 ▶ if ( -l _ ) { $_ .= q{@}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-l _) 2 12 14
72 12 ▶ if ( -d _ ) { $_ .= q{/}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-d _) 2 10 12
73 10 ▶ if ( -c _ ) { $_ .= q{%}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-c _) 0 10 10
74 10 ▶ if ( -b _ ) { $_ .= q{#}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-b _) 0 10 10
75 10 ▶ if ( -S _ ) { $_ .= q{=}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-S _) 0 10 10
76 10 ▶ if ( -p _ ) { $_ .= q{=}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-p _) 0 10 10
77 10 ▶ if ( -x _ ) { $_ .= q{*}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (-x _) 2 8 10
78 }
79 4 return @list;
80 }
81  
82 7 1;
83  
84 __END__
85  
86 =pod
87  
88 =head1 NAME
89  
90 Term::CLI::Argument::Filename - class for file name arguments in Term::CLI
91  
92 =head1 VERSION
93  
94 version 0.061000
95  
96 =head1 SYNOPSIS
97  
98 use Term::CLI::Argument::Filename;
99  
100 my $arg = Term::CLI::Argument::Filename->new(name => 'arg1');
101  
102 =head1 DESCRIPTION
103  
104 Class for file name arguments in L<Term::CLI>(3p). Inherits from
105 the L<Term::CLI::Argument>(3p) class.
106  
107 =head1 CLASS STRUCTURE
108  
109 =head2 Inherits from:
110  
111 L<Term::CLI::Argument>(3p).
112  
113 =head2 Consumes:
114  
115 None.
116  
117 =head1 CONSTRUCTORS
118  
119 See L<Term::CLI::Argument>(3p).
120  
121 =head1 ACCESSORS
122  
123 See L<Term::CLI::Argument>(3p).
124  
125 =head1 METHODS
126  
127 See L<Term::CLI::Argument>(3p). Additionally:
128  
129 =over
130  
131 =item B<complete> ( I<TEXT> )
132  
133 =item B<complete> ( I<TEXT>, I<STATE> )
134  
135 Complete the (partial) filename in I<TEXT>. The I<STATE> HashRef is
136 ignored.
137  
138 If present, this uses the C<filename_completion_function> function listed
139 in L<Term::ReadLine>'s C<Attribs>, otherwise it will use the
140 L<glob_complete|/glob_complete> method.
141  
142 Not every C<Term::ReadLine> implementation implements its own
143 filename completion function. The ones that do will have the
144 C<Attrib-E<gt>{filename_completion_function}> attribute set.
145 L<Term::ReadLine::Gnu> does this, while L<Term::ReadLine::Perl> doesn't.
146  
147 =item B<glob_complete> ( I<TEXT> )
148  
149 =item B<glob_complete> ( I<TEXT>, I<STATE> )
150  
151 Complete the (partial) filename in I<TEXT>. The I<STATE> HashRef is
152 ignored.
153  
154 Implementation uses L<bsd_glob from File::Glob|File::Glob/bsd_glob> and
155 applies some decorations on the filenames based on their type, similar
156 to what L<Term::ReadLine::Gnu|Term::ReadLine::Gnu> does.
157  
158 =back
159  
160 =head1 SEE ALSO
161  
162 L<Term::CLI::Argument|Term::CLI::Argument>(3p),
163 L<Term::ReadLine::Gnu|Term::ReadLine::Gnu>(3p),
164 L<File::Glob|File::Glob>(3p),
165 L<Term::CLI|Term::CLI>(3p).
166  
167 =head1 AUTHOR
168  
169 Steven Bakker E<lt>sbakker@cpan.orgE<gt>, 2022.
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 Copyright (c) 2022 Steven Bakker
174  
175 This module is free software; you can redistribute it and/or modify
176 it under the same terms as Perl itself. See "perldoc perlartistic."
177  
178 This software is distributed in the hope that it will be useful,
179 but WITHOUT ANY WARRANTY; without even the implied warranty of
180 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
181  
182 =cut
« CLI/Command.pm ↑ Summary CLI.pm »