std.args

Typed command-line flags, values, and subcommands.

Importuse std.args

Types

enum String#

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; parse replaces it with Value or Missing.

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 parse wrote back, from the command line or the default.

Missing

Written back when the option was absent and declared no default.

enum Int#

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; parse replaces it with Value or Missing.

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 parse wrote back, from the command line or the default.

Missing

Written back when the option was absent and declared no default.

enum Float#

A float-valued option, parsed with std.strings.parse_float semantics.

Spec

Declares the option under its field name; parse replaces it with Value or Missing.

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 parse wrote back, from the command line or the default.

Missing

Written back when the option was absent and declared no default.

enum Flag#

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 Off.

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.

enum List#

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 parse wrote back, in command-line order.

Functions

extern fn parse<T>(spec: T) -> Result<T, Error>#

Parses std.process.args() against spec, returning the same struct with each spec variant replaced by its result variant.

  • spec: A struct whose fields hold std.args spec 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()))
}
extern fn parse_from<T>(spec: T, argv: [string]) -> Result<T, Error>#

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 hold std.args spec variants.
  • argv: The arguments to read, without the program name — what std.process.args() returns.
fn usage(spec: Mirror) -> string#

Renders help text from a spec's mirror: usage(spec.mirror()).

  • spec: The spec's reflection mirror, as spec.mirror() returns it.