Passing arguments
Learn how to pass arguments to Tolk scripts from the command line
Acton allows passing arguments from the command line to the Tolk scripts. Argument parsing is driven by the ABI for main(), so the passed values are interpreted according to the parameter types declared in the script.
Basic argument passing
To define a script that accepts arguments, add parameters to the main() function. Acton maps one command-line argument to one main() parameter, in order.
For example, consider a script that adds two numbers:
import "@acton/io"
fun main(a: int, b: int) {
println("The sum is: {}", a + b);
}Run this script and pass the arguments:
acton script scripts/add.tolk 10 20The output will be:
The sum is: 30How parsing works
Acton reads the ABI for main() and parses the forwarded CLI values against those parameter types.
- The number of CLI arguments must exactly match the number of
main()parameters. - Arguments are positional.
- Parsing is type-aware. A value like
10can be interpreted differently depending on whether the parameter type isint,string, orcell.
If a shell argument contains spaces or square brackets, quote it to pass as a single value.
Nullable values
Use null for supported nullable types:
import "@acton/io"
fun main(value: int?) {
println("Value: {}", value);
}Run it with:
# Pass null
acton script scripts/processOptional.tolk null
# Pass an actual value
acton script scripts/processOptional.tolk 42null is the supported null literal for script arguments.
Arrays
Arrays are written inside square brackets. Whitespace-separated items and comma-separated items are both accepted:
import "@acton/io"
fun main(items: array<int>) {
println("Count: {}", items.size());
println("First: {}", items.get(0));
println("Sum: {}", items.get(0) + items.get(1) + items.get(2));
}Run it with:
acton script scripts/processArray.tolk "[10 20 30]"Strings
Simple strings can be passed as ordinary shell arguments:
import "@acton/io"
fun main(name: string) {
println("Hello, {}", name);
}Execute it with:
acton script scripts/helloString.tolk WorldOutput:
Hello, WorldPass a quoted string literal to provide escape sequences or explicit whitespace:
acton script scripts/helloString.tolk '"hello\nworld"'Shell quoting and Acton parsing are separate concerns. Use normal shell quoting for spaces, and preserve the inner double quotes to parse escape sequences such as \n only.
Cells, slices, and bitsN
cell, slice, and bitsN arguments use plain .boc in hex format.
import "@acton/io"
fun main(data: cell) {
val s = data.beginParse();
val value = s.loadUint(32);
println("Value from cell: {}", value);
}First, create a cell with some data and get its hex representation. Then, run the script:
# Example with a cell containing a 32-bit integer 999
acton script scripts/processCell.tolk b5ee9c72...For bitsN, pass the same plain BoC hex format as for a slice.
Addresses
Use a user-friendly TON address string for address. For address?, null is also allowed:
import "@acton/io"
fun main(owner: address, backup: address?) {
println("Owner: {}", owner);
println("Backup: {}", backup);
}acton script scripts/processAddress.tolk EQBvDB_H7FFBs0nF4ap_DBdcOrwY_rMIpNVVOR6SWYFHByMJ nullUnsupported types
The script CLI does not currently accept every ABI type directly. These parameter types are rejected:
tuplemapanddictbuilderany_address- structs, aliases, unions,
unknown, andlisp_list
Last updated on