|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.CommandLine.Subsystems; |
| 5 | +using System.Text; |
| 6 | +using System.CommandLine.Parsing; |
| 7 | + |
| 8 | +namespace System.CommandLine.Directives; |
| 9 | + |
| 10 | +public class DiagramSubsystem( IAnnotationProvider? annotationProvider = null) |
| 11 | + : DirectiveSubsystem("diagram", SubsystemKind.Diagram, annotationProvider) |
| 12 | +{ |
| 13 | + //protected internal override bool GetIsActivated(ParseResult? parseResult) |
| 14 | + // => parseResult is not null && option is not null && parseResult.GetValue(option); |
| 15 | + |
| 16 | + protected internal override CliExit Execute(PipelineContext pipelineContext) |
| 17 | + { |
| 18 | + // Gather locations |
| 19 | + //var locations = pipelineContext.ParseResult.LocationMap |
| 20 | + // .Concat(Map(pipelineContext.ParseResult.Configuration.PreProcessedLocations)); |
| 21 | + |
| 22 | + pipelineContext.ConsoleHack.WriteLine("Output diagram"); |
| 23 | + return CliExit.SuccessfullyHandled(pipelineContext.ParseResult); |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + // TODO: Capture logic in previous diagramming, shown below |
| 28 | + /// <summary> |
| 29 | + /// Formats a string explaining a parse result. |
| 30 | + /// </summary> |
| 31 | + /// <param name="parseResult">The parse result to be diagrammed.</param> |
| 32 | + /// <returns>A string containing a diagram of the parse result.</returns> |
| 33 | + internal static StringBuilder Diagram(ParseResult parseResult) |
| 34 | + { |
| 35 | + var builder = new StringBuilder(100); |
| 36 | + |
| 37 | + |
| 38 | + Diagram(builder, parseResult.RootCommandResult, parseResult); |
| 39 | + |
| 40 | + // TODO: Unmatched tokens |
| 41 | + /* |
| 42 | + var unmatchedTokens = parseResult.UnmatchedTokens; |
| 43 | + if (unmatchedTokens.Count > 0) |
| 44 | + { |
| 45 | + builder.Append(" ???-->"); |
| 46 | +
|
| 47 | + for (var i = 0; i < unmatchedTokens.Count; i++) |
| 48 | + { |
| 49 | + var error = unmatchedTokens[i]; |
| 50 | + builder.Append(' '); |
| 51 | + builder.Append(error); |
| 52 | + } |
| 53 | + } |
| 54 | + */ |
| 55 | + |
| 56 | + return builder; |
| 57 | + } |
| 58 | + |
| 59 | + private static void Diagram( |
| 60 | + StringBuilder builder, |
| 61 | + SymbolResult symbolResult, |
| 62 | + ParseResult parseResult) |
| 63 | + { |
| 64 | + if (parseResult.Errors.Any(e => e.SymbolResult == symbolResult)) |
| 65 | + { |
| 66 | + builder.Append('!'); |
| 67 | + } |
| 68 | + |
| 69 | + switch (symbolResult) |
| 70 | + { |
| 71 | + // TODO: Directives |
| 72 | + /* |
| 73 | + case DirectiveResult { Directive: not DiagramDirective }: |
| 74 | + break; |
| 75 | + */ |
| 76 | + |
| 77 | + // TODO: This logic is deeply tied to internal types/properties. These aren't things we probably want to expose like SymbolNode. See #2349 for alternatives |
| 78 | + /* |
| 79 | + case ArgumentResult argumentResult: |
| 80 | + { |
| 81 | + var includeArgumentName = |
| 82 | + argumentResult.Argument.FirstParent!.Symbol is CliCommand { HasArguments: true, Arguments.Count: > 1 }; |
| 83 | +
|
| 84 | + if (includeArgumentName) |
| 85 | + { |
| 86 | + builder.Append("[ "); |
| 87 | + builder.Append(argumentResult.Argument.Name); |
| 88 | + builder.Append(' '); |
| 89 | + } |
| 90 | +
|
| 91 | + if (argumentResult.Argument.Arity.MaximumNumberOfValues > 0) |
| 92 | + { |
| 93 | + ArgumentConversionResult conversionResult = argumentResult.GetArgumentConversionResult(); |
| 94 | + switch (conversionResult.Result) |
| 95 | + { |
| 96 | + case ArgumentConversionResultType.NoArgument: |
| 97 | + break; |
| 98 | + case ArgumentConversionResultType.Successful: |
| 99 | + switch (conversionResult.Value) |
| 100 | + { |
| 101 | + case string s: |
| 102 | + builder.Append($"<{s}>"); |
| 103 | + break; |
| 104 | +
|
| 105 | + case IEnumerable items: |
| 106 | + builder.Append('<'); |
| 107 | + builder.Append( |
| 108 | + string.Join("> <", |
| 109 | + items.Cast<object>().ToArray())); |
| 110 | + builder.Append('>'); |
| 111 | + break; |
| 112 | +
|
| 113 | + default: |
| 114 | + builder.Append('<'); |
| 115 | + builder.Append(conversionResult.Value); |
| 116 | + builder.Append('>'); |
| 117 | + break; |
| 118 | + } |
| 119 | +
|
| 120 | + break; |
| 121 | +
|
| 122 | + default: // failures |
| 123 | + builder.Append('<'); |
| 124 | + builder.Append(string.Join("> <", symbolResult.Tokens.Select(t => t.Value))); |
| 125 | + builder.Append('>'); |
| 126 | +
|
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | +
|
| 131 | + if (includeArgumentName) |
| 132 | + { |
| 133 | + builder.Append(" ]"); |
| 134 | + } |
| 135 | +
|
| 136 | + break; |
| 137 | + } |
| 138 | +
|
| 139 | + default: |
| 140 | + { |
| 141 | + OptionResult? optionResult = symbolResult as OptionResult; |
| 142 | +
|
| 143 | + if (optionResult is { Implicit: true }) |
| 144 | + { |
| 145 | + builder.Append('*'); |
| 146 | + } |
| 147 | +
|
| 148 | + builder.Append("[ "); |
| 149 | +
|
| 150 | + if (optionResult is not null) |
| 151 | + { |
| 152 | + builder.Append(optionResult.IdentifierToken?.Value ?? optionResult.Option.Name); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + builder.Append(((CommandResult)symbolResult).IdentifierToken.Value); |
| 157 | + } |
| 158 | +
|
| 159 | + foreach (SymbolResult child in symbolResult.SymbolResultTree.GetChildren(symbolResult)) |
| 160 | + { |
| 161 | + if (child is ArgumentResult arg && |
| 162 | + (arg.Argument.ValueType == typeof(bool) || |
| 163 | + arg.Argument.Arity.MaximumNumberOfValues == 0)) |
| 164 | + { |
| 165 | + continue; |
| 166 | + } |
| 167 | +
|
| 168 | + builder.Append(' '); |
| 169 | +
|
| 170 | + Diagram(builder, child, parseResult); |
| 171 | + } |
| 172 | +
|
| 173 | + builder.Append(" ]"); |
| 174 | + break; |
| 175 | + } |
| 176 | + } |
| 177 | + */ |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments