zsh-users
 help / color / mirror / code / Atom feed
* Composing completions for a wrapper script
@ 2021-10-30 22:07 Anton Suslov
  2021-10-30 23:00 ` Jérémie Roquet
  2021-10-31 13:22 ` Mikael Magnusson
  0 siblings, 2 replies; 7+ messages in thread
From: Anton Suslov @ 2021-10-30 22:07 UTC (permalink / raw)
  To: zsh-users

Hi, everyone!

I am writing some wrapper functions over existing commands, e.g.

kubectldc() {
  DC="$1"
  shift
  KUBECONFIG="$HOME/.kube/$DC.kubeconfig" kubectl "$@"
}

Kubectl is a well-behaved command, which does have rich completion available. Is it possible to somehow reuse its completion for kubectldc command? As-is the function definition becomes a tradeoff between "using kubectl directly for completion but having to select config manually" and "using the wrapper for automatic config but no completion".

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

* Re: Composing completions for a wrapper script
  2021-10-30 22:07 Composing completions for a wrapper script Anton Suslov
@ 2021-10-30 23:00 ` Jérémie Roquet
  2021-10-31  6:14   ` Anton Suslov
  2021-10-31 13:22 ` Mikael Magnusson
  1 sibling, 1 reply; 7+ messages in thread
From: Jérémie Roquet @ 2021-10-30 23:00 UTC (permalink / raw)
  To: Anton Suslov; +Cc: Zsh Users

Hi Anton,

Le dim. 31 oct. 2021 à 00:08, Anton Suslov <anton_suslov@me.com> a écrit :
> Is it possible to somehow reuse [kubectl's] completion for kubectldc command

Try the following:

    compdef kubectldc=kubectl

Best regards,

-- 
Jérémie


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

* Re: Composing completions for a wrapper script
  2021-10-30 23:00 ` Jérémie Roquet
@ 2021-10-31  6:14   ` Anton Suslov
  0 siblings, 0 replies; 7+ messages in thread
From: Anton Suslov @ 2021-10-31  6:14 UTC (permalink / raw)
  To: Jérémie Roquet; +Cc: Zsh Users

Hi, Jérémie,

kubectdc command as defined here has an extra first argument. kubectl doesn't expect
it, and because of that the completion gets extremely confused if I do compdef like this.
What I want is to be able to provide my completion for the first argument, and re-use the kubectl
completion for the rest of the args.

> On 31 Oct 2021, at 02:00, Jérémie Roquet <jroquet@arkanosis.net> wrote:
> 
> Hi Anton,
> 
>> Le dim. 31 oct. 2021 à 00:08, Anton Suslov <anton_suslov@me.com> a écrit :
>> Is it possible to somehow reuse [kubectl's] completion for kubectldc command
> 
> Try the following:
> 
>   compdef kubectldc=kubectl
> 
> Best regards,
> 
> -- 
> Jérémie


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

* Re: Composing completions for a wrapper script
  2021-10-30 22:07 Composing completions for a wrapper script Anton Suslov
  2021-10-30 23:00 ` Jérémie Roquet
@ 2021-10-31 13:22 ` Mikael Magnusson
  2021-10-31 20:32   ` Bart Schaefer
  1 sibling, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2021-10-31 13:22 UTC (permalink / raw)
  To: Anton Suslov; +Cc: zsh-users

On 10/31/21, Anton Suslov <anton_suslov@me.com> wrote:
> Hi, everyone!
>
> I am writing some wrapper functions over existing commands, e.g.
>
> kubectldc() {
>   DC="$1"
>   shift
>   KUBECONFIG="$HOME/.kube/$DC.kubeconfig" kubectl "$@"
> }
>
> Kubectl is a well-behaved command, which does have rich completion
> available. Is it possible to somehow reuse its completion for kubectldc
> command? As-is the function definition becomes a tradeoff between "using
> kubectl directly for completion but having to select config manually" and
> "using the wrapper for automatic config but no completion".

compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
words[1]=kubectl; _normal' kubectldc

There is a side effect that if you try to complete the first word, it
will try to complete command names. You can handle that with some
extra code if you want, but at that point you should probably create a
_kubectldc completer as it will become a bit unwieldy for a one-liner.

-- 
Mikael Magnusson


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

