From d04d55708caf83d7ed0094615992350cd04b2988 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 17 Feb 2022 13:01:40 +0300 Subject: [PATCH] vm: setup autocompleter --- pkg/vm/cli/cli.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index 3e696aaf0..231d600f2 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -222,6 +222,23 @@ example: }, } +var completer *readline.PrefixCompleter + +func init() { + var pcItems []readline.PrefixCompleterInterface + for _, c := range commands { + if !c.Hidden { + var flagsItems []readline.PrefixCompleterInterface + for _, f := range c.Flags { + names := strings.SplitN(f.GetName(), ", ", 2) // only long name will be offered + flagsItems = append(flagsItems, readline.PcItem("--"+names[0])) + } + pcItems = append(pcItems, readline.PcItem(c.Name, flagsItems...)) + } + } + completer = readline.NewPrefixCompleter(pcItems...) +} + // Various errors. var ( ErrMissingParameter = errors.New("missing argument") @@ -243,6 +260,10 @@ func New() *VMCLI { // NewWithConfig returns new VMCLI instance using provided config. func NewWithConfig(printLogotype bool, onExit func(int), c *readline.Config) *VMCLI { + if c.AutoComplete == nil { + // Autocomplete commands/flags on TAB. + c.AutoComplete = completer + } l, err := readline.NewEx(c) if err != nil { panic(err)