Skip to content

Gtransfer explained

fr4nk5ch31n3r edited this page Oct 5, 2012 · 38 revisions

Gtransfer explained

Gtransfer's bash completion

For reference please open the gtransfer bash completion script in another window or tab of your browser.

Bash completion boilerplate code

For writing bash completion scripts for arbitrary tools I recommend to have a look at already existing scripts in your favorite Linux/Unix distribution (e.g. Debian and Ubuntu have a lot of bash completion scripts available).


First some introductory words. A shell commandline usually consists of the command (possibly with a path prefix) and the options of the command (usually separated by a whitespace character). Together these make up the individual words of a command line.


To create your own bash completion script you need at least one (completion) function, usually named like the corresponding tool and a _ as prefix (e.g. _gtransfer()). In its simplest form this function will just add elements (words) to the global array variable COMPREPLY, like in the following example:

_tool()
{
        COMPREPLY=( "-o" "-p" "-t" "-s" )
}

These words will then be proposed after hitting the TAB key.


For a more advanced completion you can make use of the special bash completion variables COMP_WORDS and COMP_CWORD:

From Bash Variables

  • COMP_CWORD: An index into ${COMP_WORDS} of the word containing the current cursor position. This variable is available only in shell functions invoked by the programmable completion facilities.
  • COMP_WORDS: An array variable consisting of the individual words in the current command line. The line is split into words as Readline would split it, using COMP_WORDBREAKS as described above. This variable is available only in shell functions invoked by the programmable completion facilities

For example to get the word at the current cursor position you can use:

local cur
cur="${COMP_WORDS[COMP_CWORD]}"

Or to get the word before the current cursor position (e.g. to complete an option argument, like in -s gsiftp://[...]) you can use:

local prev
prev="${COMP_WORDS[COMP_CWORD-1]}"

After the completion function you need to add a call to complete like the following one:

complete -o nospace -F _gtransfer gtransfer gt
  • -o nospace prevents the appending of a whitespace after the completion. This option spares a user to hit BACKSPACE if he wants to continue with the completion of the current word (e.g. when traversing a remote directory tree with gtransfer).
  • -F _gtransfer specifies the completion function to use
  • the following non-option arguments specify the tools the completion should be applied to (here we use the gtransfer tool and its shortcut gt)

The complete example:

_gtransfer()
{
        local cur prev opts

        COMPREPLY=()

        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"

        COMPREPLY=( [...] )
        return 0
}
complete -o nospace -F _gtransfer gtransfer gt

For more details about the shown bash builtins have a look at the Bash Reference Manual.

Completion of options

Option completion is very easy to accomplish with bash completion. All that is needed for gtransfer is to prepare a variable (e.g. named opts) containing all options separated by a whitespace character and a compgen commandline as shown in the following example:

_gtransfer()
{
        local cur
        cur="${COMP_WORDS[COMP_CWORD]}"
        [...]
        #  all available gtransfer options/switches/parameters
        local opts="--source -s --destination -d --help --verbose -v --version -V --metric -m --logfile -l --auto-clean -a --configfile --"
        [...]
        #  complete possible gtransfer options/switches/parameters
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
}
complete -o nospace -F _gtransfer gtransfer gt

Completion of host URL parts

Completion of remote paths

Clone this wiki locally