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

SetHandler improvements #1729

Merged
merged 4 commits into from
May 7, 2022
Merged

Conversation

jonsequitur
Copy link
Contributor

These changes attempt to improve the clarity of the SetHandler methods by removing the params parameters and replacing them with a number of IValueDescriptor<T> parameters matching the number of generic type arguments.

Since DI-like behaviors are fairly non-obvious, I'll likely also experiment here with removing the internal service provider and see if BinderBase<T> can fulfill its role.

@@ -44,7 +44,7 @@ public async Task Sets_output_mode_to_Ansi_when_specified_by_output_directive(Ou
OutputMode detectedOutputMode = OutputMode.Auto;

var command = new Command("hello");
command.SetHandler((InvocationContext ctx) =>
command.SetHandler(ctx =>
Copy link
Member

Choose a reason for hiding this comment

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

it's really nice for the simple handlers to be naturally-typed here (and in the other 1000 callsites in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Settling on InvocationContext for the case where we do this (i.e. removing support for ParseResult, etc. as parameters) reduces a lot of DI-ish complexity in these handlers. If we pull this thread hard enough, the need for the service provider might disappear.

Copy link
Member

@baronfel baronfel left a comment

Choose a reason for hiding this comment

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

Nothing major, but man those simple callsites look a lot nicer.

Copy link
Member

@Keboo Keboo left a comment

Choose a reason for hiding this comment

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

Looks good. Might be worth reviewing System.CommandLine.Generator after this merges, as I suspect it could be cleaned up a little bit to leverage the InvocationContext SetHandler overload.

{
var valueSource = ValueDescriptorDefaultValueSource.Instance;

valueSource.TryGetValue(valueDescriptor, context, out var value);
Copy link
Member

Choose a reason for hiding this comment

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

nit: I dislike var here as it is not clear what the type of value is.

{
var valueSource = ValueDescriptorDefaultValueSource.Instance;

valueSource.TryGetValue(valueDescriptor, context, out var value);
Copy link
Member

Choose a reason for hiding this comment

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

Should we care about the case where TryGetValue returns false? Should there be something line BoundValue.None to return in that case?

@jonsequitur jonsequitur marked this pull request as ready for review May 7, 2022 17:14
@jonsequitur jonsequitur merged commit 55dbf39 into dotnet:main May 7, 2022
@jonsequitur jonsequitur deleted the SetHandler-improvements branch June 17, 2022 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants