internal static void Run(Statement statement) { run: if (statement.Command == "add" || statement.Command == "remove") { throw new Exception($"Such '{statement.Command}' command is under development "); } if (CmdFunc.TryGetValue(statement.Command, out Operation operation)) { operation.Invoke(statement); } else if (AliasFunc.ContainsValue(statement.Command)) { foreach (var item in AliasFunc) { if (item.Value == statement.Command) { statement.Command = item.Key; goto run; } } } else { throw new Exception($"Such '{statement.Command}' command is not found "); } }
static void Boot() { bool hasFile = File.Exists(FilePath), hasData = File.Exists(DataPath); if (!hasFile || !hasData) { string file = !hasFile?Path.GetFileName(FilePath) : Path.GetFileName(DataPath); throw new Exception($"File '{file}' missing. Sorry :("); } XDocument document, data; try { document = XDocument.Load(FilePath); data = XDocument.Load(DataPath); } catch (XmlException) { throw new Exception($"Invalid format loaded files. Sorry :("); } foreach (var item in document.Element(Tags["body"]).Elements()) { FuncHelp.Add(item.Name.LocalName, item); if (item.Element(Tags["param"]) != null && item.Element(Tags["alias"]).Value.Trim().Length != 0) { AliasFunc.Add(item.Name.LocalName, item.Element(Tags["alias"]).Value); } var attrs = new Dictionary <string, char> { }; if (item.Element(Tags["param"]) != null) { foreach (var attr in item.Element(Tags["param"]).Elements()) { attrs.Add(attr.Name.LocalName, attr.Value.Trim()[0]); } } else { continue; } Attributes.Add(item.Name.LocalName, attrs); } foreach (var item in data.Element(Tags["body"]).Elements()) { Accaunts.Add(item.Attribute(Tags["id"]).Value, item); } }
public static void Alias(Statement statement) { switch (statement.Blocks.Length) { case 1: Program.Print(ConsoleColor.Magenta, "Aliases: command ~ alias ", ""); if (AliasFunc.Count == 0) { Program.Print(ConsoleColor.Blue, "Upset aliases"); } else { foreach (var item in AliasFunc) { Program.Print(ConsoleColor.Yellow, $"{item.Key} ~ {item.Value}"); } } break; case 2 when statement.Params != null && statement.Property == null: var helpAttrs = Attributes["help"]; foreach (var attr in statement.Params) { if (attr == helpAttrs["help"]) { HelpOf(statement); } else { Program.Print(ConsoleColor.Red, $"This '{attr}' parametr is not correct, use {statement.Command} /{Attributes["alias"]["help"]}"); break; } } break; case 3 when statement.Params != null && statement.Property != null: List <char> list = new List <char> { }; var aliasAttrs = Attributes["alias"]; foreach (var attr in statement.Params) { if (attr == aliasAttrs["help"] && !list.Contains(attr)) { list.Add(attr); HelpOf(statement); } else if (attr == aliasAttrs["add"] && !list.Contains(attr)) { list.Add(attr); foreach (var item in statement.Property) { if (TryAddAlias(item.Key, item.Value, statement.Params.ToArray().Contains(aliasAttrs["force"]))) { Program.Print(ConsoleColor.Blue, $"Alias successful added '{statement.Command} {item.Key}:{item.Value} /{attr}'"); } else { Program.Print(ConsoleColor.Red, $"The request to add was canceled -> '{statement.Command} {item.Key}:{item.Value} /{attr}'"); } } } else if (attr == aliasAttrs["remove"] && !list.Contains(attr)) { list.Add(attr); foreach (var item in statement.Property) { if (TryRemoveAlias(item.Key, statement.Params.ToArray().Contains(aliasAttrs["force"]))) { Program.Print(ConsoleColor.Blue, $"Alias successful removed '{statement.Command} {item.Key}:{item.Value} /{attr}'"); } else { Program.Print(ConsoleColor.Red, $"The removal request was canceled '{statement.Command} {item.Key}:{item.Value} /{attr}'"); } } } else if (attr == aliasAttrs["force"] && !list.Contains(attr)) { list.Add(attr); continue; } else if (list.Contains(attr)) { Program.Print(ConsoleColor.Red, $"This '{attr}' has done"); goto fin; } else { Program.Print(ConsoleColor.Red, $"This '{attr}' parametr is not correct"); goto fin; } } break; default: Program.Print(ConsoleColor.Red, $"Invalid using command: '{statement.Command}', -> '{statement.String}'"); break; } fin: Go(); bool TryAddAlias(string key, string value, bool force = false) { if (!CmdFunc.Keys.Contains(key)) { Program.Print(ConsoleColor.Red, $"Command: '{key}' is not found"); return(false); } else if (AliasFunc.ContainsKey(key) && !force) { Program.Print(ConsoleColor.Red, $"Command: '{key}' already has alias"); return(false); } else if (AliasFunc.ContainsValue(value) && !force) { Program.Print(ConsoleColor.Red, $"Alias: '{value}' is not availble"); return(false); } else if (force) { if (AliasFunc.ContainsKey(key)) { AliasFunc.Remove(key); } if (AliasFunc.ContainsValue(value)) { foreach (var item in AliasFunc) { if (item.Value == value) { AliasFunc.Remove(item.Key); } } } } AliasFunc.Add(key, value); if (!NewAlias.ContainsKey(key)) { NewAlias.Add(key, value); } return(true); } bool TryRemoveAlias(string key, bool force = false) { if (!CmdFunc.Keys.Contains(key)) { Program.Print(ConsoleColor.Red, $"Command: '{key}' is not found"); return(false); } else if (force) { bool isRm = AliasFunc.Remove(key); if (isRm && !NewAlias.ContainsKey(key)) { NewAlias.Add(key, ""); } return(isRm); } else { string answer = Prompt(color: ConsoleColor.DarkGreen, txt: "Are you sure complite process [ 'y' | 'n' ]").Trim(); if ("y" == answer) { bool isRm = AliasFunc.Remove(key); if (isRm && !NewAlias.ContainsKey(key)) { NewAlias.Add(key, ""); } return(isRm); } else if ("n" == answer) { return(false); } else { Program.Print(color: ConsoleColor.Red, txt: $"Invalid argument: {answer} "); return(false); } } } }