File: completion-parse-args.go

package info (click to toggle)
kitty 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 28,564 kB
  • sloc: ansic: 82,787; python: 55,191; objc: 5,122; sh: 1,295; xml: 364; makefile: 143; javascript: 78
file content (196 lines) | stat: -rw-r--r-- 5,136 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>

package cli

import (
	"fmt"
	"os"
	"strings"
)

var _ = fmt.Print
var _ = os.Getenv

func (self *Completions) add_options_group(options []*Option, word string) {
	group := self.AddMatchGroup("Options")
	if strings.HasPrefix(word, "--") {
		if word == "--" {
			group.Matches = append(group.Matches, &Match{Word: "--", Description: "End of options"})
		}
		for _, opt := range options {
			for _, q := range opt.Aliases {
				if strings.HasPrefix(q.String(), word) {
					group.Matches = append(group.Matches, &Match{Word: q.String(), Description: opt.Help})
					break
				}
			}
		}
	} else {
		if word == "-" {
			group.Matches = append(group.Matches, &Match{Word: "--", Description: "End of options"})
			for _, opt := range options {
				has_single_letter_alias := false
				for _, q := range opt.Aliases {
					if q.IsShort {
						group.AddMatch("-"+q.NameWithoutHyphens, opt.Help)
						has_single_letter_alias = true
						break
					}
				}
				if !has_single_letter_alias {
					for _, q := range opt.Aliases {
						if !q.IsShort {
							group.AddMatch(q.String(), opt.Help)
							break
						}
					}
				}
			}
		} else {
			runes := []rune(word)
			last_letter := string(runes[len(runes)-1])
			for _, opt := range options {
				for _, q := range opt.Aliases {
					if q.IsShort && q.NameWithoutHyphens == last_letter {
						group.AddMatch(word, opt.Help)
						return
					}
				}
			}
		}
	}
}

func (self *Command) sub_command_allowed_at(completions *Completions, arg_num int) bool {
	if self.SubCommandMustBeFirst {
		return arg_num == 1 && completions.CurrentWordIdxInParent == 1
	}
	return arg_num == 1
}

func complete_word(word string, completions *Completions, only_args_allowed bool, expecting_arg_for *Option, arg_num int) {
	cmd := completions.CurrentCmd
	if expecting_arg_for != nil {
		if expecting_arg_for.Completer != nil {
			expecting_arg_for.Completer(completions, word, arg_num)
		}
		return
	}
	if !only_args_allowed && strings.HasPrefix(word, "-") {
		if strings.HasPrefix(word, "--") && strings.Contains(word, "=") {
			idx := strings.Index(word, "=")
			option := cmd.FindOption(word[:idx])
			if option != nil {
				if option.Completer != nil {
					option.Completer(completions, word[idx+1:], arg_num)
					completions.AddPrefixToAllMatches(word[:idx+1])
				}
			}
		} else {
			completions.add_options_group(cmd.AllOptions(), word)
		}
		return
	}
	if cmd.HasVisibleSubCommands() && cmd.sub_command_allowed_at(completions, arg_num) {
		for _, cg := range cmd.SubCommandGroups {
			group := completions.AddMatchGroup(cg.Title)
			if group.Title == "" {
				group.Title = "Sub-commands"
			}
			for _, sc := range cg.SubCommands {
				if !sc.Hidden && strings.HasPrefix(sc.Name, word) {
					t := sc.ShortDescription
					if t == "" {
						t = sc.HelpText
					}
					group.AddMatch(sc.Name, t)
				}
			}
		}
		if cmd.SubCommandIsOptional && cmd.ArgCompleter != nil {
			cmd.ArgCompleter(completions, word, arg_num)
		}
		return
	}

	if cmd.ArgCompleter != nil {
		cmd.ArgCompleter(completions, word, arg_num)
	}
}

func completion_parse_args(cmd *Command, words []string, completions *Completions) {
	completions.CurrentCmd = cmd
	if len(words) == 0 {
		complete_word("", completions, false, nil, 0)
		return
	}
	completions.AllWords = words

	var expecting_arg_for *Option
	only_args_allowed := false
	arg_num := 0

	for i, word := range words {
		cmd = completions.CurrentCmd
		completions.CurrentWordIdx = i
		completions.CurrentWordIdxInParent++
		is_last_word := i == len(words)-1
		is_option_equal := completions.split_on_equals && word == "=" && expecting_arg_for != nil
		if only_args_allowed || (expecting_arg_for == nil && !strings.HasPrefix(word, "-")) {
			if !is_option_equal {
				arg_num++
			}
			if arg_num == 1 {
				cmd.IndexOfFirstArg = completions.CurrentWordIdx
			}
		}
		if is_last_word {
			if is_option_equal {
				word = ""
			}
			complete_word(word, completions, only_args_allowed, expecting_arg_for, arg_num)
		} else {
			if expecting_arg_for != nil {
				if is_option_equal {
					continue
				}
				expecting_arg_for = nil
				continue
			}
			if word == "--" {
				only_args_allowed = true
				continue
			}
			if !only_args_allowed && strings.HasPrefix(word, "-") {
				if !strings.Contains(word, "=") {
					option := cmd.FindOption(word)
					if option != nil && option.needs_argument() {
						expecting_arg_for = option
					}
				}
				continue
			}
			if cmd.HasVisibleSubCommands() && cmd.sub_command_allowed_at(completions, arg_num) {
				sc := cmd.FindSubCommand(word)
				if sc == nil {
					only_args_allowed = true
					continue
				}
				completions.CurrentCmd = sc
				cmd = sc
				arg_num = 0
				completions.CurrentWordIdxInParent = 0
				only_args_allowed = cmd.OnlyArgsAllowed
				if cmd.ParseArgsForCompletion != nil {
					cmd.ParseArgsForCompletion(cmd, words[i+1:], completions)
					return
				}
			} else if cmd.StopCompletingAtArg > 0 && arg_num >= cmd.StopCompletingAtArg {
				return
			} else {
				only_args_allowed = true
				continue
			}
		}
	}
}