-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommand.cs
305 lines (275 loc) · 8.78 KB
/
Command.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System.Reflection;
using System.Text;
namespace HTMLBuilder;
public class Command
{
public class Option
{
public string Short;
public string Long;
public string Args;
public string Description;
public Option()
{
Short = "";
Long = "";
Args = "";
Description = "";
}
}
public class Subcommand
{
public string Name;
public string Args;
public Option[] Options;
public string Description;
public Subcommand()
{
Name = "";
Args = "";
Options = Array.Empty<Option>();
Description = "";
}
}
public string Name;
public Action<Arguments.Argument[]> Function;
public string Args;
public Option[] Options;
public string Description;
public Subcommand[] Subcommands;
public Command()
{
Name = "";
Args = "";
Options = Array.Empty<Option>();
Description = "";
Subcommands = Array.Empty<Subcommand>();
}
}
public static class Commands
{
private static Dictionary<string, Command> _Commands = new Dictionary<string, Command>();
public static bool TryGet(string key, out Command command)
{
return _Commands.TryGetValue(key, out command);
}
public static Command Get(string key)
{
return _Commands[key];
}
public static void Initialise()
{
var commandsType = typeof(Commands);
var fields = commandsType.GetFields(BindingFlags.Static | BindingFlags.NonPublic);
foreach (var field in fields)
{
var value = field.GetValue(null);
if (value is not Command command) continue;
_Commands.Add(command.Name, command);
Console.WriteLine($"Registered command '{command.Name}'");
}
}
private static void PrintOption(StringBuilder output, Command.Option option, string prefix = "\t")
{
output.Append($"{prefix}-{option.Short}, --{option.Long} ");
if (option.Args.Length > 0) output.Append($"{option.Args} ");
output.AppendLine($"{option.Description}");
}
private static void PrintSubcommand(StringBuilder output, Command.Subcommand sub)
{
output.Append($"\t{sub.Name}");
if (sub.Args.Length > 0) output.Append($" {sub.Args}");
output.AppendLine($" [Options...]");
output.AppendLine($"\t\t{sub.Description}");
if (sub.Options.Length == 0) return;
output.AppendLine("\t\tOptions:");
foreach (var option in sub.Options)
{
PrintOption(output, option, "\t\t\t");
}
}
private static StringBuilder PrintCommand(Command command)
{
var output = new StringBuilder(200);
output.AppendLine($"Help ({command.Name}):");
output.AppendLine("Usage:");
output.Append($"\t{command.Name} ");
if (command.Subcommands.Length > 0)
{
for (var i = 0; i < command.Subcommands.Length; i++)
{
output.Append(command.Subcommands[i].Name);
if (i != command.Subcommands.Length - 1) output.Append("|");
}
}
else if (command.Args.Length > 0)
{
output.Append($" {command.Args}");
}
output.AppendLine(" [Options...]");
output.AppendLine(command.Description);
if (command.Options.Length > 0) output.AppendLine("Options:");
foreach (var option in command.Options)
{
PrintOption(output, option);
}
if (command.Subcommands.Length == 0) return output;
output.AppendLine("Subcommands: ");
foreach (var sub in command.Subcommands)
{
PrintSubcommand(output, sub);
}
return output;
}
private static StringBuilder PrintAll()
{
StringBuilder output = new(10 + _Commands.Count * 50);
output.AppendLine("Help:");
foreach (KeyValuePair<string, Command> command in _Commands)
{
output.AppendLine($"\t{command.Key}: {command.Value.Description}");
}
return output;
}
private static void PrintHelp(Arguments.Argument[] args)
{
if (args.Length > 0)
{
Console.WriteLine(Commands.TryGet(args[0].Value, out var command)
? PrintCommand(command).ToString()
: $"Unknown command '{args[0].Value}'");
}
else
{
Console.WriteLine(PrintAll().ToString());
}
}
private static readonly Command _Help = new Command()
{
Name = "help",
Args = "[?string:command]",
Description = "Lists commands or provides detailed info on given command",
Function = PrintHelp
};
private static readonly Command _Build = new Command()
{
Name = "build",
Args = "",
Description = "Builds outputs from provided mappings.",
Function = Builder.BuildFiles
};
private static readonly Command _Config = new Command()
{
Name = "config",
Args = "[?string:option] [?string:value]",
Description = "Gets or sets config options. Provide value to set, empty to list.",
Function = Config.Configure
};
private static readonly Command _Exit = new Command()
{
Name = "exit",
Args = "",
Description = "Quits the application.",
Function = Program.Quit
};
private static readonly Command _Ref = new Command()
{
Name = "ref",
Args = "",
Description = "Mapping Reference manipulation and viewing.",
Function = Referencing.Command_Ref,
Subcommands = new []
{
new Command.Subcommand()
{
Name = "set",
Args = "[string:key] [file/folder] [string:path]",
Description = "Creates or modifies a reference by key."
},
new Command.Subcommand()
{
Name = "remove",
Args = "[string:key]",
Description = "Removes an existing mapping reference."
},
new Command.Subcommand()
{
Name = "list",
Args = "[?string:search]",
Description = "Lists all mapping references optionally by search term.",
Options = new []
{
new Command.Option()
{
Short = "p",
Long = "path",
Args = "",
Description = "Print reference paths."
},
new Command.Option()
{
Short = "m",
Long = "mappings",
Args = "",
Description = "Print reference mappings."
}
}
}
}
};
private static readonly Command _Map = new Command()
{
Name = "map",
Args = "",
Description = "Manipulation and viewing of mappings.",
Function = Mapper.Command_Map,
Subcommands = new []
{
new Command.Subcommand()
{
Name = "set",
Args = "[string:key] [file/folder] [string:path]",
Description = "Creates or modifies a mapping by key."
},
new Command.Subcommand()
{
Name = "remove",
Args = "[string:key]",
Description = "Removes an existing mapping."
},
new Command.Subcommand()
{
Name = "list",
Args = "[?string:search]",
Description = "Lists all mappings grouped by consumer, and optionally filtered by search term.",
Options = new []
{
new Command.Option()
{
Short = "v",
Long = "verbose",
Args = "",
Description = "Print mapping queries."
}
}
}
}
};
private static readonly Command _Deploy = new Command()
{
Name = "deploy",
Args = "",
Description = "Recursive deployment of directory to FTP server.",
Function = SFTPDeployer.Deploy,
Options = new []
{
new Command.Option()
{
Short = "d",
Long = "dry",
Args = "",
Description = "Perform operation without modifying any local or remote files."
}
}
};
}