github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/help/completion.go (about)

     1  // +build !bootstrap
     2  
     3  package help
     4  
     5  import (
     6  	"strings"
     7  
     8  	"github.com/jessevdk/go-flags"
     9  )
    10  
    11  // Topic is a help topic that implements completion for flags.
    12  // It's otherwise equivalent to a string.
    13  type Topic string
    14  
    15  // UnmarshalFlag implements the flags.Unmarshaler interface
    16  func (topic *Topic) UnmarshalFlag(value string) error {
    17  	*topic = Topic(value)
    18  	return nil
    19  }
    20  
    21  // Complete implements the flags.Completer interface, which is used for shell completion.
    22  func (topic Topic) Complete(match string) []flags.Completion {
    23  	topics := allTopics()
    24  	completions := make([]flags.Completion, len(topics))
    25  	for i, topic := range topics {
    26  		if strings.HasPrefix(topic, match) {
    27  			completions[i].Item = topic
    28  		}
    29  	}
    30  	return completions
    31  }