Comparing version 0.6.0 to 0.7.0
@@ -0,4 +1,5 @@ | ||
// Generated by CoffeeScript 1.3.3 | ||
(function() { | ||
var ARGUMENT, COMMAND, Command, DUMMY, DefaultHandlers, HEADER, OPTION, OPTION_BOOL, OPTION_DESC, OPTION_DESC_DEFAULT, OPTION_DESC_TAG, OPTION_LONG, OPTION_METAVARS, OPTION_SHORT, Option, Syntax, TEXT, USAGE, alignment, formatUsageString, handleUsage, indent, parse, printUsage, separator, width, wordwrap, wrapText, | ||
__slice = Array.prototype.slice; | ||
__slice = [].slice; | ||
@@ -37,4 +38,8 @@ wordwrap = require('wordwrap'); | ||
auto: function(value) { | ||
if (typeof value !== 'string') return value; | ||
if (!isNaN(Number(value))) return Number(value); | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
if (!isNaN(Number(value))) { | ||
return Number(value); | ||
} | ||
return value; | ||
@@ -46,3 +51,5 @@ }, | ||
int: function(value) { | ||
if (typeof value !== 'string') return value; | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
if (isNaN(parseInt(value, 10))) { | ||
@@ -54,9 +61,13 @@ throw new Error("Integer value required: " + value); | ||
flag: function(value, options, optionName, tagValue) { | ||
var _ref, _ref2; | ||
if (!(value != null)) return true; | ||
if (typeof value !== 'string') return value; | ||
var _ref, _ref1; | ||
if (!(value != null)) { | ||
return true; | ||
} | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
if ((_ref = value.toLowerCase()) === '0' || _ref === 'false' || _ref === 'no' || _ref === 'off') { | ||
return false; | ||
} | ||
if ((_ref2 = value.toLowerCase()) === '' || _ref2 === '1' || _ref2 === 'true' || _ref2 === 'yes' || _ref2 === 'on') { | ||
if ((_ref1 = value.toLowerCase()) === '' || _ref1 === '1' || _ref1 === 'true' || _ref1 === 'yes' || _ref1 === 'on') { | ||
return true; | ||
@@ -93,3 +104,5 @@ } | ||
})).join("\n"); | ||
if (otherLines.length) right += "\n"; | ||
if (otherLines.length) { | ||
right += "\n"; | ||
} | ||
return " " + left + padding + " " + right; | ||
@@ -111,3 +124,5 @@ }; | ||
this.name = this.metavars[0]; | ||
if ($ = this.name.match(/^\[(.*)\]$/)) this.name = $[1]; | ||
if ($ = this.name.match(/^\[(.*)\]$/)) { | ||
this.name = $[1]; | ||
} | ||
} | ||
@@ -135,3 +150,5 @@ this["var"] = this.name; | ||
longOpt = this.longOpt; | ||
if (longOpt && this.tags.acceptsno) longOpt = "--[no-]" + longOpt.slice(2); | ||
if (longOpt && this.tags.acceptsno) { | ||
longOpt = "--[no-]" + longOpt.slice(2); | ||
} | ||
string = (function() { | ||
@@ -167,3 +184,5 @@ switch (false) { | ||
newValue = handler(value, options, this.leftUsageComponent(), this.tags[tag]); | ||
if (typeof newValue !== void 0) value = newValue; | ||
if (typeof newValue !== void 0) { | ||
value = newValue; | ||
} | ||
any = true; | ||
@@ -207,6 +226,8 @@ } | ||
this.handlers = handlers; | ||
if (specs == null) specs = []; | ||
if (specs == null) { | ||
specs = []; | ||
} | ||
this.usage = []; | ||
this.options = []; | ||
this.arguments = []; | ||
this["arguments"] = []; | ||
this.commands = {}; | ||
@@ -220,3 +241,3 @@ this.commandsOrder = []; | ||
options: "Options:", | ||
arguments: "Arguments:", | ||
"arguments": "Arguments:", | ||
commands: "Commands:" | ||
@@ -226,3 +247,5 @@ }; | ||
this.customHeaderAdded = false; | ||
if (specs) this.add(specs); | ||
if (specs) { | ||
this.add(specs); | ||
} | ||
} | ||
@@ -246,3 +269,5 @@ | ||
var $, command, desc, gotArray, gotFunction, name, option, spec, subsyntax; | ||
if (typeof specs !== 'object') specs = [specs]; | ||
if (typeof specs !== 'object') { | ||
specs = [specs]; | ||
} | ||
specs = specs.slice(0); | ||
@@ -269,9 +294,15 @@ gotArray = function() { | ||
} | ||
if (option.longOpt) this.longOptions[option.longOpt.slice(2)] = option; | ||
if (gotFunction()) option.func = specs.shift(); | ||
if (option.longOpt) { | ||
this.longOptions[option.longOpt.slice(2)] = option; | ||
} | ||
if (gotFunction()) { | ||
option.func = specs.shift(); | ||
} | ||
this.ensureHeaderExists('options'); | ||
this.usage.push(option.toUsageString()); | ||
} else if (!gotArray() && spec.match(ARGUMENT)) { | ||
this.arguments.push((option = Option.parse(spec.trim()))); | ||
if (gotFunction()) option.func = specs.shift(); | ||
this["arguments"].push((option = Option.parse(spec.trim()))); | ||
if (gotFunction()) { | ||
option.func = specs.shift(); | ||
} | ||
this.ensureHeaderExists('arguments'); | ||
@@ -313,3 +344,3 @@ this.usage.push(option.toUsageString()); | ||
Syntax.prototype.parse = function(argv) { | ||
var $, arg, assignValue, executeHook, func, funcs, index, name, option, positional, processOption, remainder, result, subarg, value, _, _i, _j, _len, _len2, _len3, _len4, _ref, _ref2, _ref3, | ||
var $, arg, assignValue, executeHook, func, funcs, index, name, option, positional, processOption, remainder, result, subarg, value, _, _i, _j, _k, _l, _len, _len1, _len2, _len3, _ref, _ref1, _ref2, | ||
_this = this; | ||
@@ -327,3 +358,5 @@ argv = argv.slice(0); | ||
newValue = option.func(value, result, _this, option); | ||
if (newValue != null) value = newValue; | ||
if (newValue != null) { | ||
value = newValue; | ||
} | ||
} | ||
@@ -334,3 +367,3 @@ } | ||
processOption = function(result, arg, option, value) { | ||
var index, metavar, subvalue, _len, _ref; | ||
var index, metavar, subvalue, _i, _len, _ref; | ||
switch (option.metavars.length) { | ||
@@ -341,3 +374,5 @@ case 0: | ||
case 1: | ||
if (value == null) value = argv.shift(); | ||
if (value == null) { | ||
value = argv.shift(); | ||
} | ||
if (typeof value === 'undefined') { | ||
@@ -350,3 +385,3 @@ throw new Error("Option " + arg + " requires an argument: " + (option.leftUsageComponent())); | ||
_ref = option.metavars; | ||
for (index = 0, _len = _ref.length; index < _len; index++) { | ||
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { | ||
metavar = _ref[index]; | ||
@@ -363,4 +398,8 @@ value.push((subvalue = argv.shift())); | ||
if (option.tags.list) { | ||
if (!result.hasOwnProperty(option["var"])) result[option["var"]] = []; | ||
if (value != null) return result[option["var"]].push(value); | ||
if (!result.hasOwnProperty(option["var"])) { | ||
result[option["var"]] = []; | ||
} | ||
if (value != null) { | ||
return result[option["var"]].push(value); | ||
} | ||
} else { | ||
@@ -377,7 +416,7 @@ return result[option["var"]] = value; | ||
positional.push(arg); | ||
} else if (arg.match(/^--no-/) && (option = this.longOptions[arg.slice(5)]) && option.tags.flag) { | ||
} else if (arg.match(/^--no-/) && (option = this.lookupLongOption(arg.slice(5), result)) && option.tags.flag) { | ||
assignValue(result, option, false); | ||
} else if ($ = arg.match(/^--([^=]+)(?:=(.*))?$/)) { | ||
_ = $[0], name = $[1], value = $[2]; | ||
if (option = this.longOptions[name]) { | ||
if (option = this.lookupLongOption(name, result)) { | ||
value = processOption(result, arg, option, value); | ||
@@ -394,3 +433,3 @@ value = executeHook(option, value); | ||
remainder = remainder.slice(1); | ||
if (option = this.shortOptions[subarg]) { | ||
if (option = this.lookupShortOption(subarg, result)) { | ||
if (remainder && option.metavars.length > 0) { | ||
@@ -435,14 +474,16 @@ value = remainder; | ||
} | ||
for (index = 0, _len2 = positional.length; index < _len2; index++) { | ||
for (index = _j = 0, _len1 = positional.length; _j < _len1; index = ++_j) { | ||
arg = positional[index]; | ||
if (option = this.arguments[index]) { | ||
if (option = this["arguments"][index]) { | ||
value = option.coerce(arg, result, this); | ||
value = executeHook(option, value); | ||
positional[index] = value; | ||
if (option["var"]) assignValue(result, option, value); | ||
if (option["var"]) { | ||
assignValue(result, option, value); | ||
} | ||
} | ||
} | ||
_ref2 = this.arguments; | ||
for (index = 0, _len3 = _ref2.length; index < _len3; index++) { | ||
option = _ref2[index]; | ||
_ref1 = this["arguments"]; | ||
for (index = _k = 0, _len2 = _ref1.length; _k < _len2; index = ++_k) { | ||
option = _ref1[index]; | ||
if (index >= positional.length) { | ||
@@ -459,3 +500,5 @@ if (option.tags.required) { | ||
value = executeHook(option, value); | ||
if (option["var"]) assignValue(result, option, value); | ||
if (option["var"]) { | ||
assignValue(result, option, value); | ||
} | ||
if (index === positional.length) { | ||
@@ -470,4 +513,4 @@ positional.push(value); | ||
result.argv = positional; | ||
for (_j = 0, _len4 = funcs.length; _j < _len4; _j++) { | ||
_ref3 = funcs[_j], func = _ref3[0], option = _ref3[1], value = _ref3[2]; | ||
for (_l = 0, _len3 = funcs.length; _l < _len3; _l++) { | ||
_ref2 = funcs[_l], func = _ref2[0], option = _ref2[1], value = _ref2[2]; | ||
func(value, result, this, option); | ||
@@ -478,2 +521,30 @@ } | ||
Syntax.prototype.lookupLongOption = function(name, result) { | ||
var _base; | ||
if (!this.longOptions.hasOwnProperty(name)) { | ||
if (typeof (_base = this.handlers).resolveLongOption === "function") { | ||
_base.resolveLongOption(name, result, this); | ||
} | ||
} | ||
if (this.longOptions.hasOwnProperty(name)) { | ||
return this.longOptions[name]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
Syntax.prototype.lookupShortOption = function(name, result) { | ||
var _base; | ||
if (!this.shortOptions.hasOwnProperty(name)) { | ||
if (typeof (_base = this.handlers).resolveShortOption === "function") { | ||
_base.resolveShortOption(name, result, this); | ||
} | ||
} | ||
if (this.shortOptions.hasOwnProperty(name)) { | ||
return this.shortOptions[name]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
return Syntax; | ||
@@ -484,11 +555,11 @@ | ||
Option.parse = function(spec) { | ||
var $, defaultValue, desc, isOption, longOpt, metavars, options, shortOpt, tag, tags, value, _, _ref, _ref2, _ref3, _ref4, _ref5; | ||
var $, defaultValue, desc, isOption, longOpt, metavars, options, shortOpt, tag, tags, value, _, _ref, _ref1, _ref2, _ref3, _ref4; | ||
isOption = (' ' + spec).match(OPTION); | ||
_ref = spec.match(OPTION_DESC) || [void 0, spec, ""], _ = _ref[0], options = _ref[1], desc = _ref[2]; | ||
if (isOption) { | ||
_ref2 = options.match(OPTION_METAVARS) || [void 0, options, ""], _ = _ref2[0], options = _ref2[1], metavars = _ref2[2]; | ||
_ref3 = options.match(OPTION_SHORT) || [void 0, "", options], _ = _ref3[0], shortOpt = _ref3[1], options = _ref3[2]; | ||
_ref4 = (options || '').match(OPTION_LONG) || [void 0, "", options], _ = _ref4[0], longOpt = _ref4[1], options = _ref4[2]; | ||
_ref1 = options.match(OPTION_METAVARS) || [void 0, options, ""], _ = _ref1[0], options = _ref1[1], metavars = _ref1[2]; | ||
_ref2 = options.match(OPTION_SHORT) || [void 0, "", options], _ = _ref2[0], shortOpt = _ref2[1], options = _ref2[2]; | ||
_ref3 = (options || '').match(OPTION_LONG) || [void 0, "", options], _ = _ref3[0], longOpt = _ref3[1], options = _ref3[2]; | ||
} else { | ||
_ref5 = [options, ""], metavars = _ref5[0], options = _ref5[1]; | ||
_ref4 = [options, ""], metavars = _ref4[0], options = _ref4[1]; | ||
} | ||
@@ -509,3 +580,5 @@ metavars = metavars && metavars.split(/\s+/) || []; | ||
} | ||
if (isOption && metavars.length === 0) tags.push(['flag', true]); | ||
if (isOption && metavars.length === 0) { | ||
tags.push(['flag', true]); | ||
} | ||
if ($ = desc.match(OPTION_DESC_DEFAULT)) { | ||
@@ -542,4 +615,8 @@ defaultValue = $[1]; | ||
} | ||
if (handlers == null) handlers = {}; | ||
if (argv == null) argv = process.argv.slice(2); | ||
if (handlers == null) { | ||
handlers = {}; | ||
} | ||
if (argv == null) { | ||
argv = process.argv.slice(2); | ||
} | ||
syntax = new Syntax(handlers, specs); | ||
@@ -546,0 +623,0 @@ if (!syntax.longOptions.help) { |
@@ -5,3 +5,3 @@ { | ||
"description": "Command-line parser with readable syntax from your sweetest dreams", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"homepage": "https://github.com/andreyvit/dreamopt.js", | ||
@@ -8,0 +8,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
68557
1069
14