| 1 | package opt |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | type Completion struct { |
| 9 | Value string |
| 10 | Description string |
| 11 | } |
| 12 | |
| 13 | func (c *CmdSpec) unseenFlags(arg string) []Completion { |
| 14 | var flags []Completion |
| 15 | for i := 0; i < len(c.opts); i++ { |
| 16 | spec := &c.opts[i] |
| 17 | if !spec.appliesToAlias(c.name) || spec.seen { |
| 18 | continue |
| 19 | } |
| 20 | switch spec.kind { |
| 21 | case flag, option: |
| 22 | if spec.short != "" && strings.HasPrefix(spec.short, arg) { |
| 23 | flags = append(flags, Completion{ |
| 24 | Value: spec.short + " ", |
| 25 | Description: spec.description, |
| 26 | }) |
| 27 | } |
| 28 | if spec.long != "" && strings.HasPrefix(spec.long, arg) { |
| 29 | flags = append(flags, Completion{ |
| 30 | Value: spec.long + " ", |
| 31 | Description: spec.description, |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | return flags |
| 37 | } |
| 38 | |
| 39 | func (c *CmdSpec) nextPositional() *optSpec { |
| 40 | var spec *optSpec |
| 41 | for p := len(c.positionals) - 1; p >= 0; p-- { |
| 42 | spec = &c.opts[c.positionals[p]] |
| 43 | if !spec.appliesToAlias(c.name) { |
| 44 | continue |
| 45 | } |
| 46 | if spec.seenValue && (p+1) < len(c.positionals) { |
| 47 | // first "unseen" positional argument |
| 48 | return &c.opts[c.positionals[p+1]] |
| 49 | } |
| 50 | } |
| 51 | return spec |
| 52 | } |
| 53 | |
| 54 | func (s *optSpec) getCompletions(arg string) []Completion { |
| 55 | if s.complete.IsValid() { |
| 56 | in := []reflect.Value{reflect.ValueOf(arg)} |
| 57 | out := s.complete.Call(in) |
| 58 | if res, ok := out[0].Interface().([]string); ok { |
| 59 | var completions []Completion |
| 60 | for _, value := range res { |
| 61 | value, desc, found := strings.Cut(value, "\n") |
| 62 | if !found { |
| 63 | desc = s.description |
| 64 | } |
| 65 | completions = append(completions, Completion{ |
| 66 | Value: value, |
| 67 | Description: desc, |
| 68 | }) |
| 69 | } |
| 70 | return completions |
| 71 | } |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | func (c *CmdSpec) GetCompletions(args *Args) ([]Completion, string) { |
| 77 | if args.Count() == 0 || (args.Count() == 1 && args.TrailingSpace() == "") { |
| 78 | return nil, "" |
| 79 | } |
| 80 | |
| 81 | var completions []Completion |
| 82 | var prefix string |
| 83 | var flags []Completion |
| 84 | var last *seenArg |
| 85 | var spec *optSpec |
| 86 | |
| 87 | c, _ = c.parseArgs(args.Clone()) |
| 88 | |
| 89 | if len(c.seen) > 0 { |
| 90 | last = c.seen[len(c.seen)-1] |
| 91 | } |
| 92 | |
| 93 | if args.TrailingSpace() != "" { |
| 94 | // Complete new argument |
| 95 | prefix = args.String() |
| 96 | if last != nil && !last.spec.seenValue { |
| 97 | spec = last.spec |
| 98 | } |
| 99 | if spec == nil { |
| 100 | // Last argument was not a flag that required a value. |
| 101 | // Complete for the next unseen positional argument. |
| 102 | flags = c.unseenFlags("") |
| 103 | spec = c.nextPositional() |
| 104 | } |
| 105 | if spec != nil { |
| 106 | completions = spec.getCompletions("") |
| 107 | } |
| 108 | } else { |
| 109 | // Complete current argument |
| 110 | arg := args.Cut(1)[0] |
| 111 | prefix = args.String() + " " |
| 112 | |
| 113 | if last != nil && last.indexes[len(last.indexes)-1] == args.Count() { |
| 114 | s := last.spec |
| 115 | f := s.long + "=" |
| 116 | switch { |
| 117 | case (s.kind == flag || s.kind == option) && (s.short == arg || s.long == arg): |
| 118 | // Current argument is precisely a flag. |
| 119 | spec = nil |
| 120 | completions = []Completion{{Value: arg + " ", Description: s.description}} |
| 121 | case s.kind == option && f != "=" && strings.HasPrefix(arg, f): |
| 122 | // Current argument is a long flag in the format: |
| 123 | // --flag=value |
| 124 | // Strip the prefix and complete the value. |
| 125 | prefix += f |
| 126 | arg = strings.TrimPrefix(arg, f) |
| 127 | fallthrough |
| 128 | default: |
| 129 | spec = s |
| 130 | } |
| 131 | } else { |
| 132 | // Current argument was not identified, attempt |
| 133 | // completion from the next unseen positional. |
| 134 | spec = c.nextPositional() |
| 135 | } |
| 136 | if spec != nil { |
| 137 | completions = spec.getCompletions(arg) |
| 138 | } |
| 139 | if strings.HasPrefix(arg, "-") { |
| 140 | flags = c.unseenFlags(arg) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if flags != nil { |
| 145 | completions = append(completions, flags...) |
| 146 | } |
| 147 | |
| 148 | return completions, prefix |
| 149 | } |