Comparing version 0.0.2 to 1.0.0
// Load modules | ||
var Tty = require('tty'); | ||
var Hoek = require('hoek'); | ||
@@ -30,2 +31,6 @@ | ||
if (def.valid !== undefined && !Array.isArray(def.valid)) { | ||
def.valid = [def.valid]; | ||
} | ||
if (def.type === 'boolean' && def.default !== undefined) { | ||
@@ -97,2 +102,9 @@ flags[name] = def.default; | ||
if (last && | ||
last.valid && | ||
last.valid.indexOf(value) === -1) { | ||
return internals.formatError('Invalid value for option:', last.name); | ||
} | ||
var name = last ? last.name : '_'; | ||
@@ -129,4 +141,11 @@ if (flags[name]) { | ||
exports.usage = function (definition, usage) { | ||
exports.usage = function (definition, usage, options) { | ||
if ((arguments.length === 2) && (typeof usage === 'object')) { | ||
options = usage; | ||
usage = ''; | ||
} | ||
options = options || { colors: null }; | ||
var color = internals.colors(options.colors); | ||
var output = usage ? 'Usage: ' + usage + '\n\n' : '\n'; | ||
@@ -141,5 +160,8 @@ var col1 = ['Options:']; | ||
var formattedName = ' -' + name; | ||
if (def.alias) { | ||
var aliases = [].concat(def.alias); | ||
var shortName = internals.getShortName(name, def.alias); | ||
var longName = (shortName === name) ? def.alias : name; | ||
var formattedName = ' -' + shortName; | ||
if (longName) { | ||
var aliases = [].concat(longName); | ||
for (var a = 0, al = aliases.length; a < al; ++a) { | ||
@@ -150,9 +172,9 @@ formattedName += ', --' + aliases[a]; | ||
var formattedDesc = def.description ? def.description : ''; | ||
var formattedDesc = def.description ? color.gray(def.description) : ''; | ||
if (def.require) { | ||
formattedDesc += formattedDesc.length ? ' ' : ''; | ||
formattedDesc += '(required)'; | ||
formattedDesc += color.yellow('(required)'); | ||
} | ||
col1.push(formattedName); | ||
col1.push(color.green(formattedName)); | ||
col2.push(formattedDesc); | ||
@@ -182,2 +204,19 @@ } | ||
internals.getShortName = function (shortName, alias) { | ||
if (!alias) { | ||
return shortName; | ||
} | ||
var aliases = [].concat(alias); | ||
for (var i = 0, il = aliases.length; i < il; ++i) { | ||
if (aliases[i] && aliases[i].length < shortName.length) { | ||
shortName = aliases[i]; | ||
} | ||
} | ||
return shortName; | ||
}; | ||
internals.formatColumns = function (col1, col2) { | ||
@@ -241,1 +280,40 @@ | ||
}; | ||
internals.colors = function (enabled) { | ||
if (enabled === null) { | ||
enabled = Tty.isatty(1) && Tty.isatty(2); | ||
} | ||
var codes = { | ||
'black': 0, | ||
'gray': 90, | ||
'red': 31, | ||
'green': 32, | ||
'yellow': 33, | ||
'magenta': 35, | ||
'redBg': 41, | ||
'greenBg': 42 | ||
}; | ||
var colors = {}; | ||
var names = Object.keys(codes); | ||
for (var i = 0, il = names.length; i < il; ++i) { | ||
var name = names[i]; | ||
colors[name] = internals.color(name, codes[name], enabled); | ||
} | ||
return colors; | ||
}; | ||
internals.color = function (name, code, enabled) { | ||
if (enabled) { | ||
var color = '\u001b[' + code + 'm'; | ||
return function (text) { return color + text + '\u001b[0m'; }; | ||
} | ||
return function (text) { return text; }; | ||
}; |
{ | ||
"name": "bossy", | ||
"description": "Command line options parser", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"repository": "git://github.com/hapijs/bossy", | ||
@@ -6,0 +6,0 @@ "main": "index", |
@@ -53,3 +53,3 @@ # bossy | ||
### `usage(definition, [usage])` | ||
### `usage(definition, [usage], [options])` | ||
@@ -59,3 +59,6 @@ Format a *bossy* definition object for display in the console. If `usage` is provided the returned value will | ||
Options accepts the following keys: | ||
* `colors` - Determines if colors are enabled when formatting usage. Defaults to whatever TTY supports. | ||
## Definition Object | ||
@@ -80,1 +83,3 @@ | ||
* `default`: A default value to assign to the argument if its not provided as an argument. | ||
* `valid`: A value or array of values that the argument is allowed to equal. |
@@ -271,2 +271,66 @@ // Load modules | ||
}); | ||
it('returns error message when a value isn\'t found in the valid property', function (done) { | ||
var line = '-a 2'; | ||
var definition = { | ||
a: { | ||
type: 'number', | ||
valid: 1 | ||
} | ||
}; | ||
var argv = parse(line, definition); | ||
expect(argv).to.be.instanceof(Error); | ||
done(); | ||
}); | ||
it('returns error message when a value isn\'t found in array of valid values', function (done) { | ||
var line = '-a 4'; | ||
var definition = { | ||
a: { | ||
type: 'number', | ||
valid: [1, 2, 3] | ||
} | ||
}; | ||
var argv = parse(line, definition); | ||
expect(argv).to.be.instanceof(Error); | ||
done(); | ||
}); | ||
it('doesn\'t return an error when the value is in the valid array', function (done) { | ||
var line = '-a 2'; | ||
var definition = { | ||
a: { | ||
type: 'number', | ||
valid: [1, 2, 3] | ||
} | ||
}; | ||
var argv = parse(line, definition); | ||
expect(argv).to.deep.equal({ a: 2 }); | ||
done(); | ||
}); | ||
it('doesn\'t return an error when the value is in equal to the valid value', function (done) { | ||
var line = '-a 0'; | ||
var definition = { | ||
a: { | ||
type: 'number', | ||
valid: 0 | ||
} | ||
}; | ||
var argv = parse(line, definition); | ||
expect(argv).to.deep.equal({ a: 0 }); | ||
done(); | ||
}); | ||
}); | ||
@@ -315,3 +379,116 @@ | ||
}); | ||
it('returns formatted usage information with colors when enabled', function (done) { | ||
var definition = { | ||
a: { | ||
alias: 'alpha', | ||
require: true, | ||
description: 'Description for b' | ||
} | ||
}; | ||
var result = Bossy.usage(definition, { colors: true }); | ||
expect(result).to.contain('-a'); | ||
expect(result).to.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
it('when colors are missing defaults to true if tty supports colors', function (done) { | ||
var definition = { | ||
a: { | ||
alias: 'alpha', | ||
require: true, | ||
description: 'Description for b' | ||
} | ||
}; | ||
var Tty = require('tty'); | ||
var currentIsAtty = Tty.isatty; | ||
Tty.isatty = function () { | ||
Tty.isatty = currentIsAtty; | ||
return true; | ||
}; | ||
var result = Bossy.usage(definition); | ||
expect(result).to.contain('-a'); | ||
expect(result).to.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
it('when colors are missing defaults to false if tty doesn\'t support colors', function (done) { | ||
var definition = { | ||
a: { | ||
alias: 'alpha', | ||
require: true, | ||
description: 'Description for b' | ||
} | ||
}; | ||
var Tty = require('tty'); | ||
var currentIsAtty = Tty.isatty; | ||
Tty.isatty = function () { | ||
Tty.isatty = currentIsAtty; | ||
return false; | ||
}; | ||
var result = Bossy.usage(definition); | ||
expect(result).to.contain('-a'); | ||
expect(result).to.not.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
it('returns colors usage information when passed as parameter', function (done) { | ||
var definition = { | ||
a: { | ||
alias: 'alpha', | ||
require: true, | ||
description: 'Description for b' | ||
} | ||
}; | ||
var result = Bossy.usage(definition, 'bossy -c', { colors: true }); | ||
expect(result).to.contain('bossy'); | ||
expect(result).to.contain('-a'); | ||
expect(result).to.contain('\u001b[0m'); | ||
done(); | ||
}); | ||
it('formatted usage message orders as -s,--long in first column', function (done) { | ||
var definition = { | ||
a: { | ||
type: 'number', | ||
description: 'This needs a number' | ||
}, | ||
b: { | ||
alias: 'beta', | ||
description: 'Description for b' | ||
}, | ||
code: { | ||
alias: 'c' | ||
}, | ||
d: { | ||
alias: [''] | ||
} | ||
}; | ||
var result = Bossy.usage(definition); | ||
expect(result).to.contain('-a'); | ||
expect(result).to.contain('-b, --beta'); | ||
expect(result).to.contain('-c, --code'); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
26595
621
1
83