make shell autocomplete more robust

This commit is contained in:
Joshua Rubin
2016-11-04 14:56:28 -06:00
parent d86a009f5e
commit ea3df26e64
4 changed files with 62 additions and 37 deletions

18
app.go
View File

@@ -145,10 +145,6 @@ func (a *App) Setup() {
}
}
if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag)
}
if !a.HideVersion {
a.appendFlag(VersionFlag)
}
@@ -173,6 +169,14 @@ func (a *App) Setup() {
func (a *App) Run(arguments []string) (err error) {
a.Setup()
// handle the completion flag separately from the flagset since
// completion could be attempted after a flag, but before its value was put
// on the command line. this causes the flagset to interpret the completion
// flag name as the value of the flag before it which is undesirable
// note that we can only do this because the shell autocomplete function
// always appends the completion flag at the end of the command
complete, arguments := checkCompleteFlag(a, arguments)
// parse flags
set := flagSet(a.Name, a.Flags)
set.SetOutput(ioutil.Discard)
@@ -184,6 +188,7 @@ func (a *App) Run(arguments []string) (err error) {
ShowAppHelp(context)
return nerr
}
context.complete = complete
if checkCompletions(context) {
return nil
@@ -283,11 +288,6 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
}
a.Commands = newCmds
// append flags
if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag)
}
// parse flags
set := flagSet(a.Name, a.Flags)
set.SetOutput(ioutil.Discard)