Skip to content

Instantly share code, notes, and snippets.

@tzuntar
Last active March 1, 2021 14:20
Show Gist options
  • Save tzuntar/5e79a76458cd956354460b07104e207a to your computer and use it in GitHub Desktop.
Save tzuntar/5e79a76458cd956354460b07104e207a to your computer and use it in GitHub Desktop.
A simple command line interface
namespace TestApp
{
public interface ICommand
{
void Execute();
}
}
using System;
namespace TestApp
{
public class Ls : ICommand
{
public void Execute()
{
Console.WriteLine("file1\tfile2\tfile3");
}
}
}
using System;
using System.Collections.Generic;
namespace TestApp
{
internal class Program
{
private static Dictionary<string, ICommand> commands = new Dictionary<string, ICommand> {
{"ls", new Ls()}, {"cd", null}, {"pwd", null} // replace the nulls with command classes
};
public static void Main()
{
string command;
while (true) {
command = Console.ReadLine();
if (command.Equals("exit")) break;
if (!commands.ContainsKey(command))
Console.WriteLine("Invalid command");
else commands[command].Execute();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment