alecthomas / kingpin

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser
MIT License
3.5k stars 273 forks source link

Fix autocompletion of args that consume remainder #314

Closed jpcoenen closed 4 years ago

jpcoenen commented 4 years ago

The Go runtime would panic at the following line because of an out-of-bounds slice access if autocompletion was requested for a clause that consumed the remainder of a command:

https://github.com/alecthomas/kingpin/blob/cf44c60659e38b5559aa885c13ad76c9bd6e8d50/cmd.go#L35

By specifically checking for these kinds of clauses, the problem should be mitigated.

Example

package main

import "github.com/alecthomas/kingpin"

func main() {
  var strings []string
  kingpin.Arg("test", "").StringsVar(&strings)
  kingpin.Parse()
}
$ go run main.go --completion-bash 1 2  
panic: runtime error: index out of range [1] with length 1
alecthomas commented 4 years ago

Thanks!