Last active
December 13, 2015 18:39
-
-
Save yetanotherchris/4957373 to your computer and use it in GitHub Desktop.
CommandOptions usage example
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
static void Main(string[] args) | |
{ | |
string input = ""; | |
MyHandler handler = new MyHandler(); | |
CommandOptions options = new CommandOptions(); | |
options.Add("show", handler.Show) | |
.Add("name", v => handler.Name(v)) | |
.Add("age", v => handler.Age(v)) | |
while (input != "quit" && input != "exit") | |
{ | |
if (input == "cls" || input == "clear") | |
{ | |
System.Console.Clear(); | |
} | |
else | |
{ | |
if (!string.IsNullOrEmpty(input)) | |
{ | |
if (options.Parse(input)) | |
{ | |
System.Console.WriteLine(handler.OutputMessage); | |
} | |
else | |
{ | |
System.Console.WriteLine("I didn't understand that command"); | |
} | |
} | |
} | |
System.Console.Write(">"); | |
input = System.Console.ReadLine(); | |
} | |
} | |
public class MyHandler | |
{ | |
public string OutputMessage { get; set; } | |
public void Show() | |
{ | |
OutputMessage = "Do something with the Show command, list SQL tables for example"; | |
} | |
public void Name(string[] args) | |
{ | |
OutputMessage = string.Format("{0} is your name",args[0])); | |
} | |
public void Age(string[] args) | |
{ | |
OutputMessage = string.Format("{0} is your age",args[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment