Updated scripts to use ArgParser

This commit is contained in:
2022-03-09 19:06:14 +00:00
parent b44617a082
commit e67bef2f76
10 changed files with 488 additions and 673 deletions

View File

@ -11,15 +11,10 @@ export class ArgParser {
constructor(name, desc, examples, argList) {
this.name = name ?? 'example.js';
this.description = desc ?? 'Example description';
this.examples = [
...examples,
`[OPTIONS] ${argList.filter(arg => !arg.flags).map(arg => arg.name.toUpperCase())}`,
'--help'
];
this.argList = [
...argList,
{name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'}
];
this.examples = examples || [`${argList.find(arg => !!arg.flags) ? '[OPTIONS] ' : ''}${argList.filter(arg => !arg.flags).map(arg => (arg.optional ? `[${arg.name.toUpperCase()}]` : arg.name.toUpperCase()) + (arg.extras ? '...' : '')).join(' ')}`];
this.examples.push('--help');
this.argList = argList || [];
this.argList.push({name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'});
}
/**
@ -41,9 +36,11 @@ export class ArgParser {
queue = parse.substring(1).split('').map(a => `-${a}`).concat(queue);
}
// Find & add flag
const arg = this.argList.find(arg => arg.flags && arg.flags.includes(parse));
if(arg == null) throw new ArgError(`Unknown option: ${parse}`);
const value = arg.type == 'bool' ? true : parse.split('=')[1] || queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0];
const split = parse.split('=');
const arg = this.argList.find(arg => arg.flags && arg.flags.includes(split[0] || parse));
if(arg == null) throw new ArgError(`Option unknown: ${parse}`);
if(arg.name == 'help') throw new ArgError('Help');
const value = arg.type == 'bool' ? true : split[1] || queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0];
if(value == null) throw new ArgError(`Option missing value: ${arg.name}`);
parsed[arg.name] = value;
} else {
@ -52,13 +49,14 @@ export class ArgParser {
}
}
// Arguments
this.argList.filter(arg => !arg.flags).forEach(arg => {
if(!extra.length) throw new ArgError(`Argument missing: ${arg.name}`);
parsed[arg.name] = extra.splice(0, 1)[0];
this.argList.filter(arg => !arg.flags && !arg.extras).forEach(arg => {
if(!arg.optional && !extra.length) throw new ArgError(`Argument missing: ${arg.name.toUpperCase()}`);
const value = extra.splice(0, 1)[0];
if(value != null) parsed[arg.name] = value;
});
// Extras
if(extra.length) parsed['extra'] = extra;
if(parsed['help']) throw new ArgError();
const extraKey = this.argList.find(arg => arg.extras)?.name || 'extra';
parsed[extraKey] = extra;
return parsed;
}
@ -69,7 +67,7 @@ export class ArgParser {
*/
help(msg) {
// Description
let message = '\n\n' + (msg ? msg : this.description);
let message = '\n\n' + (msg && msg.toLowerCase() != 'help' ? msg : this.description);
// Usage
if(this.examples.length) message += '\n\nUsage:\t' + this.examples.map(ex => `run ${this.name} ${ex}`).join('\n\t');
// Arguments

23
scripts/lib/utils.js Normal file
View File

@ -0,0 +1,23 @@
/**
* Print a download bar to the terminal.
* @params ns {NS} - Bitburner API
* @params file - Filename to display with progress bar
*/
export async function downloadPrint(ns, file) {
const speed = ~~(Math.random() * 100) / 10;
const spacing = Array(5 - Math.floor((file.length) / 8)).fill('\t').join('');
await slowPrint(ns, `${file}${spacing}[==================>] 100% \t (${speed} MB/s)`);
}
/**
* Print text to the terminal & then delay for a random amount of time to emulate execution time.
* @params ns {NS} - Bitburner API
* @params message {string} - Text to display
* @params min {number} - minimum amount of time to wait after printing text
* @params max {number} - maximum amount of time to wait after printing text
*/
export async function slowPrint(ns, message, min = 0.5, max = 1.5) {
const time = ~~(Math.random() * (max * 1000 - min * 1000)) + min * 1000;
ns.tprint(message);
await ns.sleep(time);
}