Comparing version 2.0.0 to 3.0.0
69
index.js
@@ -7,2 +7,3 @@ 'use strict' | ||
const repeat = require('repeat-string') | ||
const getParams = require('get-params') | ||
const ap = require('ap') | ||
@@ -19,14 +20,7 @@ | ||
command (usage, description, options, action) { | ||
const parts = usage.split(' ') | ||
const name = parts[0] | ||
const args = parts.slice(1).map(function (v) { | ||
return v.substr(1, v.length - 2) | ||
}) | ||
command (name, description, options, action) { | ||
this.commands[name] = { | ||
usage: usage, | ||
description: description, | ||
options: options, | ||
args: args, | ||
action: action | ||
@@ -37,2 +31,13 @@ } | ||
run (callback) { | ||
var context_first = this.context.length > 1 ? this.context[0] : false | ||
var options = this.context[this.context.length - 1] | ||
var command | ||
var results = [] | ||
var cols = [] | ||
var longest = 0 | ||
var usage | ||
var params | ||
var missing | ||
var action | ||
callback = callback || function (err, result) { | ||
@@ -42,13 +47,7 @@ if (err) { | ||
} else { | ||
if (result) { | ||
console.log(result) | ||
if (typeof result === 'string') { | ||
console.log(chalk.green(result)) | ||
} | ||
} | ||
} | ||
var command | ||
var results = [] | ||
var cols = [] | ||
var longest = 0 | ||
var context_first = this.context.length > 1 ? this.context[0] : false | ||
var options = this.context[this.context.length - 1] | ||
@@ -64,3 +63,5 @@ try { | ||
if (options.help) { | ||
results.push(chalk.magenta('Usage:') + ' [options] ' + command.usage) | ||
usage = getParams(command.action).slice(0, -2).map(function (v) { return '<' + v + '>' }).join(' ') | ||
results.push(chalk.magenta('Usage:') + ' [options] ' + context_first + ' ' + usage) | ||
results.push(command.description) | ||
@@ -81,3 +82,2 @@ if (Object.keys(command.options).length) { | ||
if (!command) { | ||
results.push(chalk.magenta('Usage:') + ' [options] <command>') | ||
results.push(this.description) | ||
@@ -89,9 +89,9 @@ if (Object.keys(this.commands).length) { | ||
for (let c in this.commands) { | ||
c = this.commands[c] | ||
usage = '[options] ' + c + ' ' + getParams(this.commands[c].action).map(function (v) { return '<' + v + '>' }).slice(0, -2).join(' ') | ||
if (c.usage.length > longest) { | ||
longest = c.usage.length | ||
if (usage.length > longest) { | ||
longest = usage.length | ||
} | ||
cols.push([c.usage, c.description]) | ||
cols.push([usage, this.commands[c].description]) | ||
} | ||
@@ -112,9 +112,21 @@ } | ||
} else if (command) { | ||
if (this.context.length - 2 !== command.args.length) { | ||
throw new Error('incorrect usage of ' + context_first) | ||
this.context.shift() | ||
action = command.action | ||
params = getParams(action) | ||
if (params.length > 1) { | ||
if (this.context.length < params.length - 1) { | ||
missing = params.slice(this.context.length - 1, -2) | ||
throw new Error('missing argument' + (missing.length > 1 ? 's' : '') + ' (' + missing.join(', ') + ') for ' + context_first) | ||
} | ||
if (this.context.length > params.length - 1) { | ||
throw new Error('too many arguments for ' + context_first) | ||
} | ||
action = ap(this.context, action) | ||
} | ||
this.context.shift() | ||
asyncDone(ap(this.context, command.action), callback) | ||
asyncDone(action, callback) | ||
} | ||
@@ -136,2 +148,3 @@ } catch (error) { | ||
var options = {} | ||
let parts | ||
@@ -143,3 +156,3 @@ argv.forEach(function (val, key) { | ||
if (val) { | ||
let parts = val.split('=') | ||
parts = val.split('=') | ||
@@ -146,0 +159,0 @@ if (val.startsWith('no-')) { |
{ | ||
"name": "sergeant", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -29,2 +29,3 @@ "main": "index.js", | ||
"chalk": "^1.0.0", | ||
"get-params": "^0.1.2", | ||
"repeat-string": "^1.5.2", | ||
@@ -31,0 +32,0 @@ "unquote": "^1.1.0" |
49
test.js
@@ -24,6 +24,22 @@ var assert = require('assert') | ||
it('should run commands that only take a callback', function (done) { | ||
var app = sergeant('a test app', ['test', {}]) | ||
app.command('test', '', {}, function (d) { | ||
d(null, 'ran test') | ||
}) | ||
app.run(function (err, result) { | ||
assert.ifError(err) | ||
assert.equal(result, 'ran test') | ||
done() | ||
}) | ||
}) | ||
it('accept arguments', function (done) { | ||
var app = sergeant('a test app', ['test', 'testing arguments', {}]) | ||
app.command('test <arg>', '', {}, function (arg, options, d) { | ||
app.command('test', '', {}, function (arg, options, d) { | ||
assert.equal(arg, 'testing arguments') | ||
@@ -80,3 +96,3 @@ | ||
app.command('test <arg>', 'test command', {'--option': 'an option'}, function () { }) | ||
app.command('test', 'test command', {'--option': 'an option'}, function (arg, options, d) { }) | ||
@@ -87,6 +103,5 @@ app.run(function (err, result) { | ||
assert.deepEqual(result, [ | ||
chalk.magenta('Usage:') + ' [options] <command>', | ||
'a test app', | ||
chalk.magenta('Commands:'), | ||
' ' + chalk.cyan('test <arg>') + ' test command' | ||
' ' + chalk.cyan('[options] test <arg>') + ' test command' | ||
].join('\n')) | ||
@@ -101,3 +116,3 @@ | ||
app.command('test <arg>', 'test command', {'--option': 'an option'}, function () { }) | ||
app.command('test', 'test command', {'--option': 'an option'}, function (arg, options, d) { }) | ||
@@ -118,6 +133,6 @@ app.run(function (err, result) { | ||
it('errors with the incorrect number of arguments', function (done) { | ||
var app = sergeant('a test app', ['test', {}]) | ||
it('errors with too many arguments', function (done) { | ||
var app = sergeant('a test app', ['test', '1', '2', {}]) | ||
app.command('test <arg>', 'test command', {'--option': 'an option'}, function (arg, options, d) { | ||
app.command('test', 'test command', {'--option': 'an option'}, function (arg, options, d) { | ||
d(new Error('nothing bad happened')) | ||
@@ -127,3 +142,3 @@ }) | ||
app.run(function (err) { | ||
assert.equal(err.message, 'incorrect usage of test') | ||
assert.equal(err.message, 'too many arguments for test') | ||
@@ -134,6 +149,20 @@ done() | ||
it('errors with too few arguments', function (done) { | ||
var app = sergeant('a test app', ['test', '1', {}]) | ||
app.command('test', 'test command', {'--option': 'an option'}, function (arg1, arg2, options, d) { | ||
d(new Error('nothing bad happened')) | ||
}) | ||
app.run(function (err) { | ||
assert.equal(err.message, 'missing argument (arg2) for test') | ||
done() | ||
}) | ||
}) | ||
it('gathers errors from commands', function (done) { | ||
var app = sergeant('a test app', ['test', 'testing', {}]) | ||
app.command('test <arg>', 'test command', {'--option': 'an option'}, function (arg, options, d) { | ||
app.command('test', 'test command', {'--option': 'an option'}, function (arg, options, d) { | ||
d(new Error('nothing bad happened')) | ||
@@ -140,0 +169,0 @@ }) |
10970
274
6
+ Addedget-params@^0.1.2
+ Addedget-params@0.1.2(transitive)