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

Re: compsys: argument with pre-requisite argument



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.



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