Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Created July 27, 2012 11:36
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pfreixes/3187511 to your computer and use it in GitHub Desktop.
Save pfreixes/3187511 to your computer and use it in GitHub Desktop.
Supervisorctl bash autocomplete
# pfreixes, 2012-07-27
# Add to /etc/bash_completion.d/supervisorctl
_supervisor()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
#
# The basic options we'll complete.
#
opts="add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version"
#
# Complete the arguments to some of the basic commands.
#
case "${prev}" in
start|stop|restart)
local process=$(for x in `supervisorctl avail | awk '{print $1}'`; do echo ${x} ; done )
COMPREPLY=( $(compgen -W "${process}" -- ${cur}) )
return 0
;;
*)
;;
esac
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
}
complete -F _supervisor supervisorctl
@chrisbarmonde
Copy link

Perfect. Thanks for this!

@kobymeir
Copy link

I suggest to change:
COMPREPLY=( $(compgen -W "${process}" -- ${cur}) )
to:
COMPREPLY=( $(compgen -W "${process} all" -- ${cur}) )
as it will complete "supervisor restart a" to "supervisor restart all"

@kobymeir
Copy link

another good practice is also to do:
have supervisorctl && complete -F _supervisor supervisorctl
to check that the program exists before adding the bash completion for it.

@StarLightPL
Copy link

Adding 'tail' to the basic commands with argument completion would be really useful :)

@xiaozhuai
Copy link

Final version

have supervisorctl &&
_supervisor()
{
    local cur prev opts base
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    #
    #  The basic options we'll complete.
    #
    opts="add clear fg open quit remove restart start stop update avail exit maintail pid reload reread shutdown status tail version"


    #
    #  Complete the arguments to some of the basic commands.
    #
    case "${prev}" in
        start|stop|restart)
            local process=$(for x in `supervisorctl avail | awk '{print $1}'`; do echo ${x} ; done )
            COMPREPLY=( $(compgen -W "${process} all" -- ${cur}) )
            return 0
            ;;
        *)
        ;;
    esac

   COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
   return 0
} &&
complete -F _supervisor supervisorctl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment