Added a unix-style argument parser

This commit is contained in:
Zakary Timson 2022-02-10 17:10:26 +00:00
parent c69c1c954a
commit 8763c14e78
4 changed files with 127 additions and 38 deletions

View File

@ -40,21 +40,37 @@ Will weaken, spoof & hack the target in a loop.
It's recommended you run this in combination with `auto-pwn.js` to gain root & run the miner on the remote machine.
### [node-manager.js](./scripts/node-manager.js)
Manages the specified number of nodes buying any if they don't exist.
It's recommended you run this from your home computer, it useses 5.6 GB of RAM.
**RAM:** 5.70 GB
```
Usage: run node-manager.js [OPTION] LIMIT
[home ~/]> run scripts/node-manager.js --help
Running script with 1 thread(s), pid 128 and args: ["--help"].
/scripts/node-manager.js:
Limit - Limit the number of nodes the script will buy
Buy, upgrade & manage Hacknet nodes automatically.
Usage: run node-manager.js [OPTIONS] LIMIT
run node-manager.js --balance 1E6 4
run node-manager.js --help
LIMIT Limit the number of nodes the manager will buy
Options:
Help - Displays this help message
Balance - Prevent spending bellow this point
-b --balance=num Prevent spending bellow this point
-h --help Display help message
```
### [update.js](./scripts/update.js)
Automaticlly downloads all the scripts in this repo using the in-game `wget`.
**RAM:** 1.60 GB
```
Usage: run update.js
[home ~/]> run scripts/update.js --help
Running script with 1 thread(s), pid 129 and args: ["--help"].
/scripts/update.js:
Automatically update scripts from the repository using wget.
Usage: run update.js
run update.js --help
Options:
-h --help Display help message
```

49
scripts/lib/arg-parser.js Normal file
View File

@ -0,0 +1,49 @@
export class ArgParser {
constructor(opts) {
this.examples = opts.examples ?? [];
this.arguments = opts.args ?? [];
this.description = opts.desc;
}
parse(args) {
const req = this.arguments.filter(a => !a.optional);
const queue = [...args];
const parsed = {};
for(let i = 0; i < queue.length; i++) {
if(queue[i][0] != '-') continue;
let value = null, parse = queue[i].slice(queue[i][1] == '-' ? 2 : 1);
if(parse.indexOf('=')) {
const split = parse.split('=');
parse = split[0];
value = split[1];
}
let arg = this.arguments.find(a => a.key == parse) ?? this.arguments.find(a => a.alias == parse);
if(!arg) continue;
if(!value) value = arg.type && arg.type != 'bool' ? queue.splice(i + 1, 1)[0] : true;
parsed[arg.key] = value;
queue.splice(i, 1);
}
req.forEach((a, i) => parsed[a.key] = queue[i]);
queue.splice(0, req.length);
if(queue.length) parsed.extra = queue;
return parsed;
}
help(msg) {
let message = '\n\n';
message += msg ? msg : this.description;
if(this.examples.length) message += '\n\nUsage:\t' + this.examples.join('\n\t');
const required = this.arguments.filter(a => !a.optional);
if(required.length) message += '\n\n\t' + required.map(a => {
const padding = 3 - ~~(a.key.length / 8);
return `${a.key}${Array(padding).fill('\t').join('')} ${a.desc}`;
}).join('\n\t');
const optional = this.arguments.filter(a => a.optional);
if(optional.length) message += '\n\nOptions:\n\t' + optional.map(a => {
const flgs = `${a.alias ? `-${a.alias} ` : ''}--${a.key}${a.type && a.type != 'bool' ? `=${a.type}` : ''}`;
const padding = 3 - ~~(flgs.length / 8);
return `${flgs}${Array(padding).fill('\t').join('')} ${a.desc}`;
}).join('\n\t');
return `${message}\n\n`;
}
}

View File

