nategood / commando

An Elegant CLI Library for PHP
MIT License
798 stars 80 forks source link

tool to generate bash/zsh completion? #24

Open endel opened 10 years ago

endel commented 10 years ago

is there any?

nategood commented 10 years ago

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" notifications@github.com wrote:

is there any?

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

endel commented 10 years ago

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:

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
nategood commented 10 years ago

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" notifications@github.com 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 .