-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
119 lines (97 loc) · 3.74 KB
/
Program.cs
File metadata and controls
119 lines (97 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestConsole
{
class Program
{
static Dictionary<string, ReplCommand> _commands = new Dictionary<string, ReplCommand>(StringComparer.CurrentCultureIgnoreCase);
static void Main(string[] args)
{
// Create Commands
PopulateCommands();
// Run continuously until "quit" is entered
while (true)
{
// Ask the user to enter their command
Console.WriteLine("Please input your command and hit enter");
// Capture the input
string sInput = Console.ReadLine();
// Search the input from within the commands
if (_commands.TryGetValue(sInput, out ReplCommand c))
{
// Found the command. Let's execute it
c.MethodToCall(sInput);
}
else
{
// Command was not found, trigger the help text
PrintHelp(sInput);
}
}
}
static void MyCommand(string input)
{
Console.WriteLine($"MyCommand has been executed by the input '{input}'");
}
static void PrintHelp(string input)
{
// Unless the input that got us here is 'help', display the (wrong) command that was
// entered that got us here
if (input?.ToLowerInvariant() != "help")
{
// Display the wrong command
Console.WriteLine($"Command '{input}' not recognized. See below for valid commands");
}
// Loop through each command from a list sorted by the HelpSortOrder
foreach (ReplCommand c in _commands.Values.OrderBy(o => o.HelpSortOrder))
{
// Print the command and its associated HelpText
Console.WriteLine($"{c.Command}:\t{c.HelpText}");
}
}
static void Quit(string input)
{
System.Environment.Exit(0);
}
static void PopulateCommands()
{
// Add your commands here
AddCommand(new ReplCommand
{
Command = "MyCommand", // The command that the user will enter (case insensitive)
HelpText = "This is the help text of my command", // Help text
MethodToCall = MyCommand, // The actual method that we will trigger
HelpSortOrder = 1 // The order in which the command will be displayed in the help
});
// Default Commands
AddCommand(new ReplCommand
{
Command = "help",
HelpText = "Prints usage information",
MethodToCall = PrintHelp,
HelpSortOrder = 100
});
AddCommand(new ReplCommand
{
Command = "quit",
HelpText = "Terminates the console application",
MethodToCall = Quit,
HelpSortOrder = 101
});
}
static void AddCommand(ReplCommand replCommand)
{
// Add the command into the dictionary to be looked up later
_commands.Add(replCommand.Command, replCommand);
}
}
// A class we will use to capture the REPL Command information
class ReplCommand
{
public string Command { get; set; }
public string HelpText { get; set; }
public Action<string> MethodToCall { get; set; }
public int HelpSortOrder { get; set; }
}
}