This repository was archived by the owner on Feb 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.cs
More file actions
40 lines (24 loc) · 1.89 KB
/
Copy pathHelper.cs
File metadata and controls
40 lines (24 loc) · 1.89 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
using System;
using System.Reflection;
using System.Collections.Generic;
static class Helper {
public static void RegCmd(string cmd, Action action, string help = null) => RegCmd(cmd, null, action, help);
public static void RegCmd(string cmd, Action<string> action, string help = null) => RegCmd(cmd, null, action, help);
public static void RegCmd(string cmd, string abbr, Action action, string help = null) {
help = HelpCreate(cmd, abbr, help);
Shell.RegisterCommand(cmd, action, help);
if (!string.IsNullOrEmpty(abbr)) Shell.RegisterCommand(abbr, action, null);
}
public static void RegCmd(string cmd, string abbr, Action<string> action, string help = null) {
help = HelpCreate(cmd, abbr, help);
Shell.RegisterCommand(cmd, action, help);
if (!string.IsNullOrEmpty(abbr)) Shell.RegisterCommand(abbr, action, null);
}
public static string GetHelp(string cmd) => description["cmd"];
static string HelpCreate(string cmd, string abbr, string help, string val = null) => string.IsNullOrEmpty(help) ? null : HelpPattern(cmd, abbr, help, val);
static string HelpPattern(string cmd, string abbr, string help, string val = null) => cmd + (string.IsNullOrEmpty(abbr) ? null : $"({abbr})") + (string.IsNullOrEmpty(val) ? null : " " + val) + " - " + help;
static readonly FieldInfo commandsF = typeof(Shell).GetField("commands", BindingFlags.NonPublic | BindingFlags.Static);
static readonly CommandRegistry CommandRegistry = (CommandRegistry)commandsF.GetValue(null);
static readonly FieldInfo descriptionF = typeof(CommandRegistry).GetField("description", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly Dictionary<string, string> description = (Dictionary<string, string>)descriptionF.GetValue(CommandRegistry);
}