cli-define
Advanced tools
Comparing version
68
index.js
@@ -339,4 +339,5 @@ var EOL = require('os').EOL; | ||
function toObject(opts) { | ||
function toObject(opts, depth) { | ||
opts = opts || {}; | ||
if(depth === undefined) depth = 0; | ||
var o = {}; | ||
@@ -400,6 +401,10 @@ o.constructor = this.constructor; | ||
v = args[k]; | ||
target[k] = v.toObject(opts); | ||
target[k] = v.toObject(opts, depth); | ||
} | ||
} | ||
if(typeof opts.item === 'function') { | ||
o = opts.item.call(this, o, depth); | ||
} | ||
if(opts.recurse !== false | ||
@@ -413,3 +418,5 @@ && isFunction(this.options) && isFunction(this.commands)) { | ||
o.commands = {}; | ||
++depth; | ||
walk(this.commands(), o.commands); | ||
--depth; | ||
} | ||
@@ -571,2 +578,44 @@ } | ||
/** | ||
* Set options as an object group. | ||
*/ | ||
function options(value) { | ||
if(!arguments.length) return this._options; | ||
this._options = value; | ||
// convert plain objects with *name* | ||
// to Option or Flag instances | ||
var k, v, clazz; | ||
for(k in this._options) { | ||
v = this._options[k]; | ||
if(v && typeof v === 'object' && !(v instanceof Argument) && v.name) { | ||
clazz = getClassByName(v.name); | ||
this._options[k] = new clazz(v); | ||
} | ||
} | ||
return this; | ||
} | ||
define(Command.prototype, 'options', options, true); | ||
/** | ||
* Set commands as an object group. | ||
*/ | ||
function commands(value) { | ||
if(!arguments.length) return this._commands; | ||
this._commands = value; | ||
// convert plain objects with *name* | ||
// to Command instances | ||
var k, v; | ||
for(k in this._commands) { | ||
v = this._commands[k]; | ||
if(v && typeof v === 'object' && !(v instanceof Command) && v.name) { | ||
this._commands[k] = new Command(v); | ||
} | ||
} | ||
return this; | ||
} | ||
define(Command.prototype, 'commands', commands, true); | ||
// define action so we can clear execs list | ||
@@ -637,3 +686,2 @@ function action(value) { | ||
for(k in EventProxy) { | ||
@@ -645,2 +693,3 @@ define(Command.prototype, k, EventProxy[k], false); | ||
keys.forEach(function(name) { | ||
if(Command.prototype[name]) return; | ||
var read = function() { | ||
@@ -675,5 +724,6 @@ return this['_' + name]; | ||
/** | ||
* Define an option argument. | ||
* Inspect an argument name and determine the class (type) | ||
* of argument: Option or Flag. | ||
*/ | ||
function option(name, description, options, coerce, value) { | ||
function getClassByName(name) { | ||
var clazz = Option; | ||
@@ -685,2 +735,10 @@ if(typeof name === 'string') { | ||
} | ||
return clazz; | ||
} | ||
/** | ||
* Define an option argument. | ||
*/ | ||
function option(name, description, options, coerce, value) { | ||
var clazz = getClassByName(name); | ||
var opt = (name instanceof Option) || (name instanceof Flag) ? name | ||
@@ -687,0 +745,0 @@ : new clazz(name, description, options, coerce, value); |
{ | ||
"name": "cli-define", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "Chainable argument builder for a command line interface", | ||
@@ -5,0 +5,0 @@ "author": "muji <noop@xpm.io>", |
65044
4.72%28
3.7%1755
6.11%