Skip to content

Commit

Permalink
Added command aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jun 1, 2018
1 parent ee2b09c commit 46b181c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Console/Command.cs
Expand Up @@ -15,6 +15,11 @@ public abstract class Command
/// </summary>
public abstract string Name { get; }

/// <summary>
/// Alternative names for the command.
/// </summary>
public virtual string[] Aliases { get; } = null;

/// <summary>
/// How to use the command (e.g. $"{Name} argument [optional_argument]")
/// </summary>
Expand Down
10 changes: 7 additions & 3 deletions src/Console/Shell.cs
Expand Up @@ -83,7 +83,11 @@ internal static bool RegisterCommandInner(Command command, Mod mod)
if (command.Mod == null)
command.Mod = mod;

if (Registry.ContainsKey(command.Name))
bool duplicateAlias = command.Aliases != null &&
Registry.Where(o => o.Value.Aliases != null)
.Any(o => o.Value.Aliases.Any(a => command.Aliases.Contains(a)));

if (Registry.ContainsKey(command.Name) || duplicateAlias)
return false;

Registry.Add(command.Name, command);
Expand Down Expand Up @@ -143,9 +147,9 @@ internal static void ExecuteCommand(string cmd)

args = ReplaceVariables(args).ToArray();

Command command;
Command command = Registry.Values.SingleOrDefault(o => o.Aliases != null && o.Aliases.Contains(verb));

if (Registry.TryGetValue(verb, out command))
if (command != null || Registry.TryGetValue(verb, out command))
{
try
{
Expand Down

0 comments on commit 46b181c

Please sign in to comment.