Parsed structv0.1.0

Source
Arguments that matched a command.
pub struct Parsed {
    // private fields
}

Copy, owning nothing: it borrows the specification and the arguments, both of which must outlive it.

Methods

WriteHelp

pub func WriteHelp(
    self: &Parsed,
    writer: &var TextWriter
) -> ! FormatError

Writes the help text of the command this parse is for, naming its parent in the usage line when it is a subcommand. Reports whatever the writer reported.

NameTypeDescription
writer&var TextWriterthe destination

Returns: success when the whole text was accepted

Source

HelpRequested

pub func HelpRequested(self: &Parsed) -> bool

Whether --help or -h was given. A program usually writes the help text and exits successfully when this is true. Cannot fail.

Returns: whether help was asked for

Source

VersionRequested

pub func VersionRequested(self: &Parsed) -> bool

Whether --version or -V was given, which a command only understands when it has a version. Cannot fail.

Returns: whether the version was asked for

Source

Count

pub func Count(self: &Parsed, name: char8[..]) -> uint

How many times the option with long name name was given. Counting is how -vvv means more than -v. An option the command does not declare was given zero times. Runs in time linear in the number of arguments and allocates nothing.

NameTypeDescription
namechar8[..]the option's long name without its dashes

Returns: the number of times it was given

Source

Has

pub func Has(self: &Parsed, name: char8[..]) -> bool

Whether the option with long name name was given at least once.

NameTypeDescription
namechar8[..]the option's long name without its dashes

Returns: whether it was given

Source

Value

pub func Value(
    self: &Parsed,
    name: char8[..]
) -> OsStringView?

The value of the option with long name name, the last one given when it was given more than once. The last value wins so that a later argument can override an earlier one, as a script adding to a fixed command line expects. Runs in time linear in the number of arguments and allocates nothing.

NameTypeDescription
namechar8[..]the option's long name without its dashes

Returns: the value as typed, or none when the option was not given or takes no value

Source

Values

pub func Values(
    self: &Parsed,
    name: char8[..]
) -> ValueIterator

Every value the option with long name name was given, in order.

NameTypeDescription
namechar8[..]the option's long name without its dashes

Returns: a walk over the values, empty when the option was not given

Source

Positional

pub func Positional(
    self: &Parsed,
    name: char8[..]
) -> OsStringView?

The positional argument named name, the first one when it takes the remaining arguments.

NameTypeDescription
namechar8[..]the positional's name

Returns: the argument as typed, or none when it was not given

Source

Positionals

pub func Positionals(
    self: &Parsed,
    name: char8[..]
) -> ValueIterator

Every argument the positional named name was given, which is more than one only for a Remaining positional.

NameTypeDescription
namechar8[..]the positional's name

Returns: a walk over the arguments, empty when none was given

Source

CommandName

pub func CommandName(self: &Parsed) -> char8[..]?

The name of the subcommand that was given, for a command that has subcommands.

Returns: the subcommand's name as its specification spells it, or none

Source

Subcommand

pub func Subcommand(self: &Parsed) -> Parsed?

The parse of the subcommand's own arguments, everything after its name. Those arguments were validated with this parse, so this cannot fail.

Returns: the subcommand's parse, or none when no subcommand was given

Source

Integer

pub func Integer(
    self: &Parsed,
    name: char8[..]
) -> int64? ! CommandLineError

The value of the option or positional named name read as a decimal integer, or none when it was not given. An optional leading - or + is accepted, then one or more decimal digits and nothing else. Reports InvalidValue for any other text and for a number outside the range of int64. Allocates nothing.

NameTypeDescription
namechar8[..]an option's long name, or a positional's name

Returns: the number, none when the value was not given, or why it is not a number

Source

Parse

pub func Parse(
    spec: *CommandSpec,
    arguments: OsStringView[..]
) -> Parsed ! CommandLineError

Checks arguments against spec. arguments are the arguments after the program's name. Reports the first mismatch: an unknown option, an option missing its value or given one it does not take, an unknown or missing subcommand, a missing required positional, or an argument beyond what the command accepts. Asking for --help or --version succeeds whatever else is missing, and HelpRequested or VersionRequested then says so. Allocates nothing; both spec and arguments must outlive the result.

Source

Parameters

NameTypeDescription
spec*CommandSpecthe command the arguments are for
argumentsOsStringView[..]the arguments, without the program's name

Returns: the parse, or why the arguments do not match

ParseProcess

pub func ParseProcess(spec: *CommandSpec) -> Parsed ! CommandLineError

Checks the process's own arguments against spec. The same checks as Parse, over every argument after the program's name. The arguments last as long as the process; spec must outlive the result.

Source

Parameters

NameTypeDescription
spec*CommandSpecthe command the arguments are for

Returns: the parse, or why the arguments do not match

ValueIterator

pub struct ValueIterator {
    // private fields
}

A walk over the values one option or positional was given, in the order they were given. Copy, holding a position in the arguments and nothing else.

Source

Methods

Iterate

pub func Iterate(self: &ValueIterator) -> ValueIterator

The walk itself, so that a for loop reads every value.

Returns: a copy of this walk from its current position

Source

Implements Iterator

ValueIterator conforms to Iterator.

Next

pub func Next(self: &var ValueIterator) -> OsStringView?

The next value, or none after the last.

Returns: the value as typed, borrowing the arguments

Source

ArgumentSource

pub variant ArgumentSource {
    Given(OsStringView[..]),
    ProcessArguments
}

Where the arguments being parsed come from. Copy. A slice is borrowed and must outlive every parse made from it; the process's own arguments last as long as the process.

Source

Cases

NameDescription
Given(OsStringView[..])Arguments given as views, without the program's name.
ProcessArgumentsThe process's own arguments after the program's name.

Methods

Count

pub func Count(self: &ArgumentSource) -> uint

How many arguments there are.

Returns: the argument count, not counting the program's name

Source

At

pub func At(
    self: &ArgumentSource,
    index: uint
) -> OsStringView

The argument at index, counting from zero after the program's name.

NameTypeDescription
indexuintthe argument's position

Returns: the argument, or an empty view past the last one

Source