// Clones and re-parents the clone. public CommandHelp ReparentClone(IHelpNode parent) { var help = Clone(); help.Parent = parent; return(help); }
public static ReadOnlyCollection<CommandHelp> ProcessCommands( CommandHelp[] collection, string argName, IHelpNode parentNode) { if (collection == null) throw new ArgumentNullException(argName); if (collection.Length == 0) throw new ArgumentException("Cannot be an empty collection.", argName); /* Create our own copy of the array, so it cannot be changed by the caller. * But also clone the elements, not just copy the reference. This to protect * a caller that reuses the same branch with different parents from inadvertently * having the parent of the branch be the last parent used. */ var ourCopy = new CommandHelp[collection.Length]; for (int i = 0; i < collection.Length; i++) { var el = collection[i]; if (el != null) ourCopy[i] = el.ReparentClone(parentNode); else throw new ArgumentException("Cannot contain null.", argName); } return new ReadOnlyCollection<CommandHelp>(ourCopy); }
// Returns related commands and their parent node, which is where we will start // to build the prefix. static IEnumerable <string> GetRelated(CommandHelp help, out IHelpNode parentNode) { IEnumerable <string> related; // Assume subcommands and sibling commands are relevant. if (help.Siblings.Any()) { var siblings = from siblingHelp in help.Siblings.Cast <CommandHelp>() select siblingHelp.Command; var children = help.Subcommands .Select(sc => help.Command + pathSep + sc.Command); related = children.Concat(siblings); parentNode = help.Parent; } else { related = help.Subcommands.Select(sc => sc.Command); parentNode = help; } return(related); }
public static IEnumerable <IHelpNode> AllNodes <T>(this T root) where T : IHelpNode { // Root is included in all nodes. var nodes = new IHelpNode[] { root }; return(RecurseNodes(Enumerable.Empty <IHelpNode>(), nodes)); }
static void StackCommandPath(Stack <string> cmdPath, IHelpNode start, string root) { // Move backwards up the IHelpNode tree to construct the full command. var currentNode = start; while (currentNode != null) { var cmdHelp = currentNode as CommandHelp; if (cmdHelp != null) { cmdPath.Push(cmdHelp.Command); currentNode = cmdHelp.Parent; } else { break; } } // We now have the command path, _not including_ the root, ie trigger. // So that's the final push. cmdPath.Push(root); }