stecman / symfony-console-completion

Automatic tab-key completion for Symfony console application options, arguments and parameters
MIT License
421 stars 26 forks source link

Not working via 'source /dev/stdin' on Ubuntu VM #12

Closed pjcdawkins closed 9 years ago

pjcdawkins commented 9 years ago

I'm trying to get autocompletion working, via an install script, on an Ubuntu 14.04 VirtualBox VM for our project platformsh/cli.

On this machine, this command works:

eval $(platform _completion -g -p platform)

However, I'd prefer to use the following command as it would work for users across platforms / shell types, and it seems less evil. It works nicely on OS X with either bash or zsh. But on the Ubuntu VM, while it doesn't emit any errors, it doesn't enable auto-completion:

platform _completion -g -p platform | source /dev/stdin

The source /dev/stdin bit appears to work fine:

$ echo 'echo "Hello World"' | source /dev/stdin
Hello World
$

Do you know what I might be doing wrong?

stecman commented 9 years ago

I've noticed the source method in the readme doesn't work on a few machines, so it's not something you're doing. With a little bit of fiddling, it looks like in some environments (different versions of bash) using source /dev/stdin causes the code to execute in a sub-shell. A comment on SuperUser mentions this:

Using |, however, is not going to work (at least not exactly), because both sides of the pipe are separate subprocesses, so sourced commands would not affect the current shell.

Using process substitution works in environments where the piping method doesn't work, however the reverse also seems to be true - this doesn't work where the piping method works (BASH 3.2 on OSX for me):

source <(platform _completion -g -p platform)
pjcdawkins commented 9 years ago

That makes sense, thanks. I wonder if I have to find a test for that, or if it would be appropriate just to run both, on any system (since neither appear to cause errors)...

HOOK=$(platform _completion -g -p platform)
echo "$HOOK" | source /dev/stdin
source <(echo "$HOOK")
pjcdawkins commented 9 years ago

FYI the above works on Fedora, OS X, and Ubuntu.

pjcdawkins commented 9 years ago

but those two commands fail on FreeBSD, which only seems to accept eval $(platform _completion -g -p platform --shell-type=bash)

pjcdawkins commented 9 years ago

It seems bash under Git Bash on Windows 7 also only accepts eval.

I'm going to provide users with a script for this - so I can always use eval as above.

stecman commented 9 years ago

Thanks, @pjcdawkins. It's been useful to look into this. I've updated the readme to be clearer about how completion can be registered in different environments.

pjcdawkins commented 9 years ago

Thanks. It actually doesn't appear to be dependent on the Bash version (at least, not as reported by bash --version) - the FreeBSD I was testing does have Bash 4.x.