diff --git a/command.go b/command.go index 76adab8..9d10dfe 100644 --- a/command.go +++ b/command.go @@ -296,10 +296,16 @@ func copyFlagSetWithout(fs *pflag.FlagSet, without string) *pflag.FlagSet { } func (inv *Invocation) CurWords() (prev string, cur string) { - if len(inv.Args) == 1 { + switch len(inv.Args) { + // All the shells we support will supply at least one argument (empty string), + // but we don't want to panic. + case 0: + cur = "" + prev = "" + case 1: cur = inv.Args[0] prev = "" - } else { + default: cur = inv.Args[len(inv.Args)-1] prev = inv.Args[len(inv.Args)-2] }