mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-16 14:28:36 +02:00
3.0 KiB
3.0 KiB
NS.flags() method
Parse command line flags.
Signature:
flags(schema: [string, string | number | boolean | string[]][]): { [key: string]: ScriptArg | string[] };
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
schema |
[string, string | number | boolean | string[]][] |
Returns:
{ [key: string]: ScriptArg | string[] }
Remarks
RAM cost: 0 GB
Allows Unix-like flag parsing.
We support 2 forms:
-
Short form: the flag contains only 1 character, e.g. -v.
-
Long form: the flag contains more than 1 character, e.g. --version.
Note that if an argument is given and its default value is nullish, the parsed value will be a string. This may cause subtle issues if you are not careful with type coercion.
Example
export async function main(ns) {
const data = ns.flags([
['delay', 0], // a default number means this flag is a number
['server', 'foodnstuff'], // a default string means this flag is a string
['exclude', []], // a default array means this flag is a default array of string
['help', false], // a default boolean means this flag is a boolean
['v', false], // short form
]);
ns.tprint(data);
}
// [home /]> run example.js
// {"_":[],"delay":0,"server":"foodnstuff","exclude":[],"help":false,"v":false}
// [home /]> run example.js --delay 3000
// {"_":[],"delay":3000,"server":"foodnstuff","exclude":[],"help":false,"v":false}
// [home /]> run example.js --delay 3000 --server harakiri-sushi
// {"_":[],"delay":3000,"server":"harakiri-sushi","exclude":[],"help":false,"v":false}
// [home /]> run example.js --delay 3000 --server harakiri-sushi hello world
// {"_":["hello","world"],"delay":3000,"server":"harakiri-sushi","exclude":[],"help":false,"v":false}
// [home /]> run example.js --delay 3000 --server harakiri-sushi hello world --exclude a --exclude b
// {"_":["hello","world"],"delay":3000,"server":"harakiri-sushi","exclude":["a","b"],"help":false,"v":false}
// [home /]> run example.js --help
// {"_":[],"delay":0,"server":"foodnstuff","exclude":[],"help":true,"v":false}
// [home /]> run example.js -v
// {"_":[],"delay":0,"server":"foodnstuff","exclude":[],"help":false,"v":true}
export async function main(ns) {
const data = ns.flags([
["foo", null],
["bar", undefined],
]);
console.log(data);
}
// [home /]> run example.js
// { _: [], foo: null, bar: undefined }
// [home /]> run example.js --foo 1000
// { _: [], foo: "1000", bar: undefined }
// [home /]> run example.js --foo 1000 --bar false
// { _: [], foo: "1000", bar: "false" }
bar in the last example is "false" (a string), not false (a boolean). data.bar is truthy, not falsy.