Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial support for command aliases #647

Merged
merged 7 commits into from Oct 10, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions lib/command.js
Expand Up @@ -8,27 +8,35 @@ module.exports = function (yargs, usage, validation) {
const self = {}

var handlers = {}
var aliasMap = {}
self.addHandler = function (cmd, description, builder, handler) {
if (typeof cmd === 'object') {
const commandString = typeof cmd.command === 'string' ? cmd.command : moduleName(cmd)
var aliases = []
if (Array.isArray(cmd)) {
aliases = cmd.slice(1)
cmd = cmd[0]
} else if (typeof cmd === 'object') {
const commandString = (Array.isArray(cmd.command) || typeof cmd.command === 'string') ? cmd.command : moduleName(cmd)
self.addHandler(commandString, extractDesc(cmd), cmd.builder, cmd.handler)
return
}

// allow a module to be provided instead of separate builder and handler
if (typeof builder === 'object' && builder.builder && typeof builder.handler === 'function') {
self.addHandler(cmd, description, builder.builder, builder.handler)
self.addHandler([cmd].concat(aliases), description, builder.builder, builder.handler)
return
}

var parsedCommand = parseCommand(cmd)
aliases = aliases.map(function (alias) {
alias = parseCommand(alias).cmd // remove positional args
aliasMap[alias] = parsedCommand.cmd
return alias
})

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

// we should not register a handler if no
// builder is provided, e.g., user will
// handle command themselves with '_'.
var parsedCommand = parseCommand(cmd)
handlers[parsedCommand.cmd] = {
original: cmd,
handler: handler,
Expand Down Expand Up @@ -112,7 +120,7 @@ module.exports = function (yargs, usage, validation) {
}

self.getCommands = function () {
return Object.keys(handlers)
return Object.keys(handlers).concat(Object.keys(aliasMap))
}

self.getCommandHandlers = function () {
Expand All @@ -121,7 +129,7 @@ module.exports = function (yargs, usage, validation) {

self.runCommand = function (command, yargs, parsed) {
var argv = parsed.argv
var commandHandler = handlers[command]
var commandHandler = handlers[command] || handlers[aliasMap[command]]
var innerArgv = argv
var currentContext = yargs.getContext()
var parentCommands = currentContext.commands.slice()
Expand Down Expand Up @@ -204,6 +212,7 @@ module.exports = function (yargs, usage, validation) {

self.reset = function () {
handlers = {}
aliasMap = {}
return self
}

Expand Down
11 changes: 8 additions & 3 deletions lib/usage.js
Expand Up @@ -70,8 +70,8 @@ module.exports = function (yargs, y18n) {
}

var commands = []
self.command = function (cmd, description) {
commands.push([cmd, description || ''])
self.command = function (cmd, description, aliases) {
commands.push([cmd, description || '', aliases])
}
self.getCommands = function () {
return commands
Expand Down Expand Up @@ -152,10 +152,15 @@ module.exports = function (yargs, y18n) {
ui.div(__('Commands:'))

commands.forEach(function (command) {
ui.div(
ui.span(
{text: command[0], padding: [0, 2, 0, 2], width: maxWidth(commands, theWrap) + 4},
{text: command[1]}
)
if (command[2] && command[2].length) {
ui.div({text: '[' + __('aliases:') + ' ' + command[2].join(', ') + ']', padding: [0, 0, 0, 2], align: 'right'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this implementation of aliases 👍 it's nice to see cliui being dusted off.

} else {
ui.div()
}
})

ui.div()
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Expand Up @@ -10,6 +10,7 @@
"required": "required",
"default:": "default:",
"choices:": "choices:",
"aliases:": "aliases:",
"generated-value": "generated-value",
"Not enough non-option arguments: got %s, need at least %s": "Not enough non-option arguments: got %s, need at least %s",
"Too many non-option arguments: got %s, maximum of %s": "Too many non-option arguments: got %s, maximum of %s",
Expand Down
18 changes: 12 additions & 6 deletions test/command.js
Expand Up @@ -143,10 +143,11 @@ describe('Command', function () {
it('accepts string, string as first 2 arguments', function () {
var cmd = 'foo'
var desc = 'i\'m not feeling very creative at the moment'
var aliases = []

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

it('accepts string, boolean as first 2 arguments', function () {
Expand Down Expand Up @@ -221,14 +222,15 @@ describe('Command', function () {
builder: function (yargs) { return yargs },
handler: function (argv) {}
}
var aliases = []

var y = yargs([]).command(module)
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe])
commands[0].should.deep.equal([module.command, module.describe, aliases])
})

it('accepts module (description key, builder function) as 1st argument', function () {
Expand All @@ -238,14 +240,15 @@ describe('Command', function () {
builder: function (yargs) { return yargs },
handler: function (argv) {}
}
var aliases = []

var y = yargs([]).command(module)
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.description])
commands[0].should.deep.equal([module.command, module.description, aliases])
})

it('accepts module (desc key, builder function) as 1st argument', function () {
Expand All @@ -255,14 +258,15 @@ describe('Command', function () {
builder: function (yargs) { return yargs },
handler: function (argv) {}
}
var aliases = []

var y = yargs([]).command(module)
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.desc])
commands[0].should.deep.equal([module.command, module.desc, aliases])
})

it('accepts module (false describe, builder function) as 1st argument', function () {
Expand Down Expand Up @@ -309,14 +313,15 @@ describe('Command', function () {
},
handler: function (argv) {}
}
var aliases = []

var y = yargs([]).command(module)
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
handlers.foo.handler.should.equal(module.handler)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe])
commands[0].should.deep.equal([module.command, module.describe, aliases])
})

it('accepts module (missing handler function) as 1st argument', function () {
Expand All @@ -329,14 +334,15 @@ describe('Command', function () {
}
}
}
var aliases = []

var y = yargs([]).command(module)
var handlers = y.getCommandInstance().getCommandHandlers()
handlers.foo.original.should.equal(module.command)
handlers.foo.builder.should.equal(module.builder)
expect(handlers.foo.handler).to.equal(undefined)
var commands = y.getUsageInstance().getCommands()
commands[0].should.deep.equal([module.command, module.describe])
commands[0].should.deep.equal([module.command, module.describe, aliases])
})
})

Expand Down