| 1 | package optscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/ |
| 2 | |
| 3 | import ( |
| 4 | "reflectscip-go gomod github.com/golang/go/src go1.18 reflect/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.18 strings/" |
| 6 | ) |
| 7 | |
| 8 | type Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# struct { |
| 9 | Valuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Value. string |
| 10 | Descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Description. string |
| 11 | } |
| 12 | |
| 13 | func (c *CmdSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#) unseenFlagsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#unseenFlags().(arg string) []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# { |
| 14 | var flags []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# |
| 15 | for i := 0; i < len(c.optsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#opts.); i++ { |
| 16 | spec := &c.optsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#opts.[i] |
| 17 | if !spec.appliesToAliasscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#appliesToAlias().(c.namescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#name.) || spec.seenscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#seen. { |
| 18 | continue |
| 19 | } |
| 20 | switch spec.kindscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#kind. { |
| 21 | case flagscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/flag., optionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/option.: |
| 22 | if spec.shortscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#short. != "" && stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(spec.shortscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#short., arg) { |
| 23 | flags = append(flags, Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#{ |
| 24 | Valuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Value.: spec.shortscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#short. + " ", |
| 25 | Descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Description.: spec.descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#description., |
| 26 | }) |
| 27 | } |
| 28 | if spec.longscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#long. != "" && stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(spec.longscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#long., arg) { |
| 29 | flags = append(flags, Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#{ |
| 30 | Valuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Value.: spec.longscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#long. + " ", |
| 31 | Descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Description.: spec.descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#description., |
| 32 | }) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | return flags |
| 37 | } |
| 38 | |
| 39 | func (c *CmdSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#) nextPositionalscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#nextPositional().() *optSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec# { |
| 40 | var spec *optSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec# |
| 41 | for p := len(c.positionalsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#positionals.) - 1; p >= 0; p-- { |
| 42 | spec = &c.optsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#opts.[c.positionalsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#positionals.[p]] |
| 43 | if !spec.appliesToAliasscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#appliesToAlias().(c.namescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#name.) { |
| 44 | continue |
| 45 | } |
| 46 | if spec.seenValuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#seenValue. && (p+1) < len(c.positionalsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#positionals.) { |
| 47 | // first "unseen" positional argument |
| 48 | return &c.optsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#opts.[c.positionalsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#positionals.[p+1]] |
| 49 | } |
| 50 | } |
| 51 | return spec |
| 52 | } |
| 53 | |
| 54 | func (s *optSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#) getCompletionsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#getCompletions().(arg string) []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# { |
| 55 | if s.completescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#complete..IsValidscip-go gomod github.com/golang/go/src go1.18 reflect/Value#IsValid().() { |
| 56 | in := []reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.Valuescip-go gomod github.com/golang/go/src go1.18 reflect/Value#{reflectscip-go gomod github.com/golang/go/src go1.18 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.18 reflect/ValueOf().(arg)} |
| 57 | out := s.completescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#complete..Callscip-go gomod github.com/golang/go/src go1.18 reflect/Value#Call().(in) |
| 58 | if res, ok := out[0].Interfacescip-go gomod github.com/golang/go/src go1.18 reflect/Value#Interface().().([]string); ok { |
| 59 | var completions []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# |
| 60 | for _, value := range res { |
| 61 | value, desc, found := stringsscip-go gomod github.com/golang/go/src go1.18 strings/.Cutscip-go gomod github.com/golang/go/src go1.18 strings/Cut().(value, "\n") |
| 62 | if !found { |
| 63 | desc = s.descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#description. |
| 64 | } |
| 65 | completions = append(completions, Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#{ |
| 66 | Valuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Value.: value, |
| 67 | Descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Description.: desc, |
| 68 | }) |
| 69 | } |
| 70 | return completions |
| 71 | } |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | func (c *CmdSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#) GetCompletionsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#GetCompletions().(args *Argsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#) ([]Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#, string) { |
| 77 | if args.Countscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#Count().() == 0 || (args.Countscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#Count().() == 1 && args.TrailingSpacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#TrailingSpace().() == "") { |
| 78 | return nil, "" |
| 79 | } |
| 80 | |
| 81 | var completions []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# |
| 82 | var prefix string |
| 83 | var flags []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion# |
| 84 | var last *seenArgscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg# |
| 85 | var spec *optSpecscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec# |
| 86 | |
| 87 | c, _ = c.parseArgsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#parseArgs().(args.Clonescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#Clone().()) |
| 88 | |
| 89 | if len(c.seenscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#seen.) > 0 { |
| 90 | last = c.seenscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#seen.[len(c.seenscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#seen.)-1] |
| 91 | } |
| 92 | |
| 93 | if args.TrailingSpacescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#TrailingSpace().() != "" { |
| 94 | // Complete new argument |
| 95 | prefix = args.Stringscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#String().() |
| 96 | if last != nil && !last.specscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg#spec..seenValuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#seenValue. { |
| 97 | spec = last.specscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg#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.unseenFlagsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#unseenFlags().("") |
| 103 | spec = c.nextPositionalscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#nextPositional().() |
| 104 | } |
| 105 | if spec != nil { |
| 106 | completions = spec.getCompletionsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#getCompletions().("") |
| 107 | } |
| 108 | } else { |
| 109 | // Complete current argument |
| 110 | arg := args.Cutscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#Cut().(1)[0] |
| 111 | prefix = args.Stringscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#String().() + " " |
| 112 | |
| 113 | if last != nil && last.indexesscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg#indexes.[len(last.indexesscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg#indexes.)-1] == args.Countscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Args#Count().() { |
| 114 | s := last.specscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/seenArg#spec. |
| 115 | f := s.longscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#long. + "=" |
| 116 | switch { |
| 117 | case (s.kindscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#kind. == flagscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/flag. || s.kindscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#kind. == optionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/option.) && (s.shortscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#short. == arg || s.longscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#long. == arg): |
| 118 | // Current argument is precisely a flag. |
| 119 | spec = nil |
| 120 | completions = []Completionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#{{Valuescip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Value.: arg + " ", Descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/Completion#Description.: s.descriptionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#description.}} |
| 121 | case s.kindscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#kind. == optionscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/option. && f != "=" && stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 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 = stringsscip-go gomod github.com/golang/go/src go1.18 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.18 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.nextPositionalscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#nextPositional().() |
| 135 | } |
| 136 | if spec != nil { |
| 137 | completions = spec.getCompletionsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/optSpec#getCompletions().(arg) |
| 138 | } |
| 139 | if stringsscip-go gomod github.com/golang/go/src go1.18 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.18 strings/HasPrefix().(arg, "-") { |
| 140 | flags = c.unseenFlagsscip-go gomod git.sr.ht/~rjarry/go-opt/v2 . `git.sr.ht/~rjarry/go-opt/v2`/CmdSpec#unseenFlags().(arg) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if flags != nil { |
| 145 | completions = append(completions, flags...) |
| 146 | } |
| 147 | |
| 148 | return completions, prefix |
| 149 | } |