Tips for doing custom bash terminations

I am developing a custom completion command bashto capture job identifiers from a scheduling system (LSF, PBS, SLURM). I have basic functions, but I would like to expand it now with the help of "tips" that I saw at startup zsh.

For example, when I press TAB in the example grepbelow, I get:

grep -<TAB>
     --after-context          -A           -- specify lines of trailing context
     --basic-regexp           -G           -- use basic regular expression
     --before-context         -B           -- specify lines of leading context
...

This third column after --is what I would like to add to my own completion bash. What is the correct technical term for this? Advice? Does the compgenfunctionality provide for this?

I am enclosing my current working example, which contains only identifiers. This example uses LSF.

# LSF Job ID completion
function _mycurrentjobs()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER -o JOBID)" -- $cur))
    return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume

, : bjobs -noheader -u $USER -o "JOBID JOB_NAME"

+4
1

bash autocompletion: . . - id

function _mycurrentjobs()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local OLDIFS="$IFS"
    local IFS=$'\n'
    COMPREPLY=( $(compgen -W "$(bjobs -noheader -u $USER \
        -o "JOBID JOB_NAME delimiter='-'")" -- $cur))
    IFS="$OLDIFS"
    if [[ ${#COMPREPLY[*]} -eq 1 ]]; then   #Only one completion
        COMPREPLY=( ${COMPREPLY[0]%%-*} )   #Remove the separator and everything after
    fi
    return 0
}
complete -F _mycurrentjobs bkill bjobs bstatus bpeek bstop bresume
+1

Source: https://habr.com/ru/post/1684104/


All Articles