1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
From: Jeff King <peff@peff.net>
Subject: Fix perl completions with a space between option and argument
Origin: vendor, https://bugs.debian.org/614775
Bug-Debian: https://bugs.debian.org/614775
Forwarded: yes, https://github.com/scop/bash-completion/pull/258
Most perl options do not allow a space between the switch and the
argument, however, bash-completion incorrectly suggests completions for
`-d', `-M` (or `-m'), and `-V', as reported in Debian bug #614775 [1],
as well as for `-I', and `-x', which were not mentioned in the bug
report. This patch fixes the incorrect completions for these options,
by falling back to regular _filedir completion when a space is present.
On the other hand, and unlike all other perl options, a space between
the `-e' and `-E' options and their arguments, e.g. `perl -e "exit 2"',
*is* valid syntax. However, the argument is neither a filename nor a
directory, but one line of perl program. So, in order to keep the old
behavior, which was correct, this patch adds extra code that skips
_filedir completion for `-e' and `-E' with a space.
Finally, the test case for `-x' requires changes, because it expects
that `-x' with a space should complete only with directories. That is
misleading, because it might suggest that the directory would be treated
as the argument to the `-x' option, when it will not, as can be verified
with the following test:
$ cat test.pl
Some regular text with an embedded script
#!perl
use Cwd;
my $dir = getcwd;
print "$dir\n";
__END__
More text messages, not code.
By executing the script with the `-x[dir]' option, the non-code part of
the file will be ignored, i.e. only the embedded script will be
executed. It will also be executed under [dir], if it is provided:
$ perl -xperl/ test.pl
/tmp/perl
However, if a space is present between `-x' and [dir], the script fails,
because the perl interpreter thinks that [dir] is a script, not the
argument to `-x':
$ perl -x perl/ test.pl
Can't open perl script "perl/": Is a directory
The rationale for the changes is based on `perl --help', which explains
that there are no spaces between options and arguments:
$ perl --help | grep "\-d\|\-V\|-I\|-x\|-\[mM\]"
-d[:debugger] run program under debugger
-Idirectory specify @INC/#include directory (several -I's allowed)
-[mM][-]module execute "use/no module..." before executing program
-V[:variable] print configuration summary (or a single Config.pm variable)
-x[directory] ignore text before #!perl line (optionally cd to directory)
When a space is required, `perl --help' makes it explicit:
$ perl --help | grep "\-e\|-E"
-e program one line of program (several -e's allowed, omit programfile)
-E program like -e, but enables all optional features
[1] https://bugs.debian.org/614775
---
diff --git a/completions/perl b/completions/perl
index 7b91d1b4..98a0b53b 100644
--- a/completions/perl
+++ b/completions/perl
@@ -27,7 +27,6 @@ _perl()
optPrefix=-P$prev
optSuffix=-S/
prefix=$prev
- fi
case $prev in
-D|-e|-E|-i|-F|-l)
@@ -68,7 +67,15 @@ _perl()
;;
esac
- if [[ "$cur" == -* ]]; then
+ # Unlike other perl options, having a space between the `-e' and
+ # `-E' options and their arguments, e.g. `perl -e "exit 2"', is
+ # valid syntax. However, the argument is neither a filename nor a
+ # directory, but one line of perl program, thus do not suggest
+ # _filedir completion.
+ elif [[ "$prev" == -e ]] || [[ "$prev" == -E ]]; then
+ return
+
+ elif [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-C -s -T -u -U -W -X -h -v -V -c -w -d -D -p
-n -a -F -l -0 -I -m -M -P -S -x -i -e' -- "$cur" ) )
else
|