Skip to content

Instantly share code, notes, and snippets.

@dmitrymatveev
Last active May 18, 2016 23:47
Show Gist options
  • Save dmitrymatveev/2b81ff49c022f094371c88d7afb482af to your computer and use it in GitHub Desktop.
Save dmitrymatveev/2b81ff49c022f094371c88d7afb482af to your computer and use it in GitHub Desktop.
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