diff --git a/lib/App.js b/lib/App.js index cff84d2..0dbac50 100644 --- a/lib/App.js +++ b/lib/App.js @@ -226,6 +226,23 @@ class App { if (validCommandName === validUserInput) { cmd = command; break; + } else { + // Check whether the command's alias has been entered instead + let abort = false; + for (let alias in command.aliases) { + let aliasName = command.aliases[alias]; + let validAName = command.caseSensitive ? aliasName : aliasName.toLowerCase(); + + if (validUserInput === validAName) { + cmd = command; + abort = true; + break; + } + + if (abort) { + break; + } + } } } diff --git a/lib/Command.js b/lib/Command.js index 50fa745..a0f2bdd 100644 --- a/lib/Command.js +++ b/lib/Command.js @@ -100,8 +100,11 @@ class Command { ( options.async && typeof options.async !== "boolean" - ) // async is not required - + ) || // async is not required + ( + options.aliases && + !Array.isArray(options.aliases) // aliases is not required + ) ) { throw new Error("Wrong parameters passed when creating command " + options.name + @@ -146,6 +149,17 @@ class Command { } } + + this.aliases = {}; + options.aliases = options.aliases || []; + for (let i = 0; i < options.aliases.length; i++) { + + if (typeof options.aliases[i] === "string") { + this.aliases[i] = options.aliases[i]; + } else { + throw new Error("One of the items in the aliases array is not a string."); + } + } } /** @@ -158,7 +172,20 @@ class Command { _getHelp(app) { const LINE_WIDTH = 100; - let r = str.help_usage + " " + app.prefix + " " + this.name; + let r = str.help_usage + " " + app.prefix + " " + this.name + "\n"; + + // Add aliases + if (this.aliases.length > 0) { + r += "Aliases for this command: "; + for (let i in this.aliases) { + r += this.aliases[i]; + if ((i + 1) < this.aliases.length) { + r += ","; + } + } + r += "\n"; + } + let args_table; // Add every argument to the usage (Only if there are arguments)