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 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
91 changes: 91 additions & 0 deletions src/System.CommandLine.Tests/Invocation/CommandHandlerTests.cs
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);
}
}
}
3 changes: 2 additions & 1 deletion src/System.CommandLine/Binding/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ internal static class Binder
{
internal static bool IsMatch(this string parameterName, string alias) =>
string.Equals(alias?.RemovePrefix()
.Replace("-", ""),
.Replace("-", "")
.Replace("|", "or"),
parameterName,
StringComparison.OrdinalIgnoreCase);

Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Parsing/CommandResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ internal static bool TryGetValueForOption(
return false;
}
}
}
}
0