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
I'm trying to work out the "right" way to use this library, and a very simple use case that puzzles me is "detect that --help was invoked, so we can terminate the application after handling arguments", and it is not really clear to me how that should be done.
My first thought was that I should attach an Action to the Option, and have that action return non-zero, which would propagate out to the top-level Invoke call. Then I'd check the return value there, and if non-zero, terminate the application.
But if an Option's action returns non-zero the return value just seems to be discarded.
I also noted that none of the documentation seems to use actions on Option objects, and that while actions on Command objects can just be lambdas, the ones on Optionhave to be a class derived from CommandLineAction. It kind of feels like Option actions are a bit half-baked? Are they not supposed to be used?
So two questions:
what is the point of setting actions on an Option object? (And what is the point of being able to return an integer from those actions?)
what is the intended way to detect that --help was invoked so you can terminate the application?
Typically people put the main action of the app inside a command handler, in which case the flow of the application will be to terminate after --help is invoked. That top-level code flow looks something like this:
varrootCommand=newRootCommand{// options & arguments};rootCommand.SetAction((parseResult,cancellationToken)=>/* main application flow is started*/);returnrootCommand.Parse(args).InvokeAsync();
HelpOption is present on RootCommand by default and it has its own handler.
what is the point of setting actions on an Option object? (And what is the point of being able to return an integer from those actions?)
SetAction is just syntactic sugar which creates and assigns an (internal) AnonymousSynchronousCommandLineAction or AnonymousAsynchronousCommandLineAction to a command. It was left off for Option because it was considered to be relatively less common scenario than setting handlers on Command.
what is the intended way to detect that --help was invoked so you can terminate the application?
You can look up results by name once you have a ParseResult:
varhelpWasRequested=parseResult.GetResult("--help")is not null;
So am I correct that return values from actions on options are discarded? You can attach an action (but as you say it is uncommon to do so, and it doesn't have the same syntactic sugar), but if you do, its return value is silently discarded? That seems like something that should be mentioned in the documentation :)
As an aside, it seems like actions on options are only invoked if you also set an action on the Command object. Is that intentional or a bug?
Activity
jonsequitur commentedon Feb 2, 2026
Typically people put the main action of the app inside a command handler, in which case the flow of the application will be to terminate after
--helpis invoked. That top-level code flow looks something like this:HelpOptionis present onRootCommandby default and it has its own handler.SetActionis just syntactic sugar which creates and assigns an (internal)AnonymousSynchronousCommandLineActionorAnonymousAsynchronousCommandLineActionto a command. It was left off forOptionbecause it was considered to be relatively less common scenario than setting handlers onCommand.You can look up results by name once you have a
ParseResult:jalfd commentedon Feb 4, 2026
Thank you for the explanation!
So am I correct that return values from actions on options are discarded? You can attach an action (but as you say it is uncommon to do so, and it doesn't have the same syntactic sugar), but if you do, its return value is silently discarded? That seems like something that should be mentioned in the documentation :)
As an aside, it seems like actions on options are only invoked if you also set an action on the Command object. Is that intentional or a bug?
jonsequitur commentedon Feb 4, 2026
These sounds like bugs. Can you provide a simple code example that reproduces the issue?
jalfd commentedon Feb 13, 2026
Done, see #2771 and #2772 :)