silk.cli.command.

Static descriptions of a command line: the command, its options, its positional arguments, and its subcommands.

When to use#

Describe a type with impl CommandLine for T, returning a Command built with Opt, then give the type to silk.cli.parse.Cli.parse or Cli.read.

Details#

Every operation here is static, so a description is a compile-time value. A description names fields by their labels, written as string literals that the compiler checks against the type, so Command<Git>.make(...) |> Command.option("quiet", ...) stops compilation at "quiet" when Git has no field quiet. The field types choose how each value is read; the description only adds names, aliases, help text, defaults, and whether a field is positional, a subcommand, or required. A field that the description does not mention is still an option, named after its label.

A union describes subcommands: each variant is one, named after its label in kebab case, and its fields are that subcommand's options and arguments. A struct or variant field marked with Command.subcommand holds such a union.

Import as Command with import silk.cli.command { Command }.

Public declarations: 6.

OptionSpec#

silk
pub struct OptionSpec

The description of one struct field as an option or a positional argument.

Field field#

silk
pub field: string<'static>

The label of the struct field that receives the value.

Field long#

silk
pub long: string<'static>

The long name without dashes. Empty means the kebab-case form of field.

Field short#

silk
pub short: string<'static>

The one-letter alias without the dash. Empty means no alias.

Field description#

silk
pub description: string<'static>

The help text.

Field required#

silk
pub required: bool

Whether the command line must give this value.

Field positional#

silk
pub positional: bool

Whether the value comes from a positional argument instead of a named option.

Field subcommand#

silk
pub subcommand: bool

Whether the field is a subcommand: a union with CommandLine, whose variants are the subcommands.

Field flatten#

silk
pub flatten: bool

Whether the field is a group of options: a struct with CommandLine whose options the command line gives as this command's own.

Field fallback#

silk
pub fallback: string<'static>

The text read when neither the command line nor the environment gives a value. Empty means none: the field keeps the value that ArgValue.initial returns.

Field choices#

silk
pub choices: Intrinsic.StaticSequence<string<'static>>

The values the option accepts. Empty means any value its field type reads.

Field env#

silk
pub env: string<'static>

The environment variable that supplies the value when the command line does not. Empty means none.

Field check#

silk
pub check: Option<Intrinsic.FunctionRef>

The check a value must pass, a fn(&V) -> bool for the field type V, if any.

VariantSpec#

silk
pub struct VariantSpec

The description of one variant of a subcommand union.

Field label#

silk
pub label: string<'static>

The label of the variant.

Field description#

silk
pub description: string<'static>

The help text.

CommandLine#

silk
pub interface CommandLine

A type that the command line can build: a struct of options and positional arguments, or a union whose variants are subcommands.

Details#

command describes the type. The default describes nothing, so every field is an option named after its label and every variant is a subcommand named after its label, in kebab case. Write impl CommandLine for T {} to take the default, or give command to add names, help text, and positional arguments.

Operation command#

silk
static fn command() -> Command<Self>

Returns the description of the type.

Command#

silk
pub struct Command<T>

The description of a command whose parsed values fill T: a struct, or a union whose variants are subcommands.

Field name#

silk
pub name: string<'static>

The program name shown in the usage line.

Field description#

silk
pub description: string<'static>

The help text shown under the usage line.

Field version#

silk
pub version: string<'static>

The text that --version prints. Empty means the command has no --version.

Field options#

silk
pub options: Intrinsic.StaticSequence<OptionSpec>

The options and positional arguments, in the order they were added. For a union, these describe the fields of its variants.

Field variants#

silk
pub variants: Intrinsic.StaticSequence<VariantSpec>

The help text of the variants of a union.

Implementation Command<T>#

silk
impl<T> Command<T>

Associated function make#

silk
pub static fn make(name: string<'static>, description: string<'static>) -> Command<T>

Returns a command named name with the help text description and no options.

Associated function withVersion#

