| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 'use strict'; |
|
|
| let parseargs = {}; |
| let isOpt = function (arg) { return arg.indexOf('-') === 0; }; |
| let removeOptPrefix = function (opt) { return opt.replace(/^--/, '').replace(/^-/, ''); }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| parseargs.Parser = function (opts) { |
| |
| this.opts = {}; |
| this.taskNames = null; |
| this.envVars = null; |
|
|
| |
| this.reg = opts; |
| this.shortOpts = {}; |
| this.longOpts = {}; |
|
|
| let self = this; |
| [].forEach.call(opts, function (item) { |
| self.shortOpts[item.abbr] = item; |
| self.longOpts[item.full] = item; |
| }); |
| }; |
|
|
| parseargs.Parser.prototype = new function () { |
|
|
| let _trueOrNextVal = function (argParts, args) { |
| if (argParts[1]) { |
| return argParts[1]; |
| } |
| else { |
| return (!args[0] || isOpt(args[0])) ? |
| true : args.shift(); |
| } |
| }; |
|
|
| |
| |
| |
| |
| this.parse = function (args) { |
| let cmds = []; |
| let cmd; |
| let envVars = {}; |
| let opts = {}; |
| let arg; |
| let argItem; |
| let argParts; |
| let cmdItems; |
| let taskNames = []; |
| let preempt; |
|
|
| while (args.length) { |
| arg = args.shift(); |
|
|
| if (isOpt(arg)) { |
| arg = removeOptPrefix(arg); |
| argParts = arg.split('='); |
| argItem = this.longOpts[argParts[0]] || this.shortOpts[argParts[0]]; |
| if (argItem) { |
| |
| |
| |
| if (argItem.preempts) { |
| opts[argItem.full] = _trueOrNextVal(argParts, args); |
| preempt = true; |
| break; |
| } |
| |
| |
| |
| if (argItem.expectValue || argItem.allowValue) { |
| opts[argItem.full] = _trueOrNextVal(argParts, args); |
| if (argItem.expectValue && !opts[argItem.full]) { |
| throw new Error(argItem.full + ' option expects a value.'); |
| } |
| } |
| else { |
| opts[argItem.full] = true; |
| } |
| } |
| } |
| else { |
| cmds.unshift(arg); |
| } |
| } |
|
|
| if (!preempt) { |
| |
| while ((cmd = cmds.pop())) { |
| cmdItems = cmd.split('='); |
| if (cmdItems.length > 1) { |
| envVars[cmdItems[0]] = cmdItems[1]; |
| } |
| else { |
| taskNames.push(cmd); |
| } |
| } |
|
|
| } |
|
|
| return { |
| opts: opts, |
| envVars: envVars, |
| taskNames: taskNames |
| }; |
| }; |
|
|
| }; |
|
|
| if (typeof exports != 'undefined') { |
| module.exports = parseargs; |
| } |
| export default parseargs; |
|
|