nav tabs on admin dashboard
This commit is contained in:
+43
-160
@@ -1,175 +1,58 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
var Arg, Cmd, Color, Opt;
|
||||
'use strict';
|
||||
|
||||
Color = require('./color').Color;
|
||||
|
||||
Cmd = require('./cmd').Cmd;
|
||||
|
||||
Opt = require('./opt').Opt;
|
||||
const
|
||||
CoaParam = require('./coaparam'),
|
||||
chalk = require('chalk');
|
||||
|
||||
/**
|
||||
Argument
|
||||
* Argument
|
||||
*
|
||||
* Unnamed entity. From command line arguments passed as list of unnamed values.
|
||||
*
|
||||
* @class Arg
|
||||
* @extends CoaParam
|
||||
*/
|
||||
module.exports = class Arg extends CoaParam {
|
||||
/**
|
||||
* @constructs
|
||||
* @param {COA.Cmd} cmd - parent command
|
||||
*/
|
||||
constructor(cmd) {
|
||||
super(cmd);
|
||||
|
||||
Unnamed entity. From command line arguments passed as list of unnamed values.
|
||||
@namespace
|
||||
@class Presents argument
|
||||
*/
|
||||
|
||||
|
||||
exports.Arg = Arg = (function() {
|
||||
/**
|
||||
@constructs
|
||||
@param {COA.Cmd} cmd parent command
|
||||
*/
|
||||
|
||||
function Arg(_cmd) {
|
||||
this._cmd = _cmd;
|
||||
this._cmd._args.push(this);
|
||||
}
|
||||
|
||||
/**
|
||||
Set a canonical argument identifier to be used anywhere in text messages.
|
||||
@param {String} _name argument name
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.name = Opt.prototype.name;
|
||||
|
||||
/**
|
||||
Set a long description for argument to be used anywhere in text messages.
|
||||
@param {String} _title argument title
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.title = Cmd.prototype.title;
|
||||
|
||||
/**
|
||||
Makes an argument accepts multiple values.
|
||||
Otherwise, the value will be used by the latter passed.
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.arr = Opt.prototype.arr;
|
||||
|
||||
/**
|
||||
Makes an argument required.
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.req = Opt.prototype.req;
|
||||
|
||||
/**
|
||||
Set a validation (or value) function for argument.
|
||||
Value from command line passes through before becoming available from API.
|
||||
Using for validation and convertion simple types to any values.
|
||||
@param {Function} _val validating function,
|
||||
invoked in the context of argument instance
|
||||
and has one parameter with value from command line
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.val = Opt.prototype.val;
|
||||
|
||||
/**
|
||||
Set a default value for argument.
|
||||
Default value passed through validation function as ordinary value.
|
||||
@param {Object} _def
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.def = Opt.prototype.def;
|
||||
|
||||
/**
|
||||
Set custom additional completion for current argument.
|
||||
@param {Function} completion generation function,
|
||||
invoked in the context of argument instance.
|
||||
Accepts parameters:
|
||||
- {Object} opts completion options
|
||||
It can return promise or any other value treated as result.
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.comp = Cmd.prototype.comp;
|
||||
|
||||
/**
|
||||
Make argument value inputting stream.
|
||||
It's add useful validation and shortcut for STDIN.
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.input = Opt.prototype.input;
|
||||
|
||||
/**
|
||||
Make argument value outputing stream.
|
||||
It's add useful validation and shortcut for STDOUT.
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Arg.prototype.output = Opt.prototype.output;
|
||||
|
||||
Arg.prototype._parse = function(arg, args) {
|
||||
return this._saveVal(args, arg);
|
||||
};
|
||||
|
||||
Arg.prototype._saveVal = Opt.prototype._saveVal;
|
||||
|
||||
Arg.prototype._checkParsed = function(opts, args) {
|
||||
return !args.hasOwnProperty(this._name);
|
||||
};
|
||||
|
||||
Arg.prototype._usage = function() {
|
||||
var res;
|
||||
res = [];
|
||||
res.push(Color('lpurple', this._name.toUpperCase()), ' : ', this._title);
|
||||
if (this._req) {
|
||||
res.push(' ', Color('lred', '(required)'));
|
||||
this._cmd._args.push(this);
|
||||
}
|
||||
return res.join('');
|
||||
};
|
||||
|
||||
Arg.prototype._requiredText = function() {
|
||||
return 'Missing required argument:\n ' + this._usage();
|
||||
};
|
||||
_saveVal(args, val) {
|
||||
this._val && (val = this._val(val));
|
||||
|
||||
/**
|
||||
Return rejected promise with error code.
|
||||
Use in .val() for return with error.
|
||||
@param {Object} reject reason
|
||||
You can customize toString() method and exitCode property
|
||||
of reason object.
|
||||
@returns {Q.promise} rejected promise
|
||||
*/
|
||||
const name = this._name;
|
||||
this._arr
|
||||
? (args[name] || (args[name] = [])).push(val)
|
||||
: (args[name] = val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
Arg.prototype.reject = Cmd.prototype.reject;
|
||||
_parse(arg, args) {
|
||||
return this._saveVal(args, arg);
|
||||
}
|
||||
|
||||
/**
|
||||
Finish chain for current option and return parent command instance.
|
||||
@returns {COA.Cmd} parent command
|
||||
*/
|
||||
_checkParsed(opts, args) {
|
||||
return !args.hasOwnProperty(this._name);
|
||||
}
|
||||
|
||||
_usage() {
|
||||
const res = [];
|
||||
|
||||
Arg.prototype.end = Cmd.prototype.end;
|
||||
res.push(chalk.magentaBright(this._name.toUpperCase()), ' : ', this._title);
|
||||
|
||||
/**
|
||||
Apply function with arguments in context of arg instance.
|
||||
@param {Function} fn
|
||||
@param {Array} args
|
||||
@returns {COA.Arg} this instance (for chainability)
|
||||
*/
|
||||
this._req && res.push(' ', chalk.redBright('(required)'));
|
||||
|
||||
return res.join('');
|
||||
}
|
||||
|
||||
Arg.prototype.apply = Cmd.prototype.apply;
|
||||
|
||||
return Arg;
|
||||
|
||||
})();
|
||||
_requiredText() {
|
||||
return `Missing required argument:\n ${this._usage()}`;
|
||||
}
|
||||
};
|
||||
|
||||
+438
-550
File diff suppressed because it is too large
Load Diff
-25
@@ -1,25 +0,0 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
var colors;
|
||||
|
||||
colors = {
|
||||
black: '30',
|
||||
dgray: '1;30',
|
||||
red: '31',
|
||||
lred: '1;31',
|
||||
green: '32',
|
||||
lgreen: '1;32',
|
||||
brown: '33',
|
||||
yellow: '1;33',
|
||||
blue: '34',
|
||||
lblue: '1;34',
|
||||
purple: '35',
|
||||
lpurple: '1;35',
|
||||
cyan: '36',
|
||||
lcyan: '1;36',
|
||||
lgray: '37',
|
||||
white: '1;37'
|
||||
};
|
||||
|
||||
exports.Color = function(c, str) {
|
||||
return ['\x1B[', colors[c], 'm', str, '\x1B[m'].join('');
|
||||
};
|
||||
+162
-120
@@ -1,134 +1,176 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
'use strict';
|
||||
|
||||
const constants = require('constants');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const Q = require('q');
|
||||
|
||||
const shell = require('./shell');
|
||||
const escape = shell.escape;
|
||||
const unescape = shell.unescape;
|
||||
|
||||
/**
|
||||
Most of the code adopted from the npm package shell completion code.
|
||||
See https://github.com/isaacs/npm/blob/master/lib/completion.js
|
||||
*/
|
||||
* Most of the code adopted from the npm package shell completion code.
|
||||
* See https://github.com/isaacs/npm/blob/master/lib/completion.js
|
||||
*
|
||||
* @returns {COA.CoaObject}
|
||||
*/
|
||||
module.exports = function completion() {
|
||||
return this
|
||||
.title('Shell completion')
|
||||
.helpful()
|
||||
.arg()
|
||||
.name('raw')
|
||||
.title('Completion words')
|
||||
.arr()
|
||||
.end()
|
||||
.act((opts, args) => {
|
||||
if(process.platform === 'win32') {
|
||||
const e = new Error('shell completion not supported on windows');
|
||||
e.code = 'ENOTSUP';
|
||||
e.errno = constants.ENOTSUP;
|
||||
return this.reject(e);
|
||||
}
|
||||
|
||||
var Q, complete, dumpScript, escape, getOpts, unescape;
|
||||
// if the COMP_* isn't in the env, then just dump the script
|
||||
if((process.env.COMP_CWORD == null)
|
||||
|| (process.env.COMP_LINE == null)
|
||||
|| (process.env.COMP_POINT == null)) {
|
||||
return dumpScript(this._cmd._name);
|
||||
}
|
||||
|
||||
Q = require('q');
|
||||
console.error('COMP_LINE: %s', process.env.COMP_LINE);
|
||||
console.error('COMP_CWORD: %s', process.env.COMP_CWORD);
|
||||
console.error('COMP_POINT: %s', process.env.COMP_POINT);
|
||||
console.error('args: %j', args.raw);
|
||||
|
||||
escape = require('./shell').escape;
|
||||
// completion opts
|
||||
opts = getOpts(args.raw);
|
||||
|
||||
unescape = require('./shell').unescape;
|
||||
|
||||
module.exports = function() {
|
||||
return this.title('Shell completion').helpful().arg().name('raw').title('Completion words').arr().end().act(function(opts, args) {
|
||||
var argv, cmd, e, _ref;
|
||||
if (process.platform === 'win32') {
|
||||
e = new Error('shell completion not supported on windows');
|
||||
e.code = 'ENOTSUP';
|
||||
e.errno = require('constants').ENOTSUP;
|
||||
return this.reject(e);
|
||||
}
|
||||
if ((process.env.COMP_CWORD == null) || (process.env.COMP_LINE == null) || (process.env.COMP_POINT == null)) {
|
||||
return dumpScript(this._cmd._name);
|
||||
}
|
||||
console.error('COMP_LINE: %s', process.env.COMP_LINE);
|
||||
console.error('COMP_CWORD: %s', process.env.COMP_CWORD);
|
||||
console.error('COMP_POINT: %s', process.env.COMP_POINT);
|
||||
console.error('args: %j', args.raw);
|
||||
opts = getOpts(args.raw);
|
||||
_ref = this._cmd._parseCmd(opts.partialWords), cmd = _ref.cmd, argv = _ref.argv;
|
||||
return Q.when(complete(cmd, opts), function(compls) {
|
||||
console.error('filtered: %j', compls);
|
||||
return console.log(compls.map(escape).join('\n'));
|
||||
});
|
||||
});
|
||||
// cmd
|
||||
const parsed = this._cmd._parseCmd(opts.partialWords);
|
||||
return Q.when(complete(parsed.cmd, parsed.opts), compls => {
|
||||
console.error('filtered: %j', compls);
|
||||
return console.log(compls.map(escape).join('\n'));
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
dumpScript = function(name) {
|
||||
var defer, fs, path;
|
||||
fs = require('fs');
|
||||
path = require('path');
|
||||
defer = Q.defer();
|
||||
fs.readFile(path.resolve(__dirname, 'completion.sh'), 'utf8', function(err, d) {
|
||||
var onError;
|
||||
if (err) {
|
||||
return defer.reject(err);
|
||||
}
|
||||
d = d.replace(/{{cmd}}/g, path.basename(name)).replace(/^\#\!.*?\n/, '');
|
||||
onError = function(err) {
|
||||
if (err.errno === require('constants').EPIPE) {
|
||||
function dumpScript(name) {
|
||||
const defer = Q.defer();
|
||||
|
||||
fs.readFile(path.resolve(__dirname, 'completion.sh'), 'utf8', function(err, d) {
|
||||
if(err) return defer.reject(err);
|
||||
d = d.replace(/{{cmd}}/g, path.basename(name)).replace(/^#!.*?\n/, '');
|
||||
|
||||
process.stdout.on('error', onError);
|
||||
process.stdout.write(d, () => defer.resolve());
|
||||
});
|
||||
|
||||
return defer.promise;
|
||||
|
||||
function onError(err) {
|
||||
// Darwin is a real dick sometimes.
|
||||
//
|
||||
// This is necessary because the "source" or "." program in
|
||||
// bash on OS X closes its file argument before reading
|
||||
// from it, meaning that you get exactly 1 write, which will
|
||||
// work most of the time, and will always raise an EPIPE.
|
||||
//
|
||||
// Really, one should not be tossing away EPIPE errors, or any
|
||||
// errors, so casually. But, without this, `. <(cmd completion)`
|
||||
// can never ever work on OS X.
|
||||
if(err.errno !== constants.EPIPE) return defer.reject(err);
|
||||
process.stdout.removeListener('error', onError);
|
||||
return defer.resolve();
|
||||
} else {
|
||||
return defer.reject(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getOpts(argv) {
|
||||
// get the partial line and partial word, if the point isn't at the end
|
||||
// ie, tabbing at: cmd foo b|ar
|
||||
const line = process.env.COMP_LINE;
|
||||
const w = +process.env.COMP_CWORD;
|
||||
const point = +process.env.COMP_POINT;
|
||||
const words = argv.map(unescape);
|
||||
const word = words[w];
|
||||
const partialLine = line.substr(0, point);
|
||||
const partialWords = words.slice(0, w);
|
||||
|
||||
// figure out where in that last word the point is
|
||||
let partialWord = argv[w] || '';
|
||||
let i = partialWord.length;
|
||||
while(partialWord.substr(0, i) !== partialLine.substr(-1 * i) && i > 0) i--;
|
||||
|
||||
partialWord = unescape(partialWord.substr(0, i));
|
||||
partialWord && partialWords.push(partialWord);
|
||||
|
||||
return {
|
||||
line,
|
||||
w,
|
||||
point,
|
||||
words,
|
||||
word,
|
||||
partialLine,
|
||||
partialWords,
|
||||
partialWord
|
||||
};
|
||||
process.stdout.on('error', onError);
|
||||
return process.stdout.write(d, function() {
|
||||
return defer.resolve();
|
||||
});
|
||||
});
|
||||
return defer.promise;
|
||||
};
|
||||
}
|
||||
|
||||
getOpts = function(argv) {
|
||||
var i, line, partialLine, partialWord, partialWords, point, w, word, words;
|
||||
line = process.env.COMP_LINE;
|
||||
w = +process.env.COMP_CWORD;
|
||||
point = +process.env.COMP_POINT;
|
||||
words = argv.map(unescape);
|
||||
word = words[w];
|
||||
partialLine = line.substr(0, point);
|
||||
partialWords = words.slice(0, w);
|
||||
partialWord = argv[w] || '';
|
||||
i = partialWord.length;
|
||||
while (partialWord.substr(0, i) !== partialLine.substr(-1 * i) && i > 0) {
|
||||
i--;
|
||||
}
|
||||
partialWord = unescape(partialWord.substr(0, i));
|
||||
if (partialWord) {
|
||||
partialWords.push(partialWord);
|
||||
}
|
||||
return {
|
||||
line: line,
|
||||
w: w,
|
||||
point: point,
|
||||
words: words,
|
||||
word: word,
|
||||
partialLine: partialLine,
|
||||
partialWords: partialWords,
|
||||
partialWord: partialWord
|
||||
};
|
||||
};
|
||||
function complete(cmd, opts) {
|
||||
let optWord, optPrefix,
|
||||
compls = [];
|
||||
|
||||
complete = function(cmd, opts) {
|
||||
var compls, m, o, opt, optPrefix, optWord;
|
||||
compls = [];
|
||||
if (opts.partialWord.indexOf('-')) {
|
||||
compls = Object.keys(cmd._cmdsByName);
|
||||
} else {
|
||||
if (m = opts.partialWord.match(/^(--\w[\w-_]*)=(.*)$/)) {
|
||||
optWord = m[1];
|
||||
optPrefix = optWord + '=';
|
||||
} else {
|
||||
compls = Object.keys(cmd._optsByKey);
|
||||
// Complete on cmds
|
||||
if(opts.partialWord.indexOf('-'))
|
||||
compls = Object.keys(cmd._cmdsByName);
|
||||
// Complete on required opts without '-' in last partial word
|
||||
// (if required not already specified)
|
||||
//
|
||||
// Commented out because of uselessness:
|
||||
// -b, --block suggest results in '-' on cmd line;
|
||||
// next completion suggest all options, because of '-'
|
||||
//.concat Object.keys(cmd._optsByKey).filter (v) -> cmd._optsByKey[v]._req
|
||||
else {
|
||||
// complete on opt values: --opt=| case
|
||||
const m = opts.partialWord.match(/^(--\w[\w-_]*)=(.*)$/);
|
||||
if(m) {
|
||||
optWord = m[1];
|
||||
optPrefix = optWord + '=';
|
||||
} else
|
||||
// complete on opts
|
||||
// don't complete on opts in case of --opt=val completion
|
||||
// TODO: don't complete on opts in case of unknown arg after commands
|
||||
// TODO: complete only on opts with arr() or not already used
|
||||
// TODO: complete only on full opts?
|
||||
compls = Object.keys(cmd._optsByKey);
|
||||
}
|
||||
}
|
||||
if (!(o = opts.partialWords[opts.w - 1]).indexOf('-')) {
|
||||
optWord = o;
|
||||
}
|
||||
if (optWord && (opt = cmd._optsByKey[optWord])) {
|
||||
if (!opt._flag && opt._comp) {
|
||||
compls = Q.join(compls, Q.when(opt._comp(opts), function(c, o) {
|
||||
return c.concat(o.map(function(v) {
|
||||
return (optPrefix || '') + v;
|
||||
}));
|
||||
}));
|
||||
}
|
||||
}
|
||||
if (cmd._comp) {
|
||||
compls = Q.join(compls, Q.when(cmd._comp(opts)), function(c, o) {
|
||||
return c.concat(o);
|
||||
|
||||
// complete on opt values: next arg case
|
||||
opts.partialWords[opts.w - 1].indexOf('-') || (optWord = opts.partialWords[opts.w - 1]);
|
||||
|
||||
// complete on opt values: completion
|
||||
let opt;
|
||||
optWord
|
||||
&& (opt = cmd._optsByKey[optWord])
|
||||
&& !opt._flag
|
||||
&& opt._comp
|
||||
&& (compls = Q.join(compls,
|
||||
Q.when(opt._comp(opts),
|
||||
(c, o) => c.concat(o.map(v => (optPrefix || '') + v)))));
|
||||
|
||||
// TODO: complete on args values (context aware, custom completion?)
|
||||
|
||||
// custom completion on cmds
|
||||
cmd._comp && (compls = Q.join(compls, Q.when(cmd._comp(opts)), (c, o) => c.concat(o)));
|
||||
|
||||
// TODO: context aware custom completion on cmds, opts and args
|
||||
// (can depend on already entered values, especially options)
|
||||
|
||||
return Q.when(compls, complitions => {
|
||||
console.error('partialWord: %s', opts.partialWord);
|
||||
console.error('compls: %j', complitions);
|
||||
return compls.filter(c => c.indexOf(opts.partialWord) === 0);
|
||||
});
|
||||
}
|
||||
return Q.when(compls, function(compls) {
|
||||
console.error('partialWord: %s', opts.partialWord);
|
||||
console.error('compls: %j', compls);
|
||||
return compls.filter(function(c) {
|
||||
return c.indexOf(opts.partialWord) === 0;
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
+13
-9
@@ -1,10 +1,14 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
exports.Cmd = require('./cmd').Cmd;
|
||||
const
|
||||
Cmd = require('./cmd'),
|
||||
Opt = require('./opt'),
|
||||
Arg = require('./arg'),
|
||||
shell = require('./shell');
|
||||
|
||||
exports.Opt = require('./cmd').Opt;
|
||||
|
||||
exports.Arg = require('./cmd').Arg;
|
||||
|
||||
exports.shell = require('./shell');
|
||||
|
||||
exports.require = require;
|
||||
module.exports = {
|
||||
Cmd : Cmd.create,
|
||||
Opt : Opt.create,
|
||||
Arg : Arg.create,
|
||||
classes : { Cmd, Opt, Arg },
|
||||
shell,
|
||||
require
|
||||
};
|
||||
|
||||
+127
-310
@@ -1,338 +1,155 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
var Cmd, Color, Opt, Q, fs;
|
||||
'use strict';
|
||||
|
||||
fs = require('fs');
|
||||
const
|
||||
Q = require('q'),
|
||||
|
||||
Q = require('q');
|
||||
|
||||
Color = require('./color').Color;
|
||||
|
||||
Cmd = require('./cmd').Cmd;
|
||||
CoaParam = require('./coaparam'),
|
||||
chalk = require('chalk');
|
||||
|
||||
/**
|
||||
Option
|
||||
* Option
|
||||
*
|
||||
* Named entity. Options may have short and long keys for use from command line.
|
||||
*
|
||||
* @namespace
|
||||
* @class Opt
|
||||
* @extends CoaParam
|
||||
*/
|
||||
module.exports = class Opt extends CoaParam {
|
||||
/**
|
||||
* @constructs
|
||||
* @param {COA.Cmd} cmd - parent command
|
||||
*/
|
||||
constructor(cmd) {
|
||||
super(cmd);
|
||||
|
||||
Named entity. Options may have short and long keys for use from command line.
|
||||
@namespace
|
||||
@class Presents option
|
||||
*/
|
||||
this._short = null;
|
||||
this._long = null;
|
||||
this._flag = false;
|
||||
this._only = false;
|
||||
this._cmd._opts.push(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a short key for option to be used with one hyphen from command line.
|
||||
*
|
||||
* @param {String} short - short name
|
||||
* @returns {COA.Opt} - this instance (for chainability)
|
||||
*/
|
||||
short(short) {
|
||||
this._short = short;
|
||||
this._cmd._optsByKey[`-${short}`] = this;
|
||||
return this;
|
||||
}
|
||||
|
||||
exports.Opt = Opt = (function() {
|
||||
/**
|
||||
@constructs
|
||||
@param {COA.Cmd} cmd parent command
|
||||
*/
|
||||
/**
|
||||
* Set a short key for option to be used with double hyphens from command line.
|
||||
*
|
||||
* @param {String} long - long name
|
||||
* @returns {COA.Opt} - this instance (for chainability)
|
||||
*/
|
||||
long(long) {
|
||||
this._long = long;
|
||||
this._cmd._optsByKey[`--${long}`] = this;
|
||||
return this;
|
||||
}
|
||||
|
||||
function Opt(_cmd) {
|
||||
this._cmd = _cmd;
|
||||
this._cmd._opts.push(this);
|
||||
}
|
||||
/**
|
||||
* Make an option boolean, i.e. option without value.
|
||||
*
|
||||
* @returns {COA.Opt} - this instance (for chainability)
|
||||
*/
|
||||
flag() {
|
||||
this._flag = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
Set a canonical option identifier to be used anywhere in the API.
|
||||
@param {String} _name option name
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
/**
|
||||
* Makes an option to act as a command,
|
||||
* i.e. program will exit just after option action.
|
||||
*
|
||||
* @returns {COA.Opt} - this instance (for chainability)
|
||||
*/
|
||||
only() {
|
||||
this._only = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add action for current option command.
|
||||
* This action is performed if the current option
|
||||
* is present in parsed options (with any value).
|
||||
*
|
||||
* @param {Function} act - action function,
|
||||
* invoked in the context of command instance
|
||||
* and has the parameters:
|
||||
* - {Object} opts - parsed options
|
||||
* - {Array} args - parsed arguments
|
||||
* - {Object} res - actions result accumulator
|
||||
* It can return rejected promise by Cmd.reject (in case of error)
|
||||
* or any other value treated as result.
|
||||
* @returns {COA.Opt} - this instance (for chainability)
|
||||
*/
|
||||
act(act) {
|
||||
// Need function here for arguments
|
||||
const opt = this;
|
||||
this._cmd.act(function(opts) {
|
||||
if(!opts.hasOwnProperty(opt._name)) return;
|
||||
|
||||
Opt.prototype.name = function(_name) {
|
||||
this._name = _name;
|
||||
return this;
|
||||
};
|
||||
const res = act.apply(this, arguments);
|
||||
if(!opt._only) return res;
|
||||
|
||||
/**
|
||||
Set a long description for option to be used anywhere in text messages.
|
||||
@param {String} _title option title
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
return Q.when(res, out => this.reject({
|
||||
toString : () => out.toString(),
|
||||
exitCode : 0
|
||||
}));
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Opt.prototype.title = Cmd.prototype.title;
|
||||
_saveVal(opts, val) {
|
||||
this._val && (val = this._val(val));
|
||||
|
||||
/**
|
||||
Set a short key for option to be used with one hyphen from command line.
|
||||
@param {String} _short
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
const name = this._name;
|
||||
this._arr
|
||||
? (opts[name] || (opts[name] = [])).push(val)
|
||||
: (opts[name] = val);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
Opt.prototype.short = function(_short) {
|
||||
this._short = _short;
|
||||
return this._cmd._optsByKey['-' + _short] = this;
|
||||
};
|
||||
_parse(argv, opts) {
|
||||
return this._saveVal(opts, this._flag ? true : argv.shift());
|
||||
}
|
||||
|
||||
/**
|
||||
Set a short key for option to be used with double hyphens from command line.
|
||||
@param {String} _long
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
_checkParsed(opts) {
|
||||
return !opts.hasOwnProperty(this._name);
|
||||
}
|
||||
|
||||
_usage() {
|
||||
const res = [],
|
||||
nameStr = this._name.toUpperCase();
|
||||
|
||||
Opt.prototype.long = function(_long) {
|
||||
this._long = _long;
|
||||
return this._cmd._optsByKey['--' + _long] = this;
|
||||
};
|
||||
|
||||
/**
|
||||
Make an option boolean, i.e. option without value.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.flag = function() {
|
||||
this._flag = true;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Makes an option accepts multiple values.
|
||||
Otherwise, the value will be used by the latter passed.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.arr = function() {
|
||||
this._arr = true;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Makes an option required.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.req = function() {
|
||||
this._req = true;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Makes an option to act as a command,
|
||||
i.e. program will exit just after option action.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.only = function() {
|
||||
this._only = true;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Set a validation (or value) function for option.
|
||||
Value from command line passes through before becoming available from API.
|
||||
Using for validation and convertion simple types to any values.
|
||||
@param {Function} _val validating function,
|
||||
invoked in the context of option instance
|
||||
and has one parameter with value from command line
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.val = function(_val) {
|
||||
this._val = _val;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Set a default value for option.
|
||||
Default value passed through validation function as ordinary value.
|
||||
@param {Object} _def
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.def = function(_def) {
|
||||
this._def = _def;
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Make option value inputting stream.
|
||||
It's add useful validation and shortcut for STDIN.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.input = function() {
|
||||
process.stdin.pause();
|
||||
return this.def(process.stdin).val(function(v) {
|
||||
var s;
|
||||
if (typeof v === 'string') {
|
||||
if (v === '-') {
|
||||
return process.stdin;
|
||||
} else {
|
||||
s = fs.createReadStream(v, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
s.pause();
|
||||
return s;
|
||||
if(this._short) {
|
||||
res.push('-', chalk.greenBright(this._short));
|
||||
this._flag || res.push(' ' + nameStr);
|
||||
res.push(', ');
|
||||
}
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Make option value outputing stream.
|
||||
It's add useful validation and shortcut for STDOUT.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.output = function() {
|
||||
return this.def(process.stdout).val(function(v) {
|
||||
if (typeof v === 'string') {
|
||||
if (v === '-') {
|
||||
return process.stdout;
|
||||
} else {
|
||||
return fs.createWriteStream(v, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
if(this._long) {
|
||||
res.push('--', chalk.green(this._long));
|
||||
this._flag || res.push('=' + nameStr);
|
||||
}
|
||||
} else {
|
||||
return v;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
Add action for current option command.
|
||||
This action is performed if the current option
|
||||
is present in parsed options (with any value).
|
||||
@param {Function} act action function,
|
||||
invoked in the context of command instance
|
||||
and has the parameters:
|
||||
- {Object} opts parsed options
|
||||
- {Array} args parsed arguments
|
||||
- {Object} res actions result accumulator
|
||||
It can return rejected promise by Cmd.reject (in case of error)
|
||||
or any other value treated as result.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
res.push(' : ', this._title);
|
||||
|
||||
this._req && res.push(' ', chalk.redBright('(required)'));
|
||||
|
||||
Opt.prototype.act = function(act) {
|
||||
var name, opt;
|
||||
opt = this;
|
||||
name = this._name;
|
||||
this._cmd.act(function(opts) {
|
||||
var res,
|
||||
_this = this;
|
||||
if (name in opts) {
|
||||
res = act.apply(this, arguments);
|
||||
if (opt._only) {
|
||||
return Q.when(res, function(res) {
|
||||
return _this.reject({
|
||||
toString: function() {
|
||||
return res.toString();
|
||||
},
|
||||
exitCode: 0
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
});
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
Set custom additional completion for current option.
|
||||
@param {Function} completion generation function,
|
||||
invoked in the context of option instance.
|
||||
Accepts parameters:
|
||||
- {Object} opts completion options
|
||||
It can return promise or any other value treated as result.
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.comp = Cmd.prototype.comp;
|
||||
|
||||
Opt.prototype._saveVal = function(opts, val) {
|
||||
var _name;
|
||||
if (this._val) {
|
||||
val = this._val(val);
|
||||
return res.join('');
|
||||
}
|
||||
if (this._arr) {
|
||||
(opts[_name = this._name] || (opts[_name] = [])).push(val);
|
||||
} else {
|
||||
opts[this._name] = val;
|
||||
|
||||
_requiredText() {
|
||||
return `Missing required option:\n ${this._usage()}`;
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
Opt.prototype._parse = function(argv, opts) {
|
||||
return this._saveVal(opts, this._flag ? true : argv.shift());
|
||||
};
|
||||
|
||||
Opt.prototype._checkParsed = function(opts, args) {
|
||||
return !opts.hasOwnProperty(this._name);
|
||||
};
|
||||
|
||||
Opt.prototype._usage = function() {
|
||||
var nameStr, res;
|
||||
res = [];
|
||||
nameStr = this._name.toUpperCase();
|
||||
if (this._short) {
|
||||
res.push('-', Color('lgreen', this._short));
|
||||
if (!this._flag) {
|
||||
res.push(' ' + nameStr);
|
||||
}
|
||||
res.push(', ');
|
||||
}
|
||||
if (this._long) {
|
||||
res.push('--', Color('green', this._long));
|
||||
if (!this._flag) {
|
||||
res.push('=' + nameStr);
|
||||
}
|
||||
}
|
||||
res.push(' : ', this._title);
|
||||
if (this._req) {
|
||||
res.push(' ', Color('lred', '(required)'));
|
||||
}
|
||||
return res.join('');
|
||||
};
|
||||
|
||||
Opt.prototype._requiredText = function() {
|
||||
return 'Missing required option:\n ' + this._usage();
|
||||
};
|
||||
|
||||
/**
|
||||
Return rejected promise with error code.
|
||||
Use in .val() for return with error.
|
||||
@param {Object} reject reason
|
||||
You can customize toString() method and exitCode property
|
||||
of reason object.
|
||||
@returns {Q.promise} rejected promise
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.reject = Cmd.prototype.reject;
|
||||
|
||||
/**
|
||||
Finish chain for current option and return parent command instance.
|
||||
@returns {COA.Cmd} parent command
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.end = Cmd.prototype.end;
|
||||
|
||||
/**
|
||||
Apply function with arguments in context of option instance.
|
||||
@param {Function} fn
|
||||
@param {Array} args
|
||||
@returns {COA.Opt} this instance (for chainability)
|
||||
*/
|
||||
|
||||
|
||||
Opt.prototype.apply = Cmd.prototype.apply;
|
||||
|
||||
return Opt;
|
||||
|
||||
})();
|
||||
};
|
||||
|
||||
+13
-13
@@ -1,14 +1,14 @@
|
||||
// Generated by CoffeeScript 1.6.3
|
||||
exports.unescape = function(w) {
|
||||
w = w.charAt(0) === '"' ? w.replace(/^"|([^\\])"$/g, '$1') : w.replace(/\\ /g, ' ');
|
||||
return w.replace(/\\("|'|\$|`|\\)/g, '$1');
|
||||
};
|
||||
module.exports = { escape, unescape };
|
||||
|
||||
exports.escape = function(w) {
|
||||
w = w.replace(/(["'$`\\])/g, '\\$1');
|
||||
if (w.match(/\s+/)) {
|
||||
return '"' + w + '"';
|
||||
} else {
|
||||
return w;
|
||||
}
|
||||
};
|
||||
function unescape(w) {
|
||||
w = w.charAt(0) === '"'
|
||||
? w.replace(/^"|([^\\])"$/g, '$1')
|
||||
: w.replace(/\\ /g, ' ');
|
||||
|
||||
return w.replace(/\\("|'|\$|`|\\)/g, '$1');
|
||||
}
|
||||
|
||||
function escape(w) {
|
||||
w = w.replace(/(["'$`\\])/g,'\\$1');
|
||||
return w.match(/\s+/) ? `"${w}"` : w;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user