diff --git a/Microsoft.DotNet.Cli.sln b/Microsoft.DotNet.Cli.sln index 8d17ab89e..1fed5abc3 100644 --- a/Microsoft.DotNet.Cli.sln +++ b/Microsoft.DotNet.Cli.sln @@ -103,6 +103,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-dependency-tool-invo EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.DotNet.Compiler.Common.Tests", "test\Microsoft.DotNet.Compiler.Common.Tests\Microsoft.DotNet.Compiler.Common.Tests.xproj", "{44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-run.Tests", "test\dotnet-run.Tests\dotnet-run.Tests.xproj", "{35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}" +EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Performance", "test\Performance\Performance.xproj", "{6A3095FF-A7C5-4300-85A9-C025C384401D}" EndProject Global @@ -725,6 +727,22 @@ Global {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5}.RelWithDebInfo|x64.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|x64.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Debug|x64.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|Any CPU.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|x64.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.Release|x64.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F}.RelWithDebInfo|x64.Build.0 = Release|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|Any CPU.Build.0 = Debug|Any CPU {6A3095FF-A7C5-4300-85A9-C025C384401D}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -789,6 +807,7 @@ Global {1AB5B24B-B317-4142-A5D1-A6E84F15BA34} = {ADA7052B-884B-4776-8B8D-D04191D0AA70} {C26A48BB-193F-450C-AB09-4D3324C78188} = {1AB5B24B-B317-4142-A5D1-A6E84F15BA34} {44E7D1AC-DCF1-4A18-9C22-F09E6BB302B5} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} + {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} {6A3095FF-A7C5-4300-85A9-C025C384401D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} EndGlobalSection EndGlobal diff --git a/src/dotnet/CommandLine/CommandLineApplication.cs b/src/dotnet/CommandLine/CommandLineApplication.cs index 68a750daf..334167287 100644 --- a/src/dotnet/CommandLine/CommandLineApplication.cs +++ b/src/dotnet/CommandLine/CommandLineApplication.cs @@ -44,6 +44,7 @@ namespace Microsoft.DotNet.Cli.CommandLine public Func ShortVersionGetter { get; set; } public List Commands { get; private set; } public bool HandleResponseFiles { get; set; } + public bool AllowArgumentSeparator { get; set; } public CommandLineApplication Command(string name, Action configuration, bool throwOnUnexpectedArg = true) @@ -129,10 +130,18 @@ namespace Microsoft.DotNet.Cli.CommandLine if (longOption != null) { processed = true; - option = command.Options.SingleOrDefault(opt => string.Equals(opt.LongName, longOption[0], StringComparison.Ordinal)); + string longOptionName = longOption[0]; + option = command.Options.SingleOrDefault(opt => string.Equals(opt.LongName, longOptionName, StringComparison.Ordinal)); if (option == null) { + if (string.IsNullOrEmpty(longOptionName) && !command._throwOnUnexpectedArg && AllowArgumentSeparator) + { + // a stand-alone "--" is the argument separator, so skip it and + // handle the rest of the args as unexpected args + index++; + } + HandleUnexpectedArg(command, args, index, argTypeName: "option"); break; } @@ -405,6 +414,11 @@ namespace Microsoft.DotNet.Cli.CommandLine } } + if (target.AllowArgumentSeparator) + { + headerBuilder.Append(" [[--] ...]]"); + } + headerBuilder.AppendLine(); var nameAndVersion = new StringBuilder(); diff --git a/src/dotnet/commands/dotnet-run/Program.cs b/src/dotnet/commands/dotnet-run/Program.cs index d02be0c91..7d50649c1 100644 --- a/src/dotnet/commands/dotnet-run/Program.cs +++ b/src/dotnet/commands/dotnet-run/Program.cs @@ -13,21 +13,18 @@ namespace Microsoft.DotNet.Tools.Run { DebugHelper.HandleDebugSwitch(ref args); - CommandLineApplication app = new CommandLineApplication(); + CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false); app.Name = "dotnet run"; app.FullName = ".NET Run Command"; app.Description = "Command used to run .NET apps"; app.HandleResponseFiles = true; + app.AllowArgumentSeparator = true; app.HelpOption("-h|--help"); CommandOption framework = app.Option("-f|--framework", "Compile a specific framework", CommandOptionType.SingleValue); CommandOption configuration = app.Option("-c|--configuration", "Configuration under which to build", CommandOptionType.SingleValue); CommandOption project = app.Option("-p|--project", "The path to the project to run (defaults to the current directory). Can be a path to a project.json or a project directory", CommandOptionType.SingleValue); - // TODO: this is not supporting args which can be switches (i.e. --test) - // TODO: we need to make a change in CommandLine utils or parse args ourselves. - CommandArgument runArgs = app.Argument("args", "Arguments to pass to the executable or script", multipleValues: true); - app.OnExecute(() => { RunCommand runCmd = new RunCommand(); @@ -35,7 +32,7 @@ namespace Microsoft.DotNet.Tools.Run runCmd.Framework = framework.Value(); runCmd.Configuration = configuration.Value(); runCmd.Project = project.Value(); - runCmd.Args = runArgs.Values; + runCmd.Args = app.RemainingArguments; return runCmd.Start(); }); diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs index 7319e416e..6b6d2f094 100644 --- a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/RunCommand.cs @@ -1,8 +1,9 @@ // 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 Microsoft.DotNet.Cli.Utils; +using System.Linq; using System.Threading.Tasks; +using Microsoft.DotNet.Cli.Utils; namespace Microsoft.DotNet.Tools.Test.Utilities { @@ -20,22 +21,18 @@ namespace Microsoft.DotNet.Tools.Test.Utilities private string ConfigurationOption => string.IsNullOrEmpty(_configuration) ? "" : $"-c {_configuration}"; - private string PreserveTemporaryOption => _preserveTemporary ? $"-t \"{_projectPath}\"" : ""; - private string AppArgsArgument => _appArgs; public RunCommand( string projectPath, string framework = "", string configuration = "", - bool preserveTemporary = false, string appArgs = "") : base("dotnet") { _projectPath = projectPath; _framework = framework; _configuration = configuration; - _preserveTemporary = preserveTemporary; _appArgs = appArgs; } @@ -57,6 +54,17 @@ namespace Microsoft.DotNet.Tools.Test.Utilities return base.ExecuteAsync(args); } - private string BuildArgs() => $"{ProjectPathOption} {FrameworkOption} {ConfigurationOption} {PreserveTemporaryOption} {AppArgsArgument}"; + private string BuildArgs() + { + return string.Join(" ", + new[] + { + ProjectPathOption, + FrameworkOption, + ConfigurationOption, + AppArgsArgument, + } + .Where(s => !string.IsNullOrEmpty(s))); + } } } diff --git a/test/dotnet-run.Tests/RunTests.cs b/test/dotnet-run.Tests/RunTests.cs index 41de0dd2e..4cb26bc11 100644 --- a/test/dotnet-run.Tests/RunTests.cs +++ b/test/dotnet-run.Tests/RunTests.cs @@ -82,18 +82,67 @@ namespace Microsoft.DotNet.Tools.Run.Tests .HaveStdOutContaining("Hélló Wórld!"); } - private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir) + [Fact] + public void ItPassesArgumentsToTheApp() { - // copy all the files to temp dir - foreach (var file in Directory.EnumerateFiles(projectDir)) - { - tempDir.CopyFile(file); - } + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("one --two -three") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 3 args:", + "arg: [one]", + "arg: [--two]", + "arg: [-three]")); } - private string GetProjectPath(TempDirectory projectDir) + [Fact] + public void ItPassesAllArgsAfterUnexpectedArg() { - return Path.Combine(projectDir.Path, "project.json"); + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("Hello -f") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 2 args:", + "arg: [Hello]", + "arg: [-f]")); + } + + [Fact] + public void ItHandlesArgSeparatorCorrectly() + { + TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppWithArgs") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(instance.TestRoot) + .ExecuteWithCapturedOutput("-- one two") + .Should() + .Pass() + .And + .HaveStdOutContaining( + JoinWithNewlines( + "Hello World!", + "I was passed 2 args:", + "arg: [one]", + "arg: [two]")); + } + + private static string JoinWithNewlines(params string[] values) + { + return string.Join(Environment.NewLine, values); } } }