-
Notifications
You must be signed in to change notification settings - Fork 399
SetHandler improvements #1729
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
Conversation
@@ -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 => |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
There was a problem hiding this 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); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
These changes attempt to improve the clarity of the
SetHandler
methods by removing theparams
parameters and replacing them with a number ofIValueDescriptor<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.