Last active
May 18, 2016 23:47
-
-
Save dmitrymatveev/2b81ff49c022f094371c88d7afb482af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Text.RegularExpressions; | |
namespace DM.Cli | |
{ | |
public interface ICliDictionary | |
{ | |
void Command(string line); | |
} | |
/// <summary> | |
/// Command Line Interface Dictionary. | |
/// | |
/// Parses a string and invokes a corresponding Action. | |
/// Command format: '/command [body]' | |
/// </summary> | |
class CliDictionary | |
: Dictionary<string, System.Action<string>>, ICliDictionary | |
{ | |
// Matches a command string ("/somecommand [command text input]") | |
private Regex commandRegex = new Regex(@"\/(\w+)\s?"); | |
public void Command(string line) | |
{ | |
string[] substrings = commandRegex.Split(line); | |
System.Action<string> action; | |
if (substrings.Length > 1 && TryGetValue(substrings[1], out action)) | |
{ | |
string body = substrings[2]; | |
action(body.Length > 0 ? body : ""); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment