An option is resolved from the root before the matched command, so a short name means one thing for the whole line
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 56/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- javascript, typescript
- 领域
- cli
调研方向
从 RootCommand#shouldCompleteFlags() 和 #stripOptions() 开始,然后跟踪 matchCommand() 如何调用后者,以及 handleFlagCompletion() 如何使用匹配的命令。使用 Node 运行提供的复现,并比较四种 argv 情况下的补全结果。完成的标准是:选项的参数数量遵循每个位置上确定的命令,同时命令之前的 root 选项仍像以前一样工作。
由索引模型根据 Issue 内容生成。
描述
What happens
RootCommand#shouldCompleteFlags() and #stripOptions() decide whether the word after an option is that option's value. Both resolve the option with findOption(this, arg) — the root first — and fall back to the registered commands only when the root does not have it. An option therefore has one arity for the whole line, whichever command the cursor is in.
A CLI whose root carries the program's own options, and whose sub-command gives one of those letters to an option that takes a value, cannot be completed there.
Reproduction
// repro.mjs — `npm i @bomb.sh/[email protected]`, then `node repro.mjs`
import { RootCommand } from '@bomb.sh/tab'
const build = () => {
const root = new RootCommand()
root.option('help', 'Display this help message', 'h') // boolean, takes no value
const serve = root.command('serve', 'Serve the app')
serve.option('host', 'Host name', c => c('localhost', ''), 'h') // takes a value, same letter
serve.argument('env', c => {
c('prod', '')
c('staging', '')
})
return root
}
for (const argv of [
['serve', '-h', ''],
['serve', '-h', 'localhost', ''],
['serve', '--host', ''],
['-h', '']
]) {
process.stdout.write(`${JSON.stringify(argv).padEnd(36)} => `)
build().parse([...argv])
}
Actual output (@bomb.sh/tab 0.0.22, Node 24.21.0). Each request prints its candidates one per line; they are joined with / here:
["serve","-h",""] => prod / staging / :4
["serve","-h","localhost",""] => :4
["serve","--host",""] => localhost / :4
["-h",""] => serve / :4
serve -h <TAB>offers the positional, because-hwas read as the root's booleanhelp. Expected:localhost, the value of the command's--host.serve -h localhost <TAB>offers nothing, becauselocalhostwas counted as the positional. Expected:prod,staging.- The last two lines are the controls, and both are right: the long name resolves against the command, and
-hin front of a command name is the root's.
Why it is not simply "look at the matched command"
stripOptions() is what matchCommand() calls to find the command, so the command is not known when the question is asked. Fixing it means walking the words in order and resolving each option against the command that is settled at that point — the root until a command name is matched, then that command, and so on.
handleFlagCompletion() already resolves against the matched command, so once shouldCompleteFlags() answers correctly for a letter typed behind a command name, the value completion follows.
Related
Issue #110 (closed) is the same family: handlePositionalCompletion() counted words without stripping options. This is the other half — the stripping itself does not know where it is.
Downstream
gunshi (kazupon/gunshi#744) works around it by re-registering the root's entry for that one letter, for the duration of one completion request, and only when the letter was typed behind the matched command's name
(PR #753, corrected in PR #754 — the first attempt re-registered the root's entry under the option's long name, which flipped the arity of --help too). The workaround cannot express the same letter on both sides of a command name, because one Option carries one arity for the request. If the lookup here becomes positional, that workaround is deleted whole.
- 主要语言
- TypeScript
- 星标
- 280
- 派生
- 11
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
bombshell-dev/tab 的其他 Issue
-
enhancement
难度 3/5 1-2 天 新手友好度 55/100
bombshell-dev/tab#127 · 2 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
bombshell-dev/tab#118 ·
-
provide installation utilities可能已有人在做 @theoephraim 于 134 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 35/100
bombshell-dev/tab#117 ·
-
难度 4/5 3-5 天 新手友好度 48/100
bombshell-dev/tab#107 · 3 条评论 ·
-
documentation enhancement
难度 5/5 一周以上 新手友好度 30/100
bombshell-dev/tab#106 · 1 条评论 ·
查看 bombshell-dev/tab 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 72/100
supadata-ai/mcp#27 ·
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
capricorn86/happy-dom#2485 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 66/100
siyuan-note/siyuan#20430 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 70/100
Albert-Weasker/niubigeo#194 ·
维护者通常 1 天内回复