Parsed structv0.1.0
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.
| Name | Type | Description |
|---|---|---|
writer | &var TextWriter | the destination |
Returns: success when the whole text was accepted
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
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
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.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the option's long name without its dashes |
Returns: the number of times it was given
Has
pub func Has(self: &Parsed, name: char8[..]) -> bool
Whether the option with long name name was given at least once.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the option's long name without its dashes |
Returns: whether it was given
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.
| Name | Type | Description |
|---|---|---|
name | char8[..] | 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
Values
pub func Values(
self: &Parsed,
name: char8[..]
) -> ValueIterator
Every value the option with long name name was given, in order.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the option's long name without its dashes |
Returns: a walk over the values, empty when the option was not given
Positional
pub func Positional(
self: &Parsed,
name: char8[..]
) -> OsStringView?
The positional argument named name, the first one when it takes the remaining arguments.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the positional's name |
Returns: the argument as typed, or none when it was not given
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.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the positional's name |
Returns: a walk over the arguments, empty when none was given
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
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
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.
| Name | Type | Description |
|---|---|---|
name | char8[..] | 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
Related
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.
Parameters
| Name | Type | Description |
|---|---|---|
spec | *CommandSpec | the command the arguments are for |
arguments | OsStringView[..] | 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.
Parameters
| Name | Type | Description |
|---|---|---|
spec | *CommandSpec | the 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.
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
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
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.
Cases
| Name | Description |
|---|---|
Given(OsStringView[..]) | Arguments given as views, without the program's name. |
ProcessArguments | The 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
At
pub func At(
self: &ArgumentSource,
index: uint
) -> OsStringView
The argument at index, counting from zero after the program's name.
| Name | Type | Description |
|---|---|---|
index | uint | the argument's position |
Returns: the argument, or an empty view past the last one