Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools: Revert incorrect change to CommandLineApplication #24345

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions src/ef/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,54 +107,62 @@ public CommandArgument Argument(string name, string description, Action<CommandA
public int Execute(params string[] args)
{
var command = this;
IEnumerator<CommandArgument>? arguments = null;

if (HandleResponseFiles)
{
args = ExpandResponseFiles(args).ToArray();
}

for (var index = 0; index < args.Length; index++)
try
{
var arg = args[index];

var isLongOption = arg.StartsWith("--", StringComparison.Ordinal);
if (isLongOption || arg.StartsWith("-", StringComparison.Ordinal))
for (var index = 0; index < args.Length; index++)
{
var result = ParseOption(isLongOption, command, args, ref index, out var option);
if (result == ParseOptionResult.ShowHelp)
{
command.ShowHelp();
return 0;
}
var arg = args[index];

if (result == ParseOptionResult.ShowVersion)
{
command.ShowVersion();
return 0;
}
}
else
{
var subcommand = ParseSubCommand(arg, command);
if (subcommand != null)
var isLongOption = arg.StartsWith("--", StringComparison.Ordinal);
if (isLongOption || arg.StartsWith("-", StringComparison.Ordinal))
{
command = subcommand;
var result = ParseOption(isLongOption, command, args, ref index, out var option);
if (result == ParseOptionResult.ShowHelp)
{
command.ShowHelp();
return 0;
}

if (result == ParseOptionResult.ShowVersion)
{
command.ShowVersion();
return 0;
}
}
else
{
using var arguments = new CommandArgumentEnumerator(command.Arguments.GetEnumerator());

if (arguments.MoveNext())
var subcommand = ParseSubCommand(arg, command);
if (subcommand != null)
{
arguments.Current.Values.Add(arg);
command = subcommand;
}
else
{
HandleUnexpectedArg(command, args, index, argTypeName: "command or argument");
arguments ??= new CommandArgumentEnumerator(command.Arguments.GetEnumerator());

if (arguments.MoveNext())
{
arguments.Current.Values.Add(arg);
}
else
{
HandleUnexpectedArg(command, args, index, argTypeName: "command or argument");
}
}
}
}
}
finally
{
arguments?.Dispose();
}

return command.Invoke(command.ApplicationArguments.ToArray());
}
Expand Down
28 changes: 28 additions & 0 deletions test/ef.Tests/CommandLineUtils/CommandLineApplicationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Xunit;

namespace Microsoft.DotNet.Cli.CommandLine
{
public class CommandLineApplicationTests
{
[Fact]
public void Execute_can_parse_multiple_arguments()
{
var app = new CommandLineApplication();
var one = app.Argument("<ONE>", "Argument one.");
var two = app.Argument("<TWO>", "Argument two.");
app.OnExecute(
_ =>
{
Assert.Equal("1", one.Value);
Assert.Equal("2", two.Value);

return 0;
});

app.Execute("1", "2");
}
}
}