|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +namespace System.CommandLine.Help |
| 5 | +{ |
| 6 | + public class CliHelpArguments : CliHelpSection |
| 7 | + { |
| 8 | + public CliHelpArguments(CliHelpConfiguration helpConfiguration, HelpContext helpContext) |
| 9 | + : base(helpConfiguration, helpContext, LocalizationResources.HelpArgumentsTitle()) |
| 10 | + { |
| 11 | + } |
| 12 | + |
| 13 | + public override IEnumerable<string>? GetBody(CliSymbol current) |
| 14 | + { |
| 15 | + if (current is null) |
| 16 | + { return null; } |
| 17 | + |
| 18 | + var selfAndParents = current.Parents |
| 19 | + .Prepend(current) |
| 20 | + .OfType<CliCommand>() |
| 21 | + .Reverse(); |
| 22 | + |
| 23 | + var table = selfAndParents |
| 24 | + .SelectMany(cmd => cmd.Arguments.Where(a => !a.Hidden)) |
| 25 | + .Select(a => GetTwoColumnRow(a)) |
| 26 | + .Distinct(); |
| 27 | + |
| 28 | + return table is null |
| 29 | + ? null |
| 30 | + : CliHelpHelpers.WriteTwoColumns(table, HelpContext.MaxWidth, Indent); |
| 31 | + } |
| 32 | + |
| 33 | + public override IEnumerable<string>? GetClosing(CliSymbol current) |
| 34 | + => null; |
| 35 | + |
| 36 | + private TwoColumnHelpRow? GetTwoColumnRow(CliArgument argument) |
| 37 | + { |
| 38 | + _ = argument ?? throw new ArgumentNullException(nameof(argument)); |
| 39 | + |
| 40 | + string firstColumnText = SymbolOutput.GetUsage(argument); |
| 41 | + string secondColumnText = GetSecondColumnText(argument); |
| 42 | + |
| 43 | + return new TwoColumnHelpRow(firstColumnText, secondColumnText); |
| 44 | + |
| 45 | + string GetSecondColumnText(CliArgument argument) |
| 46 | + { |
| 47 | + var symbolDescription = argument.Description ?? string.Empty; |
| 48 | + |
| 49 | + var defaultValueDescription = SymbolOutput.GetDefaultValueText(argument, true); |
| 50 | + if (string.IsNullOrEmpty(defaultValueDescription)) |
| 51 | + { |
| 52 | + return $"{symbolDescription}".Trim(); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + return $"{symbolDescription} [{defaultValueDescription}]".Trim(); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + ///// <summary> |
| 64 | + ///// Gets the usage title for an argument (for example: <c><value></c>, typically used in the first column text in the arguments usage section, or within the synopsis. |
| 65 | + ///// </summary> |
| 66 | + //private static string GetArgumentUsageLabel(CliArgument argument) |
| 67 | + //{ |
| 68 | + // // Argument.HelpName is always first choice |
| 69 | + // if (!string.IsNullOrWhiteSpace(argument.HelpName)) |
| 70 | + // { |
| 71 | + // return $"<{argument.HelpName}>"; |
| 72 | + // } |
| 73 | + |
| 74 | + // if (argument.ValueType == typeof(bool)) |
| 75 | + // { |
| 76 | + // return string.Empty; |
| 77 | + // } |
| 78 | + |
| 79 | + // var completionItems = argument.GetCompletions(CompletionContext.Empty); |
| 80 | + // if (completionItems.Any()) |
| 81 | + // { |
| 82 | + |
| 83 | + // IEnumerable<string> completions = completionItems |
| 84 | + // .Select(item => item.Label); |
| 85 | + |
| 86 | + // string joined = string.Join("|", completions); |
| 87 | + |
| 88 | + // if (!string.IsNullOrEmpty(joined)) |
| 89 | + // { |
| 90 | + // return $"<{joined}>"; |
| 91 | + // } |
| 92 | + // } |
| 93 | + |
| 94 | + // return $"<{argument.Name}>"; |
| 95 | + //} |
| 96 | + |
| 97 | + |
| 98 | + //private string GetArgumentDefaultValue( |
| 99 | + // CliArgument argument, |
| 100 | + // bool displayArgumentName) |
| 101 | + //{ |
| 102 | + // string? displayedDefaultValue = GetArgumentDefaultValue(argument); |
| 103 | + |
| 104 | + // return string.IsNullOrWhiteSpace(displayedDefaultValue) |
| 105 | + // ? "" |
| 106 | + // : $"{GetLabel(argument, displayArgumentName)}: {displayedDefaultValue}"; |
| 107 | + |
| 108 | + // static string GetLabel(CliArgument argument, bool displayArgumentName) => |
| 109 | + // displayArgumentName |
| 110 | + // ? LocalizationResources.HelpArgumentDefaultValueLabel() |
| 111 | + // : argument.Name; |
| 112 | + //} |
| 113 | + |
| 114 | + //public static string GetArgumentDefaultValue(CliSymbol symbol) |
| 115 | + //{ |
| 116 | + // var defaultValue = symbol switch |
| 117 | + // { |
| 118 | + // CliArgument argument => argument.GetHelpDefaultValue(), |
| 119 | + // CliOption option => option.GetHelpDefaultValue(), |
| 120 | + // _ => null |
| 121 | + // }; |
| 122 | + // if (defaultValue is not null) |
| 123 | + // { |
| 124 | + // if (defaultValue is IEnumerable enumerable and not string) |
| 125 | + // { |
| 126 | + // return string.Join("|", enumerable.OfType<object>().ToArray()); |
| 127 | + // } |
| 128 | + // else |
| 129 | + // { |
| 130 | + // return defaultValue.ToString() ?? ""; |
| 131 | + // } |
| 132 | + // } |
| 133 | + // return string.Empty; |
| 134 | + //} |
| 135 | + } |
| 136 | +} |
0 commit comments