From cdf750081e7cabf77e8c43443aec7f24df09e6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Untersch=C3=BCtz?= Date: Sat, 23 May 2026 14:42:41 +0200 Subject: [PATCH] feat: show subcommands after hostnames in shell completion Use ShellCompDirectiveKeepOrder so fish/zsh/bash preserve the returned order instead of sorting alphabetically. ValidArgsFunction now appends subcommand names (with their short descriptions) after all hostname entries, so hosts always appear first in the completion list. Co-Authored-By: Claude Sonnet 4.6 --- cmd/netssh/main.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/cmd/netssh/main.go b/cmd/netssh/main.go index cc7300c..de9be54 100644 --- a/cmd/netssh/main.go +++ b/cmd/netssh/main.go @@ -199,12 +199,18 @@ func rootCmd() *cobra.Command { } c := cache.New(cfg.Cache.Path, cfg.Cache.TTL) _ = c.Load() - entries := c.Search(toComplete) - names := make([]cobra.Completion, len(entries)) - for i, e := range entries { - names[i] = cobra.Completion(e.Name) + + var completions []cobra.Completion + for _, e := range c.Search(toComplete) { + completions = append(completions, cobra.Completion(e.Name)) } - return names, cobra.ShellCompDirectiveNoFileComp + // Subcommands at the end, after all hostnames. + for _, sub := range cmd.Commands() { + if sub.IsAvailableCommand() && strings.HasPrefix(sub.Name(), toComplete) { + completions = append(completions, cobra.Completion(sub.Name()+"\t"+sub.Short)) + } + } + return completions, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveKeepOrder }, }