Types
A string-valued option. Spec variants are what the caller writes; Value and Missing are what parse writes back.
Spec | Declares the option under its field name; |
|---|---|
WithAlias(string) | Declares the option with a short alias alongside its field name. |
Default(string) | Declares the option with the text an absent option falls back to. |
WithAliasDefault(string, string) | Declares the option with both a short alias and a default. |
Value(string) | The text |
Missing | Written back when the option was absent and declared no default. |
An integer-valued option. Values parse with std.strings.parse_int semantics; text that will not parse is a Err naming the flag.
Spec | Declares the option under its field name; |
|---|---|
WithAlias(string) | Declares the option with a short alias alongside its field name. |
Default(int) | Declares the option with the number an absent option falls back to. |
WithAliasDefault(string, int) | Declares the option with both a short alias and a default. |
Value(int) | The number |
Missing | Written back when the option was absent and declared no default. |
A float-valued option, parsed with std.strings.parse_float semantics.
Spec | Declares the option under its field name; |
|---|---|
WithAlias(string) | Declares the option with a short alias alongside its field name. |
Default(float) | Declares the option with the number an absent option falls back to. |
WithAliasDefault(string, float) | Declares the option with both a short alias and a default. |
Value(float) | The number |
Missing | Written back when the option was absent and declared no default. |
A boolean switch. Every Flag is negatable: --verbose sets On, --no-verbose sets Off, and the last occurrence wins. Absent falls to the declared default, or Off.
Spec | Declares the switch under its field name; absent, it reads as |
|---|---|
WithAlias(string) | Declares the switch with a short alias alongside its field name. |
Default(bool) | Declares the switch with the state an absent flag takes. |
WithAliasDefault(string, bool) | Declares the switch with both a short alias and a default state. |
On | Written back when the last occurrence set the switch on. |
Off | Written back when the switch was negated, absent, or defaulted off. |
A repeatable option (--tag a --tag b) or, as Rest, the destination for a scope's loose positionals and everything after a literal --. The result is Values, empty when the option never appeared.
Spec | Declares a repeatable option under its field name. |
|---|---|
WithAlias(string) | Declares a repeatable option with a short alias alongside its field name. |
Rest | Collects the scope's loose positionals and everything after a literal |
Values([string]) | The accumulated values |
Functions
Parses std.process.args() against spec, returning the same struct with each spec variant replaced by its result variant.
spec: A struct whose fields holdstd.argsspec variants; the same struct type comes back with each field replaced by its result.
use std.args
use std.reflect.Mirror
struct Options {
name: args.String,
jobs: args.Int,
verbose: args.Flag,
tags: args.List,
}
fn main() {
let spec = Options {
name: args.String.WithAliasDefault("-n", "moon"),
jobs: args.Int.Default(1),
verbose: args.Flag.WithAlias("-v"),
tags: args.List.WithAlias("-t"),
}
let parsed = args.parse_from(spec, ["-n", "tour", "-v", "-t", "docs"]).unwrap()
println(parsed.name.or("moon"))
println(parsed.jobs.or(1))
println(parsed.verbose.on())
println(parsed.tags.values())
println(args.usage(spec.mirror()))
}parse against an explicit argv. The primitive of the two, and the testable one: parse(spec) is parse_from(spec, process.args()).
spec: A struct whose fields holdstd.argsspec variants.argv: The arguments to read, without the program name — whatstd.process.args()returns.
Renders help text from a spec's mirror: usage(spec.mirror()).
spec: The spec's reflection mirror, asspec.mirror()returns it.