LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-16-2010, 11:36 AM   #1
azrael_ru
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Handling wordbreaks in bash completion


Hello, people.

I am writing completion function for one PHP framework called symfony.
It has command line interface with syntax:

Code:
symfony [options] [namespace:]action
I want to make action be autocompletable.
The function is simplest so far:

Code:
function _symfony_commands()
{
   [ -r "cache/completion/.sf" ] && cat cache/completion/.sf
}

function _symfony
{
    COMPREPLY=( $( compgen -W "$(_symfony_commands)" -- ${COMP_WORDS[COMP_CWORD]} ) )
}

complete -F _symfony symfony
In the .sf file placed list of all available commands divided by space.
And it works fine if there is no namespace in symfony action, e.g.:

symfony he[TAB]
will be properly completed to
symfony help

But, if there is : symbol which separate namespace from action problems coming:
symfony doct[TAB]
will be completed to
symfony doctrine:

But nothing happens if you want complete after : symbol.
I've found out that for readline there is three words because it splits line with $COMP_WORDBREAKS

Code:
$ echo $COMP_WORDBREAKS
"'><=;|&(:
I played with $COMP_WORDS array and tried every thought I had to make it work, but failed.
What I should do to escape colon and make readline consider it as one word?
Or there is way perhaps to workaround it?

Any help are appreciated.
Attached Files
File Type: txt commands.txt (832 Bytes, 21 views)
 
Old 02-18-2010, 12:56 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Can't you just modify COMP_WORDBREAKS locally for the function?

Code:
local COMP_WORDBREAKS=${COMP_WORDBREAKS/:}
Edit: P.S. Do you have any particular purpose for having a separate _symfony_commands function? I don't see any reason here not to simply incorporate it directly into the _symfony function. I'd personally just do something like this:

Code:
function _symfony
{
    local COMP_WORDBREAKS=${COMP_WORDBREAKS/:}

    simfony_commands=$([ -r "cache/completion/.sf" ] && cat cache/completion/.sf)

    COMPREPLY=( $( compgen -W "$symfony_commands" -- ${COMP_WORDS[COMP_CWORD]} ) )
}

complete -F _symfony symfony
I can't test this though, not having symfony.

Last edited by David the H.; 02-18-2010 at 01:39 AM.
 
Old 02-18-2010, 08:06 AM   #3
azrael_ru
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thanks for the answer.

No, it won't help though.
Because $COMP_WORDBREAKS are used before this function call.

In fact bash fill COMP_WORDS array and pass it to the defined function.

Yes, there is no practical reason for now, you right.
But code will be growed to process options and change behavior depends on word completing.

P.S.:

You don't have to install symfony to test it.
You define the function in your shell, download the command list to /tmp/cache/completion/.sf
Then do something like this:

Code:
cd /tmp
touch symfony
chmod +x symfony
Don't forget load the function into your shell, including complete command.
It will work as:

Code:
/tmp/symfony log:[TAB]
Some clue may give this function:
(/etc/bash_completion:539)
Code:
# This function completes on user:group format
#
_usergroup()
{
        local IFS=$'\n'
        cur=${cur//\\\\ / }
        if [[ $cur = *@(\\:|.)* ]] && [ -n "$bash205" ]; then
                user=${cur%%*([^:.])}
                COMPREPLY=( $(compgen -P ${user/\\\\} -g -- ${cur##*[.:]}) )
        elif [[ $cur = *:* ]] && [ -n "$bash205" ]; then
                COMPREPLY=( $( compgen -g -- ${cur##*[.:]} ) )
        else
                COMPREPLY=( $( compgen -S : -u -- $cur ) )
        fi
}
It it doesn't work properly neither, despite the fact that it in debian bash-completion package.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash hostname completion not working. bartonski Linux - Software 2 11-25-2009 03:35 PM
xterm and bash completion athanatos Slackware 12 07-22-2006 12:47 PM
question about bash completion Guest1234 Linux - Software 4 02-25-2006 04:09 PM
Can we enable bash completion in Sarge?How?Please help... deepclutch Debian 6 12-02-2005 07:02 PM
auto-completion in bash with wildcards juanbobo Linux - Software 2 04-07-2005 12:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration