Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Lambdas
  5. {
  6. class CommandNotFoundException : Exception
  7. {
  8.  
  9. }
  10.  
  11. internal class Repl
  12. {
  13. readonly Dictionary<string, Action> commands = new Dictionary<string, Action>
  14. {
  15. {"1", () => Console.WriteLine("Command 1 executed") },
  16. {"2", () => Console.WriteLine("Command 2 executed") },
  17. {"3", () =>
  18. {
  19. var d = new Dictionary<string, string>();
  20. Console.WriteLine(d["doron"]);
  21. }
  22. },
  23. };
  24.  
  25. public Repl()
  26. {
  27. }
  28.  
  29. internal void Run()
  30. {
  31. while (true)
  32. {
  33. try
  34. {
  35. var commandName = InputCommand();
  36. var command = FindCommand(commandName);
  37. command();
  38. }
  39. catch (CommandNotFoundException)
  40. {
  41. Console.WriteLine("Error: Bad command name");
  42. }
  43. catch (Exception e)
  44. {
  45. Console.WriteLine($"Error: {e.Message}");
  46. }
  47. }
  48. }
  49.  
  50. private Action FindCommand(string commandName)
  51. {
  52. // commands[commandName];
  53. Action command;
  54. if (commands.TryGetValue(commandName, out command))
  55. {
  56. return command;
  57. }
  58. else
  59. {
  60. throw new CommandNotFoundException();
  61. }
  62. }
  63.  
  64. private string InputCommand()
  65. {
  66. Console.Write("> ");
  67. return Console.ReadLine();
  68. }
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement