diff --git a/src/dotnet/ParseResultExtensions.cs b/src/dotnet/ParseResultExtensions.cs index 1eeac1662..894f494fb 100644 --- a/src/dotnet/ParseResultExtensions.cs +++ b/src/dotnet/ParseResultExtensions.cs @@ -2,13 +2,23 @@ // 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 ParserExtensions + { + public static ParseResult ParseFrom( + this CommandLine.Parser parser, + string context, + string[] args) => + parser.Parse(context.Split(' ').Concat(args).ToArray()); + } + public static class ParseResultExtensions { - public static void ShowHelp(this ParseResult parseResult) => + public static void ShowHelp(this ParseResult parseResult) => Console.WriteLine(parseResult.Command().HelpView()); } } \ 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 c314f4141..444ecfe08 100644 --- a/src/dotnet/commands/dotnet-complete/CompleteCommand.cs +++ b/src/dotnet/commands/dotnet-complete/CompleteCommand.cs @@ -19,10 +19,10 @@ namespace Microsoft.DotNet.Cli DebugHelper.HandleDebugSwitch(ref args); // get the parser for the current subcommand - var parser = Parser.Instance["dotnet"]["complete"]; + var parser = Parser.Instance; // parse the arguments - var result = parser.Parse(args); + var result = parser.ParseFrom("dotnet complete", args); var complete = result["complete"]; diff --git a/src/dotnet/commands/dotnet-complete/CompleteCommandParser.cs b/src/dotnet/commands/dotnet-complete/CompleteCommandParser.cs index f8313355a..c42db5fc1 100644 --- a/src/dotnet/commands/dotnet-complete/CompleteCommandParser.cs +++ b/src/dotnet/commands/dotnet-complete/CompleteCommandParser.cs @@ -12,10 +12,10 @@ namespace Microsoft.DotNet.Cli Create.Command( "complete", "", Accept.ExactlyOneArgument - .With(name: "path") - .MaterializeAs(o => int.Parse(o.Arguments.Single())), + .With(name: "path"), Create.Option("--position", "", - Accept.ExactlyOneArgument - .With(name: "command"))); + Accept.ExactlyOneArgument + .With(name: "command") + .MaterializeAs(o => int.Parse(o.Arguments.Single())))); } } \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-restore/Program.cs b/src/dotnet/commands/dotnet-restore/Program.cs index 65ab11442..39d42a87f 100644 --- a/src/dotnet/commands/dotnet-restore/Program.cs +++ b/src/dotnet/commands/dotnet-restore/Program.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.MSBuild; @@ -22,9 +23,9 @@ namespace Microsoft.DotNet.Tools.Restore { DebugHelper.HandleDebugSwitch(ref args); - var parser = Parser.Instance["dotnet"]; + var parser = Parser.Instance; - var result = parser.Parse(args); + var result = parser.ParseFrom("dotnet restore", args); Reporter.Verbose.WriteLine(result.Diagram()); diff --git a/test/dotnet.Tests/ParserTests/RestoreParserTests.cs b/test/dotnet.Tests/ParserTests/RestoreParserTests.cs index 2dc32cde9..44733f907 100644 --- a/test/dotnet.Tests/ParserTests/RestoreParserTests.cs +++ b/test/dotnet.Tests/ParserTests/RestoreParserTests.cs @@ -22,9 +22,9 @@ namespace Microsoft.DotNet.Tests.ParserTests [Fact] public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsSpecified() { - var parser = Parser.Instance["dotnet"]; + var parser = Parser.Instance["dotnet"]["restore"]; - var result = parser.Parse(@"restore .\some.csproj --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); + var result = parser.Parse(@".\some.csproj --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); output.WriteLine(result.Diagram()); @@ -37,9 +37,9 @@ namespace Microsoft.DotNet.Tests.ParserTests [Fact] public void RestoreCapturesArgumentsToForwardToMSBuildWhenTargetIsNotSpecified() { - var parser = Parser.Instance["dotnet"]; + var parser = Parser.Instance["dotnet"]["restore"]; - var result = parser.Parse(@"restore --packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); + var result = parser.Parse(@"--packages c:\.nuget\packages /p:SkipInvalidConfigurations=true"); output.WriteLine(result.Diagram());