silk
pub static fn withVersion(command: Command<T>, version: string<'static>) -> Command<T>

Returns command with a --version option that prints version.

Associated function option#

silk
pub static fn option(
    command: Command<T>,
    field: Intrinsic.FieldName<T>,
    option: OptionSpec,
  ) -> Command<T>

Returns command with the field field read as a named option, described by option.

Details#

field is checked against the fields of T where it is written, so a misspelled name is a compile error at the name. For a union, field names a field of any of its variants.

Associated function arg#

silk
pub static fn arg(
    command: Command<T>,
    field: Intrinsic.FieldName<T>,
    argument: OptionSpec,
  ) -> Command<T>

Returns command with the field field read as a positional argument, described by argument. Positional arguments are read in the order they are added. A List field takes every remaining positional argument.

Associated function subcommand#

silk
pub static fn subcommand(command: Command<T>, field: Intrinsic.FieldName<T>) -> Command<T>

Returns command with the field field read as a subcommand. The field's type is a union with CommandLine, and the command line names one of its variants in kebab case, such as remote for the variant Remote.

Associated function flatten#

silk
pub static fn flatten(command: Command<T>, field: Intrinsic.FieldName<T>) -> Command<T>

Returns command with the field field read as a group of options. The field's type is a struct with CommandLine, and its options are written as options of this command, so one group, such as the options every build command shares, is described once and used by many commands.

Details#

A group holds named options only: its own positional arguments or subcommands are a compile error. Help lists the group's options among the command's own.

Associated function variant#

silk
pub static fn variant(
    command: Command<T>,
    variant: Intrinsic.VariantName<T>,
    description: string<'static>,
  ) -> Command<T>

Returns command with the help text description for the variant variant, for a union whose variants are subcommands.

Associated function find#

silk
pub static fn find(command: Command<T>, field: string<'static>) -> OptionSpec

Returns the description of field: the one added to command, or the default option named after the field.

Associated function field#

silk
pub static fn field<V>(command: Command<T>, field: Intrinsic.Field<T, V>) -> OptionSpec

Returns the description of field, as Command.find does, with the first line of the field's /// comment as its help text when the description gives none.

Associated function describeVariant#

silk
pub static fn describeVariant(command: Command<T>, label: string<'static>) -> string<'static>

Returns the help text of the variant labeled label, or empty text.

Associated function position#

silk
pub static fn position(
    command: Command<T>,
    fields: Intrinsic.Fields<T>,
    field: string<'static>,
  ) -> usize

Returns the zero-based position of field among the positional arguments of command that belong to fields.

Associated function count#

silk
pub static fn count(
    command: Command<T>,
    fields: Intrinsic.Fields<T>,
    subcommands: bool,
  ) -> usize

Returns the number of entries of command among fields that are positional arguments, or with subcommands, that are subcommands.

Associated function validate#

silk
pub static fn validate(command: Command<T>, fields: Intrinsic.Fields<T>) -> bool

Checks the entries of command among fields and stops compilation with a message when two clash. Returns true when they do not. Field names are already checked where they are written.

Implementation OptionSpec#

silk
impl OptionSpec

Associated function longFlag#

silk
pub static fn longFlag(option: OptionSpec) -> string<'static>

Returns -- and the long name of option.

Associated function shortFlag#

silk
pub static fn shortFlag(option: OptionSpec) -> string<'static>

Returns - and the alias of option, or empty text when it has none.

Associated function placeholder#

silk
pub static fn placeholder(option: OptionSpec) -> string<'static>

Returns the name of a positional argument in the usage line, such as <files>.

Opt#

silk
pub struct Opt

The static builders of the description of one option or positional argument. Start with Opt.about or Opt.plain and give the result to Command.option or Command.arg.

Implementation Opt#

silk
impl Opt

Associated function about#

silk
pub static fn about(description: string<'static>) -> OptionSpec

Returns a description with the help text description and nothing else.

Associated function plain#

silk
pub static fn plain() -> OptionSpec

Returns a description with no help text. The option is named -- and the kebab-case form of its field.

Associated function short#

silk
pub static fn short(option: OptionSpec, short: string<'static>) -> OptionSpec

Returns option with the one-letter alias short, written without the dash.

Associated function long#

silk
pub static fn long(option: OptionSpec, long: string<'static>) -> OptionSpec

Returns option with the long name long, written without the dashes.

Associated function describe#

silk
pub static fn describe(option: OptionSpec, description: string<'static>) -> OptionSpec

Returns option with the help text description.

Associated function required#

silk
pub static fn required(option: OptionSpec) -> OptionSpec

Returns option as one that the command line must give.

Associated function choice#

silk
pub static fn choice(option: OptionSpec, value: string<'static>) -> OptionSpec

Returns option that also accepts the value value. Once an option has a choice, the command line can give only one of its choices, and the help text lists them.

Associated function default#

silk
pub static fn default(option: OptionSpec, text: string<'static>) -> OptionSpec

Returns option that reads text when neither the command line nor the environment gives a value, such as Opt.default("1") for a count. The help text shows it.

Associated function env#

silk
pub static fn env(option: OptionSpec, name: string<'static>) -> OptionSpec

Returns option that reads the environment variable name when the command line does not give it. Only Cli.fromHost and Cli.parseWith read the environment.

Associated function check#

silk
pub static fn check<V>(option: OptionSpec, check: fn<'static>(&V) -> bool) -> OptionSpec

Returns option whose value must pass check, an ordinary function of the field's value, such as Opt.check<u32>(positive) for a u32 field. A value that fails it is an invalid value; the parser calls check directly.

kebab#

silk
pub static fn kebab(label: string<'static>) -> string<'static>

Category: Command line.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page