* Re: Composing completions for a wrapper script
  2021-10-31 13:22 ` Mikael Magnusson
@ 2021-10-31 20:32   ` Bart Schaefer
  2021-11-01  7:13     ` Anton Suslov
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2021-10-31 20:32 UTC (permalink / raw)
  To: Zsh Users; +Cc: Anton Suslov

On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>
> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
> words[1]=kubectl; _normal' kubectldc
>
> There is a side effect that if you try to complete the first word, it
> will try to complete command names. You can handle that with some
> extra code if you want, but at that point you should probably create a
> _kubectldc completer as it will become a bit unwieldy for a one-liner.

It's not too bad, assuming the first argument just needs ordinary
default completions.

compdef -e '((CURRENT>2)) && {
words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
} || words[1]=:; _normal' kubectldc

If there is already a completion function for the DC argument, you can
replace "words[1]=:" with an assignment to service to select that
completion.


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

* Re: Composing completions for a wrapper script
  2021-10-31 20:32   ` Bart Schaefer
@ 2021-11-01  7:13     ` Anton Suslov
  2021-11-01  7:53       ` Anton Suslov
  0 siblings, 1 reply; 7+ messages in thread
From: Anton Suslov @ 2021-11-01  7:13 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users, mikachu

Oh, thanks, that was exactly what I was looking for!

I have considered doing something similar, but I am still a bit afraid of the completer being sufficiently smart and observing some other things and breaking.

By the way, how do I define completion for a function?
I tried creating a _kubectldc file in a directory in my fpath, but the completion doesn't
 work, and it doesn't seem like it even gets loaded automatically. When I do compdef
 like you suggested, everything works, but I want to do some more complex logic than it
 is nice to fit in the compdef argument. I tried doing the RTFM, but it suggests that
 proper filename and #compdef kubectldc should be enough.

> On 31 Oct 2021, at 22:32, Bart Schaefer <schaefer@brasslantern.com> wrote:
> 
> On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>> 
>> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
>> words[1]=kubectl; _normal' kubectldc
>> 
>> There is a side effect that if you try to complete the first word, it
>> will try to complete command names. You can handle that with some
>> extra code if you want, but at that point you should probably create a
>> _kubectldc completer as it will become a bit unwieldy for a one-liner.
> 
> It's not too bad, assuming the first argument just needs ordinary
> default completions.
> 
> compdef -e '((CURRENT>2)) && {
> words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
> } || words[1]=:; _normal' kubectldc
> 
> If there is already a completion function for the DC argument, you can
> replace "words[1]=:" with an assignment to service to select that
> completion.


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

* Re: Composing completions for a wrapper script
  2021-11-01  7:13     ` Anton Suslov
@ 2021-11-01  7:53       ` Anton Suslov
  0 siblings, 0 replies; 7+ messages in thread
From: Anton Suslov @ 2021-11-01  7:53 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users, mikachu

Please disregard my last e-mail, I did not do compinit.

> On 1 Nov 2021, at 09:13, Anton Suslov <anton_suslov@me.com> wrote:
> 
> Oh, thanks, that was exactly what I was looking for!
> 
> I have considered doing something similar, but I am still a bit afraid of the completer being sufficiently smart and observing some other things and breaking.
> 
> By the way, how do I define completion for a function?
> I tried creating a _kubectldc file in a directory in my fpath, but the completion doesn't
> work, and it doesn't seem like it even gets loaded automatically. When I do compdef
> like you suggested, everything works, but I want to do some more complex logic than it
> is nice to fit in the compdef argument. I tried doing the RTFM, but it suggests that
> proper filename and #compdef kubectldc should be enough.
> 
>>> On 31 Oct 2021, at 22:32, Bart Schaefer <schaefer@brasslantern.com> wrote:
>>> 
>>> On Sun, Oct 31, 2021 at 6:23 AM Mikael Magnusson <mikachu@gmail.com> wrote:
>>> 
>>> compdef -e 'shift words; (( CURRENT-- )); service=kubectl;
>>> words[1]=kubectl; _normal' kubectldc
>>> 
>>> There is a side effect that if you try to complete the first word, it
>>> will try to complete command names. You can handle that with some
>>> extra code if you want, but at that point you should probably create a
>>> _kubectldc completer as it will become a bit unwieldy for a one-liner.
>> 
>> It's not too bad, assuming the first argument just needs ordinary
>> default completions.
>> 
>> compdef -e '((CURRENT>2)) && {
>> words[1,2]=(kubectl); (( CURRENT-- )); service=kubectl;
>> } || words[1]=:; _normal' kubectldc
>> 
>> If there is already a completion function for the DC argument, you can
>> replace "words[1]=:" with an assignment to service to select that
>> completion.


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

end of thread, other threads:[~2021-11-01  7:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-30 22:07 Composing completions for a wrapper script Anton Suslov
2021-10-30 23:00 ` Jérémie Roquet
2021-10-31  6:14   ` Anton Suslov
2021-10-31 13:22 ` Mikael Magnusson
2021-10-31 20:32   ` Bart Schaefer
2021-11-01  7:13     ` Anton Suslov
2021-11-01  7:53       ` Anton Suslov

Code repositories for project(s) associated with this 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).