Repository navigation
fix(invocation): accept clustered short options (#382) - #697
Conversation
GNU shell.c parse_shell_options reads `-eo pipefail` as `-e` plus `-o` whose value is the NEXT argv word (shell.c:931-943), so the clustered form is equivalent to the separated one. ShellInvocation::parse only expanded clusters that contained no `o`/`O` and then rejected `-eo` with "-eo: invalid option", which made `bash -eo pipefail -c '...'` (common in CI and Makefiles) fail against niu. Normalize the leading option words the way GNU walks them: every letter becomes a flag token, `o`/`O` consume the following argv word (never the rest of the cluster), and the first non-option word ends the option run. Long options and lone `-`/`--` are copied verbatim. This also stops splitting a trailing cluster that is really a positional parameter. The engine binary's wording check now shares the accepted set through the new rubash::usage::SHORT_OPTIONS table, which is also the generated source of truth for the niu product help (niubash#385), so the parser and the advertised options cannot drift apart. Tests: src/invocation.rs clustered-option regressions; src/usage.rs table drift guard.
c18d7b9 to
0f2d5d5
Compare
--encoded-command silently drops all invocation flags (-e, -u, -o pipefail, ...)
unixwin/niubash#390
caomengxuan666
left a comment
There was a problem hiding this comment.
Independent adversarial review, detached worktree at 0f2d5d5d (merge-base/origin/master = 8f596779). I verified the behaviour against genuine bashes, not the Niubash shim: WSL bash 5.3.0(1) (Linux) and D:/Git/bin/bash.exe 5.2.37(1) (MSYS). The parser was exercised directly by compiling a probe against the built librubash rlib, and end-to-end by building niu from niubash #388's head with [patch] rubash = { path = "…/rubash-review-697" }.
verified-ok — -eo pipefail ≡ -e -o pipefail
Parser probe, byte-for-byte equal:
A -eo pipefail -c true : OK flags=[("errexit",true),("pipefail",true)] cmd=Some("true")
B -e -o pipefail -c true: OK flags=[("errexit",true),("pipefail",true)] cmd=Some("true")
End-to-end, niu -eo pipefail -c true and niu -e -o pipefail -c true both exit 0 with no output.
verified-ok — -euo pipefail really activates errexit (+ nounset + pipefail)
niu -euo pipefail -c 'false; echo unreachable' => rc=1, stdout empty (errexit fired)
niu -e -u -o pipefail -c 'false; echo unreachable' => rc=1, stdout empty
niu -euo pipefail -c 'echo "$X"; echo after' => rc=127, "X: unbound variable"
niu -euo pipefail -c 'false | true; echo after' => rc=1, stdout empty (pipefail)
verified-ok — GNU does not let a value-taking letter eat the rest of its cluster
GNU (WSL 5.3): bash -eopipefail -c hi => "-c: invalid option name", rc=2
GNU (Git 5.2): bash -eopipefail -c hi => "-c: invalid option name", rc=2
GNU (WSL 5.3): bash -ecmd -c hi => "-d: invalid option", rc=1
niu (this PR): -eopipefail -c true => "-c: invalid shell option name", rc=2
niu (this PR): -ecmd -c true => "-ecmd: invalid option", rc=2 (usage printed)
Both reject; the o/O letters take the following argv word, never the cluster tail. The wording and the -ecmd exit code differ from GNU (non-blocking cosmetics).
verified-ok — -- matches GNU
niu -- script.sh a b → script script.sh, positional a,b. niu -- -c hi → tries to open -c as a script, exactly like bash -- -c hi ("No such file or directory").
verified-ok (no regression) — lone -
GNU reads commands from stdin for bash -; ShellInvocation::parse(["-"]) returns Err("-: invalid option") and niu - fails the same way. origin/master rejected it identically (empty flag word → Err("-: invalid option")), so the PR does not regress it — it just does not accept it either. The header comment "lone - … copied verbatim" is true only for the first normalisation pass; the second loop still rejects it.
non-blocking — single-dash long options are GNU features this parser does not support
GNU accepts -norc, -noprofile, -login, -posix, -restricted, -verbose, -rcfile FILE, -init-file FILE, -debug, -noediting (verified on both bashes). This parser treats any such word as a letter cluster of known options, e.g.
niu -norc -c true => rc=2 "-c: invalid shell option name" (GNU: runs)
niu -rcfile x -c 'echo hi' => rc=127 "x: command not found" (GNU: runs "hi")
No regression: origin/master also rejected/misparsed these (the -o-containing ones via flags.contains('o'), -rcfile as the same cluster). Worth a follow-up, and the -rcfile case is a wrong command rather than a clean error.
non-blocking — the main.rs TODO leaves the product inconsistent
niubash's niu now accepts -eo pipefail, but the engine binary's own run_args (the documented TODO) still does not:
rubash.exe -eo pipefail -c true => "…-eo: No such file or directory" (treated as a script)
rubash.exe -e -o pipefail -c true => OK
rubash.exe -euo pipefail -c true => "…-euo: No such file or directory"
The report is the bash/niu route and the PR says so, so leaving it is defensible; but it is a genuine niu-vs-rubash.exe inconsistency that a follow-up should close.
verified-ok — main.rs now uses the shared table without losing options
SHORT_OPTIONS = {c,i,l,r,s,D,o,O} ∪ the set -o letters = exactly the union of the two old local sets, so nothing is dropped, and -l/-r are added to the post--c invalid-option check. That matches GNU better than before:
bash -c -l => "-c: option requires an argument" rubash(old set) => "-l: invalid option"
bash -c -l => "-c: option requires an argument" rubash(this PR) => "-c: option requires an argument"
(-D/-O after -c still diverge from GNU, but those letters were already in the old set — pre-existing.)
verified-ok — cross-repo API and merge order
niubash #388 (gh pr diff 388 -R unixwin/niubash) iterates rubash::usage::SHORT_OPTIONS and reads option.letter, option.value_name, option.long_alias, option.description — exactly the struct this PR adds. I built niu from #388's head against this worktree and ran its tests:
cargo test --test issue382_clustered_short_options --test issue385_help_short_options
issue382_clustered_short_options: 3 passed
issue385_help_short_options: 3 passed
#388 also adds tests/issue382_clustered_short_options.rs, whose behaviour comes from this PR's parser fix, and its own body says "merge rubash first or CI will not see the new API". Merge order rubash-first is correct.
non-blocking — -o/-O with a missing value
if let Some(value) = args.get(next_value) silently drops the value when the cluster is the last argument, so the second pass can consume a later flag token as the option name (-eop → -o takes -p), and the diagnostic becomes -p: invalid shell option name instead of GNU's -o: option requires an argument. Still fails, so no silent success.
tests / fmt
cargo fmt --check # clean (exit 0)
cargo test --lib # 687 passed
cargo test --test issue106_107_108_regressions # 10 passed
cargo test --test issue585_invocation_aware_help_regressions # 4 passed
cargo test --test issue626_restricted_invocation # 8 passed
Rebase is behaviour-neutral: single commit on top of 8f596779; the preceding master commits touched only CHANGELOG.md and three test files that this PR does not touch.
Recommendation: merge. The core claim (-eo pipefail) is correct and verified against genuine GNU bashes and end-to-end through niu; the cross-repo API matches #388. The items above are follow-ups, not blockers.
Fixes #382.
bash -eo pipefail -c '...'is a common CI/Makefile invocation. GNUshell.cparse_shell_options(shell.c:931-943) reads-eoas-eplus-owhose value is the next argv word, so it is equivalent to-e -o pipefail.ShellInvocation::parseonly expanded clusters that contained noo/O, then rejected-eowith-eo: invalid option, so the clustered form failed against niu.What changed
src/invocation.rs: normalize the leading option words the way GNU walks them. Each letter becomes a flag token;o/Oconsume the following argv word (never the rest of the cluster — empirical GNU 5.3.0:-eopipefailbinds the following-cas the option name and fails); the first non-option word ends the option run. Long options and lone-/--are copied verbatim. This also stops splitting a trailing cluster that is really a positional parameter (script.sh -eokeeps["-eo", ...]).src/usage.rs: newSHORT_OPTIONStable +is_short_option_letter, the single accepted set shared with the product help generated in docs:--helpomits the working GNU-bash invocation options (-e, -o, -u, -x, --noprofile, --norc) niubash#385.src/main.rs: the engine binary's invalid-option check now uses the shared table.Tests
cargo test --lib— 687 passed (includes the newinvocation::tests::clustered_*andusage::tests::*).cargo test --test issue106_107_108_regressions --test issue585_invocation_aware_help_regressions --test issue626_restricted_invocation— 10 + 4 + 8 passed.The engine binary's
run_argsis a separate, older short-option expander (documented TODO) and was left untouched; the reported message and the niu route come fromShellInvocation::parse.