CommandContext

public struct CommandContext

Contains required data for running a command such as the Console and CommandInput.

See CommandRunnable for more information.

  • The Console this command was run on.

    Declaration

    Swift

    public var console: Console
  • The parsed arguments (according to declared signature).

    Declaration

    Swift

    public var arguments: [String: String]
  • The parsed options (according to declared signature).

    Declaration

    Swift

    public var options: [String: String]
  • The container this command is running on.

    Declaration

    Swift

    public var container: Container
  • Create a new CommandContext.

    Declaration

    Swift

    public init(
        console: Console,
        arguments: [String: String],
        options: [String: String],
        on container: Container
    )
  • Requires an option, returning the value or throwing.

    let option = try context.requireOption("foo")
    

    Use .options to access in a non-required manner.

    Declaration

    Swift

    public func requireOption(_ name: String) throws -> String

    Parameters

    name

    Name of the CommandOption to fetch.

  • Accesses an argument by name. This will only throw if the argument was not properly declared in your signature.

    let arg = try context.argument("message")
    

    Declaration

    Swift

    public func argument(_ name: String) throws -> String

    Parameters

    name

    Name of the CommandArgument to fetch.