refactor(argparse): drop the program name from argv with a list pattern - #4206
Merged
Conversation
`default_argv` returned everything after `@env.args()[0]` via a length guard plus an `args[1:]` slice. The `> 1` bound only exists because `args[1:]` would panic on an empty array, so the emptiness handling is spread across a comparison and a branch. Express the intent directly: `[_, .. rest] => rest` drops the head element, with `[] => []` as the total empty-array fallback. `.. rest` binds an `ArrayView[String]` — the same zero-copy view `args[1:]` produced — so behaviour is unchanged. A structural sweep for sibling sites (moongrep over `match` blocks that destructure the head off a sequence and return the tail with an empty fallback, plus `rg` over length-guarded `[1:]` slices) found no further instances. The remaining `[.. rest]`-shaped code in the tree — char/byte decoders peeling one element per iteration, `FixedArray::rev` and `debug::compact_lines` needing the last element, quickcheck shrink and `ArrayView::join` reconstruction — genuinely uses the head or tail, so a slice or drop is not equivalent there. ## Verification - `moon check argparse` clean - `moon fmt --check argparse` clean - `moon test argparse`: 153 passed, 0 failed Co-Authored-By: SeekMoon <seekmoon@moonbitlang.com>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The change is a small, behavior-preserving refactor consistent with existing list-pattern usage and the PR notes indicate relevant checks/tests passed.
Pull request overview
Refactors argparse’s default_argv helper to drop the program name using a list-pattern match instead of a length guard plus slicing, preserving the existing zero-copy ArrayView[String] behavior.
Changes:
- Replace
args.length() > 1+args[1:]withmatch args { [_, .. rest] => rest; [] => [] }indefault_argv. - Keep allocation profile and return type (
ArrayView[String]) consistent with prior behavior.
File summaries
| File | Description |
|---|---|
| argparse/parser.mbt | Simplifies argv defaulting by using a list pattern to strip the program name while still returning an ArrayView[String]. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
bobzhang
enabled auto-merge (rebase)
September 6, 2026 02:03
Collaborator
Coverage Report for CI Build 6557Coverage remained the same at 89.19%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
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.
Summary
default_argvreturns the process arguments without the program name. Itdid so with a length guard plus an
args[1:]slice:The
> 1bound exists only becauseargs[1:]would panic on an emptyarray, so the emptiness handling is split across a comparison and a
branch. A list pattern says the same thing in one shape:
.. restbinds anArrayView[String]— the same zero-copy view thatargs[1:]produced — so behaviour and allocation profile are unchanged.Sweep for similar sites
Follow-up to #4158 / #4159 (list patterns instead of manual prefix or
head handling). A structural sweep (moongrep over
matchblocks thatdestructure the head off a sequence and return the tail with an empty
fallback, plus
rgover length-guarded[1:]slices and.. rest]patterns) found no further instances of this shape. The remaining
[.. rest]-style code in the tree — char/byte decoders peeling oneelement per iteration,
FixedArray::revanddebug::compact_linesneeding the last element, quickcheck shrink and
ArrayView::joinreconstruction — genuinely uses the head or tail values, so a slice or
drop would not be equivalent.
Verification
moon check argparsecleanmoon fmt --check argparsecleanmoon test argparse: 153 passed, 0 failedGenerated with SeekMoon