diff --git a/src/dotnet/Parser.cs b/src/dotnet/Parser.cs index c57e9d87b..ec1b8b76b 100644 --- a/src/dotnet/Parser.cs +++ b/src/dotnet/Parser.cs @@ -2,36 +2,37 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; -using System.Linq; using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli { public static class Parser { - public static Command DotnetCommand { get; } = - Create.Command("dotnet", - ".NET Command Line Tools", - Accept.NoArguments, - NewCommandParser.New(), - RestoreCommandParser.Restore(), - BuildCommandParser.Build(), - PublishCommandParser.Publish(), - RunCommandParser.Run(), - TestCommandParser.Test(), - PackCommandParser.Pack(), - MigrateCommandParser.Migrate(), - CleanCommandParser.Clean(), - SlnCommandParser.Sln(), - AddCommandParser.Add(), - RemoveCommandParser.Remove(), - ListCommandParser.List(), - NuGetCommandParser.NuGet(), - Create.Command("msbuild", ""), - Create.Command("vstest", ""), CompleteCommandParser.Complete(), - CommonOptions.HelpOption(), - Create.Option("--info", ""), - CommonOptions.VerbosityOption(), - Create.Option("-d", "")); + public static CommandLine.Parser Instance { get; } = new CommandLine.Parser( + delimiters: Array.Empty(), + options: Create.Command("dotnet", + ".NET Command Line Tools", + Accept.NoArguments, + NewCommandParser.New(), + RestoreCommandParser.Restore(), + BuildCommandParser.Build(), + PublishCommandParser.Publish(), + RunCommandParser.Run(), + TestCommandParser.Test(), + PackCommandParser.Pack(), + MigrateCommandParser.Migrate(), + CleanCommandParser.Clean(), + SlnCommandParser.Sln(), + AddCommandParser.Add(), + RemoveCommandParser.Remove(), + ListCommandParser.List(), + NuGetCommandParser.NuGet(), + Create.Command("msbuild", ""), + Create.Command("vstest", ""), + CompleteCommandParser.Complete(), + CommonOptions.HelpOption(), + Create.Option("--info", ""), + CommonOptions.VerbosityOption(), + Create.Option("-d", ""))); } } \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-complete/CompleteCommand.cs b/src/dotnet/commands/dotnet-complete/CompleteCommand.cs index 8cb88994b..c314f4141 100644 --- a/src/dotnet/commands/dotnet-complete/CompleteCommand.cs +++ b/src/dotnet/commands/dotnet-complete/CompleteCommand.cs @@ -19,7 +19,7 @@ namespace Microsoft.DotNet.Cli DebugHelper.HandleDebugSwitch(ref args); // get the parser for the current subcommand - var parser = Parser.DotnetCommand["complete"]; + var parser = Parser.Instance["dotnet"]["complete"]; // parse the arguments var result = parser.Parse(args); @@ -62,7 +62,7 @@ namespace Microsoft.DotNet.Cli } } - var result = Parser.DotnetCommand.Parse(input); + var result = Parser.Instance.Parse(input); return result.Suggestions() .ToArray(); diff --git a/src/dotnet/commands/dotnet-restore/Program.cs b/src/dotnet/commands/dotnet-restore/Program.cs index 73c1d9794..65ab11442 100644 --- a/src/dotnet/commands/dotnet-restore/Program.cs +++ b/src/dotnet/commands/dotnet-restore/Program.cs @@ -22,9 +22,7 @@ namespace Microsoft.DotNet.Tools.Restore { DebugHelper.HandleDebugSwitch(ref args); - var parser = new Cli.CommandLine.Parser( - delimiters: Array.Empty(), - options: Parser.DotnetCommand["restore"]); + var parser = Parser.Instance["dotnet"]; var result = parser.Parse(args); @@ -40,8 +38,9 @@ namespace Microsoft.DotNet.Tools.Restore }; msbuildArgs.AddRange(restore.ArgsToBeForwarded()); - msbuildArgs.AddRange(restore.Arguments); + msbuildArgs.AddRange(restore.Arguments); + return new RestoreCommand(msbuildArgs, msbuildPath); } diff --git a/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs b/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs index e9ed45f7a..62ab2f870 100644 --- a/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs +++ b/src/dotnet/commands/dotnet-restore/RestoreCommandParser.cs @@ -11,7 +11,7 @@ namespace Microsoft.DotNet.Cli Create.Command( "restore", ".NET dependency restorer", - Accept.ExactlyOneArgument, + Accept.ZeroOrMoreArguments, CommonOptions.HelpOption(), Create.Option( "-s|--source", diff --git a/src/dotnet/dotnet.csproj b/src/dotnet/dotnet.csproj index b1963421e..53de43916 100644 --- a/src/dotnet/dotnet.csproj +++ b/src/dotnet/dotnet.csproj @@ -40,7 +40,7 @@ - + diff --git a/test/dotnet.Tests/ParserTests/AddReferenceParserTests.cs b/test/dotnet.Tests/ParserTests/AddReferenceParserTests.cs index 51bb79f75..68121383d 100644 --- a/test/dotnet.Tests/ParserTests/AddReferenceParserTests.cs +++ b/test/dotnet.Tests/ParserTests/AddReferenceParserTests.cs @@ -25,7 +25,7 @@ namespace Microsoft.DotNet.Tests.ParserTests [Fact] public void AddReferenceHasDefaultArgumentSetToCurrentDirectory() { - var command = Parser.DotnetCommand; + var command = Parser.Instance; var result = command.Parse("dotnet add reference my.csproj"); @@ -41,7 +41,7 @@ namespace Microsoft.DotNet.Tests.ParserTests [Fact] public void AddReferenceWithoutArgumentResultsInAnError() { - var command = Parser.DotnetCommand; + var command = Parser.Instance; var result = command.Parse("dotnet add reference"); diff --git a/test/dotnet.Tests/ParserTests/RestoreParserTests.cs b/test/dotnet.Tests/ParserTests/RestoreParserTests.cs new file mode 100644 index 000000000..2dc32cde9 --- /dev/null +++ b/test/dotnet.Tests/ParserTests/RestoreParserTests.cs @@ -0,0 +1,52 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using FluentAssertions; +using Microsoft.DotNet.Cli.CommandLine; +using Xunit; +using Xunit.Abstractions; +using Parser = Microsoft.DotNet.Cli.Parser; + +namespace Microsoft.DotNet.Tests.ParserTests +{ + public class RestoreParserTests + { + private readonly ITestOutputHelper output; + + public RestoreParserTests(ITestOutputHelper output) + { + this.output = output; + } + + [Fact] + public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsSpecified() + { + var parser = Parser.Instance["dotnet"]; + + var result = parser.Parse(@"restore .\some.csproj --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); + + output.WriteLine(result.Diagram()); + + result["restore"] + .Arguments + .Should() + .BeEquivalentTo(@".\some.csproj", @"/p:SkipInvalidConfigurations=true"); + } + + [Fact] + public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsNotSpecified() + { + var parser = Parser.Instance["dotnet"]; + + var result = parser.Parse(@"restore --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); + + output.WriteLine(result.Diagram()); + + result["restore"] + .Arguments + .Should() + .BeEquivalentTo(@"/p:SkipInvalidConfigurations=true"); + } + } +} \ No newline at end of file diff --git a/test/dotnet.Tests/dotnet.Tests.csproj b/test/dotnet.Tests/dotnet.Tests.csproj index d7766cd89..3c6555049 100644 --- a/test/dotnet.Tests/dotnet.Tests.csproj +++ b/test/dotnet.Tests/dotnet.Tests.csproj @@ -42,6 +42,6 @@ - +