UNPKG

@qbyco/tjs-cli

Version:

TrafaletJS CLI Tool

115 lines (97 loc) 2 kB
/** * @module lib/cli/Parser */ class Parser { /** * @class Parser * @constructor * @param {lib/cli/Command/Command} command */ constructor (command) { this.command = command; this.input = []; } /** * @method setInput * @param input * @returns {Parser} */ setInput (input) { this.input = input; return this; } /** * @method getInput * @returns {Array|*} */ getInput () { return this.input; } parse (input) { this.setInput(input); this.parseCommand(); this.parseFlags(); this.parseParams(); } /** * @method parseCommand * @returns {bool | Parser} */ parseCommand () { let command = this.getInput()[0]; if ("-" === command.slice(0,1)) { return false; } this.command.setName(command); if (/\:/.test(command)) { this.command.setName(command.split(":").shift()); this.command.setAction(command.split(":").pop()); } return this; } /** * @method parseFlags * @returns {Parser} */ parseFlags () { if (0 < this.getInput().length) { this.getInput().forEach((param) => { let key, value; if ("--" !== param.slice(0,2)) { return false; } param = param.slice(2); key = param; value = true; if (/=/.test(param)) { key = param.split("=").shift(); value= param.split("=").pop(); } if (value === "false") { value = false; } this.command.addFlag(key, value); }); } return this; } /** * @method parseParams * @returns {Parser} */ parseParams () { if (0 < this.getInput().length) { this.getInput().forEach((param) => { if ("-" === param.slice(0,1)) { return false; } if (/\:/.test(param)) { return false; } this.command.addParam(param); }); } return this; } } module.exports = Parser;