Skip to content

Return values from actions on Options? #2768

Open

Description

@jalfd

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 Option have 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:

  1. 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?)
  2. what is the intended way to detect that --help was invoked so you can terminate the application?

Activity

  1. jonsequitur commented on Feb 2, 2026

    @jonsequitur
    Contributor

    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:

    var rootCommand = new RootCommand
      {
        // options & arguments
      };
    rootCommand.SetAction((parseResult, cancellationToken) => /* 
      main application flow is started
    */);
    
    return rootCommand.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:

    var helpWasRequested = parseResult.GetResult("--help") is not null;
  2. jalfd commented on Feb 4, 2026

    @jalfd
    Author

    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?

  3. jonsequitur commented on Feb 4, 2026

    @jonsequitur
    Contributor

    These sounds like bugs. Can you provide a simple code example that reproduces the issue?

  4. jalfd commented on Feb 13, 2026

    @jalfd
    Author

    Done, see #2771 and #2772 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions