Updates to everything
This commit is contained in:
		@@ -1,62 +0,0 @@
 | 
			
		||||
export class ArgParser {
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
 | 
			
		||||
	 * @param opts - {examples: string[], arguments: {key: string, alias: string, type: string, optional: boolean, desc: string}[], desc: string}
 | 
			
		||||
	 */
 | 
			
		||||
	constructor(opts) {
 | 
			
		||||
		this.examples = opts.examples ?? [];
 | 
			
		||||
		this.arguments = opts.args ?? [];
 | 
			
		||||
		this.description = opts.desc;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Parse the list for arguments & create a dictionary.
 | 
			
		||||
	 * @param args {any[]} - Array of arguments
 | 
			
		||||
	 * @returns Dictionary of matched flags + unmatched args under 'extra'
 | 
			
		||||
	 */
 | 
			
		||||
	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);
 | 
			
		||||
		parsed.extra = queue;
 | 
			
		||||
		return parsed;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a help message of the expected paramters & usage.
 | 
			
		||||
	 * @param msg {String} - Optional message to display with help
 | 
			
		||||
	 */
 | 
			
		||||
	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`;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user