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

Aliases for commands #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}

Expand Down
33 changes: 30 additions & 3 deletions lib/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
Expand Down Expand Up @@ -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.");
}
}
}

/**
Expand All @@ -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)
Expand Down