ArgParer2

This commit is contained in:
2022-03-24 00:34:09 -04:00
parent d3e9943505
commit 98fe91ad36
3 changed files with 115 additions and 92 deletions

View File

@ -47,21 +47,32 @@ export class ArgParser {
// Find & add flag
const combined = arg.split('=');
const argDef = this.flags.find(flag => flag.flags.includes(combined[0] || arg));
if(!argDef) extras.push(arg); // Not found, add to extras
const value = argDef.default === false ? true : argDef.default === true ? false : queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0];
if(value == null) parsed['_error'] = `${argDef.name.toUpperCase()} missing value`
if(argDef == null) { // Not found, add to extras
extras.push(arg);
continue;
}
const value = argDef.default === false ? true : argDef.default === true ? false : argDef.default || queue.splice(queue.findIndex(q => q[0] != '-'), 1)[0];
if(value == null) parsed['_error'] = `Option missing value: ${arg.name}`;
parsed[argDef.name] = value;
} else { // Command
const c = this.commands.find(command => command.name == arg);
if(!!c) {
parsed['_command'] = c.name;
parsed[c.name] = c.parse(queue.splice(0, queue.length));
const parsedCommand = c.parse(queue.splice(0, queue.length));
Object.keys(parsedCommand).forEach(key => {
if(parsed[key] != parsedCommand[key] && parsedCommand[key] == c.defaults[key])
delete parsedCommand[key];
});
parsed = {
...parsed,
...parsedCommand,
_command: c.name
};
} else extras.push(arg); // Not found, add to extras
}
}
// Arguments
this.args.filter(arg => !arg.extras).forEach(arg => {
if(!arg.optional && !extras.length) parsed['_error'] = `${arg.name.toUpperCase()} is missing`;
if(!arg.optional && !extras.length) parsed['_error'] = `Argument missing: ${arg.name.toUpperCase()}`;
parsed[arg.name] = extras.splice(0, 1)[0];
});
// Extras

View File

@ -12,8 +12,15 @@ export class Logger {
this.fns = lineFns;
this.historyLen -= lineFns.length * 2;
this.history = Array(this.historyLen).fill('');
this.log();
}
/**
* Add red error message to logs
* @param message {string} - Text that will be added
*/
error(message) { this.log(`ERROR: ${message}`); }
/**
* Add a linebreak
*/
@ -33,13 +40,20 @@ export class Logger {
}
/**
* Add message to logs & output
* Add message to the logs
* @param message {string} - Text that will be added
*/
log(message) {
log(message = '') {
this.ns.clearLog();
this.header();
if(message != null) this.history.push(message);
if(message) this.history.push(message);
this.history.splice(0, this.history.length - this.historyLen);
this.history.forEach(m => this.ns.print(m));
}
/**
* Add orange warning to the logs
* @param message {string} - Text that will be added
*/
warn(message) { this.log(`WARN: ${message}`); }
}