completions.go 903 B

123456789101112131415161718192021222324252627282930313233
  1. package kingpin
  2. // HintAction is a function type who is expected to return a slice of possible
  3. // command line arguments.
  4. type HintAction func() []string
  5. type completionsMixin struct {
  6. hintActions []HintAction
  7. builtinHintActions []HintAction
  8. }
  9. func (a *completionsMixin) addHintAction(action HintAction) {
  10. a.hintActions = append(a.hintActions, action)
  11. }
  12. // Allow adding of HintActions which are added internally, ie, EnumVar
  13. func (a *completionsMixin) addHintActionBuiltin(action HintAction) {
  14. a.builtinHintActions = append(a.builtinHintActions, action)
  15. }
  16. func (a *completionsMixin) resolveCompletions() []string {
  17. var hints []string
  18. options := a.builtinHintActions
  19. if len(a.hintActions) > 0 {
  20. // User specified their own hintActions. Use those instead.
  21. options = a.hintActions
  22. }
  23. for _, hintAction := range options {
  24. hints = append(hints, hintAction()...)
  25. }
  26. return hints
  27. }