commando icon indicating copy to clipboard operation
commando copied to clipboard

tool to generate bash/zsh completion?

Open endel opened this issue 11 years ago • 3 comments

is there any?

endel avatar Mar 21 '14 16:03 endel

That falls outside the realm of php and more into the realm of the shell language as far as I am aware. While you could write php to help with the autocomplete, in the case of bash at least, it would still require sticking something in your .bashrc. again afaik. On Mar 21, 2014 12:02 PM, "Endel Dreyer" [email protected] wrote:

is there any?

Reply to this email directly or view it on GitHubhttps://github.com/nategood/commando/issues/24 .

nategood avatar Mar 22 '14 00:03 nategood

Yes, but I think it's totally possible to PHP generate the bash completion file, since the necessary definitions are already there.

Completions AFAIK have four main variables:

  • COMP_WORDS (array): list of words on the line
  • COMP_CWORD (integer): index of current typing word
  • COMP_LINE (string): the current commandline
  • COMPREPLY (array): reply for completion

The following template provides a very simple completion, just for commands/subcommands and options. Maybe commando could generate that, and leave to the user to create more advanced completions, if necessary.

#!/usr/bin/env bash

__commando()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local prev=${COMP_WORDS[COMP_CWORD-1]}

    local first=$(echo $cur | cut -d ':' -f 1)
    local second=$(echo $cur | cut -d ':' -f 2)

    # Going deep on sub-commands.
    case "$first" in
      "subcommand")
        COMPREPLY=( $( compgen -W "sub1 sub2 sub3" -- $second ) )
        return 0
        ;;

      "subcommand2")
        COMPREPLY=( $( compgen -W "cmd1 cmd2 cmd3" -- $second ) )
        return 0
        ;;
    esac

    # All options here
    if [[ "$cur" == -* ]]; then
      COMPREPLY=( $( compgen -W "--all --options --here" -- $cur ) )
      return 0
    fi

    # List of all "top" commands here.
    # Subcommands should be listed on the "case" statement.
    # Sub-commands are separated by ":"
    COMPREPLY=( $(compgen -W "all commands here subcommand: subcommand2:" -- $cur) )
    return 0
}

# zsh compatibility
if [[ -n ${ZSH_VERSION-} ]]; then
  autoload -U +X bashcompinit && bashcompinit
fi

# install "complete" for session
complete -F __commando -o nospace commando-binary

endel avatar Mar 22 '14 16:03 endel

Right. It's the "leave it up to the user" part that feels funky.

If it's kept isolated enough, I'm open to exploring it I suppose. On Mar 22, 2014 12:54 PM, "Endel Dreyer" [email protected] wrote:

Yes, but I think it's totally possible to PHP generate the bash completion file, since the necessary definitions are already there.

Completions AFAIK have four main variables:

  • COMP_WORDS (array): list of words on the line
  • COMP_CWORD (integer): index of current typing word
  • COMP_LINE (string): the current commandline
  • COMPREPLY (array): reply for completion

The following template provides a very simple completion, just for commands/subcommands and options. Maybe commando could generate that, and leave to the user to create more advanced completions, if necessary.

#!/usr/bin/env bash

__commando(){ local cur=${COMP_WORDS[COMP_CWORD]} local prev=${COMP_WORDS[COMP_CWORD-1]}

local first=$(echo $cur | cut -d ':' -f 1)
local second=$(echo $cur | cut -d ':' -f 2)

# Going deep on sub-commands.
case "$first" in
  "subcommand")
    COMPREPLY=( $( compgen -W "sub1 sub2 sub3" -- $second ) )
    return 0
    ;;

  "subcommand2")
    COMPREPLY=( $( compgen -W "cmd1 cmd2 cmd3" -- $second ) )
    return 0
    ;;
esac

# All options here
if [[ "$cur" == -* ]]; then      COMPREPLY=( $( compgen -W "--all --options --here" -- $cur ) )
  return 0
fi

# List of all "top" commands here.
# Subcommands should be listed on the "case" statement.
# Sub-commands are separated by ":"
COMPREPLY=( $(compgen -W "all commands here subcommand: subcommand2:" -- $cur) )
return 0}

zsh compatibilityif [[ -n ${ZSH_VERSION-} ]]; then autoload -U +X bashcompinit && bashcompinitfi

install "complete" for sessioncomplete -F __commando -o nospace commando-binary

Reply to this email directly or view it on GitHubhttps://github.com/nategood/commando/issues/24#issuecomment-38357137 .

nategood avatar Mar 22 '14 17:03 nategood