Various fixes from the completion merge

This commit is contained in:
Kovid Goyal
2022-09-26 11:22:48 +05:30
parent 97716fea8b
commit 262e2fb7a3
8 changed files with 15 additions and 9 deletions

View File

@@ -209,6 +209,8 @@ func (self *Command) ResetAfterParseArgs() {
o.reset()
}
}
self.option_map = nil
self.IndexOfFirstArg = 0
self.Args = make([]string, 0, 8)
}

View File

@@ -91,6 +91,7 @@ func GenerateCompletions(args []string) error {
all_completions := make([]*Completions, 0, 1)
for _, argv := range all_argv {
all_completions = append(all_completions, root.GetCompletions(argv, init_completions[output_type]))
root.ResetAfterParseArgs()
}
output, err := output_serializer(all_completions, shell_state)
if err == nil {

View File

@@ -113,7 +113,7 @@ func complete_word(word string, completions *Completions, only_args_allowed bool
}
}
}
if !cmd.SubCommandMustBeFirst && cmd.ArgCompleter != nil {
if cmd.SubCommandIsOptional && cmd.ArgCompleter != nil {
cmd.ArgCompleter(completions, word, arg_num)
}
return

View File

@@ -107,10 +107,10 @@ type Completions struct {
Groups []*MatchGroup `json:"groups,omitempty"`
Delegate Delegate `json:"delegate,omitempty"`
CurrentCmd *Command
AllWords []string // all words passed to parse_args()
CurrentWordIdx int // index of current word in all_words
CurrentWordIdxInParent int // index of current word in parents command line 1 for first word after parent
CurrentCmd *Command `json:"-"`
AllWords []string `json:"-"` // all words passed to parse_args()
CurrentWordIdx int `json:"-"` // index of current word in all_words
CurrentWordIdxInParent int `json:"-"` // index of current word in parents command line 1 for first word after parent
split_on_equals bool // true if the cmdline is split on = (BASH does this because readline does this)
}

View File

@@ -24,9 +24,9 @@ func (self *CommandGroup) HasVisibleSubCommands() bool {
}
func (self *CommandGroup) Clone(parent *Command) *CommandGroup {
ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, 0, len(self.SubCommands))}
ans := CommandGroup{Title: self.Title, SubCommands: make([]*Command, len(self.SubCommands))}
for i, o := range self.SubCommands {
self.SubCommands[i] = o.Clone(parent)
ans.SubCommands[i] = o.Clone(parent)
}
return &ans
}

View File

@@ -95,7 +95,7 @@ func run_CMD_NAME(cmd *cli.Command, args []string) (return_code int, err error)
func setup_CMD_NAME(parent *cli.Command) *cli.Command {
ans := parent.AddSubCommand(&cli.Command{
Name: "CMD_NAME",
Name: "CLI_NAME",
Usage: "ARGSPEC",
ShortDescription: "SHORT_DESC",
HelpText: "LONG_DESC",