deno.land / x / yargs@v17.6.0-deno / build / lib / completion.js

نووسراو ببینە
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { isCommandBuilderCallback } from './command.js';import { assertNotStrictEqual } from './typings/common-types.js';import * as templates from './completion-templates.js';import { isPromise } from './utils/is-promise.js';import { parseCommand } from './parse-command.js';export class Completion { constructor(yargs, usage, command, shim) { var _a, _b, _c; this.yargs = yargs; this.usage = usage; this.command = command; this.shim = shim; this.completionKey = 'get-yargs-completions'; this.aliases = null; this.customCompletionFunction = null; this.indexAfterLastReset = 0; this.zshShell = (_c = (((_a = this.shim.getEnv('SHELL')) === null || _a === void 0 ? void 0 : _a.includes('zsh')) || ((_b = this.shim.getEnv('ZSH_NAME')) === null || _b === void 0 ? void 0 : _b.includes('zsh')))) !== null && _c !== void 0 ? _c : false; } defaultCompletion(args, argv, current, done) { const handlers = this.command.getCommandHandlers(); for (let i = 0, ii = args.length; i < ii; ++i) { if (handlers[args[i]] && handlers[args[i]].builder) { const builder = handlers[args[i]].builder; if (isCommandBuilderCallback(builder)) { this.indexAfterLastReset = i + 1; const y = this.yargs.getInternalMethods().reset(); builder(y, true); return y.argv; } } } const completions = []; this.commandCompletions(completions, args, current); this.optionCompletions(completions, args, argv, current); this.choicesFromOptionsCompletions(completions, args, argv, current); this.choicesFromPositionalsCompletions(completions, args, argv, current); done(null, completions); } commandCompletions(completions, args, current) { const parentCommands = this.yargs .getInternalMethods() .getContext().commands; if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current && !this.previousArgHasChoices(args)) { this.usage.getCommands().forEach(usageCommand => { const commandName = parseCommand(usageCommand[0]).cmd; if (args.indexOf(commandName) === -1) { if (!this.zshShell) { completions.push(commandName); } else { const desc = usageCommand[1] || ''; completions.push(commandName.replace(/:/g, '\\:') + ':' + desc); } } }); } } optionCompletions(completions, args, argv, current) { if ((current.match(/^-/) || (current === '' && completions.length === 0)) && !this.previousArgHasChoices(args)) { const options = this.yargs.getOptions(); const positionalKeys = this.yargs.getGroups()[this.usage.getPositionalGroupName()] || []; Object.keys(options.key).forEach(key => { const negable = !!options.configuration['boolean-negation'] && options.boolean.includes(key); const isPositionalKey = positionalKeys.includes(key); if (!isPositionalKey && !options.hiddenOptions.includes(key) && !this.argsContainKey(args, key, negable)) { this.completeOptionKey(key, completions, current); if (negable && !!options.default[key]) this.completeOptionKey(`no-${key}`, completions, current); } }); } } choicesFromOptionsCompletions(completions, args, argv, current) { if (this.previousArgHasChoices(args)) { const choices = this.getPreviousArgChoices(args); if (choices && choices.length > 0) { completions.push(...choices.map(c => c.replace(/:/g, '\\:'))); } } } choicesFromPositionalsCompletions(completions, args, argv, current) { if (current === '' && completions.length > 0 && this.previousArgHasChoices(args)) { return; } const positionalKeys = this.yargs.getGroups()[this.usage.getPositionalGroupName()] || []; const offset = Math.max(this.indexAfterLastReset, this.yargs.getInternalMethods().getContext().commands.length + 1); const positionalKey = positionalKeys[argv._.length - offset - 1]; if (!positionalKey) { return; } const choices = this.yargs.getOptions().choices[positionalKey] || []; for (const choice of choices) { if (choice.startsWith(current)) { completions.push(choice.replace(/:/g, '\\:')); } } } getPreviousArgChoices(args) { if (args.length < 1) return; let previousArg = args[args.length - 1]; let filter = ''; if (!previousArg.startsWith('-') && args.length > 1) { filter = previousArg; previousArg = args[args.length - 2]; } if (!previousArg.startsWith('-')) return; const previousArgKey = previousArg.replace(/^-+/, ''); const options = this.yargs.getOptions(); const possibleAliases = [ previousArgKey, ...(this.yargs.getAliases()[previousArgKey] || []), ]; let choices; for (const possibleAlias of possibleAliases) { if (Object.prototype.hasOwnProperty.call(options.key, possibleAlias) && Array.isArray(options.choices[possibleAlias])) { choices = options.choices[possibleAlias]; break; } } if (choices) { return choices.filter(choice => !filter || choice.startsWith(filter)); } } previousArgHasChoices(args) { const choices = this.getPreviousArgChoices(args); return choices !== undefined && choices.length > 0; } argsContainKey(args, key, negable) { const argsContains = (s) => args.indexOf((/^[^0-9]$/.test(s) ? '-' : '--') + s) !== -1; if (argsContains(key)) return true; if (negable && argsContains(`no-${key}`)) return true; if (this.aliases) { for (const alias of this.aliases[key]) { if (argsContains(alias)) return true; } } return false; } completeOptionKey(key, completions, current) { const descs = this.usage.getDescriptions(); const startsByTwoDashes = (s) => /^--/.test(s); const isShortOption = (s) => /^[^0-9]$/.test(s); const dashes = !startsByTwoDashes(current) && isShortOption(key) ? '-' : '--'; if (!this.zshShell) { completions.push(dashes + key); } else { const desc = descs[key] || ''; completions.push(dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`); } } customCompletion(args, argv, current, done) { assertNotStrictEqual(this.customCompletionFunction, null, this.shim); if (isSyncCompletionFunction(this.customCompletionFunction)) { const result = this.customCompletionFunction(current, argv); if (isPromise(result)) { return result .then(list => { this.shim.process.nextTick(() => { done(null, list); }); }) .catch(err => { this.shim.process.nextTick(() => { done(err, undefined); }); }); } return done(null, result); } else if (isFallbackCompletionFunction(this.customCompletionFunction)) { return this.customCompletionFunction(current, argv, (onCompleted = done) => this.defaultCompletion(args, argv, current, onCompleted), completions => { done(null, completions); }); } else { return this.customCompletionFunction(current, argv, completions => { done(null, completions); }); } } getCompletion(args, done) { const current = args.length ? args[args.length - 1] : ''; const argv = this.yargs.parse(args, true); const completionFunction = this.customCompletionFunction ? (argv) => this.customCompletion(args, argv, current, done) : (argv) => this.defaultCompletion(args, argv, current, done); return isPromise(argv) ? argv.then(completionFunction) : completionFunction(argv); } generateCompletionScript($0, cmd) { let script = this.zshShell ? templates.completionZshTemplate : templates.completionShTemplate; const name = this.shim.path.basename($0); if ($0.match(/\.js$/)) $0 = `./${$0}`; script = script.replace(/{{app_name}}/g, name); script = script.replace(/{{completion_command}}/g, cmd); return script.replace(/{{app_path}}/g, $0); } registerFunction(fn) { this.customCompletionFunction = fn; } setParsed(parsed) { this.aliases = parsed.aliases; }}export function completion(yargs, usage, command, shim) { return new Completion(yargs, usage, command, shim);}function isSyncCompletionFunction(completionFunction) { return completionFunction.length < 3;}function isFallbackCompletionFunction(completionFunction) { return completionFunction.length > 3;}
yargs

Version Info

Tagged at
a year ago