Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,15 +353,13 @@ System.CommandLine.Help
public System.Boolean Equals(TwoColumnHelpRow other)
public System.Int32 GetHashCode()
System.CommandLine.Invocation
public interface IInvocationResult
public System.Void Apply(InvocationContext context)
public class InvocationContext, System.IDisposable
.ctor(System.CommandLine.ParseResult parseResult, System.CommandLine.IConsole console = null, System.Threading.CancellationToken cancellationToken = null)
public System.CommandLine.Binding.BindingContext BindingContext { get; }
public System.CommandLine.IConsole Console { get; set; }
public System.Int32 ExitCode { get; set; }
public System.CommandLine.Help.HelpBuilder HelpBuilder { get; }
public IInvocationResult InvocationResult { get; set; }
public System.Action<InvocationContext> InvocationResult { get; set; }
public System.CommandLine.LocalizationResources LocalizationResources { get; }
public System.CommandLine.Parsing.Parser Parser { get; }
public System.CommandLine.ParseResult ParseResult { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public async Task Unspecified_option_arguments_with_no_default_value_are_bound_t

await handler.InvokeAsync(invocationContext);

var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;

boundValue.Should().Be(expectedValue);
BoundValueCapturer.GetBoundValue(invocationContext).Should().Be(expectedValue);
}

[Theory]
Expand Down Expand Up @@ -125,7 +123,7 @@ public async Task Handler_method_receives_option_arguments_bound_to_the_specifie

await handler.InvokeAsync(invocationContext);

var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);

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

Expand Down Expand Up @@ -188,7 +186,7 @@ public async Task Handler_method_receives_command_arguments_bound_to_the_specifi

await handler.InvokeAsync(invocationContext);

var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);

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

Expand Down Expand Up @@ -240,7 +238,7 @@ public async Task Handler_method_receives_command_arguments_explicitly_bound_to_

await handler.InvokeAsync(invocationContext);

var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);

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

Expand Down Expand Up @@ -293,7 +291,7 @@ public async Task Handler_method_receive_option_arguments_explicitly_bound_to_th

await handler.InvokeAsync(invocationContext);

var boundValue = ((BoundValueCapturer)invocationContext.InvocationResult).BoundValue;
var boundValue = BoundValueCapturer.GetBoundValue(invocationContext);

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

Expand Down Expand Up @@ -323,26 +321,32 @@ public async Task Unexpected_return_types_result_in_exit_code_0_if_no_exception_

private static void CaptureMethod<T>(T value, InvocationContext invocationContext)
{
invocationContext.InvocationResult = new BoundValueCapturer(value);
BoundValueCapturer.Capture(value, invocationContext);

invocationContext.InvocationResult = ctx => BoundValueCapturer.Apply(ctx);
}

private static Action<T, InvocationContext> CaptureDelegate<T>()
{
return (value, invocationContext) => invocationContext.InvocationResult = new BoundValueCapturer(value);
}
=> (value, invocationContext) =>
{
BoundValueCapturer.Capture(value, invocationContext);

private class BoundValueCapturer : IInvocationResult
invocationContext.InvocationResult = ctx => BoundValueCapturer.Apply(ctx);
};

private static class BoundValueCapturer
{
public BoundValueCapturer(object boundValue)
private static readonly Dictionary<InvocationContext, object> _boundValues = new ();

public static void Apply(InvocationContext context)
{
BoundValue = boundValue;
}

public object BoundValue { get; }
public static void Capture(object value, InvocationContext invocationContext)
=> _boundValues.Add(invocationContext, value);

public void Apply(InvocationContext context)
{
}
public static object GetBoundValue(InvocationContext context)
=> _boundValues.TryGetValue(context, out var value) ? value : null;
}

