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
ask
result if the user only press Enter key.Declaration
Swift
case `default`(_: T)
Parameters
value
The default value.
-
Defines that the
ask
result 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
message
The closure to define static confirmation string.
block
The 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
message
The error message to show if the entered value is not valid.
validator
The custom
AskOption.Validator
which returnstrue
for valid value orfalse
for 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
message
The 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 : RangeExpression
Parameters
range
The range for specify value bounds.
message
The error message to display to the user if value is not valid.
Return Value
The created validator.