Added a unix-style argument parser
This commit is contained in:
		
							
								
								
									
										49
									
								
								scripts/lib/arg-parser.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								scripts/lib/arg-parser.js
									
									
									
									
									
										Normal 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`;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -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();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -108,4 +113,4 @@ export async function main(ns) {
 | 
			
		||||
		// Check again in 1s
 | 
			
		||||
		await ns.sleep(1000);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
@@ -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 = [
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user