Comparing version 23.0.0 to 23.1.0
114
help.js
@@ -5,2 +5,59 @@ const chalk = require('chalk') | ||
const getUsage = (title, { options, parameters }) => { | ||
let usage = [title] | ||
if (options && options.length) { | ||
usage = usage.concat(options.map((definition) => { | ||
const valPart = definition.type != null | ||
? ' <' + definition.key + '>' | ||
: '' | ||
return wrapUsage(addDashes(definition.alias != null ? definition.alias : definition.key) + valPart, definition) | ||
})) | ||
} | ||
if (parameters && parameters.length) { | ||
usage = usage.concat(parameters.map((definition) => { | ||
return wrapUsage('<' + definition.key + '>', definition) | ||
})) | ||
} | ||
return usage.join(' ') | ||
} | ||
const wrapUsage = (usage, { required, multiple }) => { | ||
const opt = usage.startsWith('-') | ||
return (required !== true ? '[' : (opt ? '(' : '')) + usage + (required !== true ? ']' : (opt ? ')' : '')) + (multiple === true ? '...' : '') | ||
} | ||
const getOptionSignature = (definition) => { | ||
const val = definition.type != null | ||
? ' <' + definition.key + '>' | ||
: '' | ||
let signature = addDashes(definition.key) + val | ||
if (definition.alias != null) { | ||
signature = addDashes(definition.alias) + val + ', ' + signature | ||
} | ||
return signature | ||
} | ||
const commandList = (title, commands) => { | ||
for (const command of commands) { | ||
console.error('') | ||
console.error(getUsage(title + ' ' + command.title, command)) | ||
if (command.description) { | ||
console.error('') | ||
console.error(' ' + command.description) | ||
} | ||
commandList(title + ' ' + command.title, command.commands) | ||
} | ||
} | ||
module.exports = (title, description, { options, parameters, commands }) => { | ||
@@ -93,58 +150,1 @@ process.exitCode = 1 | ||
} | ||
function getUsage (title, { options, parameters }) { | ||
let usage = [title] | ||
if (options && options.length) { | ||
usage = usage.concat(options.map((definition) => { | ||
const valPart = definition.type != null | ||
? ' <' + definition.key + '>' | ||
: '' | ||
return wrapUsage(addDashes(definition.alias != null ? definition.alias : definition.key) + valPart, definition) | ||
})) | ||
} | ||
if (parameters && parameters.length) { | ||
usage = usage.concat(parameters.map((definition) => { | ||
return wrapUsage('<' + definition.key + '>', definition) | ||
})) | ||
} | ||
return usage.join(' ') | ||
} | ||
function wrapUsage (usage, { required, multiple }) { | ||
const opt = usage.startsWith('-') | ||
return (required !== true ? '[' : (opt ? '(' : '')) + usage + (required !== true ? ']' : (opt ? ')' : '')) + (multiple === true ? '...' : '') | ||
} | ||
function getOptionSignature (definition) { | ||
const val = definition.type != null | ||
? ' <' + definition.key + '>' | ||
: '' | ||
let signature = addDashes(definition.key) + val | ||
if (definition.alias != null) { | ||
signature = addDashes(definition.alias) + val + ', ' + signature | ||
} | ||
return signature | ||
} | ||
function commandList (title, commands) { | ||
for (const command of commands) { | ||
console.error('') | ||
console.error(getUsage(title + ' ' + command.title, command)) | ||
if (command.description) { | ||
console.error('') | ||
console.error(' ' + command.description) | ||
} | ||
commandList(title + ' ' + command.title, command.commands) | ||
} | ||
} |
30
index.js
@@ -6,3 +6,3 @@ const parse = require('./parse') | ||
module.exports = function sergeant (path, description, define) { | ||
const sergeant = (path, description, define) => { | ||
const title = path.split(' ').reverse()[0] | ||
@@ -16,3 +16,3 @@ | ||
function cli (argv) { | ||
const cli = (argv) => { | ||
const filtered = argv.filter((arg) => arg !== '-' && !arg.startsWith('-')) | ||
@@ -56,12 +56,3 @@ const command = cli.commands.find((command) => command.title === filtered[0]) | ||
const action = define({ option, parameter, command }) | ||
option('help', { | ||
alias: 'h', | ||
description: 'get help' | ||
}) | ||
return cli | ||
function command (subtitle, description, define) { | ||
const command = (subtitle, description, define) => { | ||
if (define == null) { | ||
@@ -76,3 +67,3 @@ define = description | ||
function option (key, definition) { | ||
const option = (key, definition) => { | ||
const current = Object.assign(definition, { key }) | ||
@@ -83,3 +74,3 @@ | ||
function parameter (key, definition) { | ||
const parameter = (key, definition) => { | ||
const current = Object.assign(definition, { key }) | ||
@@ -89,2 +80,13 @@ | ||
} | ||
const action = define({ option, parameter, command }) | ||
option('help', { | ||
alias: 'h', | ||
description: 'get help' | ||
}) | ||
return cli | ||
} | ||
module.exports = sergeant |
{ | ||
"name": "sergeant", | ||
"version": "23.0.0", | ||
"version": "23.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,8 +0,6 @@ | ||
module.exports = { addDashes, longest, spaces, camelCaseFromDash } | ||
function addDashes (key) { | ||
const addDashes = (key) => { | ||
return (key.length === 1 ? '-' : '--') + key | ||
} | ||
function longest (arr) { | ||
const longest = (arr) => { | ||
return arr.reduce((longest, item) => { | ||
@@ -13,7 +11,7 @@ return item.length > longest ? item.length : longest | ||
function spaces (number) { | ||
const spaces = (number) => { | ||
return ' '.repeat(number) | ||
} | ||
function camelCaseFromDash (key) { | ||
const camelCaseFromDash = (key) => { | ||
const split = key.split('-').filter((part) => part !== '') | ||
@@ -24,1 +22,3 @@ const property = split[0] + split.slice(1).map((part) => part.substr(0, 1).toUpperCase() + part.substr(1)).join('') | ||
} | ||
module.exports = { addDashes, longest, spaces, camelCaseFromDash } |
66
test.js
@@ -204,3 +204,3 @@ const test = require('tape') | ||
function FN_DEFAULT (val) { | ||
const FN_DEFAULT = (val) => { | ||
if (val == null) { | ||
@@ -571,9 +571,3 @@ return DEFAULT | ||
test('test ./command - no help. no errors', (t) => { | ||
const command = proxyquire('./', { | ||
'./parse': mockedParse | ||
}) | ||
t.plan(3) | ||
function mockedParse (argv, definitions) { | ||
const mockedParse = (argv, definitions) => { | ||
t.deepEquals(['testing'], argv) | ||
@@ -602,2 +596,8 @@ | ||
const command = proxyquire('./', { | ||
'./parse': mockedParse | ||
}) | ||
t.plan(3) | ||
const testCommand = command('test-command', ({ option, parameter }) => { | ||
@@ -621,10 +621,3 @@ parameter('aaa', { | ||
test('test ./command - help', (t) => { | ||
const command = proxyquire('./', { | ||
'./parse': mockedParse, | ||
'./help': mockedHelp | ||
}) | ||
t.plan(2) | ||
function mockedParse (argv, definitions) { | ||
const mockedParse = (argv, definitions) => { | ||
return { | ||
@@ -635,3 +628,3 @@ help: true | ||
function mockedHelp (name, description, definitions) { | ||
const mockedHelp = (name, description, definitions) => { | ||
t.equals('test-command', name) | ||
@@ -659,2 +652,9 @@ | ||
const command = proxyquire('./', { | ||
'./parse': mockedParse, | ||
'./help': mockedHelp | ||
}) | ||
t.plan(2) | ||
const testCommand = command('test-command', ({ parameter, option }) => { | ||
@@ -676,2 +676,10 @@ parameter('aaa', { | ||
test('test ./command - thrown error', (t) => { | ||
const mockedParse = () => { | ||
return {} | ||
} | ||
const mockedError = (error) => { | ||
t.deepEquals(ourError, error) | ||
} | ||
const command = proxyquire('./', { | ||
@@ -686,10 +694,2 @@ './parse': mockedParse, | ||
function mockedParse () { | ||
return {} | ||
} | ||
function mockedError (error) { | ||
t.deepEquals(ourError, error) | ||
} | ||
const testCommand = command('test-command', () => () => { | ||
@@ -703,2 +703,10 @@ throw ourError | ||
test('test ./command - rejected promise', (t) => { | ||
const mockedParse = () => { | ||
return {} | ||
} | ||
const mockedError = (error) => { | ||
t.deepEquals(ourError, error) | ||
} | ||
const command = proxyquire('./', { | ||
@@ -713,10 +721,2 @@ './parse': mockedParse, | ||
function mockedParse () { | ||
return {} | ||
} | ||
function mockedError (error) { | ||
t.deepEquals(ourError, error) | ||
} | ||
const testCommand = command('test-command', () => async () => { | ||
@@ -723,0 +723,0 @@ throw ourError |
29323
987