Argument
A property wrapper that represents a positional command-line argument.
@propertyWrapper struct Argument<Value>
Use the @Argument
wrapper to define a property of your custom command as a positional argument. A positional argument for a command-line tool is specified without a label and must appear in declaration order. @Argument
properties with Optional
type or a default value are optional for the user of your command-line tool.
For example, the following program has two positional arguments. The name
argument is required, while greeting
is optional because it has a default value.
@main
struct Greet: ParsableCommand {
@Argument var name: String
@Argument var greeting: String = "Hello"
mutating func run() {
print("\(greeting) \(name)!")
}
}
You can call this program with just a name or with a name and a greeting. When you supply both arguments, the first argument is always treated as the name, due to the order of the property declarations.
$ greet Nadia
Hello Nadia!
$ greet Tamara Hi
Hi Tamara!
Single Arguments
init(help: ArgumentHelp?, completion: CompletionKind?
) Creates a property with no default value.
init<T>(help: ArgumentHelp?, completion: CompletionKind?
) Creates an optional property that reads its value from an argument.
init(help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> Value
) Creates a property with no default value, parsing with the given closure.
init<T>(help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates an optional property that reads its value from an argument.
init(wrappedValue:help:completion:)
init(wrappedValue:help:completion:transform:)
Array Arguments
init<T>(parsing: ArgumentArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates a property with no default value that reads an array from zero or more arguments.
init<T>(parsing: ArgumentArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates a property with no default value that reads an array from zero or more arguments, parsing each element with the given closure.
init<T>(wrappedValue: Array<T>, parsing: ArgumentArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates a property that reads an array from zero or more arguments.
init<T>(wrappedValue: Array<T>, parsing: ArgumentArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates a property that reads an array from zero or more arguments, parsing each element with the given closure.
struct ArgumentArrayParsingStrategy
The strategy to use when parsing multiple values from positional arguments into an array.
Infrequently Used APIs
var wrappedValue: Value
The value presented by this property wrapper.