Ask
ask asks the user for information and returns their answer as a string.
Overview
Use ask when your program needs the user to type an answer.
name | ask("What is your name? ") :say("Hello, " + name + "!")
name | ask("What is your name? ") :say("Hello, " + name + "!")
ask can display a question or instruction before waiting for the user to answer. Their answer is returned as a string.
input is an alias for ask.
Prompt
Pass a string to tell the user what to enter:
destination | ask("Where are you traveling? ") :say("Preparing a route to " + destination)
destination | ask("Where are you traveling? ") :say("Preparing a route to " + destination)
The prompt is printed exactly as written. Goblin does not add a space or newline, so include any spacing you want inside the prompt:
answer | ask("Continue? [yes/no] ")
answer | ask("Continue? [yes/no] ")
No Prompt
Call ask without an argument when the program has already explained what to enter:
:say("Enter the order number:") order_number | ask()
:say("Enter the order number:") order_number | ask()
No text is printed before Goblin waits for input.
Return Value
ask always returns a string, even when the user enters something that looks like a number or boolean:
age_text | ask("How old are you? ") age | int(age_text)
age_text | ask("How old are you? ") age | int(age_text)
Goblin removes trailing whitespace from the entered line, including the line ending and any spaces or tabs at the end. Leading whitespace is preserved.
Input Alias
input behaves exactly like ask:
name | ask("Name: ") email | input("Email: ")
name | ask("Name: ") email | input("Email: ")
See Input for the alias page.
Non-Interactive Mode
ask cannot be used when the GOBLIN_NONINTERACTIVE environment variable is set to 1.
name | ask("Name: ") /// error: R0505 non-interactive
name | ask("Name: ") /// error: R0505 non-interactive
This prevents automated jobs and server processes from waiting indefinitely for someone to type a response.
Signature
ask() ask(prompt)
ask() ask(prompt)
| Argument | Type | Description |
|---|---|---|
prompt |
string | Optional text displayed before Goblin waits for input. |
ask returns the entered line as a string.
Errors
The prompt must be a string:
ask(42) /// error: T0205 type-mismatch
ask(42) /// error: T0205 type-mismatch
If Goblin cannot receive the user's answer, ask raises an I/O error:
/// error: R0501 io-error
/// error: R0501 io-error
Interactive input is unavailable in non-interactive mode:
/// GOBLIN_NONINTERACTIVE=1 ask("Name: ") /// error: R0505 non-interactive
/// GOBLIN_NONINTERACTIVE=1 ask("Name: ") /// error: R0505 non-interactive