插件命令在没有位置参数时会丢失命令行选项
- Dominant language
- JavaScript
- Stars
- 29.5k
- Forks
- 2.9k
- Avg merge
- 15h 36m
- Merged PRs (30d)
- 70
Description
## 问题描述
在 `opencli` 调用插件命令时,如果该命令没有位置参数(positional args),插件函数 `func(_page, kwargs)` 可能收不到命令行里传入的选项参数,最终表现为 `kwargs` 是空对象 `{}`,命令会静默回退到默认值。
这会影响纯 options 风格的插件命令。
## 复现示例
例如本地插件里的这类命令:
```bash
opencli company-pm forum-requirements --days 7 --limit 2 --hot-value 50 --mode all
opencli company-pm collect-requirements --sources forum --days 1 --limit 2 --hot-value 50 --forum-mode all
```
预期结果:
- 插件函数能收到类似下面的参数:
```js
{
days: 7,
limit: 2,
'hot-value': 50,
mode: 'all'
}
```
实际结果:
- 插件函数可能收到:
```js
{}
```
从而导致命令静默使用默认值,比如:
- `days=30`
- `limit=50`
## 原因分析
问题看起来出在 `src/commanderAdapter.ts` 中对 Commander action 回调参数的处理。
当前逻辑类似:
```ts
const actionOpts = actionArgs[positionalArgs.length] ?? {};
```
但对于**没有位置参数**的命令,Commander 的 action 参数形态是:
- `actionArgs[0]`:解析后的 options 对象
- 最后一个参数:Commander 的 `Command` 实例
如果直接按当前位置去取,容易把参数判断错,导致 options 没有正确进入 `kwargs`。
## 最小复现观察
我本地用 Commander 做了最小实验,发现无位置参数命令时:
- `args[0]` 是普通对象,包含 `days / limit`
- `args[1]` 是 `Command` 实例
所以这一类命令需要显式区分:
- 哪个是 options 对象
- 哪个是 trailing `Command`
## 建议修复方向
建议在 `registerCommandToProgram()` 里处理 action 回调时:
- 只有当 `actionArgs[positionalArgs.length]` 是普通对象
- 且它不是最后那个 `Command` 实例
才把它当成 optionsRecord
这样既能兼容有位置参数的命令,也能兼容纯 options 命令。
## 影响范围
这类问题主要影响:
- 插件命令
- 无位置参数
- 主要依赖命名选项(`--days`、`--limit`、`--mode` 等)
## 备注
我这边已经在本地插件层做了兜底,所以当前业务可以继续用;但从根上看,还是 `opencli` 核心参数传递这里更适合修一下。
Contributor guide
Research direction
Start in src/commanderAdapter.ts, especially registerCommandToProgram(), and inspect how Commander action arguments are mapped when a command has no positional arguments. Reproduce the issue with the forum-requirements or collect-requirements examples, then verify that options such as days, limit, and mode reach the plugin kwargs instead of an empty object.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 64/100