'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 1:[1]

There is no easy way to do that with just compadd. That's why _arguments calls _describe under the hood. You might want to look into that function, if you want to customize things a bit more than _arguments allows.

However, if you really want to do it by making calls to compadd: What _describe does is add empty (that is, unselectable) completions with the -E option to compadd and then sets descriptions for them with the -d. Laying them out correctly, though, is a major PITA. That's why _describe uses a builtin function compdescribe for this—which is unfortunately poorly documented.

You're probably better off just sticking to _arguments and/or _describe.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Salvador Real