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

how to complete argument only if an option was specified?


 I'm trying to write a completer for htpasswd command (both because it's
useful on its own and as an exercise before writing more complicated
completer for a custom command) but I'm unable to find a way to complete an
argument only if a specified option was given with _arguments. Here is my
current attempt:

#compdef htpasswd

local users
users() {
    compadd $expl[@] $(cut -d: -f1 $line[1])

local enc # encryption options (all mutually exclusive)
enc='-m -d -p -s'

_arguments -s \
    '(-D)-c[Create a new file]' \
    '-n[Don''t update file; display results on stdout]' \
    "($enc)-m[Force MD5 encryption of the password]" \
    "($enc)-d[Force CRYPT encryption of the password (default)]" \
    "($enc)-p[Do not encrypt the password (plaintext)]" \
    "($enc)-s[Force SHA encryption of the password]" \
    '(-c)-D[Delete the specified user]' \
    ':password file:_files' \
    ':name of the user:users' \
  - withoutpassword \
  - withpassword \
    '-b[Use the password from the command line rather than prompting for it]' \
    ':password for this user:' && return 0

Unfortunately (but not unexpectedly) it doesn't work and the password is
always completed. This is not a huge problem, of course, as I don't
complete it anyhow but it's annoying and I'd like to know how to deal with
this for my more complicated custom command completer anyhow. So is there
any way to complete password argument only if "-b" option had been given? I
suspect that it can't be done with just _arguments but I don't know what
would be the best/easiest way to do it otherwise.

 Thanks in advance for any hints (or complete solutions :-),

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