Skip to content

Instance Method

confirm(_:)

Requests yes / no confirmation from the user after a prompt.
func confirm(_ prompt: ConsoleText) -> Bool

Parameters

prompt

ConsoleText to display before the confirmation input.

Return Value

true if the user answered yes, false if no.

Discussion

if console.confirm("Delete everything?") {
    console.warning("Deleting everything!")
} else {
    console.print("OK, I won't.")
}

The above code outputs:

Delete everything?
> no
OK, I won't.

This method will attempt to convert the response into a Bool using String.bool. It will continue to ask until the result is a proper format, providing additional help after a few failed attempts.

See confirmOverride for enabling automatic answers to all confirmation prompts.