Merge pull request #6313 from wli3/fix-dotnetrun--passing-args
Fix dotnet run double dash passing arguments
This commit is contained in:
commit
d55ed34d2e
7 changed files with 116 additions and 21 deletions
|
@ -9,6 +9,10 @@ namespace MSBuildTestApp
|
|||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
if (args.Length > 0)
|
||||
{
|
||||
Console.WriteLine("echo args:" + string.Join(";", args));
|
||||
}
|
||||
Console.WriteLine("Hello World!");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Cli.CommandLine;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
|
@ -24,6 +24,12 @@ namespace Microsoft.DotNet.Cli
|
|||
|
||||
Console.WriteLine(result.Diagram());
|
||||
|
||||
if (result.UnparsedTokens.Any())
|
||||
{
|
||||
Console.WriteLine("Unparsed Tokens: ");
|
||||
Console.WriteLine(string.Join(" ", result.UnparsedTokens));
|
||||
}
|
||||
|
||||
var optionValuesToBeForwarded = result.AppliedCommand()
|
||||
.OptionValuesToBeForwarded();
|
||||
if (optionValuesToBeForwarded.Any())
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
// 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 Microsoft.DotNet.Cli.CommandLine;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.DotNet.Cli.Utils;
|
||||
using Microsoft.DotNet.Cli;
|
||||
using Parser = Microsoft.DotNet.Cli.Parser;
|
||||
|
@ -13,13 +13,22 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
{
|
||||
public static RunCommand FromArgs(string[] args, string msbuildPath = null)
|
||||
{
|
||||
var parser = Parser.Instance;
|
||||
|
||||
var result = parser.ParseFrom("dotnet run", args);
|
||||
var result = Parser.Instance.ParseFrom("dotnet run", args);
|
||||
|
||||
result.ShowHelpOrErrorIfAppropriate();
|
||||
|
||||
return result["dotnet"]["run"].Value<RunCommand>();
|
||||
var runCommand = result["dotnet"]["run"].Value<RunCommand>();
|
||||
return IncludingArgumentsAfterDoubleDash(runCommand, result.UnparsedTokens);
|
||||
}
|
||||
|
||||
private static RunCommand IncludingArgumentsAfterDoubleDash(
|
||||
RunCommand runCommand,
|
||||
IEnumerable<string> unparsedTokens)
|
||||
{
|
||||
return runCommand.MakeNewWithReplaced(
|
||||
args: runCommand.Args
|
||||
.Concat(unparsedTokens)
|
||||
.ToList());
|
||||
}
|
||||
|
||||
public static int Run(string[] args)
|
||||
|
@ -27,7 +36,7 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
DebugHelper.HandleDebugSwitch(ref args);
|
||||
|
||||
RunCommand cmd;
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
cmd = FromArgs(args);
|
||||
|
|
|
@ -13,11 +13,11 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
{
|
||||
public partial class RunCommand
|
||||
{
|
||||
public string Configuration { get; set; }
|
||||
public string Framework { get; set; }
|
||||
public bool NoBuild { get; set; }
|
||||
public string Project { get; set; }
|
||||
public IReadOnlyCollection<string> Args { get; set; }
|
||||
public string Configuration { get; private set; }
|
||||
public string Framework { get; private set; }
|
||||
public bool NoBuild { get; private set; }
|
||||
public string Project { get; private set; }
|
||||
public IReadOnlyCollection<string> Args { get; private set; }
|
||||
|
||||
private List<string> _args;
|
||||
private bool ShouldBuild => !NoBuild;
|
||||
|
@ -38,6 +38,34 @@ namespace Microsoft.DotNet.Tools.Run
|
|||
.ExitCode;
|
||||
}
|
||||
|
||||
public RunCommand(string configuration,
|
||||
string framework,
|
||||
bool noBuild,
|
||||
string project,
|
||||
IReadOnlyCollection<string> args)
|
||||
{
|
||||
Configuration = configuration;
|
||||
Framework = framework;
|
||||
NoBuild = noBuild;
|
||||
Project = project;
|
||||
Args = args;
|
||||
}
|
||||
|
||||
public RunCommand MakeNewWithReplaced(string configuration = null,
|
||||
string framework = null,
|
||||
bool? noBuild = null,
|
||||
string project = null,
|
||||
IReadOnlyCollection<string> args = null)
|
||||
{
|
||||
return new RunCommand(
|
||||
configuration ?? this.Configuration,
|
||||
framework ?? this.Framework,
|
||||
noBuild ?? this.NoBuild,
|
||||
project ?? this.Project,
|
||||
args ?? this.Args
|
||||
);
|
||||
}
|
||||
|
||||
private void EnsureProjectIsBuilt()
|
||||
{
|
||||
List<string> buildArgs = new List<string>();
|
||||
|
|
|
@ -15,14 +15,14 @@ namespace Microsoft.DotNet.Cli
|
|||
LocalizableStrings.AppFullName,
|
||||
treatUnmatchedTokensAsErrors: false,
|
||||
arguments: Accept.ZeroOrMoreArguments()
|
||||
.MaterializeAs(o => new RunCommand
|
||||
{
|
||||
Configuration = o.SingleArgumentOrDefault("--configuration"),
|
||||
Framework = o.SingleArgumentOrDefault("--framework"),
|
||||
NoBuild = o.HasOption("--no-build"),
|
||||
Project = o.SingleArgumentOrDefault("--project"),
|
||||
Args = o.Arguments
|
||||
}),
|
||||
.MaterializeAs(o => new RunCommand
|
||||
(
|
||||
configuration: o.SingleArgumentOrDefault("--configuration"),
|
||||
framework: o.SingleArgumentOrDefault("--framework"),
|
||||
noBuild: o.HasOption("--no-build"),
|
||||
project: o.SingleArgumentOrDefault("--project"),
|
||||
args: o.Arguments
|
||||
)),
|
||||
options: new[]
|
||||
{
|
||||
CommonOptions.HelpOption(),
|
||||
|
|
|
@ -181,5 +181,24 @@ namespace Microsoft.DotNet.Cli.Run.Tests
|
|||
.Should().Fail()
|
||||
.And.HaveStdErrContaining("--framework");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ItCanPassArgumentsToSubjectAppByDoubleDash()
|
||||
{
|
||||
const string testAppName = "MSBuildTestApp";
|
||||
var testInstance = TestAssets.Get(testAppName)
|
||||
.CreateInstance()
|
||||
.WithSourceFiles()
|
||||
.WithRestoreFiles();
|
||||
|
||||
var testProjectDirectory = testInstance.Root.FullName;
|
||||
|
||||
new RunCommand()
|
||||
.WithWorkingDirectory(testProjectDirectory)
|
||||
.ExecuteWithCapturedOutput("-- foo bar baz")
|
||||
.Should()
|
||||
.Pass()
|
||||
.And.HaveStdOutContaining("echo args:foo;bar;baz");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
29
test/dotnet.Tests/ParserTests/RunParserTests.cs
Normal file
29
test/dotnet.Tests/ParserTests/RunParserTests.cs
Normal file
|
@ -0,0 +1,29 @@
|
|||
// 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.Linq;
|
||||
using FluentAssertions;
|
||||
using Microsoft.DotNet.Tools.Run;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
using System;
|
||||
|
||||
namespace Microsoft.DotNet.Tests.ParserTests
|
||||
{
|
||||
public class RunParserTests
|
||||
{
|
||||
public RunParserTests(ITestOutputHelper output)
|
||||
{
|
||||
this.output = output;
|
||||
}
|
||||
|
||||
private readonly ITestOutputHelper output;
|
||||
|
||||
[Fact]
|
||||
public void RunParserCanGetArguementFromDoubleDash()
|
||||
{
|
||||
var runCommand = RunCommand.FromArgs(new[]{ "--", "foo" });
|
||||
runCommand.Args.Single().Should().Be("foo");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue