Comparing version 1.0.0-alpha.2 to 1.0.0-beta
106
index.js
@@ -14,3 +14,3 @@ | ||
function pad(width, str) { | ||
function pad(width, str){ | ||
return str + Array(Math.max(0, width - str.length) + 1).join(' '); | ||
@@ -43,3 +43,4 @@ } | ||
}); | ||
} while (tmp != left); | ||
} | ||
while (tmp != left); | ||
@@ -54,6 +55,7 @@ do { | ||
}); | ||
} while (tmp != left); | ||
} | ||
while (tmp != left); | ||
if (left) | ||
throw new Error('Bad parameter description: ' + str); | ||
throw new ParseError('Bad parameter description: ' + str); | ||
@@ -64,4 +66,14 @@ return result.args.length ? result : false; | ||
/** | ||
* | ||
* @class | ||
*/ | ||
var ParseError = function(message){ | ||
this.message = message; | ||
}; | ||
ParseError.prototype = Object.create(Error.prototype); | ||
ParseError.prototype.name = 'ParseError'; | ||
/** | ||
* @class | ||
*/ | ||
var Argument = function(name, required){ | ||
@@ -87,3 +99,3 @@ this.name = name; | ||
var self = this; | ||
var params; | ||
var params; | ||
var left = usage.trim() | ||
@@ -109,3 +121,3 @@ // short usage | ||
if (!this.long) | ||
throw new Error('Usage has no long name: ' + usage); | ||
throw new ParseError('Usage has no long name: ' + usage); | ||
@@ -116,3 +128,3 @@ try { | ||
console.log(e.message); | ||
throw new Error('Bad paramenter description in usage for option: ' + usage); | ||
throw new ParseError('Bad paramenter description in usage for option: ' + usage); | ||
} | ||
@@ -132,3 +144,3 @@ | ||
if (left) | ||
throw new Error('Bad usage description for option: ' + usage); | ||
throw new ParseError('Bad usage description for option: ' + usage); | ||
@@ -141,3 +153,3 @@ if (!this.name) | ||
this.camelName = camelize(this.name); | ||
} | ||
}; | ||
@@ -154,3 +166,3 @@ Option.prototype = { | ||
args: null, | ||
defValue: undefined, | ||
@@ -169,3 +181,3 @@ normalize: returnFirstArg | ||
// if (option.bool && arguments.length > 2) | ||
// throw new Error('bool flags can\'t has default value or validator'); | ||
// throw new ParseError('bool flags can\'t has default value or validator'); | ||
@@ -200,3 +212,3 @@ if (arguments.length == 3) | ||
if (commandOption) | ||
throw new Error('Short option name -' + option.short + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
throw new ParseError('Short option name -' + option.short + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
@@ -210,3 +222,3 @@ command.short[option.short] = option; | ||
if (commandOption) | ||
throw new Error('Long option --' + option.long + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
throw new ParseError('Long option --' + option.long + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
@@ -219,3 +231,3 @@ command.long[option.long] = option; | ||
if (commandOption) | ||
throw new Error('Name option ' + option.camelName + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
throw new ParseError('Name option ' + option.camelName + ' already in use by ' + commandOption.usage + ' ' + commandOption.description); | ||
@@ -245,3 +257,3 @@ command.options[option.camelName] = option; | ||
if (!option) | ||
throw new Error('Unknown option name: ' + token); | ||
throw new ParseError('Unknown option name: ' + token); | ||
@@ -261,3 +273,3 @@ if (option.maxArgsCount) | ||
if (params.length < option.minArgsCount) | ||
throw new Error('Option ' + token + ' should be used with at least ' + option.minArgsCount + ' argument(s)\nUsage: ' + option.usage); | ||
throw new ParseError('Option ' + token + ' should be used with at least ' + option.minArgsCount + ' argument(s)\nUsage: ' + option.usage); | ||
} | ||
@@ -336,3 +348,3 @@ else | ||
else | ||
throw new Error('Unknown option: ' + token); | ||
throw new ParseError('Unknown option: ' + token); | ||
} | ||
@@ -347,3 +359,3 @@ | ||
if (!/^-[a-zA-Z]+$/.test(token)) | ||
throw new Error('Wrong short option sequence: ' + token); | ||
throw new ParseError('Wrong short option sequence: ' + token); | ||
@@ -355,3 +367,3 @@ if (token.length == 2) | ||
if (!option) | ||
throw new Error('Unknown short option name: -' + token[j]); | ||
throw new ParseError('Unknown short option name: -' + token[j]); | ||
@@ -369,6 +381,6 @@ // single option | ||
if (!option) | ||
throw new Error('Unknown short option name: -' + token[j]); | ||
throw new ParseError('Unknown short option name: -' + token[j]); | ||
if (option.maxArgsCount) | ||
throw new Error('Non-boolean option -' + token[j] + ' can\'t be used in short option sequence: ' + token); | ||
throw new ParseError('Non-boolean option -' + token[j] + ' can\'t be used in short option sequence: ' + token); | ||
@@ -407,3 +419,3 @@ processOption(option, command); | ||
else | ||
throw new Error('Unknown command: ' + token); | ||
throw new ParseError('Unknown command: ' + token); | ||
} | ||
@@ -442,3 +454,3 @@ } | ||
} catch(e) { | ||
throw new Error('Bad paramenter description in command definition: ' + this.name + ' ' + params); | ||
throw new ParseError('Bad paramenter description in command definition: ' + this.name + ' ' + params); | ||
} | ||
@@ -459,3 +471,3 @@ | ||
}, undefined); | ||
} | ||
}; | ||
@@ -485,3 +497,3 @@ Command.prototype = { | ||
if (typeof fn != 'function') | ||
throw new Error('fn should be a function'); | ||
throw new ParseError('fn should be a function'); | ||
@@ -494,3 +506,3 @@ var command = this; | ||
var values; | ||
value = normalize.call(command, value); | ||
@@ -509,3 +521,3 @@ values = fn(value); | ||
return value; | ||
} | ||
}; | ||
@@ -525,3 +537,3 @@ return this; | ||
if (!this.hasOption(name)) | ||
throw new Error('Option `' + name + '` is not defined'); | ||
throw new ParseError('Option `' + name + '` is not defined'); | ||
@@ -555,3 +567,3 @@ var option = this.options[name]; | ||
if (!/^[a-zA-Z][a-zA-Z0-9\-\_]*$/.test(name)) | ||
throw new Error('Wrong command name: ' + name); | ||
throw new ParseError('Wrong command name: ' + name); | ||
} | ||
@@ -579,4 +591,4 @@ | ||
if (this.version_) | ||
throw new Error('Version for command could be set only once'); | ||
throw new ParseError('Version for command could be set only once'); | ||
this.version_ = version; | ||
@@ -597,3 +609,3 @@ this.option( | ||
if (this.description_) | ||
throw new Error('Description for command could be set only once'); | ||
throw new ParseError('Description for command could be set only once'); | ||
@@ -607,6 +619,6 @@ this.description_ = description; | ||
if (this.init_ !== noop) | ||
throw new Error('Init function for command could be set only once'); | ||
throw new ParseError('Init function for command could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new Error('Value for init should be a function'); | ||
throw new ParseError('Value for init should be a function'); | ||
@@ -619,6 +631,6 @@ this.init_ = fn; | ||
if (this.args_ !== noop) | ||
throw new Error('Arguments handler for command could be set only once'); | ||
throw new ParseError('Arguments handler for command could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new Error('Value for arguments handler should be a function'); | ||
throw new ParseError('Value for arguments handler should be a function'); | ||
@@ -631,6 +643,6 @@ this.args_ = fn; | ||
if (this.delegate_ !== noop) | ||
throw new Error('Delegate function could be set only once'); | ||
throw new ParseError('Delegate function could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new Error('Value for delegate should be a function'); | ||
throw new ParseError('Value for delegate should be a function'); | ||
@@ -643,6 +655,6 @@ this.delegate_ = fn; | ||
if (this.action_ !== noop) | ||
throw new Error('Action for command could be set only once'); | ||
throw new ParseError('Action for command could be set only once'); | ||
if (typeof fn != 'function') | ||
throw new Error('Value for action should be a function'); | ||
throw new ParseError('Value for action should be a function'); | ||
@@ -697,3 +709,3 @@ this.action_ = fn; | ||
if (i == commands.length - 1) | ||
command.action_(item.args, item.literalArgs) | ||
command.action_(item.args, item.literalArgs); | ||
else | ||
@@ -757,3 +769,3 @@ prevCommand = command; | ||
lines.push( | ||
' ' + | ||
' ' + | ||
pad(22, name + (cmd.hasOptions() ? ' [options]' : '') + ' ' + args) + | ||
@@ -785,3 +797,3 @@ (cmd.description_ ? ' ' + cmd.description_ : '') | ||
}, 0); | ||
// Prepend the help information | ||
@@ -819,2 +831,4 @@ return [ | ||
module.exports = { | ||
Error: ParseError, | ||
Argument: Argument, | ||
Command: Command, | ||
@@ -825,6 +839,6 @@ Option: Option, | ||
if (errorHandler) | ||
throw new Error('Error handler should be set only once'); | ||
throw new ParseError('Error handler should be set only once'); | ||
if (typeof fn != 'function') | ||
throw new Error('Error handler should be a function'); | ||
throw new ParseError('Error handler should be a function'); | ||
@@ -831,0 +845,0 @@ errorHandler = fn; |
@@ -6,3 +6,3 @@ { | ||
"author": "Roman Dvornov <rdvornov@gmail.com>", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-beta", | ||
"keywords": ["cli", "command", "option", "argument", "completion"], | ||
@@ -9,0 +9,0 @@ "homepage": "https://github.com/lahmatiy/clap", |
20011
660