Comparing version 1.0.5 to 1.0.6
175
index.js
@@ -9,2 +9,3 @@ var MAX_LINE_WIDTH = process.stdout.columns || 200; | ||
var ansiRegex = /\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[m|K]/g; | ||
var hasOwnProperty = Object.prototype.hasOwnProperty; | ||
@@ -24,2 +25,10 @@ function stringLength(str){ | ||
function assign(dest, source){ | ||
for (var key in source) | ||
if (hasOwnProperty.call(source, key)) | ||
dest[key] = source[key]; | ||
return dest; | ||
} | ||
function returnFirstArg(value){ | ||
@@ -136,4 +145,3 @@ return value; | ||
} catch(e) { | ||
console.log(e.message); | ||
throw new ParseError('Bad paramenter description in usage for option: ' + usage); | ||
throw new ParseError('Bad paramenter description in usage for option: ' + usage, e); | ||
} | ||
@@ -147,5 +155,3 @@ | ||
for (var key in params) | ||
if (params.hasOwnProperty(key)) | ||
this[key] = params[key]; | ||
assign(this, params); | ||
} | ||
@@ -170,2 +176,3 @@ | ||
hot: false, | ||
required: false, | ||
@@ -193,6 +200,17 @@ minArgsCount: 0, | ||
{ | ||
if (typeof opt_1 == 'function') | ||
option.normalize = opt_1; | ||
if (opt_1 && opt_1.constructor === Object) | ||
{ | ||
for (var key in opt_1) | ||
if (key == 'normalize' || | ||
key == 'defValue' || | ||
key == 'hot') | ||
option[key] = opt_1[key]; | ||
} | ||
else | ||
option.defValue = opt_1; | ||
{ | ||
if (typeof opt_1 == 'function') | ||
option.normalize = opt_1; | ||
else | ||
option.defValue = opt_1; | ||
} | ||
} | ||
@@ -261,5 +279,2 @@ | ||
if (!option) | ||
throw new ParseError('Unknown option name: ' + token); | ||
if (option.maxArgsCount) | ||
@@ -372,3 +387,3 @@ { | ||
if (!option) | ||
throw new ParseError('Unknown short option name: -' + token[j]); | ||
throw new ParseError('Unknown short option name: -' + token[1]); | ||
@@ -380,3 +395,3 @@ // single option | ||
{ | ||
// sequence | ||
// short options sequence | ||
for (var j = 1; j < token.length; j++) | ||
@@ -461,3 +476,18 @@ { | ||
function setFunctionFactory(name){ | ||
return function(fn){ | ||
var property = name + '_'; | ||
if (this[property] !== noop) | ||
throw new ParseError('Method `' + name + '` could be invoked only once'); | ||
if (typeof fn != 'function') | ||
throw new ParseError('Value for `' + name + '` method should be a function'); | ||
this[property] = fn; | ||
return this; | ||
} | ||
} | ||
/** | ||
@@ -504,2 +534,3 @@ * @class | ||
version_: '', | ||
initContext_: noop, | ||
init_: noop, | ||
@@ -531,4 +562,4 @@ delegate_: noop, | ||
for (var name in values) | ||
if (values.hasOwnProperty(name)) | ||
if (command.options.hasOwnProperty(name)) | ||
if (hasOwnProperty.call(values, name)) | ||
if (hasOwnProperty.call(command.options, name)) | ||
command.setOption(name, values[name]); | ||
@@ -546,9 +577,6 @@ else | ||
hasOption: function(name){ | ||
return this.options.hasOwnProperty(name); | ||
return hasOwnProperty.call(this.options, name); | ||
}, | ||
hasOptions: function(){ | ||
var count = 0; | ||
for (var key in this.options) | ||
if (count++) | ||
return true; | ||
return Object.keys(this.options).length > 0; | ||
}, | ||
@@ -571,3 +599,3 @@ setOption: function(name, value, isDefault){ | ||
for (var name in values) | ||
if (values.hasOwnProperty(name) && this.hasOption(name)) | ||
if (hasOwnProperty.call(values, name) && this.hasOption(name)) | ||
this.setOption(name, values[name]); | ||
@@ -578,5 +606,3 @@ }, | ||
for (var optionName in this.defaults_) | ||
if (hasOwnProperty.call(this.defaults_, optionName)) | ||
this.values[optionName] = this.defaults_[optionName]; | ||
assign(this.values, this.defaults_); | ||
}, | ||
@@ -615,4 +641,3 @@ | ||
hasCommands: function(){ | ||
for (var key in this.commands) | ||
return true; | ||
return Object.keys(this.commands).length > 0; | ||
}, | ||
@@ -646,47 +671,8 @@ | ||
init: function(fn){ | ||
if (this.init_ !== noop) | ||
throw new ParseError('Init function for command could be set only once'); | ||
init: setFunctionFactory('init'), | ||
initContext: setFunctionFactory('initContext'), | ||
args: setFunctionFactory('args'), | ||
delegate: setFunctionFactory('delegate'), | ||
action: setFunctionFactory('action'), | ||
if (typeof fn != 'function') | ||
throw new ParseError('Value for init should be a function'); | ||
this.init_ = fn; | ||
return this; | ||
}, | ||
args: function(fn){ | ||
if (this.args_ !== noop) | ||
throw new ParseError('Arguments handler for command could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new ParseError('Value for arguments handler should be a function'); | ||
this.args_ = fn; | ||
return this; | ||
}, | ||
delegate: function(fn){ | ||
if (this.delegate_ !== noop) | ||
throw new ParseError('Delegate function could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new ParseError('Value for delegate should be a function'); | ||
this.delegate_ = fn; | ||
return this; | ||
}, | ||
action: function(fn){ | ||
if (this.action_ !== noop) | ||
throw new ParseError('Action for command could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new ParseError('Value for action should be a function'); | ||
this.action_ = fn; | ||
return this; | ||
}, | ||
parse: function(args, suggest){ | ||
@@ -714,3 +700,3 @@ var suggestions; | ||
var prevCommand; | ||
var context = context || {}; | ||
var context = assign({}, context || this.initContext_()); | ||
for (var i = 0; i < commands.length; i++) | ||
@@ -724,2 +710,3 @@ { | ||
command.context = context; | ||
command.root = this; | ||
@@ -729,2 +716,11 @@ if (prevCommand) | ||
// apply hot options | ||
command.setOptions( | ||
item.options.reduce(function(res, entry){ | ||
if (entry.option.hot) | ||
res[entry.option.camelName] = entry.value; | ||
return res; | ||
}, {}) | ||
); | ||
command.init_(item.args); | ||
@@ -735,6 +731,10 @@ | ||
command.setOptions(item.options.reduce(function(res, optionItem){ | ||
res[optionItem.option.camelName] = optionItem.value; | ||
return res; | ||
}, {})); | ||
// apply regular options | ||
command.setOptions( | ||
item.options.reduce(function(res, entry){ | ||
if (!entry.option.hot) | ||
res[entry.option.camelName] = entry.value; | ||
return res; | ||
}, {}) | ||
); | ||
@@ -756,4 +756,4 @@ prevCommand = command; | ||
for (var name in this.values) | ||
if (this.values.hasOwnProperty(name)) | ||
result[name] = values.hasOwnProperty(name) && this.options.hasOwnProperty(name) | ||
if (hasOwnProperty.call(this.values, name)) | ||
result[name] = hasOwnProperty.call(values, name) && hasOwnProperty(this.options, name) | ||
? this.options[name].normalize.call(this, values[name]) | ||
@@ -763,3 +763,3 @@ : this.values[name]; | ||
for (var name in values) | ||
if (values.hasOwnProperty(name) && !this.options.hasOwnProperty(name)) | ||
if (hasOwnProperty.call(values, name) && !hasOwnProperty.call(this.options, name)) | ||
result[name] = values[name]; | ||
@@ -822,8 +822,8 @@ | ||
var lines = Object.keys(command.commands).sort().map(function(name){ | ||
var cmd = command.commands[name]; | ||
var subcommand = command.commands[name]; | ||
var line = { | ||
name: chalk.green(name) + | ||
(cmd.hasOptions() ? ' [<options>]' : '') + | ||
(cmd._args && cmd._args.length ? ' [<args>]' : ''), | ||
description: cmd.description_ || '' | ||
(subcommand.hasOptions() ? ' [<options>]' : '') + | ||
(subcommand._args && subcommand._args.length ? ' [<args>]' : ''), | ||
description: subcommand.description_ || '' | ||
}; | ||
@@ -851,2 +851,3 @@ | ||
var hasShortOptions = Object.keys(command.short).length > 0; | ||
var maxNameLength = MIN_OFFSET - 2; | ||
@@ -856,5 +857,9 @@ var lines = Object.keys(command.long).sort().map(function(name){ | ||
var line = { | ||
name: option.usage.replace(/(^|\s)(-[^\s,]+)/ig, function(m, p, flag){ | ||
return p + chalk.yellow(flag); | ||
}), | ||
name: option.usage | ||
.replace(/^(?:-., |)/, function(m, short){ | ||
return m || (hasShortOptions ? ' ' : ''); | ||
}) | ||
.replace(/(^|\s)(-[^\s,]+)/ig, function(m, p, flag){ | ||
return p + chalk.yellow(flag); | ||
}), | ||
description: option.description | ||
@@ -861,0 +866,0 @@ }; |
@@ -6,3 +6,3 @@ { | ||
"author": "Roman Dvornov <rdvornov@gmail.com>", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"keywords": ["cli", "command", "option", "argument", "completion"], | ||
@@ -20,3 +20,3 @@ "homepage": "https://github.com/lahmatiy/clap", | ||
"dependencies": { | ||
"chalk": "0.5.1" | ||
"chalk": "1.1.0" | ||
}, | ||
@@ -23,0 +23,0 @@ "devDependencies": { |
22165
741
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedchalk@1.1.0(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
- Removedansi-regex@0.2.1(transitive)
- Removedansi-styles@1.1.0(transitive)
- Removedchalk@0.5.1(transitive)
- Removedhas-ansi@0.1.0(transitive)
- Removedstrip-ansi@0.3.0(transitive)
- Removedsupports-color@0.2.0(transitive)
Updatedchalk@1.1.0