* Completion: make certain options' arguments complete according to arguments given to previous options
@ 2018-05-23 19:30 Doron Behar
2018-05-24 1:57 ` Jun T
0 siblings, 1 reply; 5+ messages in thread
From: Doron Behar @ 2018-05-23 19:30 UTC (permalink / raw)
To: zsh-workers
Hello zsh-users,
I would like to write a completion function for a command and I need
that some of the options' arguments will be completed in awareness to
arguments given to previous options. I've no idea if this is possible
and if so, where to start?
Allow me to explain:
Let's say the command is called `cmd`. `cmd` accepts the options
`--datadir=` and `--template=` which both accept a single argument.
If the `--datadir=` option is used, `cmd` knows that templates should be
looked for in the specified directory only. If `--datadir` isn't
specified, there is a default data directory `cmd` looks for templates
in it.
I want the `--template=` option's argument to be completed with a file
from the default data directory. But, if the `--datadir` is specified, I
want the `--template=` option's argument to be completed with a file
from the specified data directory.
Is it possible with the way zsh completion functions are implemented?
Essentially it'll look like that:
_cmd_datadir(){
# I need to get the choice from this completion into a
# temporary file or something like that, perhaps in $data_dir
_files -/
}
_cmd_template(){
if [[ -z $datadir ]]; then
_files -W $default_data_dir
else
_files -W $data_dir
fi
}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Completion: make certain options' arguments complete according to arguments given to previous options
2018-05-23 19:30 Completion: make certain options' arguments complete according to arguments given to previous options Doron Behar
@ 2018-05-24 1:57 ` Jun T
2018-05-25 19:46 ` Doron Behar
0 siblings, 1 reply; 5+ messages in thread
From: Jun T @ 2018-05-24 1:57 UTC (permalink / raw)
To: zsh-workers
> 2018/05/24 4:30, Doron Behar <doron.behar@gmail.com> wrote:
>
> If the `--datadir=` option is used, `cmd` knows that templates should be
> looked for in the specified directory only. If `--datadir` isn't
> specified, there is a default data directory `cmd` looks for templates
> in it.
You can use the associative array 'opt_args' to find the options and their
args on the command line so far:
#compdef cmd
local curcontext="$curcontext" state state_descr line ret=1
typeset -A opt_args
_arguments -C : \
'--datadir=[specify data directory]:data directory:_files -/' \
'--template=[specify template file]:template file:->template' && ret=0
case $state in
template)
local data_dir=$opt_args[--datadir]
[[ -z $data_dir ]] && data_dir=/etc # default data directory
_files -W data_dir && ret=0
;;
esac
return ret
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Completion: make certain options' arguments complete according to arguments given to previous options
2018-05-24 1:57 ` Jun T
@ 2018-05-25 19:46 ` Doron Behar
2018-05-25 20:03 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Doron Behar @ 2018-05-25 19:46 UTC (permalink / raw)
To: Jun T; +Cc: zsh-workers
Cool!
Is there a way to know if a certain argument was given? I'm talking
about the case where this argument doesn't accept an argument.
On Thu, May 24, 2018 at 10:57:23AM +0900, Jun T wrote:
>
> > 2018/05/24 4:30, Doron Behar <doron.behar@gmail.com> wrote:
> >
> > If the `--datadir=` option is used, `cmd` knows that templates should be
> > looked for in the specified directory only. If `--datadir` isn't
> > specified, there is a default data directory `cmd` looks for templates
> > in it.
>
> You can use the associative array 'opt_args' to find the options and their
> args on the command line so far:
>
>
> #compdef cmd
> local curcontext="$curcontext" state state_descr line ret=1
> typeset -A opt_args
>
> _arguments -C : \
> '--datadir=[specify data directory]:data directory:_files -/' \
> '--template=[specify template file]:template file:->template' && ret=0
>
> case $state in
> template)
> local data_dir=$opt_args[--datadir]
> [[ -z $data_dir ]] && data_dir=/etc # default data directory
> _files -W data_dir && ret=0
> ;;
> esac
>
> return ret
>
>
>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Completion: make certain options' arguments complete according to arguments given to previous options
2018-05-25 19:46 ` Doron Behar
@ 2018-05-25 20:03 ` Bart Schaefer
2018-05-25 20:30 ` Doron Behar
0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2018-05-25 20:03 UTC (permalink / raw)
To: zsh-workers
On Fri, May 25, 2018 at 12:46 PM, Doron Behar <doron.behar@gmail.com> wrote:
>
> Is there a way to know if a certain argument was given? I'm talking
> about the case where this argument doesn't accept an argument.
It'll still be in the $opt_args array, just with an empty value. So
e.g. ${+opt_args[--option]} will be 1.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Completion: make certain options' arguments complete according to arguments given to previous options
2018-05-25 20:03 ` Bart Schaefer
@ 2018-05-25 20:30 ` Doron Behar
0 siblings, 0 replies; 5+ messages in thread
From: Doron Behar @ 2018-05-25 20:30 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-workers
Excellent, that worked. Thanks again!
On Fri, May 25, 2018 at 01:03:18PM -0700, Bart Schaefer wrote:
> On Fri, May 25, 2018 at 12:46 PM, Doron Behar <doron.behar@gmail.com> wrote:
> >
> > Is there a way to know if a certain argument was given? I'm talking
> > about the case where this argument doesn't accept an argument.
>
> It'll still be in the $opt_args array, just with an empty value. So
> e.g. ${+opt_args[--option]} will be 1.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-05-25 20:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-23 19:30 Completion: make certain options' arguments complete according to arguments given to previous options Doron Behar
2018-05-24 1:57 ` Jun T
2018-05-25 19:46 ` Doron Behar
2018-05-25 20:03 ` Bart Schaefer
2018-05-25 20:30 ` Doron Behar
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).