Repository navigation
fix(argopt): --help/--version 按 getopt_long 顺序契约短路 (#1573) - #1582
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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。<cmd> --help --bogus<cmd> --version --bogus<cmd> --bogus --help<cmd> --help --version<cmd> --version --help--help --bogus与--version --bogus的 stdout 与 GNU cmp 全 SAME。--help=x仍报错);cargo test --workspace427+18+16 全过。记录在案(不在本 PR 范围)
产品 exe 的 C++ 侧同族顺序缺陷(
dispatcher.cppm拦截扫描 +main.cppverb-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 改动。