Skip to content

refactor(argparse): drop the program name from argv with a list pattern - #4206

Merged
bobzhang merged 1 commit into
mainfrom
refactor/argparse-argv-head-drop
Sep 6, 2026
Merged

refactor(argparse): drop the program name from argv with a list pattern#4206
bobzhang merged 1 commit into
mainfrom
refactor/argparse-argv-head-drop

Conversation

@bobzhang

@bobzhang bobzhang commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Summary

default_argv returns the process arguments without the program name. It
did so with a length guard plus an args[1:] slice:

let args = @env.args()
if args.length() > 1 {
  args[1:]
} else {
  []
}

The > 1 bound exists only because args[1:] would panic on an empty
array, so the emptiness handling is split across a comparison and a
branch. A list pattern says the same thing in one shape:

let args = @env.args()
match args {
  [_, .. rest] => rest
  [] => []
}

.. rest binds an ArrayView[String] — the same zero-copy view that
args[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 match blocks that
destructure the head off a sequence and return the tail with an empty
fallback, plus rg over 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 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 values, so a slice or
drop would not be equivalent.

Verification

  • moon check argparse clean
  • moon fmt --check argparse clean
  • moon test argparse: 153 passed, 0 failed

Generated with SeekMoon

`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>
Copilot AI lite review requested due to automatic review settings September 6, 2026 02:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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:] with match args { [_, .. rest] => rest; [] => [] } in default_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
bobzhang enabled auto-merge (rebase) September 6, 2026 02:03
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 6557

Coverage remained the same at 89.19%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 1 uncovered change across 1 file (2 of 3 lines covered, 66.67%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
argparse/parser.mbt 3 2 66.67%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 18169
Covered Lines: 16205
Line Coverage: 89.19%
Coverage Strength: 275644.62 hits per line

💛 - Coveralls

@bobzhang
bobzhang merged commit 63a0e05 into main Sep 6, 2026
21 checks passed
@bobzhang
bobzhang deleted the refactor/argparse-argv-head-drop branch September 6, 2026 02:11
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.

3 participants