UNPKG

the-shepherd

Version:
231 lines (220 loc) 5.34 kB
// Generated by CoffeeScript 2.5.1 (function() { //!/usr/bin/env coffee var $, argumentMachine, count, debug, parseArguments, parseArgv, parsed; $ = require('bling'); count = function(substr, str, i = 0) { var j, ret; j = -1; ret = 0; while (-1 < (j = str.indexOf(substr, i))) { i += j + 1; ret += 1; } return ret; }; argumentMachine = { reset: function() { this._ = ''; this.obj = { _: [] }; this.param = this.value = ''; return this; }, end: function() { this._.length && this.obj._.push(this._); return this.endParam(); }, endParam: function() { var f; return this.param === '' || (this.obj[this.param] = (this.value === '') || isFinite(f = parseFloat(this.value)) && !isNaN(f) && count(".", this.value) < 2 && f || this.value); }, run: $.StateMachine([ // state 0: init { enter: function() { this.reset(); return 1; } }, { // state 1: read unknown '-': function() { if (this._.length) { this._ += '-'; return 1; } else { return 2; } }, ' ': function() { this._.length && this.obj._.push(this._); this._ = ''; return 1; }, def: function(c) { this._ += c; return 1; }, eof: function() { return this.end(); } }, { // state 2: start reading a -p or --param '-': function() { return 3; // read a full --param }, ' ': function() { return 4; // end this -p }, def: function(c) { this.obj[c] = true; return 2; // -qvf is the same as -q -v -f, and not the same as --qvf }, eof: function() { return this.end(); } }, { // state 3: read a full --param ' ': function() { return 4; // read a value }, def: function(c) { this.param += c; return 3; }, eof: function() { return this.end(); } }, { // state 4: read value after -p if there is one ' ': function() { this.next = 1; return 9; }, '-': function() { return (this.value === '') && (this.next = 2, 9) || (this.value += '-', 4); }, '"': function() { return 5; }, "'": function() { return 6; }, '\\': function() { return 10; // read one escaped char }, def: function(c) { this.value += c; return 4; }, eof: function() { return this.end(); } }, { // state 5: read a double-quoted value '"': function() { return 4; }, '\\': function() { return 7; }, def: function(c) { this.value += c; return 5; }, eof: function() { throw new Error(`Unclosed double-quote from \"${this.value.substring(0, 10)} `); return null; } }, { // state 6: read a single-quoted value "'": function() { this.next = 1; return 9; }, '\\': function() { return 8; }, def: function(c) { this.value += c; return 6; }, eof: function() { throw new Error(`Unclosed single-quote from \'${this.value.substring(0, 10)} `); return null; } }, { // state 7: escape one value from inside a double-quote def: function(c) { this.value += c; return 5; }, eof: function() { this.value += '\\'; return this.end(); } }, { // state 8: escape one value from inside a single quote def: function(c) { this.value += c; return 6; }, eof: function() { this.value += '\\'; return this.end(); } }, { // state 9: end a value and go to state @next enter: function() { this.endParam(); this.param = this.value = ''; return this.next; } }, { // state 10: read one escaped value from a raw value def: function(c) { this.value += c; return 4; }, eof: function() { this.value += '\\'; return this.end(); } } ], debug = require.main === module) }; parseArguments = function(str) { return argumentMachine.reset().run(str, 1).obj; }; parsed = null; parseArgv = function() { var argv, ret; if (parsed != null) { return parsed; } argv = process.argv.slice(2).join(' '); ret = parseArguments(argv); ret.quiet || (ret.quiet = ret.q); ret.verbose || (ret.verbose = ret.v); ret.force || (ret.force = ret.f); return parsed = ret; }; Object.assign(module.exports, {parseArguments, parseArgv}); if (require.main === module) { console.log(parseArgv()); } }).call(this);