Repository navigation
fix(builtins): getopts honors local OPTIND/OPTARG - #2591
Merged
Merged
Conversation
getopts read and wrote the global OPTIND/OPTARG, so `local OPTIND` in a function made every option loop parse nothing. Use dynamic scope, start at 1 on empty OPTIND, keep OPTIND on an option group, and restart the group when OPTIND is assigned. Claude-Session: https://claude.ai/code/session_019aFikmptPc91Fj4N2iDXQA
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
bashkit | 3be3d24 | Commit Preview URL Branch Preview URL |
Oct 07 2026, 07:54 PM |
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.
Requested by Михайло · project thread
What changed
getoptsnow reads and writesOPTIND,OPTARGand the name variable through dynamic scope (set_variable/expand_variable), not the global map. Three smaller fixes come with it:OPTINDstarts at 1.OPTINDstays on a clustered group (-ab) until the group is done.OPTINDrestarts the group, as bash does.Why
This is a roadmap step 1 fidelity gap. The idiomatic option parser in a function,
local OPTIND opt; while getopts ...; done; shift $((OPTIND-1)), parsed nothing in bashkit. It then failed withshift: -1: shift count out of range. Debian-oracle casegetopts-loopnow matches bash.Before / After
f(){ local OPTIND o; while getopts v o; do echo $o; done; }; f -vshifterrorvgetopts ab o -ab; echo $OPTIND1OPTIND=1; getopts ab o -baafter a partial groupabf(){ local OPTARG; getopts x: o -x v; }; OPTARG=g; f; echo $OPTARGvgProof: new spec
getopts-local-optind.test.sh(3 cases, bash-verified), andbash_comparisonpasses.Risk
Low. The change is scoped to
getoptsplus oneOPTINDassignment hook that resets the group index.Checklist
cargo test -p bashkit --lib/--test integration/bash_comparison-D warnings, fmthttps://claude.ai/code/session_019aFikmptPc91Fj4N2iDXQA
Generated by Claude Code