8000 Replace | with or when searching for Argument value by tdashworth · Pull Request #1053 · dotnet/command-line-api · GitHub
[go: up one dir, main page]

Skip to content

Replace | with or when searching for Argument value #1053

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

Merged
merged 4 commits into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
91 changes: 91 additions & 0 deletions src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs
8000
Original file line number Diff line number Diff line change
Expand Up @@ -415,5 +415,96 @@ public async Task Method_parameters_on_the_invoked_member_method_are_bound_to_ma
testClass.boundName.Should().Be("Gandalf");
testClass.boundAge.Should().Be(425);
}

[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names()
{
string boundName = default;
int boundAge = default;

void Execute(string name, int age)
{
boundName = name;
boundAge = age;
}

var command = new Command("command");
command.AddArgument(new Argument<int>("age"));
command.AddArgument(new Argument<string>("name"));
command.Handler = CommandHandler.Create<string, int>(Execute);

await command.InvokeAsync("command 425 Gandalf", _console);

boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}


[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_hyphenated_argument_names()
{
string boundFirstName = default;

void Execute(string firstName)
{
boundFirstName = firstName;
}

var command = new Command("command")
{
new Argument<string>("first-name")
};
command.Handler = CommandHandler.Create<string>(Execute);

await command.InvokeAsync("command Gandalf", _console);

boundFirstName.Should().Be("Gandalf");
}

[Fact]
public async Task Method_parameters_on_the_invoked_method_can_be_bound_to_argument_names_case_insensitively()
{
string boundName = default;
int boundAge = default;

void Execute(string name, int AGE)
{
boundName = name;
boundAge = AGE;
}

var command = new Command("command");
command.AddArgument(new Argument<int>("AGE"));
command.AddArgument(new Argument<string>("Name"));
command.Handler = CommandHandler.Create<string, int>(Execute);

await command.InvokeAsync("command 425 Gandalf", _console);

boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}

[Fact]
public async Task Method_parameters_on_the_invoked_method_are_bound_to_matching_argument_names_with_pipe_in()
{
string boundName = default;
int boundAge = default;

void Execute(string fullnameOrNickname, int age)
{
boundName = fullnameOrNickname;
boundAge = age;
}

var command = new Command("command");
command.AddArgument(new Argument<int>("age"));
command.AddArgument(new Argument<string>("fullname|nickname"));
command.Handler = CommandHandler.Create<string, int>(Execute);

await command.InvokeAsync("command 425 Gandalf", _console);

boundName.Should().Be("Gandalf");
boundAge.Should().Be(425);
}
}
}
4 changes: 2 additions & 2 deletions src/System.CommandLine/Parsing/CommandResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static bool TryGetValueForArgument(
{
foreach (var argument in commandResult.Command.Arguments)
{
if (valueName.IsMatch(argument.Name))
if (valueName.IsMatch(argument.Name) || valueName.IsMatch(argument.Name.Replace("|","or")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should maybe be moved to the IsMatch method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I had considered not doing to before to allow the or with and without the replacement. After realising that a | is invalid in a parameter name - this shouldn't cause any breaking changes.

{
value = commandResult.ArgumentConversionResults[argument.Name]?.GetValueOrDefault();
return true;
Expand Down Expand Up @@ -84,4 +84,4 @@ internal static bool TryGetValueForOption(
return false;
}
}
}
}
0