| File: parent\dotnet\Commands\Hidden\Complete\CompleteCommand.cs | Web Access |
| Project: dotnet-aot.csproj (dotnet-aot) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #nullable disable using System.CommandLine; using System.CommandLine.Completions; using Microsoft.DotNet.Cli.Extensions; using Microsoft.DotNet.Cli.Utils; namespace Microsoft.DotNet.Cli.Commands.Hidden.Complete; public class CompleteCommand { public static int Run(ParseResult parseResult) { return RunWithReporter(parseResult, Reporter.Output); } public static int RunWithReporter(string[] args, IReporter reporter) { var result = Parser.Parse(["dotnet", "complete", .. args]); return RunWithReporter(result, reporter); } public static int RunWithReporter(ParseResult result, IReporter reporter) { if (reporter == null) { throw new ArgumentNullException(nameof(reporter)); } try { #if !CLI_AOT result.HandleDebugSwitch(); #endif var completions = Completions(result); foreach (var completion in completions) { reporter.WriteLine(completion.Label); } } #if CLI_AOT catch (CommandNotAvailableInAotException) { throw; } #endif catch (Exception) { return 1; } return 0; } private static CompletionItem[] Completions(ParseResult complete) { var definition = (CompleteCommandDefinition)complete.CommandResult.Command; var input = complete.GetValue(definition.PathArgument) ?? string.Empty; var position = complete.GetValue(definition.PositionOption); if (position > input.Length) { input += " "; } var result = Parser.Parse(input); #if CLI_AOT // Managed parsers supply command-specific providers. Decide before enumerating or // writing completions, using the same normalized parse that produces the candidates. if (!ReferenceEquals(result.CommandResult.Command, Parser.RootCommand)) { throw new CommandNotAvailableInAotException(); } #endif return [.. result.GetCompletions(position).Distinct()]; } }