internal static readonly BindingTestSet BindingCases = new()
Expand Down
12 changes: 6 additions & 6 deletions src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public static CommandLineBuilder UseParseDirective(
{
if (context.ParseResult.Directives.Contains("parse"))
{
context.InvocationResult = new ParseDirectiveResult(errorExitCode);
context.InvocationResult = ctx => ParseDirectiveResult.Apply(ctx, errorExitCode);
}
else
{
Expand All @@ -525,7 +525,7 @@ public static CommandLineBuilder UseParseErrorReporting(
{
if (context.ParseResult.Errors.Count > 0)
{
context.InvocationResult = new ParseErrorResult(errorExitCode);
context.InvocationResult = ctx => ParseErrorResult.Apply(ctx, errorExitCode);
}
else
{
Expand Down Expand Up @@ -560,7 +560,7 @@ public static CommandLineBuilder UseSuggestDirective(
position = context.ParseResult.CommandLineText?.Length ?? 0;
}

context.InvocationResult = new SuggestDirectiveResult(position);
context.InvocationResult = ctx => SuggestDirectiveResult.Apply(ctx, position);
}
else
{
Expand Down Expand Up @@ -649,7 +649,7 @@ public static CommandLineBuilder UseVersionOption(
{
if (context.ParseResult.Errors.Any(e => e.SymbolResult?.Symbol is VersionOption))
{
context.InvocationResult = new ParseErrorResult(null);
context.InvocationResult = static ctx => ParseErrorResult.Apply(ctx, null);
}
else
{
Expand Down Expand Up @@ -690,7 +690,7 @@ public static CommandLineBuilder UseVersionOption(
{
if (context.ParseResult.Errors.Any(e => e.SymbolResult?.Symbol is VersionOption))
{
context.InvocationResult = new ParseErrorResult(null);
context.InvocationResult = static ctx => ParseErrorResult.Apply(ctx, null);
}
else
{
Expand All @@ -712,7 +712,7 @@ private static bool ShowHelp(
{
if (context.ParseResult.FindResultFor(helpOption) is { })
{
context.InvocationResult = new HelpResult();
context.InvocationResult = HelpResult.Apply;
return true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/System.CommandLine/Help/HelpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

namespace System.CommandLine.Help
{
internal class HelpResult : IInvocationResult
internal static class HelpResult
{
/// <inheritdoc />
public void Apply(InvocationContext context)
internal static void Apply(InvocationContext context)
{
var output = context.Console.Out.CreateTextWriter();

Expand Down
17 changes: 0 additions & 17 deletions src/System.CommandLine/Invocation/IInvocationResult.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/System.CommandLine/Invocation/InvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public IConsole Console
/// The result of the current invocation.
/// </summary>
/// <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>
public IInvocationResult? InvocationResult { get; set; }
public Action<InvocationContext>? InvocationResult { get; set; }

/// <summary>
/// Gets a cancellation token that can be used to check if cancellation has been requested.
Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine/Invocation/InvocationPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static InvocationMiddleware BuildInvocationChain(InvocationContext conte

private static int GetExitCode(InvocationContext context)
{
context.InvocationResult?.Apply(context);
context.InvocationResult?.Invoke(context);

return context.ExitCode;
}
Expand Down
13 changes: 3 additions & 10 deletions src/System.CommandLine/Invocation/ParseDirectiveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@

namespace System.CommandLine.Invocation
{
internal class ParseDirectiveResult : IInvocationResult
internal static class ParseDirectiveResult
{
private readonly int? _errorExitCode;

public ParseDirectiveResult(int? errorExitCode)
{
_errorExitCode = errorExitCode;
}

public void Apply(InvocationContext context)
internal static void Apply(InvocationContext context, int? errorExitCode)
{
var parseResult = context.ParseResult;
context.Console.Out.WriteLine(parseResult.Diagram());
context.ExitCode = parseResult.Errors.Count == 0
? 0
: _errorExitCode ?? 1;
: errorExitCode ?? 1;
}
}
}
16 changes: 4 additions & 12 deletions src/System.CommandLine/Invocation/ParseErrorResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@

namespace System.CommandLine.Invocation
{
internal class ParseErrorResult : IInvocationResult
internal static class ParseErrorResult
{
private readonly int? _errorExitCode;

public ParseErrorResult(int? errorExitCode)
{
_errorExitCode = errorExitCode;
}

/// <inheritdoc />
public void Apply(InvocationContext context)
internal static void Apply(InvocationContext context, int? errorExitCode)
{
context.Console.ResetTerminalForegroundColor();
context.Console.SetTerminalForegroundRed();
Expand All @@ -28,11 +20,11 @@ public void Apply(InvocationContext context)

context.Console.Error.WriteLine();

context.ExitCode = _errorExitCode ?? 1;
context.ExitCode = errorExitCode ?? 1;

context.Console.ResetTerminalForegroundColor();

new HelpResult().Apply(context);
HelpResult.Apply(context);
}
}
}
13 changes: 3 additions & 10 deletions src/System.CommandLine/Invocation/SuggestDirectiveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@

namespace System.CommandLine.Invocation
{
internal class SuggestDirectiveResult : IInvocationResult
internal static class SuggestDirectiveResult
{
private readonly int _position;

public SuggestDirectiveResult(int position)
{
_position = position;
}

public void Apply(InvocationContext context)
internal static void Apply(InvocationContext context, int position)
{
var commandLineToComplete = context.ParseResult.Tokens.LastOrDefault(t => t.Type != TokenType.Directive)?.Value ?? "";

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

var completions = completionParseResult.GetCompletions(_position);
var completions = completionParseResult.GetCompletions(position);

context.Console.Out.WriteLine(
string.Join(
Expand Down