BASH completion for boinc_cmd

Frank S. Thomas frank at thomas-alfeld.de
Fri Jun 22 09:13:58 UTC 2007


Hello all,

The BASH shell has the neat facility that it can complete commands when you 
press [TAB]. For boinc_cmd I've written a small script (which is attached) 
which goes a bit further and can complete boinc_cmds own commands and 
options. For example if you type boinc_cmd --get[TAB] it gives you all 
commands which begin with --get:
--get_cc_status            --get_project_status
--get_disk_usage           --get_proxy_settings
--get_file_transfers       --get_results
--get_host_info            --get_screensaver_mode
--get_messages             --get_simple_gui_info
--get_project_config       --get_state
--get_project_config_poll
If you now press 'p' the selection is reduced to:
--get_project_config       --get_project_status
--get_project_config_poll  --get_proxy_settings
and so on. The script also completes host names after the --host option.

I will distribute this script within the Debian package of the BOINC client 
(there it is /etc/bash_completion.d/boinc_cmd), but I think it could be 
interesting for a broader audience and therefore posting here on boinc_dev to 
ask if it should be included in your Subversion repository. To test it just 
download the file and source it: . ./boinc_cmd and then type and let it 
complete boinc_cmd's commands.

Comments are welcome.

Grüße,
Frank

BTW: the latest version of this file is always at: 
http://svn.debian.org/wsvn/pkg-boinc/trunk/boinc/debian/extra/bash/boinc_cmd?op=file
-------------- next part --------------
_boinc_cmd()
{
    local cur prev opts cmds
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    opts="--host --passwd -h --help -V --version"
    cmds="$(boinc_cmd --help 2>&1 | sed -r 's/^[[:space:]](--[a-z_]*).*/\1/' \
        | grep '^--')"

    # The following if construct assures that:
    # - no command follows if one of $opts or $cmds was given
    # - after --host follows only one command or --passwd and one command
    # - after --passwd follows only one command
    if [[ $COMP_CWORD -eq 1 ]]; then
        COMPREPLY=( $(compgen -W "$opts $cmds" -- "$cur") )
        return 0
    elif [[ "${COMP_WORDS[@]}" =~ ".* --passwd .*" ]]; then
        if [[ "$prev" == --passwd ]]; then
            return 0
        elif [[ $COMP_CWORD -lt 6 ]]; then
            COMPREPLY=( $(compgen -W "$cmds" -- "$cur") )
            return 0
        fi
    elif [[ "${COMP_WORDS[@]}" =~ ".* --host .*" ]]; then
        if [[ "$prev" == --host ]]; then
            _known_hosts
        elif [[ $COMP_CWORD -lt 4 ]]; then
            COMPREPLY=( $(compgen -W "--passwd $cmds" -- "$cur") )
            return 0
        fi
    fi
}
complete -F _boinc_cmd boinc_cmd
# vim: syntax=sh
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.alioth.debian.org/pipermail/pkg-boinc-devel/attachments/20070622/c7d834d8/attachment.pgp 


More information about the pkg-boinc-devel mailing list