You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add SetHandler overrides for command returning an exit code #1570
The original CommandHandler.Create API supports handler functions that return int so that the command can return an exit code other than 0 if there's an error. It looks like the new SetHandler API requires that the handler function return either void or Task. Can we add overrides for int and Task<int> before GA? Thanks!
Reacted by Tau Gärtli, Tom Deseyn, Omair Majid, Brian Bowman and Cédric Belin
We had this same situation on the dotnet CLI, and the approach we took was to:
make our own ICommandHandler that fit the lambda signature we needed and transformed our lambdas into Task<int> returns
made an SetHandler extension method on the Command type (like is already done in this repo) for our specific use case to create our ICommandHandler instead
so not a ton of work, but a slight change in the expected API.
It's hard to know what's the right number of overloads. I think there are too many and it makes intellisense a bit confusing. (The source generator approach we've been experimenting with is basically just generating the exact signature you need.)
Doing away with return types (other than simple Task to support async) can help keep this under control and leaves the door open to have other return types from handlers, e.g. data objects that a rendering layer can then present in the console.
(These parameters can include types like CancellationToken or InvocationContext using a custom binder, which we'll simplify in beta 5 by introducing something like Bind.FromServiceProvider<T>().)
Those that provide an InvocationContext, like this:
This would be a usability improvement in some ways and not in others. IntelliSense is already quite crowded for this method.
Adding support for directly returning exit codes would nearly double this. We're looking for a more elegant solution and would love suggestions. This is related to #1776 so I'll add these details there.
I suppose another option that wouldn't result in an overload explosion is to require returning an exit code on all of the SetHandler APIs. It would be a fairly disruptive change and it's not clear to me which is the more common pattern.
(These parameters can include types like CancellationToken or InvocationContext using a custom binder, which we'll simplify in beta 5 by introducing something like Bind.FromServiceProvider().)
Ack. This is what I need to combine Options and InvocationContext with the strongly-typed Action. I'll wait for beta5.
I suppose another option that wouldn't result in an overload explosion is to require returning an exit code on all of the SetHandler APIs. It would be a fairly disruptive change and it's not clear to me which is the more common pattern.
I think that encourages the use of exit codes and printing user-friendly error messages over throwing Exception, which will be good for the end-user.
On Windows, there is some precedent for using a Win32 error code, HRESULT, or NTSTATUS as an exit code of a process, but those do not have complete enum types and it would be weird if one had to define an enum type just to satisfy SetHandler.
The proposed API would also allow command.SetHandler<Enum>(() => null), but that's easy to fix.
I mean, if you want to return Exception.HResult, it would look like this
command.SetHandler(()=>{try{
…
return(HResult)0;}catch(Exceptionexception){return(HResult)exception.HResult;}});// Type not used elsewhere, but SetHandler requires an enum for the exit codeenumHResult{}
The UseExceptionHandler, UseParseDirective, and UseParseErrorReporting methods of CommandLineBuilderExtensions take the exit code as an int? parameter. It would be unfortunate if SetHandler were inconsistent with these and supported enum types but not int.
That some of these work with int doesn't mean SetHandler needs to do the same. Similar to how ASP.NET doesn't make you work with HttpContext when implementing an endpoint.
Anyway, I'll be happy to have a directy way to control the exit code, be it an int or an enum.
This is no longer fixed. In #2095, the SetHandler(Func<InvocationContext, int>) and SetHandler(Func<InvocationContext, CancellationToken, Task<int>>) methods were replaced with SetAction(Action<InvocationContext>) and SetAction(Func<InvocationContext, CancellationToken, Task>), so it is no longer easy to return an exit code from a handler delegate, especially now that the InvocationContext.ExitCode property has been deleted. It can still be done but the solutions have drawbacks:
By deriving an application-specific class from CliAction and setting the Command.Action property. That requires somewhat more code.
By making System.CommandLine.SourceGenerator generate the application-specific action class. This is only available for C# and does not work in older versions of Visual Studio that are still supported.
Perhaps by using CommandHandler.Create from the System.CommandLine.NamingConventionBinder package. This would be an additional dependency that needs to be shipped with the application.
Activity
jonsequitur commentedon Jan 9, 2022
You can always take in an
InvocationContextin your handler and setInvocationContext.ExitCode.baronfel commentedon Jan 9, 2022
We had this same situation on the
dotnetCLI, and the approach we took was to:Task<int>returnsSetHandlerextension method on theCommandtype (like is already done in this repo) for our specific use case to create our ICommandHandler insteadso not a ton of work, but a slight change in the expected API.
jonsequitur commentedon Jan 9, 2022
It's hard to know what's the right number of overloads. I think there are too many and it makes intellisense a bit confusing. (The source generator approach we've been experimenting with is basically just generating the exact signature you need.)
Doing away with return types (other than simple
Taskto support async) can help keep this under control and leaves the door open to have other return types from handlers, e.g. data objects that a rendering layer can then present in the console.tmds commentedon Jun 29, 2022
Commands are expected to handle failure situations and turn them into exit codes.
I'm surprised
SetHandlerAPI doesn't foresee this.The current API encourages throwing exceptions over returning exit codes.
That gives a bad UX for the end-user.
tmds commentedon Jun 29, 2022
@jonsequitur does that mean I can't use any of the overloads that take a strongly-typed
Action<T1, ..., Tx>?jonsequitur commentedon Jun 29, 2022
In beta 4, in order to keep the combinatorial overload explosion to a minimum, we only provide two flavors of
SetHandler:Those that bind parameters for you, like this:
(These parameters can include types like
CancellationTokenorInvocationContextusing a custom binder, which we'll simplify in beta 5 by introducing something likeBind.FromServiceProvider<T>().)Those that provide an
InvocationContext, like this:Exceptions are caught and translated into non-zero exit codes by default, or you can assign the exit code to
InvocationContext.ExitCode.Ok, so where does that leave us? Right now there are 20
SetHandleroverloads, accepting these delegate types:ActionAction<InvocationContext>Action<T1>throughAction<T1, T2, T3, T4, T5, T6, T7, T8>Func<Task>Func<InvocationContext, Task>Func<T1, Task>throughAction<T1, T2, T3, T4, T5, T6, T7, T8, Task>If you return
Task<int>from any of theFunc*overloads, it will be used to set the exit code:The problem here is that you get a compiler error if you try to use an
asyncdelegate:Supporting directly returning an exit code would double the number of
SetHandleroverloads, adding:Func<int>Func<InvocationContext, int>Func<T1, int>throughAction<T1, T2, T3, T4, T5, T6, T7, T8, int>Func<Task<int>>Func<InvocationContext, Task<int>>Func<T1, Task<int>>throughAction<T1, T2, T3, T4, T5, T6, T7, T8, Task<int>>This would be a usability improvement in some ways and not in others. IntelliSense is already quite crowded for this method.
Adding support for directly returning exit codes would nearly double this. We're looking for a more elegant solution and would love suggestions. This is related to #1776 so I'll add these details there.
dgolub commentedon Jun 29, 2022
Is there a particular exception class that can be used to specify an exit code and error message, or do we need to define our own exceptions?
jonsequitur commentedon Jun 29, 2022
You could define you own if you wanted to take this approach. I don't encourage using exceptions to communicate with the user.
dgolub commentedon Jun 29, 2022
Right, which is why a number of us have been asking for a simple way to return an error code.
jonsequitur commentedon Jun 29, 2022
I suppose another option that wouldn't result in an overload explosion is to require returning an exit code on all of the
SetHandlerAPIs. It would be a fairly disruptive change and it's not clear to me which is the more common pattern.dgolub commentedon Jun 29, 2022
I would suspect that there are developers who have strong preferences for both, much like with
int main()andvoid main()in C/C++.tmds commentedon Jun 30, 2022
Ack. This is what I need to combine
Options andInvocationContextwith the strongly-typedAction. I'll wait for beta5.I think that encourages the use of exit codes and printing user-friendly error messages over throwing
Exception, which will be good for the end-user.Maybe also consider adding something like:
So users can write
return ExitCode.Success;overreturn 0;.tmds commentedon Nov 16, 2022
Maybe we should use enums to represent command results?
The
SetHandlermethods can have anenumconstraint:We can include a default
enum:Example with a custom
enum:tmds commentedon Nov 16, 2022
@KalleOlaviNiemitalo since you gave it a thumbs down, can you elaborate on what you don't like about the
enumsuggestion?KalleOlaviNiemitalo commentedon Nov 16, 2022
There's no way to constrain it to enums in which the underlying type is int.
KalleOlaviNiemitalo commentedon Nov 16, 2022
On Windows, there is some precedent for using a Win32 error code, HRESULT, or NTSTATUS as an exit code of a process, but those do not have complete enum types and it would be weird if one had to define an enum type just to satisfy SetHandler.
The proposed API would also allow
command.SetHandler<Enum>(() => null), but that's easy to fix.tmds commentedon Nov 16, 2022
Right, we can't constrain the
enumto have underlyinginttype. This is more an implementation concern.The alternatives are to use const fields or literal constants.
KalleOlaviNiemitalo commentedon Nov 16, 2022
I mean, if you want to return Exception.HResult, it would look like this
KalleOlaviNiemitalo commentedon Nov 17, 2022
The UseExceptionHandler, UseParseDirective, and UseParseErrorReporting methods of CommandLineBuilderExtensions take the exit code as an
int?parameter. It would be unfortunate if SetHandler were inconsistent with these and supported enum types but notint.command-line-api/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Lines 319 to 322 in 350a618
command-line-api/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Lines 495 to 497 in 350a618
command-line-api/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Lines 520 to 522 in 350a618
tmds commentedon Nov 17, 2022
That some of these work with
intdoesn't meanSetHandlerneeds to do the same. Similar to how ASP.NET doesn't make you work withHttpContextwhen implementing an endpoint.Anyway, I'll be happy to have a directy way to control the exit code, be it an
intor anenum.The maintainers can decide what they want.
tmds commentedon Nov 22, 2022
@jonsequitur if you decide what the API should be, I can look into implementing it, if you want.
tmds commentedon Mar 15, 2023
This is fixed by #2092.
KalleOlaviNiemitalo commentedon Mar 22, 2023
This is no longer fixed. In #2095, the SetHandler(Func<InvocationContext, int>) and SetHandler(Func<InvocationContext, CancellationToken, Task<int>>) methods were replaced with SetAction(Action<InvocationContext>) and SetAction(Func<InvocationContext, CancellationToken, Task>), so it is no longer easy to return an exit code from a handler delegate, especially now that the InvocationContext.ExitCode property has been deleted. It can still be done but the solutions have drawbacks:
See also #2109 for the async case.