import { isArray } from "util"; export default function(input: string | string[]) { let matched: boolean = false; // temporary workaround input = isArray(input) ? input.join(" ") : input; const options: any = {}, command = input .match(/^[a-zA-Z0-9\s.\/]*/gim) .join(" ") .trim() .toLowerCase(); input // filter out the options and flags, put them into an object .match(/--(\w+)\s([a-zA-Z0-9:\/.\s\"~]+)|-(\w+)/gim) .forEach(function(part) { const parts = part.split(" ").slice(0), name = parts.shift().replace(/-/g, ""); let value = parts.join(" ").trim(); try { value = JSON.parse(value); } catch (exception) {} options[name] = value || true; }); /** Get all options **/ function getOptions(): any { return options; } /** Get a specific option. **/ function getOption(key: string): any { return options[key]; } /** Get the full command. **/ function getCommand(): string { return command; } function on(command: string, callback: (options?: any) => void) { command = !command ? "" : command; // match against the command if (!matched && command.includes(command.toLowerCase())) { matched = true; callback(options); } return; } return { getOption, getOptions, getCommand, on }; }