AskOption
enum AskOption<T> where T : ExpressibleByStringArgument
Options for CLI.ask(_:options:) functions which can customize value handling and custom prompt validations.
This options are passed to CLI.ask(_:options:) functions, for example:
// Define a default value and require the entered value confirmation.
CLI.ask("Choose directory [/tmp]: ", options: .default("/tmp"), .confirm())
// Prints:
// $ Choose directory [/tmp]: <<user input>>
// $ Are you sure? <<y>>
// The result is "<<user input>>" or "/tmp" if the user only press Enter key.
-
Type alias for validator function. This function takes the parsed value as parameter and returns a Boolean value to indicate if this value is valid or not.
Declaration
Swift
public typealias Validator = (T) -> Bool -
Defines a default value for
askresult if the user only press Enter key.Declaration
Swift
case `default`(_: T)Parameters
valueThe default value.
-
Defines that the
askresult will require confirmation. Parameters are used to define confirmation prompt.// Different use of parameters and its confirmation messages .confirm() // "Are you sure? " .confirm(message: "Realy?\n") // "Realy?\n" .confirm(block: { "Use \($0)? " }) // "Use <<user input>>? "Declaration
Swift
case confirm(message: @autoclosure () -> String = "Are you sure? ", block: ((String) -> String)? = nil)Parameters
messageThe closure to define static confirmation string.
blockThe closure to define dynamic confirmation string with entered value as parameter.
-
Defines a validator for validate the entered value.
.validator("Entered value must be greater than 0.", { $0 > 0 }) .validator("Value cannot be empty!", { !$0.isEmpty() })Declaration
Swift
case validator(_: String, _: Validator)Parameters
messageThe error message to show if the entered value is not valid.
validatorThe custom
AskOption.Validatorwhich returnstruefor valid value orfalsefor invalid.
-
Predefined validator for validate if entered string is not empty.
Declaration
Swift
@inlinable static func notEmptyValidator(_ message: String = "The entered value cannot be empty!\n: ") -> CLI.AskOption<T>Parameters
messageThe error message to display to the user if entered value is empty string.
Return Value
The created validator.
-
Predefined validator for validate if entered value is satisfy specified range.
Declaration
Swift
@inlinable static func rangeValidator<R>(_ range: R, _ message: String? = nil) -> CLI.AskOption<T> where T == R.Bound, R : RangeExpressionParameters
rangeThe range for specify value bounds.
messageThe error message to display to the user if value is not valid.
Return Value
The created validator.
View on GitHub
AskOption Enumeration Reference