Skip to content

Commit e2cdee3

Browse files
adamsitnikjonsequitur
authored andcommitted
replace IInvocationResult with Action<InvocationContext>, fixes #1936
1 parent 0adeb26 commit e2cdee3

10 files changed

+43
-81
lines changed

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

+1-3
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,13 @@ System.CommandLine.Help
332332
public System.Boolean Equals(TwoColumnHelpRow other)
333333
public System.Int32 GetHashCode()
334334
System.CommandLine.Invocation
335-
public interface IInvocationResult
336-
public System.Void Apply(InvocationContext context)
337335
public class InvocationContext, System.IDisposable
338336
.ctor(System.CommandLine.ParseResult parseResult, System.CommandLine.IConsole console = null, System.Threading.CancellationToken cancellationToken = null)
339337
public System.CommandLine.Binding.BindingContext BindingContext { get; }
340338
public System.CommandLine.IConsole Console { get; set; }
341339
public System.Int32 ExitCode { get; set; }
342340
public System.CommandLine.Help.HelpBuilder HelpBuilder { get; }
343-
public IInvocationResult InvocationResult { get; set; }
341+
public System.Action<InvocationContext> InvocationResult { get; set; }
344342
public System.CommandLine.LocalizationResources LocalizationResources { get; }
345343
public System.CommandLine.Parsing.Parser Parser { get; }
346344
public System.CommandLine.ParseResult ParseResult { get; set; }

src/System.CommandLine.NamingConventionBinder.Tests/ModelBindingCommandHandlerTests.cs

+22-18
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public async Task Unspecified_option_arguments_with_no_default_value_are_bound_t
4646

4747
await handler.InvokeAsync(invocationContext);
4848

49-
var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
50-
51-
boundValue.Should().Be(expectedValue);
49+
BoundValueCapturer.GetBoundValue(invocationContext).Should().Be(expectedValue);
5250
}
5351

5452
[Theory]
@@ -125,7 +123,7 @@ public async Task Handler_method_receives_option_arguments_bound_to_the_specifie
125123

126124
await handler.InvokeAsync(invocationContext);
127125

128-
var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
126+
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);
129127

130128
boundValue.Should().BeAssignableTo(testCase.ParameterType);
131129

@@ -188,7 +186,7 @@ public async Task Handler_method_receives_command_arguments_bound_to_the_specifi
188186

189187
await handler.InvokeAsync(invocationContext);
190188

191-
var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
189+
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);
192190

193191
boundValue.Should().BeOfType(testCase.ParameterType);
194192

@@ -240,7 +238,7 @@ public async Task Handler_method_receives_command_arguments_explicitly_bound_to_
240238

241239
await handler.InvokeAsync(invocationContext);
242240

243-
var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
241+
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);
244242

245243
boundValue.Should().BeOfType(testCase.ParameterType);
246244

@@ -293,7 +291,7 @@ public async Task Handler_method_receive_option_arguments_explicitly_bound_to_th
293291

294292
await handler.InvokeAsync(invocationContext);
295293

296-
var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
294+
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);
297295

298296
boundValue.Should().BeOfType(testCase.ParameterType);
299297

@@ -323,26 +321,32 @@ public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_
323321

324322
private static void CaptureMethod<T>(T value, InvocationContext invocationContext)
325323
{
326-
invocationContext.InvocationResult = new BoundValueCapturer(value);
324+
BoundValueCapturer.Capture(value, invocationContext);
325+
326+
invocationContext.InvocationResult = ctx => BoundValueCapturer.Apply(ctx);
327327
}
328328

