/// <summary> /// Zeros, clears and nulls, itself and all subcommands. /// </summary> public void Clear() { parentNode = null; if (Command != null) { //clear and abandon it Command.callback = null; Command.help = string.Empty; Command.localName = string.Empty; Command = null; } //all children to clear their commands and children foreach (var item in subCommandsLookUp) { item.Value.Clear(); } //clear the actual dictionary to abandon refs subCommandsLookUp.Clear(); }
/// <summary> /// Find a command of given separated name. If no exact match is found, returns false and out node is the closest /// node that was found. /// </summary> /// <param name="commandName"></param> /// <param name="node"></param> /// <returns>true on perfect match.</returns> public bool FindClosestMatch(string[] commandName, out ConsoleCommandTreeNode node) { int index = 0; node = this; while (index != commandName.Length) { string lowerToken = commandName[index].ToLowerInvariant(); if (node.subCommandsLookUp.TryGetValue(lowerToken, out ConsoleCommandTreeNode outTemp)) { index++; node = outTemp; } else { return(false); } } return(true); }
/// <summary> /// Add a new command with names separated by folders/namespaces, last is command name, others are holders. /// These will be made as required /// during the add. /// </summary> /// <param name="names">separated command name e.g. Physics.gravity is now {"Physics","gravity"}</param> /// <param name="callback">action of the command to add</param> /// <param name="helpText">help text to show to user about the command</param> /// <param name="command_index">used in the recursion, indicates current depth in names array</param> public void Add(string[] names, Console.CommandCallback callback, string helpText, int command_index = 0) { if (names.Length == command_index) { Command = new ConsoleCommandData { localName = names.Last(), callback = callback, help = helpText }; return; } string token = names[command_index]; string lowerToken = token.ToLowerInvariant(); if (!subCommandsLookUp.ContainsKey(lowerToken)) { ConsoleCommandData data = new ConsoleCommandData { localName = token }; subCommandsLookUp[lowerToken] = new ConsoleCommandTreeNode(this, data); } subCommandsLookUp[lowerToken].Add(names, callback, helpText, command_index + 1); }
private Console() { commandRoot = new ConsoleCommandTreeNode(); }
protected ConsoleCommandTreeNode(ConsoleCommandTreeNode parent, ConsoleCommandData data) { parentNode = parent; Command = data; }