Skip to content

Commit

Permalink
Merge pull request mitchellh#82 from mitchellh/noop-autocomplete
Browse files Browse the repository at this point in the history
Noop autocomplete setup if we're not autocompleting
  • Loading branch information
mitchellh authored Apr 6, 2020
2 parents 902dcca + 44f0650 commit 171fbb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -403,6 +404,16 @@ func (c *CLI) initAutocomplete() {
c.autocompleteInstaller = &realAutocompleteInstaller{}
}

// We first set c.autocomplete to a noop autocompleter that outputs
// to nul so that we can detect if we're autocompleting or not. If we're
// not, then we do nothing. This saves a LOT of compute cycles since
// initAutoCompleteSub has to walk every command.
c.autocomplete = complete.New(c.Name, complete.Command{})
c.autocomplete.Out = ioutil.Discard
if !c.autocomplete.Complete() {
return
}

// Build the root command
cmd := c.initAutocompleteSub("")

Expand Down
4 changes: 4 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,10 @@ func TestCLIAutocomplete_subcommandArgs(t *testing.T) {
Autocomplete: true,
}

// We need to initialize the autocomplete environment so that
// the cli doesn't no-op the autocomplete init
defer testAutocomplete(t, "must be non-empty")()

// Initialize
cli.init()

Expand Down

0 comments on commit 171fbb5

Please sign in to comment.