diff --git a/src/Cli/dotnet/commands/dotnet-nuget/NuGetCommandParser.cs b/src/Cli/dotnet/commands/dotnet-nuget/NuGetCommandParser.cs index c945b588e174..28f469787080 100644 --- a/src/Cli/dotnet/commands/dotnet-nuget/NuGetCommandParser.cs +++ b/src/Cli/dotnet/commands/dotnet-nuget/NuGetCommandParser.cs @@ -36,6 +36,7 @@ private static CliCommand ConstructCommand() command.Subcommands.Add(GetVerifyCommand()); command.Subcommands.Add(GetTrustCommand()); command.Subcommands.Add(GetSignCommand()); + command.Subcommands.Add(GetWhyCommand()); command.SetAction(NuGetCommand.Run); @@ -216,5 +217,19 @@ private static CliCommand GetSignCommand() return signCommand; } + + private static CliCommand GetWhyCommand() + { + CliCommand whyCommand = new("why"); + + whyCommand.Arguments.Add(new CliArgument("PROJECT|SOLUTION") { Arity = ArgumentArity.ExactlyOne }); + whyCommand.Arguments.Add(new CliArgument("PACKAGE") { Arity = ArgumentArity.ExactlyOne }); + + whyCommand.Options.Add(new CliOption("--framework", "-f") { Arity = ArgumentArity.ZeroOrMore }); + + whyCommand.SetAction(NuGetCommand.Run); + + return whyCommand; + } } } diff --git a/test/dotnet-nuget.UnitTests/GivenANuGetCommand.cs b/test/dotnet-nuget.UnitTests/GivenANuGetCommand.cs index b405aa74e257..02080bc9c0a1 100644 --- a/test/dotnet-nuget.UnitTests/GivenANuGetCommand.cs +++ b/test/dotnet-nuget.UnitTests/GivenANuGetCommand.cs @@ -75,6 +75,9 @@ public GivenANuGetCommand(ITestOutputHelper log) : base(log) "--interactive", "--verbosity", "detailed", "--format", "json"}, 0)] + [InlineData(new[] { "why" }, 0)] + [InlineData(new[] { "why", "C:\\path", "Fake.Package" }, 0)] + [InlineData(new[] { "why", "C:\\path", "Fake.Package", "--framework", "net472", "-f", "netcoreapp5.0" }, 0)] public void ItPassesCommandIfSupported(string[] inputArgs, int result) { diff --git a/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs b/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs index a895374c1802..4cc23ba5c6c8 100644 --- a/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/CompleteCommandTests.cs @@ -118,7 +118,8 @@ public void GivenNuGetCommandItDisplaysCompletions() "push", "verify", "trust", - "sign" + "sign", + "why" }; var reporter = new BufferedReporter(); @@ -286,6 +287,24 @@ public void GivenNuGetSignCommandItDisplaysCompletions() reporter.Lines.OrderBy(c => c).Should().Equal(expected.OrderBy(c => c)); } + [Fact] + public void GivenNuGetWhyCommandItDisplaysCompletions() + { + var expected = new[] { + "--framework", + "--help", + "-?", + "-f", + "-h", + "/?", + "/h" + }; + + var reporter = new BufferedReporter(); + CompleteCommand.RunWithReporter(new[] { "dotnet nuget why " }, reporter).Should().Be(0); + reporter.Lines.OrderBy(c => c).Should().Equal(expected.OrderBy(c => c)); + } + [Fact] public void GivenDotnetAddPackWithPosition() {