-
Notifications
You must be signed in to change notification settings - Fork 3
Gtransfer explained
For reference please open the gtransfer bash completion script in another window or tab of your browser.
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:
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
After the completion function you need to add a call to complete
like the following one:
complete -o nospace -F _gtransfer gtransfer gt
-
-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 shortcutgt
) -
-o nospace
prevents the appending of a whitespace after the completion. This option spares a user to hitBACKSPACE
if he wants to continue with the completion of the current word (e.g. when traversing a remote directory tree with gtransfer).
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.
Option completion is very easy to accomplish with bash completion. All you need 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:
# all available gtransfer options/switches/parameters
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