329329
private static Action<T, InvocationContext> CaptureDelegate<T>()
330-
{
331-
return (value, invocationContext) => invocationContext.InvocationResult = new BoundValueCapturer(value);
332-
}
330+
=> (value, invocationContext) =>
331+
{
332+
BoundValueCapturer.Capture(value, invocationContext);
333333

334-
private class BoundValueCapturer : IInvocationResult
334+
invocationContext.InvocationResult = ctx => BoundValueCapturer.Apply(ctx);
335+
};
336+
337+
private static class BoundValueCapturer
335338
{
336-
public BoundValueCapturer(object boundValue)
339+
private static readonly Dictionary<InvocationContext, object> _boundValues = new ();
340+
341+
public static void Apply(InvocationContext context)
337342
{
338-
BoundValue = boundValue;
339343
}
340344

341-
public object BoundValue { get; }
345+
public static void Capture(object value, InvocationContext invocationContext)
346+
=> _boundValues.Add(invocationContext, value);
342347

343-
public void Apply(InvocationContext context)
344-
{
345-
}
348+
public static object GetBoundValue(InvocationContext context)
349+
=> _boundValues.TryGetValue(context, out var value) ? value : null;
346350
}
347351

348352
internal static readonly BindingTestSet BindingCases = new()

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public static CommandLineBuilder UseParseDirective(
500500
{
501501
if (context.ParseResult.Directives.ContainsKey("parse"))
502502
{
503-
context.InvocationResult = new ParseDirectiveResult(errorExitCode);
503+
context.InvocationResult = ctx => ParseDirectiveResult.Apply(ctx, errorExitCode);
504504
}
505505
else
506506
{
@@ -525,7 +525,7 @@ public static CommandLineBuilder UseParseErrorReporting(
525525
{
526526
if (context.ParseResult.Errors.Count > 0)
527527
{
528-
context.InvocationResult = new ParseErrorResult(errorExitCode);
528+
context.InvocationResult = ctx => ParseErrorResult.Apply(ctx, errorExitCode);
529529
}
530530
else
531531
{
@@ -560,7 +560,7 @@ public static CommandLineBuilder UseSuggestDirective(
560560
position = context.ParseResult.CommandLineText?.Length ?? 0;
561561
}
562562

563-
context.InvocationResult = new SuggestDirectiveResult(position);
563+
context.InvocationResult = ctx => SuggestDirectiveResult.Apply(ctx, position);
564564
}
565565
else
566566
{
@@ -649,7 +649,7 @@ public static CommandLineBuilder UseVersionOption(
649649
{
650650
if (context.ParseResult.Errors.Any(e => e.SymbolResult?.Symbol is VersionOption))
651651
{
652-
context.InvocationResult = new ParseErrorResult(null);
652+
context.InvocationResult = static ctx => ParseErrorResult.Apply(ctx, null);
653653
}
654654
else
655655
{
@@ -690,7 +690,7 @@ public static CommandLineBuilder UseVersionOption(
690690
{
691691
if (context.ParseResult.Errors.Any(e => e.SymbolResult?.Symbol is VersionOption))
692692
{
693-
context.InvocationResult = new ParseErrorResult(null);
693+
context.InvocationResult = static ctx => ParseErrorResult.Apply(ctx, null);
694694
}
695695
else
696696
{
@@ -712,7 +712,7 @@ private static bool ShowHelp(
712712
{
713713
if (context.ParseResult.FindResultFor(helpOption) is { })
714714
{
715-
context.InvocationResult = new HelpResult();
715+
context.InvocationResult = HelpResult.Apply;
716716
return true;
717717
}
718718

src/System.CommandLine/Help/HelpResult.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
namespace System.CommandLine.Help
88
{
9-
internal class HelpResult : IInvocationResult
9+
internal static class HelpResult
1010
{
11-
/// <inheritdoc />
12-
public void Apply(InvocationContext context)
11+
internal static void Apply(InvocationContext context)
1312
{
1413
var output = context.Console.Out.CreateTextWriter();
1514

src/System.CommandLine/Invocation/IInvocationResult.cs

-17
This file was deleted.

src/System.CommandLine/Invocation/InvocationContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public IConsole Console
105105
/// The result of the current invocation.
106106
/// </summary>
107107
/// <remarks>As the <see cref="InvocationContext"/> is passed through the invocation pipeline to the <see cref="ICommandHandler"/> associated with the invoked command, only the last value of this property will be the one applied.</remarks>
108-
public IInvocationResult? InvocationResult { get; set; }
108+
public Action<InvocationContext>? InvocationResult { get; set; }
109109

110110
/// <summary>
111111
/// Gets a cancellation token that can be used to check if cancellation has been requested.

src/System.CommandLine/Invocation/InvocationPipeline.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private static InvocationMiddleware BuildInvocationChain(InvocationContext conte
9393

9494
private static int GetExitCode(InvocationContext context)
9595
{
96-
context.InvocationResult?.Apply(context);
96+
context.InvocationResult?.Invoke(context);
9797

9898
return context.ExitCode;
9999
}

src/System.CommandLine/Invocation/ParseDirectiveResult.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,15 @@
66

77
namespace System.CommandLine.Invocation
88
{
9-
internal class ParseDirectiveResult : IInvocationResult
9+
internal static class ParseDirectiveResult
1010
{
11-
private readonly int? _errorExitCode;
12-
13-
public ParseDirectiveResult(int? errorExitCode)
14-
{
15-
_errorExitCode = errorExitCode;
16-
}
17-
18-
public void Apply(InvocationContext context)
11+
internal static void Apply(InvocationContext context, int? errorExitCode)
1912
{
2013
var parseResult = context.ParseResult;
2114
context.Console.Out.WriteLine(parseResult.Diagram());
2215
context.ExitCode = parseResult.Errors.Count == 0
2316
? 0
24-
: _errorExitCode ?? 1;
17+
: errorExitCode ?? 1;
2518
}
2619
}
2720
}

src/System.CommandLine/Invocation/ParseErrorResult.cs

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,9 @@
66

77
namespace System.CommandLine.Invocation
88
{
9-
internal class ParseErrorResult : IInvocationResult
9+
internal static class ParseErrorResult
1010
{
11-
private readonly int? _errorExitCode;
12-
13-
public ParseErrorResult(int? errorExitCode)
14-
{
15-
_errorExitCode = errorExitCode;
16-
}
17-
18-
/// <inheritdoc />
19-
public void Apply(InvocationContext context)
11+
internal static void Apply(InvocationContext context, int? errorExitCode)
2012
{
2113
context.Console.ResetTerminalForegroundColor();
2214
context.Console.SetTerminalForegroundRed();
@@ -28,11 +20,11 @@ public void Apply(InvocationContext context)
2820

2921
context.Console.Error.WriteLine();
3022

31-
context.ExitCode = _errorExitCode ?? 1;
23+
context.ExitCode = errorExitCode ?? 1;
3224

3325
context.Console.ResetTerminalForegroundColor();
3426

35-
new HelpResult().Apply(context);
27+
HelpResult.Apply(context);
3628
}
3729
}
3830
}

src/System.CommandLine/Invocation/SuggestDirectiveResult.cs

+3-10
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,15 @@
77

88
namespace System.CommandLine.Invocation
99
{
10-
internal class SuggestDirectiveResult : IInvocationResult
10+
internal static class SuggestDirectiveResult
1111
{
12-
private readonly int _position;
13-
14-
public SuggestDirectiveResult(int position)
15-
{
16-
_position = position;
17-
}
18-
19-
public void Apply(InvocationContext context)
12+
internal static void Apply(InvocationContext context, int position)
2013
{
2114
var commandLineToComplete = context.ParseResult.Tokens.LastOrDefault(t => t.Type != TokenType.Directive)?.Value ?? "";
2215

2316
var completionParseResult = context.Parser.Parse(commandLineToComplete);
2417

25-
var completions = completionParseResult.GetCompletions(_position);
18+
var completions = completionParseResult.GetCompletions(position);
2619

2720
context.Console.Out.WriteLine(
2821
string.Join(

0 commit comments

Comments
 (0)