forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliasCommand.cs
More file actions
70 lines (55 loc) · 2.01 KB
/
AliasCommand.cs
File metadata and controls
70 lines (55 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System.Linq;
using ScriptCs.Contracts;
namespace ScriptCs.ReplCommands
{
using System;
using System.Globalization;
public class AliasCommand : IReplCommand
{
private readonly IConsole _console;
public AliasCommand(IConsole console)
{
Guard.AgainstNullArgument("console", console);
_console = console;
}
public string Description
{
get { return "Allows you to alias a command with a custom name"; }
}
public string CommandName
{
get { return "alias"; }
}
public object Execute(IRepl repl, object[] args)
{
Guard.AgainstNullArgument("repl", repl);
if (args == null || args.Length != 2)
{
_console.WriteLine("You must specifiy the command name and alias, e.g. :alias \"clear\" \"cls\"");
return null;
}
var commandName = args[0].ToString();
var alias = args[1].ToString();
if (repl.Commands.Any(x => string.Equals(x.Key, alias, StringComparison.InvariantCultureIgnoreCase)))
{
var message = string.Format(
CultureInfo.InvariantCulture,
"\"{0}\" cannot be used as an alias since it is the name of an existing command.",
alias);
_console.WriteLine(message);
return null;
}
IReplCommand command;
if (!repl.Commands.TryGetValue(commandName, out command))
{
var message = string.Format(
CultureInfo.InvariantCulture, "There is no command named or aliased \"{0}\".", alias);
_console.WriteLine(message);
return null;
}
repl.Commands[alias] = command;
_console.WriteLine(string.Format("Aliased \"{0}\" as \"{1}\".", commandName, alias));
return null;
}
}
}