views:

137

answers:

2

I'm trying to create a Bash completion script for a Java program. A typical invocation of that program might look like this:

$ javaProgram -Dproperty=foo option1 option2

Part of my script will be to suggest the various Java properties that are available for this program (i.e., when the user types "-D", the script would suggest, say, "property=", allowing the user to then type the value). I'd like the completion to not insert a space after the equals sign. However, when the user is typing the various options for the program ("option1" and "option2" in the above example), I'd like the script to do the completion, and insert a space when it is completed.

I'm new to Bash completion scripting, but I'm aware of the "nospace" option for the "complete" shell builtin. It doesn't seem to work for the "compgen" builtin, though, which seems like what I want. I've tried using "nospace", and then explicitly including spaces at the end of the appropriate options, but they don't seem to be making it through.

Does anyone know how to get spaces at the end of some options, but no spaces at the end of others? Thanks in advance.

A: 

It would be helpful to see the code you have so far.

However, look at the completion code for scp in /etc/bash_completion.d/ssh if you have that. It puts spaces after local filenames but not after hostnames.

Dennis Williamson
+2  A: 

Dennis is right on the money, in the function you reference with 'complete -F _your_func', you want something like this (taken from /etc/bash_completion):

    # escape spaces; remove executables, aliases, pipes and sockets;
    # add space at end of file names
    COMPREPLY=( $( ssh -o 'Batchmode yes' $userhost \
               command ls -aF1d "$path*" 2>/dev/null | \
               sed -e "s/[][(){}<>\",:;^&!$&=?\`|\\ ']/\\\\\\\\\\\\&/g" \
               -e 's/[*@|=]$//g' -e 's/[^\/]$/& /g' ) )
    return 0

This particular example uses the -o nospace option but adds the space manually. It's the last -e ('s/[^\/]$/& /g') that adds the space. In this case it's adding only if the last parameter doesn't end in a slash (which would be a directory and have files to auto-complete).

The key difference seems to be adding an ampersand before the space. Try that and see if it works.

C4H5As