private static void WriteUsage(RenderableComposer composer, CommandModel model, CommandInfo command) { var parameters = new Stack <IRenderable>(); if (command == null) { parameters.Push(new ColorElement(ConsoleColor.Cyan, new TextElement("<COMMAND>"))); parameters.Push(new ColorElement(ConsoleColor.DarkGray, new TextElement("[OPTIONS]"))); } else { var current = command; if (command.IsBranch) { parameters.Push(new ColorElement(ConsoleColor.Cyan, new TextElement("<COMMAND>"))); } while (current != null) { var builder = new BlockElement(); if (!current.IsDefaultCommand) { builder.Append(new TextElement(current.Name)); } if (current.Parameters.OfType <CommandArgument>().Any()) { var isCurrent = current == command; if (isCurrent) { foreach (var argument in current.Parameters.OfType <CommandArgument>() .Where(a => a.Required).OrderBy(a => a.Position).ToArray()) { var text = new TextElement($" <{argument.Value}>"); builder.Append(new ColorElement(ConsoleColor.Cyan, text)); } } var optionalArguments = current.Parameters.OfType <CommandArgument>().Where(x => !x.Required).ToArray(); if (optionalArguments.Length > 0 || !isCurrent) { foreach (var optionalArgument in optionalArguments) { var text = new TextElement($" [{optionalArgument.Value}]"); builder.Append(new ColorElement(ConsoleColor.Gray, text)); } } } if (current == command) { builder.Append(new ColorElement(ConsoleColor.DarkGray, new TextElement(" [OPTIONS]"))); } parameters.Push(builder); current = current.Parent; } } composer.Color(ConsoleColor.Yellow, c => c.Text("USAGE:")).LineBreak(); composer.Tab().Text(model.ApplicationName ?? Path.GetFileName(Assembly.GetEntryAssembly().Location)); // Root or not a default command? if (command?.IsDefaultCommand != true) { composer.Space(); } composer.Join(" ", parameters); }
private static void WriteOptions(RenderableComposer composer, CommandInfo command) { // Collect all options into a single structure. var parameters = new List <HelpOption> { new HelpOption("h", "help", (string)null, "Prints help information") }; parameters.AddRange(command?.Parameters?.OfType <CommandOption>()?.Select(o => new HelpOption(o.ShortNames.FirstOrDefault(), o.LongNames.FirstOrDefault(), o.ValueName, o.Description)) ?? Array.Empty <HelpOption>()); var options = parameters.ToArray(); if (options.Length > 0) { composer.Color(ConsoleColor.Yellow, c => c.Text("OPTIONS:")).LineBreak(); // Start with composing a list of lines. var result = new List <Tuple <string, BlockElement> >(); foreach (var option in options) { var item = new BlockElement(); item.Append(new TabElement()); // Short if (option.Short != null) { item.Append(new TextElement($"-{option.Short}")); if (option.Long != null) { item.Append(new TextElement(",")); } } else { item.Append(new RepeatingElement(3, new TextElement(" "))); } // Long if (option.Long != null) { item.Append(new TextElement(" ")); item.Append(new TextElement($"--{option.Long}")); } // Value if (option.Value != null) { item.Append(new TextElement(" ")); item.Append(new ColorElement(ConsoleColor.Gray, new TextElement($"<{option.Value}>"))); } result.Add(Tuple.Create(option.Description, item)); } // Now add the descriptions to all lines. var maxLength = result.Max(x => x.Item2.Length); foreach (var item in result) { var description = item.Item1; var element = item.Item2; if (!string.IsNullOrWhiteSpace(description)) { var neededSpaces = maxLength - element.Length; if (neededSpaces > 0) { element.Append(new RepeatingElement(neededSpaces, new TextElement(" "))); } element.Append(new TabElement()); element.Append(new TextElement(description.TrimEnd('.').Trim())); } element.Append(new LineBreakElement()); } // Append the items. composer.Append(result.Select(x => x.Item2)); composer.LineBreak(); } }