Allow unknown flags through the ArgParser

This commit is contained in:
Zakary Timson 2022-03-14 01:44:47 +00:00
parent 9058f2807f
commit 2c646e42ec
3 changed files with 10 additions and 4 deletions

View File

@ -14,7 +14,7 @@ export async function main(ns) {
{name: 'depth', desc: 'Depth to scan to, defaults to 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'}, {name: 'depth', desc: 'Depth to scan to, defaults to 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
{name: 'level', desc: 'Exclude targets with higher hack level, defaults to current hack level', flags: ['-l', '--level'], default: ns.getHackingLevel(), type: 'num'}, {name: 'level', desc: 'Exclude targets with higher hack level, defaults to current hack level', flags: ['-l', '--level'], default: ns.getHackingLevel(), type: 'num'},
{name: 'ports', desc: 'Exclute targets with too many closed ports', flags: ['-p', '--ports'], optional: true, default: Infinity, type: 'num'}, {name: 'ports', desc: 'Exclute targets with too many closed ports', flags: ['-p', '--ports'], optional: true, default: Infinity, type: 'num'},
]); ], true);
let args; let args;
try { try {
args = argParser.parse(ns.args); args = argParser.parse(ns.args);

View File

@ -7,14 +7,16 @@ export class ArgParser {
* @param desc {string} - Help text desciption * @param desc {string} - Help text desciption
* @param examples {string[]} - Help text examples * @param examples {string[]} - Help text examples
* @param argList {name: string, desc: string, flags: string[], type: string, default: any}[] - Array of CLI arguments * @param argList {name: string, desc: string, flags: string[], type: string, default: any}[] - Array of CLI arguments
* @param allowUnknown {boolean} - Allow unknown flags
*/ */
constructor(name, desc, examples, argList) { constructor(name, desc, examples, argList, allowUnknown = false) {
this.name = name ?? 'example.js'; this.name = name ?? 'example.js';
this.description = desc ?? 'Example description'; this.description = desc ?? 'Example description';
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 = 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.examples.push('--help');
this.argList = argList || []; this.argList = argList || [];
this.argList.push({name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'}); this.argList.push({name: 'help', desc: 'Display this help message', flags: ['-h', '--help'], type: 'bool'});
this.allowUnknown = allowUnknown;
} }
/** /**
@ -38,7 +40,11 @@ export class ArgParser {
// Find & add flag // Find & add flag
const split = parse.split('='); const split = parse.split('=');
const arg = this.argList.find(arg => arg.flags && arg.flags.includes(split[0] || parse)); 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 == null) {
if(!this.allowUnknown) throw new ArgError(`Option unknown: ${parse}`);
extra.push(parse);
continue;
}
if(arg.name == 'help') throw new ArgError('Help'); 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]; 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}`); if(value == null) throw new ArgError(`Option missing value: ${arg.name}`);

View File

@ -13,7 +13,7 @@ export async function main(ns) {
{name: 'script', desc: 'Script to copy & execute', optional: true, type: 'string'}, {name: 'script', desc: 'Script to copy & execute', optional: true, type: 'string'},
{name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true, type: 'string'}, {name: 'args', desc: 'Arguments for script. Forward the current target with: {{TARGET}}', optional: true, extras: true, type: 'string'},
{name: 'cpu', desc: 'Number of CPU threads to use with script', flags: ['-c', '--cpu'], type: 'num'}, {name: 'cpu', desc: 'Number of CPU threads to use with script', flags: ['-c', '--cpu'], type: 'num'},
]); ], true);
let args; let args;
try { try {
args = argParser.parse(ns.args); args = argParser.parse(ns.args);