private static IEnumerable <IRenderable> GetExamples(CommandModel model, CommandInfo?command) { var maxExamples = int.MaxValue; var examples = command?.Examples ?? model.Examples ?? new List <string[]>(); if (examples.Count == 0) { // Since we're not checking direct examples, // make sure that we limit the number of examples. maxExamples = 5; // Get the current root command. var root = command ?? (ICommandContainer)model; var queue = new Queue <ICommandContainer>(new[] { root }); // Traverse the command tree and look for examples. // As soon as a node contains commands, bail. while (queue.Count > 0) { var current = queue.Dequeue(); foreach (var cmd in current.Commands) { if (cmd.Examples.Count > 0) { examples.AddRange(cmd.Examples); } queue.Enqueue(cmd); } if (examples.Count >= maxExamples) { break; } } } if (examples.Count > 0) { var composer = new Composer(); composer.Style("yellow", "EXAMPLES:").LineBreak(); for (var index = 0; index < Math.Min(maxExamples, examples.Count); index++) { var args = string.Join(" ", examples[index]); composer.Tab().Text(model.GetApplicationName()).Space().Style("grey", args); composer.LineBreak(); } composer.LineBreak(); return(new[] { composer }); } return(Array.Empty <IRenderable>()); }
private static IEnumerable <IRenderable> GetUsage(CommandModel model, CommandInfo?command) { var composer = new Composer(); composer.Style("yellow", "USAGE:").LineBreak(); composer.Tab().Text(model.GetApplicationName()); var parameters = new Stack <string>(); if (command == null) { parameters.Push("[aqua]<COMMAND>[/]"); parameters.Push("[grey][[OPTIONS]][/]"); } else { var current = command; if (command.IsBranch) { parameters.Push("[aqua]<COMMAND>[/]"); } while (current != null) { var isCurrent = current == command; if (isCurrent) { parameters.Push("[grey][[OPTIONS]][/]"); } if (current.Parameters.OfType <CommandArgument>().Any()) { var optionalArguments = current.Parameters.OfType <CommandArgument>().Where(x => !x.Required).ToArray(); if (optionalArguments.Length > 0 || !isCurrent) { foreach (var optionalArgument in optionalArguments) { parameters.Push($"[silver][[{optionalArgument.Value.SafeMarkup()}]][/]"); } } if (isCurrent) { foreach (var argument in current.Parameters.OfType <CommandArgument>() .Where(a => a.Required).OrderBy(a => a.Position).ToArray()) { parameters.Push($"[aqua]<{argument.Value.SafeMarkup()}>[/]"); } } } if (!current.IsDefaultCommand) { if (isCurrent) { parameters.Push($"[underline]{current.Name.SafeMarkup()}[/]"); } else { parameters.Push($"{current.Name.SafeMarkup()}"); } } current = current.Parent; } } composer.Join(" ", parameters); composer.LineBreaks(2); return(new[] { composer, }); }