Search code examples
zshzsh-completion

zsh compadd - how to specify argument's description?


With _arguments I can do _arguments {-h,--help}'[Show help]', but how to specify 'Show help' message in compadd parameters? Cannot find that in documentation


Solution

  • I know this question is old. I had this question recently and this is how I solved it.

    function _foo {
      
      local -a _descriptions _values
    
      _descriptions=(
        'foo -- Description of foo'
        'bar -- Description of bar'
        'baz -- Description of baz'
      )
    
      _values=(
        'bar'
        'foo'
        'baz'
      )
    
      # Vertical window completion
      compadd -d _descriptions -a _values
      #$~ foo
      # bar -- Description of bar
      # baz -- Description of baz
      # foo -- Description of foo
    
      # Horizontal completion
      # compadd -d _descriptions -a _values
      #$~ foo
      # bar -- Description of bar  baz -- Description of baz  foo -- Description of foo
    }
    
    compdef _foo foo
    

    Note: that this works only with arrays. If you have a string, you must convert it to an array