Skip to content

fix(argopt): --help/--version 按 getopt_long 顺序契约短路 (#1573) - #1582

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/argopt-help-1573
Nov 12, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/argopt-help-1573

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1573

根因

rust2/crates/winux-cmds/src/argopt.rs::parse() 把 --help/--version 当普通 flag 记下后继续扫描,后面的未知选项仍触发 Err(usage_status)。GNU getopt_long 每次只返回一个选项,coreutils 的 main 在拿到 help/version 值的当下就 usage(EXIT_SUCCESS) / 打版本退出(case_GETOPT_HELP_CHAR),argv 里其后的 token 根本不会被校验——所以 GNU 是 <cmd> --help --bogus 出 usage rc 0,我们却是 unrecognized rc 1。

修法:修解析循环的顺序契约,不是给 --help 加特例

flag 记录点是循环里唯一的终结点:help/version 只能经选项表解析(精确匹配或唯一缩写——coreutils longopts 表里虚拟条目的同款形态)或裸 -h 映射到达这里;到达即 return Ok(out),此前解析过的选项保留在 Parsed 里,其后 token 不再校验。

保持不变的既有语义(探针验证):

  • 值选项把 --help 吃成 optarg(mkdir --mode --help → missing operand rc 1,与 GNU 一致)——它不进 flags,不会被短路;
  • --bogus --help 仍 unrecognized rc 1(顺序敏感反向用例);
  • --help --version 首个 flag 胜出、--version --help 版本胜出;
  • --help=x 仍 doesn't allow an argument。

验证矩阵

GNU 基线:WSL coreutils 9.4(Ubuntu 24.04),脚本文件探针(rust2/target/probe/),禁用内联 wsl bash -c。

probe GNU 9.4 driver(修后)
<cmd> --help --bogus rc 0,usage → stdout rc 0,一致(19 命令全绿:basename dirname whoami nproc hostid users mkdir b2sum head cut tr tac seq realpath readlink chmod truncate numfmt id uname)
<cmd> --version --bogus rc 0,版本块 → stdout rc 0,一致
<cmd> --bogus --help rc 1 unrecognized rc 1,一致
<cmd> --help --version rc 0 help 胜出 rc 0 一致
<cmd> --version --help rc 0 version 胜出 rc 0 一致
  • 逐字节对照:9 个 gnuhelp 命令(whoami nproc b2sum head cut tr tac id uname)--help --bogus 与 --version --bogus 的 stdout 与 GNU cmp 全 SAME。
  • 单测:argopt::tests 新增 9 个顺序语义 pin(双向短路、首个 flag 胜出、help 前选项/操作数保留、help 作值不短路、--help=x 仍报错);cargo test --workspace 427+18+16 全过。
  • 全量 rust2 差分 gate:1406 GNU-gated + 145 win-only,0 fail(无回归)。
  • 全量 ctest:失败集与 origin/main base 完全一致(21 条既有环境绑定失败,无新增)。

记录在案(不在本 PR 范围)

产品 exe 的 C++ 侧同族顺序缺陷(dispatcher.cppm 拦截扫描 + main.cpp verb-form version 预扫)单独立案:#1581(mkdir -q --help / mkdir --bogus --help 出 usage rc 0;mkdir --help --version 出顶层 banner)。同一不变量,不同的循环,需自己的 gate 矩阵。另注:argopt 的裸 -h→help 映射与 GNU 9.4 相悖(GNU 对未定义 -h 的命令报 invalid option -- 'h',本 issue 里 basename -h --bogus rc=0 一行实测不成立),属既有独立分歧,未随本 PR 改动。

GNU getopt_long returns one option per call and coreutils' main exits at
the moment it receives the help/version value (case_GETOPT_HELP_CHAR ->
usage(EXIT_SUCCESS), case_GETOPT_VERSION_CHAR -> version_etc + exit), so
options after the first help/version in argv order are never validated.
argopt::parse() recorded --help/--version as flags and kept scanning, so
a later unknown option turned `cmd --help --bogus` into a usage error
rc 1 — the reverse of GNU (usage to stdout, rc 0).

Fix the parse loop's order contract itself, not a --help guard: the
flag-record site returns Ok at the first help/version. That site is only
reachable through option-table resolution (exact or unique-abbreviation
match — the same virtual entries coreutils' longopts tables hold) and
the bare `-h` mapping. Everything parsed before the terminal flag stays
in Parsed; a help/version consumed as the VALUE of a preceding value
option never lands in flags and is not short-circuited (getopt hands it
to the command as optarg: `mkdir --mode --help` still reads --help as
the mode value). `--bogus --help` still fails unrecognized rc 1, and
`--help --version` keeps first-flag-wins.

GNU probes (WSL coreutils 9.4, script-file, rust2/target/probe/1573):
- `basename --help --bogus` rc=0 usage; `--version --bogus` rc=0 version
- `basename --bogus --help` rc=1 unrecognized option '--bogus'
- `--help --version` help wins; `--version --help` version wins

Verification:
- 19-command driver matrix (argopt commands) matches GNU rc/stream on
  every row; the 9 gnuhelp commands (whoami nproc b2sum head cut tr tac
  id uname) are byte-identical to GNU for both directions.
- cargo test --workspace: 427 + 18 + 16 pass (9 new order pins in
  argopt::tests).
- Full rust2 differential gate: 1406 GNU-gated + 145 win-only, 0 fail.
- ctest failure set identical to the origin/main base: 21 pre-existing
  environment-bound failures, none new.

Known separate surface (filed #1581): the C++ dispatcher's --help/
--version interception scan and main's verb-form version scan still
honor a later help/version over an earlier invalid token on the product
exe (`mkdir -q --help`, `mkdir --bogus --help`, `mkdir --help --version`)
— same order invariant, different loop, own gate matrix.
@caomengxuan666
caomengxuan666 merged commit 6cff792 into main Nov 12, 2026
11 of 14 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/argopt-help-1573 branch November 18, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

argopt: --help/--version 之后的未知选项未按 getopt_long 短路(GNU 先出帮助 rc 0)

1 participant