Skip to content

The return value from Option actions is discarded #2772

Closed

Description

@jalfd

If you set an Action on an Option object, it is required to return an integer, but the return value is silently discarded
The problem is on this line, where the return value is discarded.

The following reproduces the problem

using System.CommandLine;
using System.CommandLine.Invocation;

namespace CommandLineTest
{
    [TestClass]
    public sealed class CommandLineTests
    {
        [TestMethod]
        public void OptionActionReturnValuePropagates()
        {
            RootCommand root = new("CommandLine Test");

            Option<bool> option = new("--arg");
            option.Action = new CustomAction(_ => 42);
            root.Options.Add(option);
            root.SetAction(r => { });

            ParseResult parsedArgs = root.Parse(["--arg"]);
            int result = parsedArgs.Invoke();

            Assert.AreEqual(42, result);
        }

        class CustomAction : SynchronousCommandLineAction
        {
            private readonly Func<ParseResult, int> _callback;

            internal CustomAction(Func<ParseResult, int> callback) => _callback = callback;
            public override int Invoke(ParseResult parseResult) => _callback(parseResult);
            public override bool Terminating => false;
        }
    }
}

Activity

  1. added a commit that references this issue on Mar 11, 2026
  2. added a commit that references this issue on Apr 9, 2026
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