-
Notifications
You must be signed in to change notification settings - Fork 419
Open
Description
If you set an Action on an Option object, it is not automatically invoked.
It is only invoked if the Command object also has an action.
The following reproduces the problem, as the first test fails, but the second one passes:
using System.CommandLine;
using System.CommandLine.Invocation;
namespace CommandLineTest
{
[TestClass]
public sealed class CommandLineTests
{
[TestMethod]
public void OptionActionIsInvokedWithoutCommandAction()
{
bool actionWasInvoked = false;
RootCommand root = new("CommandLine Test");
Option<bool> option = new("--arg");
option.Action = new CustomAction(_ =>
{
actionWasInvoked = true;
return 0;
});
root.Options.Add(option);
ParseResult parsedArgs = root.Parse(["--arg"]);
int result = parsedArgs.Invoke();
Assert.IsTrue(actionWasInvoked);
}
[TestMethod]
public void OptionActionIsInvokedWithCommandAction()
{
bool actionWasInvoked = false;
RootCommand root = new("CommandLine Test");
Option<bool> option = new("--arg");
option.Action = new CustomAction(_ =>
{
actionWasInvoked = true;
return 0;
});
root.Options.Add(option);
root.SetAction(r => { });
ParseResult parsedArgs = root.Parse(["--arg"]);
int result = parsedArgs.Invoke();
Assert.IsTrue(actionWasInvoked);
}
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;
}
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels