Skip to content

Commit

Permalink
refactor: usage.command() to take {…object} param instead of […array]
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex committed Mar 9, 2020
1 parent 188ec7e commit 9717c20
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = function command (yargs, usage, validation, globalMiddleware) {
})

if (description !== false) {
usage.command(cmd, description, isDefault, aliases)
usage.command({ cmd, description, isDefault, aliases })
}

handlers[parsedCommand.cmd] = {
Expand Down
4 changes: 2 additions & 2 deletions lib/completion.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ module.exports = function completion (yargs, usage, command) {

if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
usage.getCommands().forEach((usageCommand) => {
const commandName = command.parseCommand(usageCommand[0]).cmd
const commandName = command.parseCommand(usageCommand.cmd).cmd
if (args.indexOf(commandName) === -1) {
if (!zshShell) {
completions.push(commandName)
} else {
const desc = usageCommand[1] || ''
const desc = usageCommand.description || ''
completions.push(commandName.replace(/:/g, '\\:') + ':' + desc)
}
}
Expand Down
27 changes: 14 additions & 13 deletions lib/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ module.exports = function usage (yargs, y18n) {
}

let commands = []
self.command = function command (cmd, description, isDefault, aliases) {
self.command = function command (command) {
// the last default wins, so cancel out any previously set default
if (isDefault) {
commands = commands.map((cmdArray) => {
cmdArray[2] = false
return cmdArray
if (command.isDefault) {
commands = commands.map((cmdObject) => {
cmdObject.isDefault = false
return cmdObject
})
}
commands.push([cmd, description || '', isDefault, aliases])
commands.push({ ...command, description: command.description || '' })
}
self.getCommands = () => commands

Expand Down Expand Up @@ -208,23 +208,23 @@ module.exports = function usage (yargs, y18n) {
const parentCommands = context.commands.length ? `${context.commands.join(' ')} ` : ''

if (yargs.getParserConfiguration()['sort-commands'] === true) {
commands = commands.sort((a, b) => a[0].localeCompare(b[0]))
commands = commands.sort((a, b) => a.cmd.localeCompare(b.cmd))
}

commands.forEach((command) => {
const commandString = `${base$0} ${parentCommands}${command[0].replace(/^\$0 ?/, '')}` // drop $0 from default commands.
const commandString = `${base$0} ${parentCommands}${command.cmd.replace(/^\$0 ?/, '')}` // drop $0 from default commands.
ui.span(
{
text: commandString,
padding: [0, 2, 0, 2],
width: maxWidth(commands, theWrap, `${base$0}${parentCommands}`) + 4
},
{ text: command[1] }
{ text: command.description }
)
const hints = []
if (command[2]) hints.push(`[${__('default')}]`)
if (command[3] && command[3].length) {
hints.push(`[${__('aliases:')} ${command[3].join(', ')}]`)
if (command.isDefault) hints.push(`[${__('default')}]`)
if (command.aliases && command.aliases.length) {
hints.push(`[${__('aliases:')} ${command.aliases.join(', ')}]`)
}
if (hints.length) {
ui.div({ text: hints.join(' '), padding: [0, 0, 0, 2], align: 'right' })
Expand Down Expand Up @@ -381,8 +381,9 @@ module.exports = function usage (yargs, y18n) {
}

table.forEach((v) => {
const cmd = v.cmd || v[0]
width = Math.max(
stringWidth(modifier ? `${modifier} ${v[0]}` : v[0]),
stringWidth(modifier ? `${modifier} ${cmd}` : cmd),
width
)
})
Expand Down
30 changes: 15 additions & 15 deletions test/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,24 @@ describe('Command', () => {
describe('API', () => {
it('accepts string, string as first 2 arguments', () => {
const cmd = 'foo'
const desc = 'i\'m not feeling very creative at the moment'
const description = 'i\'m not feeling very creative at the moment'
const isDefault = false
const aliases = []

const y = yargs([]).command(cmd, desc)
const y = yargs([]).command(cmd, description)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([cmd, desc, isDefault, aliases])
commands[0].should.deep.equal({ cmd, description, isDefault, aliases })
})

it('accepts array, string as first 2 arguments', () => {
const aliases = ['bar', 'baz']
const cmd = 'foo <qux>'
const desc = 'i\'m not feeling very creative at the moment'
const description = 'i\'m not feeling very creative at the moment'
const isDefault = false

const y = yargs([]).command([cmd].concat(aliases), desc)
const y = yargs([]).command([cmd].concat(aliases), description)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([cmd, desc, isDefault, aliases])
usageCommands[0].should.deep.equal({ cmd, description, isDefault, aliases })
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
Expand Down Expand Up @@ -388,7 +388,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
commands[0].should.deep.equal({ cmd: module.command, description: module.describe, isDefault, aliases })
})

it('accepts module (description key, builder function) as 1st argument', () => {
Expand All @@ -407,7 +407,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.description, isDefault, aliases])
commands[0].should.deep.equal({ cmd: module.command, description: module.description, isDefault, aliases })
})

it('accepts module (desc key, builder function) as 1st argument', () => {
Expand All @@ -426,7 +426,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.desc, isDefault, aliases])
commands[0].should.deep.equal({ cmd: module.command, description: module.desc, isDefault, aliases })
})

it('accepts module (false describe, builder function) as 1st argument', () => {
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
commands[0].should.deep.equal({ cmd: module.command, description: module.describe, isDefault, aliases })
})

it('accepts module (missing handler function) as 1st argument', () => {
Expand All @@ -504,7 +504,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
expect(typeof handlers.foo.handler).to.equal('function')
const commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe, isDefault, aliases])
commands[0].should.deep.equal({ cmd: module.command, description: module.describe, isDefault, aliases })
})

it('accepts module (with command array) as 1st argument', () => {
Expand All @@ -522,7 +522,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command[0], module.describe, isDefault, ['bar', 'baz']])
usageCommands[0].should.deep.equal({ cmd: module.command[0], description: module.describe, isDefault, aliases: ['bar', 'baz'] })
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
Expand All @@ -543,7 +543,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command, module.describe, isDefault, module.aliases])
usageCommands[0].should.deep.equal({ cmd: module.command, description: module.describe, isDefault, aliases: module.aliases })
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz'])
})
Expand All @@ -564,7 +564,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command[0], module.describe, isDefault, ['bar', 'baz', 'nat']])
usageCommands[0].should.deep.equal({ cmd: module.command[0], description: module.describe, isDefault, aliases: ['bar', 'baz', 'nat'] })
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar', 'baz', 'nat'])
})
Expand All @@ -585,7 +585,7 @@ describe('Command', () => {
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
const usageCommands = y.getUsageInstance().getCommands()
usageCommands[0].should.deep.equal([module.command, module.describe, isDefault, ['bar']])
usageCommands[0].should.deep.equal({ cmd: module.command, description: module.describe, isDefault, aliases: ['bar'] })
const cmdCommands = y.getCommandInstance().getCommands()
cmdCommands.should.deep.equal(['foo', 'bar'])
})
Expand Down

0 comments on commit 9717c20

Please sign in to comment.