2018-12-04 02:20:58 +01:00
|
|
|
.. _netscript_script_arguments:
|
|
|
|
|
|
|
|
Netscript Script Arguments
|
|
|
|
==========================
|
|
|
|
|
|
|
|
Arguments passed into a script can be accessed in Netscript using a
|
2022-11-21 18:19:16 +01:00
|
|
|
special array called ``args``. The arguments can be accessed using a
|
|
|
|
normal array using the ``[]`` operator (``args[0]``, ``args[1]``, etc...).
|
|
|
|
These arguments can be string, number, or boolean.
|
2018-12-04 02:20:58 +01:00
|
|
|
|
|
|
|
For example, let's say we want to make a generic script
|
2022-11-21 18:19:16 +01:00
|
|
|
``generic-run.script`` and we plan to pass two arguments into that script.
|
2018-12-04 02:20:58 +01:00
|
|
|
The first argument will be the name of another script, and the second
|
|
|
|
argument will be a number. This generic script will run the
|
|
|
|
script specified in the first argument with the amount of threads
|
2022-11-21 18:19:16 +01:00
|
|
|
specified in the second argument. The code would look like::
|
2018-12-04 02:20:58 +01:00
|
|
|
|
2023-01-06 13:43:09 +01:00
|
|
|
.. code:: javascript
|
|
|
|
|
2018-12-04 02:20:58 +01:00
|
|
|
run(args[0], args[1]);
|
|
|
|
|
2022-11-21 18:19:16 +01:00
|
|
|
And it could be ran from the terminal like:
|
2018-12-04 02:20:58 +01:00
|
|
|
|
2022-11-21 18:19:16 +01:00
|
|
|
``run generic-run.script myscript.script 7``
|
2018-12-04 02:20:58 +01:00
|
|
|
|
2022-11-21 18:19:16 +01:00
|
|
|
In .js / ns2, the above script would look like::
|
2022-06-19 18:59:15 +02:00
|
|
|
|
2023-01-06 13:43:09 +01:00
|
|
|
.. code:: javascript
|
|
|
|
|
|
|
|
export async function main(ns) {
|
|
|
|
ns.run(ns.args[0], ns.args[1]);
|
|
|
|
}
|
2022-06-19 18:59:15 +02:00
|
|
|
|
2022-11-21 18:19:16 +01:00
|
|
|
It is also possible to get the number of arguments that were passed
|
|
|
|
into a script using ``args.length``.
|