Option
A property wrapper that represents a command-line option.
@propertyWrapper struct Option<Value>
Use the @Option
wrapper to define a property of your custom command as a command-line option. An option is a named value passed to a command-line tool, like --configuration debug
. Options can be specified in any order.
An option can have a default value specified as part of its declaration; options with optional Value
types implicitly have nil
as their default value. Options that are neither declared as Optional
nor given a default value are required for users of your command-line tool.
For example, the following program defines three options:
@main
struct Greet: ParsableCommand {
@Option var greeting = "Hello"
@Option var age: Int? = nil
@Option var name: String
mutating func run() {
print("\(greeting) \(name)!")
if let age {
print("Congrats on making it to the ripe old age of \(age)!")
}
}
}
greeting
has a default value of "Hello"
, which can be overridden by providing a different string as an argument, while age
defaults to nil
. name
is a required option because it is non-nil
and has no default value.
$ greet --name Alicia
Hello Alicia!
$ greet --age 28 --name Seungchin --greeting Hi
Hi Seungchin!
Congrats on making it to the ripe old age of 28!
Single-Value Options
init(name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates a required property that reads its value from a labeled option.
init<T>(name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates an optional property that reads its value from a labeled option.
init(wrappedValue: Value, name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates a property with a default value that reads its value from a labeled option.
init(name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> Value
) Creates a required property that reads its value from a labeled option, parsing with the given closure.
init<T>(name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates an optional property that reads its value from a labeled option, parsing with the given closure.
init(wrappedValue: Value, name: NameSpecification, parsing: SingleValueParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> Value
) Creates a property with a default value that reads its value from a labeled option, parsing with the given closure.
struct SingleValueParsingStrategy
The strategy to use when parsing a single value from
@Option
arguments.
Array Options
init<T>(name: NameSpecification, parsing: ArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates a required array property that reads its values from zero or more labeled options.
init<T>(name: NameSpecification, parsing: ArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates a required array property that reads its values from zero or more labeled options, parsing each element with the given closure.
init<T>(wrappedValue: Array<T>, name: NameSpecification, parsing: ArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?
) Creates an array property that reads its values from zero or more labeled options.
init<T>(wrappedValue: Array<T>, name: NameSpecification, parsing: ArrayParsingStrategy, help: ArgumentHelp?, completion: CompletionKind?, transform: @escaping (String) throws -> T
) Creates an array property that reads its values from zero or more labeled options, parsing each element with the given closure.
struct ArrayParsingStrategy
The strategy to use when parsing multiple values from
@Option
arguments into an array.
Infrequently Used APIs
var wrappedValue: Value
The value presented by this property wrapper.