Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Leveraging the completion system to discover available options in a command



On Mar 1,  3:23pm, Idan Kamara wrote:
}
} Is it possible to use the completion system to get a list of available
} options for a command zsh knows how to auto-complete?

It's for most practical purposes impossible to use the completion system
unless you have an active ZLE $BUFFER to contain the inputs.

Within that constraint, you can create a function wrapper around compadd
that always passes one of the -O or -A options (and probably also -U) to
the builtin compadd whenever one of those options is not already present
in $@, and then invoke the appropriate completion widget with the "zle"
builtin to cause an array that you control to be populated with matches.

However, I don't think that actually addresses your desire:
 
} Ideally, I'd like given the name of a program to get a list of pairs
} (option string, desc) for each available option.

As far as I know there is no way to extract the "display strings" that
are loaded into compadd along with the matches, which would include the
option descriptions.

There is one fairly egregious hack you could employ; copy the technique
from Test/comptest from the zsh sources, which goes something like this:

1. Create a second interactive zsh running on a pseudo-terminal by using
   the zsh/zpty module.

2. Configure the list coloring styles in that copy of zsh to emit strings
   that format the completion list output into something resembling an
   XML structure.

3. Send input through zpty that causes the interactive slave shell to
   perform the completion you want, in list mode.

4. Parse the bits you want from the XML-ish output read with zpty.

You can then erase the command line from the slave shell and keep that
shell around to be able to repeat at (3) without setting up a new pty.
You get to deal with all the possible ways that having a second shell
running commands in the background might go horribly wrong for the user
of the shell in the foreground.



Messages sorted by: Reverse Date, Date, Thread, Author