|
2 | 2 |  |
3 | 3 |  |
4 | 4 |
|
5 | | -Argument parsing for lambda-rs applications. |
| 5 | +Lightweight, dependency-free CLI argument parsing used by lambda tools. |
6 | 6 |
|
7 | | -## Getting started |
8 | | -TODO |
| 7 | +It provides a simple builder API, a non-panicking `parse` that returns rich errors, auto-generated usage/help, and pragmatic features like booleans with `--no-flag`, short-flag clusters and counts, positionals, env/config merging, subcommands, and validation helpers. |
| 8 | + |
| 9 | +See the examples in `crates/lambda-rs-args/examples/` for runnable snippets. |
| 10 | + |
| 11 | +## Quick Start |
| 12 | + |
| 13 | +Code: |
| 14 | + |
| 15 | +```rust |
| 16 | +use args::{Argument, ArgumentParser, ArgumentType, ArgumentValue}; |
| 17 | + |
| 18 | +fn main() { |
| 19 | + let parser = ArgumentParser::new("my-tool") |
| 20 | + .with_description("Demo tool") |
| 21 | + .with_author("you") |
| 22 | + .with_argument(Argument::new("--name").is_required(true).with_type(ArgumentType::String)) |
| 23 | + .with_argument(Argument::new("--count").with_type(ArgumentType::Integer).with_default_value(ArgumentValue::Integer(1))); |
| 24 | + |
| 25 | + let argv: Vec<String> = std::env::args().collect(); |
| 26 | + match parser.parse(&argv) { |
| 27 | + Ok(parsed) => { |
| 28 | + println!("name={}, count={}", parsed.get_string("--name").unwrap(), parsed.get_i64("--count").unwrap()); |
| 29 | + } |
| 30 | + Err(e) => eprintln!("{}", e), // includes --help output |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +CLI: |
| 36 | + |
| 37 | +``` |
| 38 | +$ my-tool --help |
| 39 | +Usage: my-tool [options] |
| 40 | +
|
| 41 | +Demo tool |
| 42 | +
|
| 43 | +Author(s): you |
| 44 | +
|
| 45 | +Options: |
| 46 | + --name <string> |
| 47 | + (required) |
| 48 | + --count <int> |
| 49 | + [default: 1] |
| 50 | +$ my-tool --name Alice |
| 51 | +name=Alice, count=1 |
| 52 | +``` |
| 53 | + |
| 54 | +## Features and Examples |
| 55 | + |
| 56 | +Each feature below shows a minimal code snippet and the CLI it enables. |
| 57 | + |
| 58 | +- Non‑panicking parse with help |
| 59 | + - Code: see Quick Start (match on `Err(ArgsError::HelpRequested(usage))`). |
| 60 | + - CLI: `--help`, `-h` prints usage and exits with error containing the usage text. |
| 61 | + |
| 62 | +- Booleans with presence and `--no-flag` |
| 63 | + - Code (examples/bools.rs): |
| 64 | + ```rust |
| 65 | + let parser = ArgumentParser::new("bools") |
| 66 | + .with_argument(Argument::new("--verbose").with_type(ArgumentType::Boolean)) |
| 67 | + .with_argument(Argument::new("--dry-run").with_type(ArgumentType::Boolean)); |
| 68 | + ``` |
| 69 | + - CLI: |
| 70 | + ``` |
| 71 | + $ bools --verbose --no-dry-run |
| 72 | + verbose=true, dry_run=false |
| 73 | + ``` |
| 74 | + |
| 75 | +- `--arg=value` equals syntax |
| 76 | + - Code (examples/equals.rs): |
| 77 | + ```rust |
| 78 | + let parser = ArgumentParser::new("equals") |
| 79 | + .with_argument(Argument::new("--threshold").with_type(ArgumentType::Float)) |
| 80 | + .with_argument(Argument::new("--title").with_type(ArgumentType::String)); |
| 81 | + ``` |
| 82 | + - CLI: |
| 83 | + ``` |
| 84 | + $ equals --threshold=0.75 --title=Demo |
| 85 | + threshold=0.75, title=Demo |
| 86 | + ``` |
| 87 | + |
| 88 | +- Short flags, clusters, and counting verbosity |
| 89 | + - Code (examples/short_count.rs): |
| 90 | + ```rust |
| 91 | + let parser = ArgumentParser::new("short-count") |
| 92 | + .with_argument(Argument::new("-v").with_aliases(&["-v"]).with_type(ArgumentType::Count)); |
| 93 | + ``` |
| 94 | + - CLI: |
| 95 | + ``` |
| 96 | + $ short-count -vvv |
| 97 | + verbosity=3 |
| 98 | + $ short-count -v -v |
| 99 | + verbosity=2 |
| 100 | + ``` |
| 101 | + |
| 102 | +- Aliases (short and long) |
| 103 | + - Code: |
| 104 | + ```rust |
| 105 | + Argument::new("--output").with_type(ArgumentType::String).with_aliases(&["-o"]) |
| 106 | + ``` |
| 107 | + - CLI: |
| 108 | + ``` |
| 109 | + $ tool -o out.bin |
| 110 | + ``` |
| 111 | + |
| 112 | +- Positional arguments and `--` terminator |
| 113 | + - Code (examples/positionals.rs): |
| 114 | + ```rust |
| 115 | + let parser = ArgumentParser::new("pos") |
| 116 | + .with_argument(Argument::new("input").as_positional().with_type(ArgumentType::String)) |
| 117 | + .with_argument(Argument::new("output").as_positional().with_type(ArgumentType::String)); |
| 118 | + ``` |
| 119 | + - CLI: |
| 120 | + ``` |
| 121 | + $ pos -- fileA fileB |
| 122 | + fileA -> fileB |
| 123 | + ``` |
| 124 | + |
| 125 | +- Subcommands |
| 126 | + - Code (examples/subcommands.rs): |
| 127 | + ```rust |
| 128 | + let root = ArgumentParser::new("tool") |
| 129 | + .with_subcommand(ArgumentParser::new("serve").with_argument(Argument::new("--port").with_type(ArgumentType::Integer))) |
| 130 | + .with_subcommand(ArgumentParser::new("build").with_argument(Argument::new("--release").with_type(ArgumentType::Boolean))); |
| 131 | + // parsed.subcommand() -> Option<(name, &ParsedArgs)> |
| 132 | + ``` |
| 133 | + - CLI: |
| 134 | + ``` |
| 135 | + $ tool serve --port 8080 |
| 136 | + serving on :8080 |
| 137 | + $ tool build --release |
| 138 | + building (release=true) |
| 139 | + ``` |
| 140 | + |
| 141 | +- Env var merge (prefix) and simple config file |
| 142 | + - Code (examples/env_config.rs): |
| 143 | + ```rust |
| 144 | + let parser = ArgumentParser::new("envcfg") |
| 145 | + .with_env_prefix("APP") |
| 146 | + .with_config_file("app.cfg") |
| 147 | + .with_argument(Argument::new("--host").with_type(ArgumentType::String)) |
| 148 | + .with_argument(Argument::new("--port").with_type(ArgumentType::Integer)); |
| 149 | + ``` |
| 150 | + - Env and config format: |
| 151 | + - Env: `APP_HOST=127.0.0.1`, `APP_PORT=8080` |
| 152 | + - Config: `app.cfg` lines like `--host=127.0.0.1` or `HOST=127.0.0.1` |
| 153 | + - CLI: |
| 154 | + ``` |
| 155 | + $ APP_PORT=5000 envcfg |
| 156 | + 0.0.0.0:5000 |
| 157 | + ``` |
| 158 | + |
| 159 | +- Validation: requires and mutually exclusive |
| 160 | + - Code (examples/exclusives.rs): |
| 161 | + ```rust |
| 162 | + let parser = ArgumentParser::new("exclusive") |
| 163 | + .with_exclusive_group(&["--json", "--yaml"]).with_requires("--out", "--format") |
| 164 | + .with_argument(Argument::new("--json").with_type(ArgumentType::Boolean)) |
| 165 | + .with_argument(Argument::new("--yaml").with_type(ArgumentType::Boolean)) |
| 166 | + .with_argument(Argument::new("--format").with_type(ArgumentType::String)) |
| 167 | + .with_argument(Argument::new("--out").with_type(ArgumentType::String)); |
| 168 | + ``` |
| 169 | + - CLI: |
| 170 | + ``` |
| 171 | + $ exclusive --json --yaml |
| 172 | + Validation error on --json, --yaml: mutually exclusive (choose one) |
| 173 | + $ exclusive --out out.json |
| 174 | + Validation error on --out: requires --format |
| 175 | + ``` |
| 176 | + |
| 177 | +- Ignore unknown flags |
| 178 | + - Code: |
| 179 | + ```rust |
| 180 | + let parser = ArgumentParser::new("tool").ignore_unknown(true); |
| 181 | + ``` |
| 182 | + - CLI: `tool --unknown --still-works` |
| 183 | + |
| 184 | +## More Useful Features (Explained) |
| 185 | + |
| 186 | +- Booleans and `--no-flag` |
| 187 | + - Presence sets to true (e.g., `--verbose`), and `--no-verbose` sets to false. |
| 188 | + - Works well for toggles and is more ergonomic than `--verbose=false`. |
| 189 | + |
| 190 | +- Short clusters and counting |
| 191 | + - `-vvv` increments a `Count` argument three times; useful for verbosity. |
| 192 | + - Also works with separated flags: `-v -v`. |
| 193 | + |
| 194 | +- Env + Config merging |
| 195 | + - Handy for defaulting from environment or a checked-in config file. |
| 196 | + - CLI always wins over env, and env wins over config. |
| 197 | + - Uses a simple `key=value` format; both canonical names (`--host`) and uppercase keys (`HOST`) are supported. |
| 198 | + |
| 199 | +- Subcommands |
| 200 | + - Let you structure CLIs (e.g., `tool serve`, `tool build`). |
| 201 | + - Each subcommand has its own parser and arguments. Use `parsed.subcommand()` to route. |
| 202 | + |
| 203 | +- Validation helpers |
| 204 | + - `.with_requires(a, b)` enforces that if `a` is provided, `b` must be too. |
| 205 | + - `.with_exclusive_group([a, b, c])` ensures only one of them is present. |
| 206 | + |
| 207 | +## Design Notes |
| 208 | + |
| 209 | +- Non-panicking `parse` returns `Result<ParsedArgs, ArgsError>` for predictable flows and better UX. |
| 210 | +- `compile` remains for backward compatibility and will panic on error with friendly messages. |
| 211 | +- Dependency-free; keeps binary sizes small and build times fast. |
| 212 | + |
| 213 | +## Examples |
| 214 | + |
| 215 | +- See all examples under `crates/lambda-rs-args/examples/`: |
| 216 | + - `basic.rs`, `bools.rs`, `equals.rs`, `short_count.rs`, `positionals.rs`, `subcommands.rs`, `env_config.rs`, `exclusives.rs`. |
0 commit comments