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

Completion exercise; a few misc. questions



Greetings,

I finally decided to learn how to make simple completion functions.
I chose a toy package manager called lpm for which to write
completion support; the usage is as follows:

Usage: [OPTION]... COMMAND [COMMAND ARGUMENT]...

At the moment there are no OPTIONs.

COMMAND is one of the following:
--help       Display this usage.
-i           Install/create package.  The first command argument is the name
             of the package, the rest form the command that creates
             the package.  A typical example is "lpm -i foo-1.0 make install".
-l           List installed packages.
-L           List files owned by the package.  The first command argument is
             the name of the package.
-u           Uninstall a package.  The first command argument is
             the name of the package.
-p           Package files owned by the package into a .tar.bz2 archive.
             The first command argument is the name of the package to be
             archived.  The second, optional argument is the name of the
             archive.  If it is not provided, the archive name shall be
             ./<name-of-the-package>.tar.bz2

What I managed to write is this:

_lpm_package () {
    #_files -g "${PACKAGEDIR:-/usr/local/etc/packages/}/*(.N:t)"
    compadd $(lpm -l)
}    

_lpm () {
    _arguments \
        - '(commands)' \
        '--help[display usage]' \
        '-l[list installed packages]' \
        '-L[list files owned by a package]:package name: _lpm_package' \
        '-i[install/create a package]:package name: :*:::installation command: _normal' \
        '-u[uninstall a package]:package name: _lpm_package' \
        '-p[repackage into a .tar.bz2 archive]:package name: _lpm_package::archive name: _files -/'
}

compdef _lpm lpm

Does this seem like a reasonable start?  One thing that was a bit
unclear to me was the usage of a space in front of ACTIONs.  Should
I remove it in the case of the last OPTSPEC and let the system pass
its magic options to _files?  I understood that it accepts at least
some of compadd's options and passes them through.  I tried it in
the case of my _lpm_package function and modified it to insert to a
temp file what was passed to it, and that was "-J -default-".
Would it be important to pass these to compadd and in which case it
would have some effect?  I haven't noticed any difference this far.

Another problem is the $(lpm -l) in _lpm_package.  Is there an easy
way to make it expand so that each _line_ (that may contain spaces)
of "lpm -l"'s output is passed to compadd as a separate argument?
Now lines that contain spaces turn to multiple arguments.  Since
lpm -l lists files, I also tried a _files approach that is now
comented out.  It does not have problems with spaces in the
filenames but it seems that _files allows one to complete other
files than those listed by my glob pattern even though the
documentation of the -g option says "Specifies that only files
matching the PATTERN should be completed."  Is this because of the
option -f which according to the documentation "Complete all
filenames.  This is the default." is on by default?  If that is the
case, how can I turn it off?

-- 
Hannu



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