/112d96a1efdbc92179
Created 6 months ago...
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 |
using CommandLine;
namespace Builtins
{
// Echo command
class EchoCommand : Command
{
public EchoCommand()
: base("echo", docstring:
"""
A simple echo command that repeats back whatever the user sends.
If the first argument is a number, it will be treated as the number of times
to repeat the following sequence of characters, joined by a space.
Usage
-----
>>> echo hello world
hello world
>>> echo 3 hi
hi hi hi
"""
)
{
}
public override void Callback()
{
string echoString = ReturnedArguments[0];
string[] echoStringParts = echoString.Split(' ');
if (echoStringParts.Length == 1)
{
Console.WriteLine(echoString);
return;
}
string firstEchoWord = echoStringParts[0];
string restOfEchoWord = string.Join(" ", echoStringParts[1 .. echoStringParts.Length]);
if (int.TryParse(firstEchoWord, out int count))
{
Console.WriteLine(string.Join(" ", Enumerable.Range(0, count + 1).Select(_ => restOfEchoWord)));
}
else
{
Console.WriteLine(echoString);
}
}
}
// Close command
class CloseCommand : Command
{
public CloseCommand()
: base("close", docstring:
"""
A simple close command that stops the process entirely and closes the window.
Usage
-----
>>> close
""")
{
}
public override void Callback()
{
Environment.Exit(0);
}
}
// Help command
class HelpCommand : Command
{
public HelpCommand()
: base("help")
{
}
public override void Callback()
{
// Get what the user queried with the help command
string query = ReturnedArguments[0];
// Get all the commands subclassing command
List<Command> search = CreatedCommands.Where(cmd => cmd.Name == query).ToList();
if (search.Count == 0)
{
Console.WriteLine($"The search for command '{query}' yielded no results.");
return;
}
Command command = search[0];
string title = $"Help on command '{command}':";
Console.WriteLine($"{title}\n{new string('-', title.Length)}\n{command.Docstring}\n{new string('-', title.Length)}");
}
}
}