prompts
prompts.tolk standard library file
Module for user prompts.
This module enables interactive scripts by providing functions to query information from the user via the command line. It is useful for deployment scripts or configuration wizards running in an interactive terminal.
Deterministic fallback matrix:
prompt(...): returnsdefaultin non-interactive modeselect(...): returns thedefaultIndexoption in non-interactive modeconfirm(...): returnsdefaultin non-interactive modepromptInt(...): parsesdefaultin non-interactive modepromptAddress(...): parsesdefaultin non-interactive modepromptWallet(...): returns"emulated-wallet"in emulate mode; in broadcast mode it auto-selects the only configured wallet, but otherwise fails when no wallet is available or wallet choice cannot be collected interactively
Examples:
// Simple text input
val name = prompt("Enter your name:", "Guest");
// Confirmation
if (confirm("Do you want to deploy to mainnet?", false, "This will cost real TON.")) {
// perform deployment
} else {
println("Aborted.");
}
// Selection
val network = select("Choose network:", ["Mainnet", "Testnet", "Local"]);Definitions
prompt
fun prompt(message: string, placeholder: string = "", default: string = ""): stringPrompts the user for input.
placeholderis a grey hint shown inside the empty input field. It is not returned as the result and disappears as soon as the user starts typing.defaultis the value returned when the user presses Enter without typing anything. It is displayed next to the question (e.g.(John)).
Non-interactive behavior:
- Returns
default; ifdefaultis omitted, returns"".
Example:
val name = prompt("What is your name?", "type your name", "John");
println("Hello {}", name);select
fun select(message: string, variants: array<string>, defaultIndex: int = 0): stringPrompts the user to select an option from a list of variants.
Input and fallback behavior:
variantsis expected to be a tuple of strings.- If
variantsis not a tuple, returns"". - Non-string tuple items are silently ignored.
defaultIndexpositions the cursor on the given option when the prompt opens, so the user can accept it by pressing Enter. Out-of-range values are clamped to a valid index (defaults to0).- In non-interactive mode returns the
defaultIndexoption. - If interactive prompt execution fails, returns
"".
Example:
val name = select("What is your name?", ["John", "Jane", "Jim"], 1);
println("Hello {}", name);confirm
fun confirm(message: string, default: bool, helpMessage: string): boolPrompts the user to confirm an action.
Default and fallback behavior:
defaultis forwarded to the interactive host confirm prompt.- In non-interactive mode returns
default. - If interactive prompt execution fails, result is
default.
Example:
val answer = confirm("Are you sure?", true, "Your data will be deleted");
if (answer) {
println("Data deleted");
}promptInt
fun promptInt(message: string, default: string = "", placeholder: string = ""): intPrompts the user for an integer.
Interactive behavior:
- Invalid input is rejected by the host prompt validator until the user corrects it.
- Pressing Enter accepts
defaultwhen it is non-empty.
Non-interactive behavior:
- Parses and returns
default. - If the selected value is empty or invalid, execution fails with the same
integer parser error as
parseInt(...).
promptAddress
fun promptAddress(
message: string,
default: address? = null,
placeholder: string = "EQ... / kQ...",
): addressPrompts the user for a TON address.
Interactive behavior:
- Invalid input is rejected by the host prompt validator until the user corrects it.
- Pressing Enter accepts
defaultwhen it is notnull.
Non-interactive behavior:
- Parses and returns
default. - If the selected value is empty or invalid, execution fails with the same
address parser error as
parseAddress(...).
promptWallet
fun promptWallet(message: string): stringPrompts the user to select a wallet from the configured wallets.
Behavior (broadcast mode, i.e. --net is set):
- If no wallets are configured, fails with an error suggesting how to create one.
- If only one wallet is configured, returns its name automatically.
- If multiple wallets are configured, shows an interactive selection.
- In non-interactive mode (e.g. CI), fails with an error asking to specify the wallet explicitly.
- If the interactive wallet picker is opened but cancelled or otherwise fails,
promptWallet()throws.
In emulate mode (no --net), returns the stable placeholder "emulated-wallet":
scripts.wallet(name) accepts any name in emulate mode, so there is nothing real to pick.
Example:
val walletName = promptWallet("Select a wallet:");Last updated on