private void GetAllUsage(CommandNode <TSource> node, TSource source, List <string> result, string prefix, bool restricted) { if (restricted && !node.CanUse(source)) { return; } if (node.Command != null) { result.Add(prefix); } if (node.Redirect != null) { var redirect = node.Redirect == _root ? "..." : "-> " + node.Redirect.UsageText; result.Add(prefix.Length == 0 ? node.UsageText + ArgumentSeparator + redirect : prefix + ArgumentSeparator + redirect); } else if (node.Children.Count > 0) { foreach (var child in node.Children) { GetAllUsage(child, source, result, prefix.Length == 0 ? child.UsageText : prefix + ArgumentSeparator + child.UsageText, restricted); } } }
private string GetSmartUsage(CommandNode <TSource> node, TSource source, bool optional, bool deep) { if (!node.CanUse(source)) { return(null); } var self = optional ? UsageOptionalOpen + node.UsageText + UsageOptionalClose : node.UsageText; var childOptional = node.Command != null; var open = childOptional ? UsageOptionalOpen : UsageRequiredOpen; var close = childOptional ? UsageOptionalClose : UsageRequiredClose; if (!deep) { if (node.Redirect != null) { var redirect = node.Redirect == _root ? "..." : "-> " + node.Redirect.UsageText; return(self + ArgumentSeparator + redirect); } else { var children = node.Children.Where(c => c.CanUse(source)).ToList(); if (children.Count == 1) { var usage = GetSmartUsage(children.Single(), source, childOptional, childOptional); if (usage != null) { return(self + ArgumentSeparator + usage); } } else if (children.Count > 1) { ISet <string> childUsage = new HashSet <string>(); foreach (var child in children) { var usage = GetSmartUsage(child, source, childOptional, true); if (usage != null) { childUsage.Add(usage); } } if (childUsage.Count == 1) { var usage = childUsage.Single(); return(self + ArgumentSeparator + (childOptional ? UsageOptionalOpen + usage + UsageOptionalClose : usage)); } else if (childUsage.Count > 1) { var builder = new StringBuilder(open); var count = 0; foreach (var child in children) { if (count > 0) { builder.Append(UsageOr); } builder.Append(child.UsageText); count++; } if (count > 0) { builder.Append(close); return(self + ArgumentSeparator + builder); } } } } } return(self); }