Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Lambdas
- {
- class CommandNotFoundException : Exception
- {
- }
- internal class Repl
- {
- readonly Dictionary<string, Action> commands = new Dictionary<string, Action>
- {
- {"1", () => Console.WriteLine("Command 1 executed") },
- {"2", () => Console.WriteLine("Command 2 executed") },
- {"3", () =>
- {
- var d = new Dictionary<string, string>();
- Console.WriteLine(d["doron"]);
- }
- },
- };
- public Repl()
- {
- }
- internal void Run()
- {
- while (true)
- {
- try
- {
- var commandName = InputCommand();
- var command = FindCommand(commandName);
- command();
- }
- catch (CommandNotFoundException)
- {
- Console.WriteLine("Error: Bad command name");
- }
- catch (Exception e)
- {
- Console.WriteLine($"Error: {e.Message}");
- }
- }
- }
- private Action FindCommand(string commandName)
- {
- // commands[commandName];
- Action command;
- if (commands.TryGetValue(commandName, out command))
- {
- return command;
- }
- else
- {
- throw new CommandNotFoundException();
- }
- }
- private string InputCommand()
- {
- Console.Write("> ");
- return Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement