Last active
March 1, 2021 14:20
-
-
Save tzuntar/5e79a76458cd956354460b07104e207a to your computer and use it in GitHub Desktop.
A simple command line interface
This file contains hidden or 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
namespace TestApp | |
{ | |
public interface ICommand | |
{ | |
void Execute(); | |
} | |
} |
This file contains hidden or 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; | |
namespace TestApp | |
{ | |
public class Ls : ICommand | |
{ | |
public void Execute() | |
{ | |
Console.WriteLine("file1\tfile2\tfile3"); | |
} | |
} | |
} |
This file contains hidden or 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; | |
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