Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 390
Grief Avatar asked Oct 29 '22 10:10

Grief


2 Answers

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.

like image 66
Marlon Richert Avatar answered Jan 02 '23 21:01

Marlon Richert


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

like image 37
Salvador Real Avatar answered Jan 02 '23 20:01

Salvador Real