Skip to content

Core\Script::arguments

Core\Script::arguments(): mixed

Core\Script::arguments() returns the value that this script was started with. One script starts another with spawn script, and it can give the new script a value with arguments:. The new script calls Core\Script::arguments() to read that value. It receives its own copy, so a change in one script does not change the other.

The value can be anything that arguments: accepts: a string, a number, an array or a nested array. Its type is mixed, so you convert it before you use it. When no value was given, the result is null. This is also the result in the first script, because no other script started it.

Every string that you read from this value is tainted. Tainted means the text came from outside the program, so you must check it before you use it in SQL or as a file path. This is also true for a job that Core\Queue::push started.

The examples below show a script that greets the name it was given, a script that uses a default when it was given nothing, and a list of orders split between three scripts that run at the same time.

Return value

Returns mixed. The value the parent passed, with the same shape. It is null when the parent gave no arguments:, and in the first script, which no other script started.

Changelog

VersionChange
0.0.6Core\Script::arguments() returns the value a script was started with. The option of a spawned script's with() is now arguments: too. The old name was args in both places.

Examples

Read the value the parent passed

<?nvs
// Shows how a script reads the value that it was started with. This script
// passes "Ada" with `arguments:`, and the child reads it with `Core\Script::arguments()`.
// The path starts at the folder of this file.
// `output: "capture"` keeps what the child prints, so this script can print it.
var $child = spawn script 'scripts/greet.nvs' with(arguments: 'Ada', output: 'capture');
var $result = await $child;
// This prints "Hello, Ada!".
echo $result->output;
Output
Hello, Ada!

A script started with nothing

<?nvs
// Shows what `Core\Script::arguments()` returns when a script was given no value.
// The result is `null`, so the script uses a default.
// You start this script from the command line, and no other script started it.
// So the result is `null`, and this prints "given: nothing".
var $given = Core\Script::arguments();
echo 'given: ', $given == null ? 'nothing' : 'a value', "\n";
// This child is started with no `arguments:`. It reads `null` too, and it prints
// "Hello, everyone!".
var $child = spawn script 'scripts/greet.nvs' with(output: 'capture');
echo (await $child)->output;
Output
given: nothing
Hello, everyone!

Split orders between three scripts

<?nvs
// Splits a list of order totals between three scripts that run at the same time.
// Each script adds up its own part, and this script adds the three sums.
// Each child receives its own copy of one part of the list.
// The path starts at the folder of this file.
var $first = spawn script 'scripts/sum-orders.nvs' with(arguments: [120, 80, 45]);
var $second = spawn script 'scripts/sum-orders.nvs' with(arguments: [300, 15]);
var $third = spawn script 'scripts/sum-orders.nvs' with(arguments: [99, 1, 60, 40]);
// `value` is what the child returned. The three sums are 245, 315 and 200.
int $total = 0;
$total = $total + ((await $first)->value as int);
$total = $total + ((await $second)->value as int);
$total = $total + ((await $third)->value as int);
// This prints "total: 760".
echo 'total: ', $total, "\n";
Output
total: 760