@ -1,16 +1,11 @@
import {ArgParser} from './scripts/lib/arg-parser';
/**
* Manages hacknet nodes, purchasing nodes to reach the desired amount.
* Upgrades (Level, RAM, Cores & Cache) will be automatically purchased.
*/
export async function main(ns) {
/**
* How to use this script
* message - optional message to add
*/
function help(message) {
ns.tprint(`\n\n${!message ? '' : `${message}\n\n`}Usage: run node-manager.js [OPTION] LIMIT\n\n\tLimit - Limit the number of nodes the script will buy\n\nOptions:\n\tHelp - Displays this help message\n\tBalance - Prevent spending bellow this point\n\n`);
ns.exit();
}
ns.disableLog('ALL');
/**
* Print header with logs
@ -26,24 +21,34 @@ export async function main(ns) {
messageHistory.forEach(m => ns.print(m));
}
// Initilize script arguments
const argParser = new ArgParser({
desc: 'Buy, upgrade & manage Hacknet nodes automatically.',
examples: [
'run node-manager.js [OPTIONS] LIMIT',
'run node-manager.js --balance 1E6 4',
'run node-manager.js --help',
],
args: [
{key: 'LIMIT', desc: 'Limit the number of nodes the manager will buy'},
{key: 'balance', alias: 'b', type: 'num', optional: true, desc: 'Prevent spending bellow this point'},
{key: 'help', alias: 'h', optional: true, desc: 'Display help message'},
]
});
const args = argParser.parse(ns.args);
// Check arguments
if(args['help']) return ns.tprint(argParser.help());
if(!args['LIMIT']) return ns.tprint(argParser.help('Missing LIMIT'));
if(isNaN(args['LIMIT'])) return ns.tprint(argParser.help('LIMIT must be a number'));
if(!!args['balance'] && isNaN(args['balance'])) return ns.tprint(argParser.help('LIMIT must be a number'));
// Setup
ns.disableLog('ALL');
if(ns.args.length == 0) help('Missing number of nodes');
if(ns.args[0] == 'help') help();
const historyLength = 17;
const messageHistory = Array(historyLength).fill('');
let limit, savings, nodeCount = ns.hacknet.numNodes();
if(ns.args.length == 1) {
if(isNaN(ns.args[0])) help('Limit must be a number');
limit = ns.args[0];
savings = 0;
} else if(ns.args.length == 2) {
if(isNaN(ns.args[1])) help('Limit must be a number');
limit = ns.args[1];
if(isNaN(ns.args[0])) help('Balance must be a number');
savings = ns.args[0];
}
const limit = args['LIMIT'];
const savings = args['balance'] ?? 0
const nodeCount = ns.hacknet.numNodes();
log();
while(true) {
@ -67,25 +72,25 @@ export async function main(ns) {
})).map(node => { // Figure out cheapest upgrade
if(node.cacheCost != 0 && node.cacheCost != Infinity && node.cacheCost <= node.coreCost && node.cacheCost <= node.levelCost && node.cacheCost <= node.ramCost) {
node.bestUpgrade = {
name: 'Cache',
name: 'cache',
cost: node.cacheCost,
purchase: () => ns.hacknet.upgradeCache(node.index)
};
} else if(node.coreCost != 0 && node.coreCost != Infinity && node.coreCost <= node.cacheCost && node.coreCost <= node.levelCost && node.coreCost <= node.ramCost) {
node.bestUpgrade = {
name: 'Core',
name: 'cores',
cost: node.coreCost,
purchase: () => ns.hacknet.upgradeCore(node.index)
};
} else if(node.ramCost != 0 && node.ramCost != Infinity && node.ramCost <= node.cacheCost && node.ramCost <= node.levelCost && node.ramCost <= node.coreCost) {
node.bestUpgrade = {
name: 'RAM',
name: 'ram',
cost: node.ramCost,
purchase: () => ns.hacknet.upgradeRam(node.index)
};
} else {
node.bestUpgrade = {
name: 'Level',
name: 'level',
cost: node.levelCost,
purchase: () => ns.hacknet.upgradeLevel(node.index)
};
@ -100,7 +105,7 @@ export async function main(ns) {
// Apply the cheapest upgrade
if(nodes.length && balance - nodes[0].bestUpgrade.cost >= savings) {
const cost = Math.round(nodes[0].bestUpgrade.cost * 100) / 100;
log(`Upgrading Node ${nodes[0].index} ${nodes[0].bestUpgrade.name}: $${cost}`);
log(`Node ${nodes[0].index} - ${nodes[0].bestUpgrade.name} ${nodes[0][nodes[0].bestUpgrade.name] + 1} - $${cost}`);
nodes[0].bestUpgrade.purchase();
}
}

View File

@ -1,7 +1,26 @@
import {ArgParser} from './scripts/lib/arg-parser';
/**
* Automatically download all the scripts in the repository.
*/
export async function main(ns) {
// Initilize script arguments
const argParser = new ArgParser({
desc: 'Automatically update scripts from the repository using wget.',
examples: [
'run update.js',
'run update.js --help',
],
args: [
{key: 'help', alias: 'h', optional: true, desc: 'Display help message'},
]
});
const args = argParser.parse(ns.args);
// Check arguments
if(args['help']) return ns.tprint(argParser.help());
// Setup
const src = 'https://gitlab.zakscode.com/ztimson/BitBurner/-/raw/develop/scripts/';
const dist = '/scripts/';
const fileList = [