Commit 1ba81c2
feat(parse): add Parser builder for custom env var handling (#464)
## Summary
- Adds a `Parser` struct that allows customizing parsing behavior
- Particularly useful for providing a custom environment variable map
instead of using the process environment
This is needed for tools like mise where monorepo tasks may have env
vars defined in child config files that aren't in the current process
environment.
## Example usage
```rust
use std::collections::HashMap;
use usage::parse::Parser;
let env: HashMap<String, String> = [("NAME".into(), "john".into())].into();
let result = Parser::new(&spec)
.with_env(env)
.parse(&args)?;
```
The existing `parse()` function is unchanged and continues to work as
before (it now uses `Parser` internally with default options).
## API Design
The `Parser` struct uses `#[non_exhaustive]` to allow adding new options
in the future without breaking changes.
🤖 Generated with [Claude Code](https://claude.ai/code)
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Introduce `Parser` for customizable parsing**
>
> - Export new `parse::Parser` with `with_env` to supply a custom
`HashMap<String, String>` for env lookups; `parse()` now delegates to
`Parser`
> - Refactor `parse_partial` into internal `parse_partial_with_env` and
update required arg/flag validation to consider the custom env map
before `std::env`
> - Preserve existing behavior by default (process env) while enabling
monorepo/task workflows needing non-process env vars
> - Add tests covering custom env for required args/flags, precedence,
and error cases
>
> **Public API**: `Parser` exported via `lib.rs`
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
6294833. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>1 parent 6125f1f commit 1ba81c2
2 files changed
+281
-106
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
0 commit comments