zsh-users
 help / color / mirror / code / Atom feed
* compsys: argument with pre-requisite argument
@ 2013-11-24 15:44 John
  2013-11-24 16:00 ` llua
  2013-11-24 19:01 ` Bart Schaefer
  0 siblings, 2 replies; 3+ messages in thread
From: John @ 2013-11-24 15:44 UTC (permalink / raw)
  To: zsh-users

I understand the exclusion option for an _arguments list, but I'm 
wondering if there is anything of the sort that would allow me to have 
pre-requisite arguments.  I haven't found an example to work off, but 
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.

TIA,
John


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: compsys: argument with pre-requisite argument
  2013-11-24 15:44 compsys: argument with pre-requisite argument John
@ 2013-11-24 16:00 ` llua
  2013-11-24 19:01 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: llua @ 2013-11-24 16:00 UTC (permalink / raw)
  To: zsh-users

On 11/24/2013 10:44 AM, John wrote:
> '--special[special case]'
'--special[special case]: : _values "special case" "-a[special case a]"
"-b[special case b]"'

Is one way that comes to mind.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: compsys: argument with pre-requisite argument
  2013-11-24 15:44 compsys: argument with pre-requisite argument John
  2013-11-24 16:00 ` llua
@ 2013-11-24 19:01 ` Bart Schaefer
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2013-11-24 19:01 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-11-24 19:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-24 15:44 compsys: argument with pre-requisite argument John
2013-11-24 16:00 ` llua
2013-11-24 19:01 ` Bart Schaefer

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).