zsh-users
 help / color / mirror / code / Atom feed
From: Hannu Koivisto <azure@iki.fi>
To: Zsh Users' List <zsh-users@sunsite.dk>
Subject: Completion exercise; a few misc. questions
Date: Sat, 18 Oct 2003 17:57:30 +0300	[thread overview]
Message-ID: <87n0bytwyt.fsf@lynx.tre-1.ionific.com> (raw)

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


                 reply	other threads:[~2003-10-18 15:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87n0bytwyt.fsf@lynx.tre-1.ionific.com \
    --to=azure@iki.fi \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).