zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: compsys: argument with pre-requisite argument
Date: Sun, 24 Nov 2013 11:01:09 -0800	[thread overview]
Message-ID: <131124110109.ZM26202@torch.brasslantern.com> (raw)
In-Reply-To: <52921EF8.8080909@gmail.com>

On Nov 24,  8:44am, John wrote:
} Subject: compsys: argument with pre-requisite argument
}
} what I want is something like:
} 
} _arguments \
}          '-D+[debug]:debug level(0 1)' \
}          '--opt1[opt1 description]' \
}          '--special[special case]' \
}          '-a[special case a]' \
}          '-b[special case b]'
} 
} such that -a and -b are only available if --special was already 
} specified, but --opt1 and -D are always available for completing.

My first thought was that this is actually similar to what happens in
_sh for zsh completions (about which we had a thread on zsh-workers
earlier this year).

Crudely, two calls to _arguments something like:

    _arguments \
	    '-D+[debug]:debug level:(0 1)' \
	    '--opt1[opt1 description]' \
	    '--special[special case]'
    local ret=$?

    if [[ "${words[1,CURRENT]}" == *" --special"* ]] &&
	_arguments \
	    '-a[special case a]' \
	    '-b[special case b]'
    then
	ret=0
    fi

    return ret

(I added the missing colon in the -D spec.)

However, making two calls to _arguments can sometimes have side-effects.
So here's a different approach which makes only one call to _arguments:

    local ifspecial='!'
    [[ "${words[1,CURRENT]}" = *" --special"* ]] && ifspecial='' 

    _arguments \
        '-D+[debug]:debug level:(0 1)' \
	'--opt1[opt1 description]' \
	'--special[special case]' \
	$ifspecial'-a[special case a]' \
	$ifspecial'-b[special case b]'

Or a different twist on the same:

    local -a special
    [[ "${words[1,CURRENT]}" = *" --special"* ]] &&
        special=(
	    '-a[special case a]'
	    '-b[special case b]'
	 )

    _arguments \
        '-D+[debug]:debug level:(0 1)' \
	'--opt1[opt1 description]' \
	'--special[special case]' \
	$special

In most cases these three will be equivalent, so pick whichever one you
find easiest to understand / maintain.


      parent reply	other threads:[~2013-11-24 19:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-24 15:44 John
2013-11-24 16:00 ` llua
2013-11-24 19:01 ` Bart Schaefer [this message]

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=131124110109.ZM26202@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /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).