Skip to content

Remove InvocationContext #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 28, 2023
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
6 changes: 3 additions & 3 deletions samples/RenderingPlayground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CommandLine.Invocation;
using System.CommandLine;
using System.CommandLine.IO;
using System.CommandLine.Rendering;
using System.CommandLine.Rendering.Views;
Expand All @@ -18,7 +18,7 @@ class Program
/// <summary>
/// Demonstrates various rendering capabilities.
/// </summary>
/// <param name="invocationContext"></param>
/// <param name="parseResult"></param>
/// <param name="sample">Renders a specified sample</param>
/// <param name="height">The height of the rendering area</param>
/// <param name="width">The width of the rendering area</param>
Expand All @@ -28,7 +28,7 @@ class Program
/// <param name="overwrite">Overwrite the specified region. (If not, scroll.)</param>
public static void Main(
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
InvocationContext invocationContext,
ParseResult parseResult,
SampleName sample = SampleName.Dir,
int? height = null,
int? width = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ System.CommandLine.Hosting
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLineDirectives(this Microsoft.Extensions.Configuration.IConfigurationBuilder config, System.CommandLine.ParseResult commandline, System.CommandLine.Directive directive)
public static class HostingExtensions
public static OptionsBuilder<TOptions> BindCommandLine<TOptions>(this OptionsBuilder<TOptions> optionsBuilder)
public static Microsoft.Extensions.Hosting.IHost GetHost(this System.CommandLine.Invocation.InvocationContext invocationContext)
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder)
public static System.CommandLine.Invocation.InvocationContext GetInvocationContext(this Microsoft.Extensions.Hosting.HostBuilderContext context)
public static Microsoft.Extensions.Hosting.IHost GetHost(this System.CommandLine.ParseResult parseResult)
public static System.CommandLine.ParseResult GetParseResult(this Microsoft.Extensions.Hosting.IHostBuilder hostBuilder)
public static System.CommandLine.ParseResult GetParseResult(this Microsoft.Extensions.Hosting.HostBuilderContext context)
public static System.CommandLine.Command UseCommandHandler<THandler>(this System.CommandLine.Command command)
public static System.CommandLine.CommandLineConfiguration UseHost(this System.CommandLine.CommandLineConfiguration builder, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
public static System.CommandLine.CommandLineConfiguration UseHost(this System.CommandLine.CommandLineConfiguration builder, System.Func<System.String[],Microsoft.Extensions.Hosting.IHostBuilder> hostBuilderFactory, System.Action<Microsoft.Extensions.Hosting.IHostBuilder> configureHost = null)
public static Microsoft.Extensions.Hosting.IHostBuilder UseInvocationLifetime(this Microsoft.Extensions.Hosting.IHostBuilder host, System.CommandLine.Invocation.InvocationContext invocation, System.Action<InvocationLifetimeOptions> configureOptions = null)
public static Microsoft.Extensions.Hosting.IHostBuilder UseInvocationLifetime(this Microsoft.Extensions.Hosting.IHostBuilder host, System.Action<InvocationLifetimeOptions> configureOptions = null)
public class InvocationLifetime, Microsoft.Extensions.Hosting.IHostLifetime
.ctor(Microsoft.Extensions.Options.IOptions<InvocationLifetimeOptions> options, Microsoft.Extensions.Hosting.IHostEnvironment environment, Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime, Microsoft.Extensions.Logging.ILoggerFactory loggerFactory = null)
public Microsoft.Extensions.Hosting.IHostApplicationLifetime ApplicationLifetime { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
System.CommandLine.NamingConventionBinder
public static class BindingContextExtensions
public static System.Void AddModelBinder(this System.CommandLine.Binding.BindingContext bindingContext, ModelBinder binder)
public static System.CommandLine.Binding.BindingContext GetBindingContext(this System.CommandLine.Invocation.InvocationContext ctx)
public static System.CommandLine.Binding.BindingContext GetBindingContext(this System.CommandLine.ParseResult parseResult)
public static ModelBinder GetOrCreateModelBinder(this System.CommandLine.Binding.BindingContext bindingContext, System.CommandLine.Binding.IValueDescriptor valueDescriptor)
public abstract class BindingHandler : System.CommandLine.CliAction
public System.CommandLine.Binding.BindingContext GetBindingContext(System.CommandLine.Invocation.InvocationContext invocationContext)
public System.CommandLine.Binding.BindingContext GetBindingContext(System.CommandLine.ParseResult parseResult)
public static class CommandHandler
public static BindingHandler Create(System.Delegate delegate)
public static BindingHandler Create<T>(Action<T> action)
Expand Down Expand Up @@ -114,8 +114,8 @@ System.CommandLine.NamingConventionBinder
public class ModelBindingCommandHandler : BindingHandler
public System.Void BindParameter(System.Reflection.ParameterInfo param, System.CommandLine.Argument argument)
public System.Void BindParameter(System.Reflection.ParameterInfo param, System.CommandLine.Option option)
public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null)
public System.Int32 Invoke(System.CommandLine.ParseResult parseResult)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.ParseResult parseResult, System.Threading.CancellationToken cancellationToken = null)
public class ModelDescriptor
public static ModelDescriptor FromType<T>()
public static ModelDescriptor FromType(System.Type type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ System.CommandLine
public static Argument<System.IO.FileSystemInfo> AcceptExistingOnly(this Argument<System.IO.FileSystemInfo> argument)
public static Argument<T> AcceptExistingOnly<T>(this Argument<T> argument)
public abstract class CliAction
public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null)
public System.Int32 Invoke(ParseResult parseResult)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(ParseResult parseResult, System.Threading.CancellationToken cancellationToken = null)
public class Command : Symbol, System.Collections.Generic.IEnumerable<Symbol>, System.Collections.IEnumerable
.ctor(System.String name, System.String description = null)
public CliAction Action { get; set; }
Expand All @@ -53,10 +53,10 @@ System.CommandLine
public System.Collections.Generic.IEnumerator<Symbol> GetEnumerator()
public ParseResult Parse(System.Collections.Generic.IReadOnlyList<System.String> args, CommandLineConfiguration configuration = null)
public ParseResult Parse(System.String commandLine, CommandLineConfiguration configuration = null)
public System.Void SetAction(System.Action<System.CommandLine.Invocation.InvocationContext> action)
public System.Void SetAction(System.Func<System.CommandLine.Invocation.InvocationContext,System.Int32> action)
public System.Void SetAction(System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task> action)
public System.Void SetAction(System.Func<System.CommandLine.Invocation.InvocationContext,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Int32>> action)
public System.Void SetAction(System.Action<ParseResult> action)
public System.Void SetAction(System.Func<ParseResult,System.Int32> action)
public System.Void SetAction(System.Func<ParseResult,System.Threading.CancellationToken,System.Threading.Tasks.Task> action)
public System.Void SetAction(System.Func<ParseResult,System.Threading.CancellationToken,System.Threading.Tasks.Task<System.Int32>> action)
public class CommandLineConfiguration
.ctor(Command rootCommand)
public System.Collections.Generic.List<Directive> Directives { get; }
Expand Down Expand Up @@ -190,8 +190,8 @@ System.CommandLine.Help
public class HelpAction : System.CommandLine.CliAction
.ctor()
public HelpBuilder Builder { get; set; }
public System.Int32 Invoke(System.CommandLine.Invocation.InvocationContext context)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.Invocation.InvocationContext context, System.Threading.CancellationToken cancellationToken = null)
public System.Int32 Invoke(System.CommandLine.ParseResult parseResult)
public System.Threading.Tasks.Task<System.Int32> InvokeAsync(System.CommandLine.ParseResult parseResult, System.Threading.CancellationToken cancellationToken = null)
public class HelpBuilder
.ctor(System.Int32 maxWidth = 2147483647)
public System.Int32 MaxWidth { get; }
Expand Down Expand Up @@ -235,12 +235,6 @@ System.CommandLine.Help
public System.Boolean Equals(System.Object obj)
public System.Boolean Equals(TwoColumnHelpRow other)
public System.Int32 GetHashCode()
System.CommandLine.Invocation
public class InvocationContext
.ctor(System.CommandLine.ParseResult parseResult)
public System.CommandLine.ParseResult ParseResult { get; set; }
public T GetValue<T>(Option<T> option)
public T GetValue<T>(Argument<T> argument)
System.CommandLine.Parsing
public class ArgumentResult : SymbolResult
public System.CommandLine.Argument Argument { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ private static RootCommand BuildCommand()
stringOption
};

command.SetAction(ctx =>
command.SetAction(parseResult =>
{
bool boolean = ctx.ParseResult.GetValue(boolOption);
string text = ctx.ParseResult.GetValue(stringOption);
bool boolean = parseResult.GetValue(boolOption);
string text = parseResult.GetValue(stringOption);
});

return command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Setup()
{
typeof(object).GetTypeInfo().Assembly.Location,
typeof(Enumerable).GetTypeInfo().Assembly.Location,
typeof(System.CommandLine.Invocation.InvocationContext).GetTypeInfo().Assembly.Location
typeof(System.CommandLine.ParseResult).GetTypeInfo().Assembly.Location
}
);
_testAssembly = Assembly.Load(File.ReadAllBytes(_testAssemblyFilePath));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Perf_XmlDocReader()
{
typeof(object).GetTypeInfo().Assembly.Location,
typeof(Enumerable).GetTypeInfo().Assembly.Location,
typeof(System.CommandLine.Invocation.InvocationContext).GetTypeInfo().Assembly.Location
typeof(ParseResult).GetTypeInfo().Assembly.Location
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/System.CommandLine.Benchmarks/Input/Sample1.Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Program
/// <param name="overwrite">Overwrite the specified region. (If not, scroll.)</param>
public static void Main(
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
InvocationContext invocationContext,
ParseResult parseResult,
SampleName sample = SampleName.Dir,
int? height = null,
int? width = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Rendering;
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Binding;
using System.CommandLine.Invocation;
using System.CommandLine.Tests.Binding;
using System.CommandLine.Tests.Utility;
using System.IO;
Expand Down Expand Up @@ -198,7 +197,6 @@ public async Task When_method_returns_Task_of_int_then_return_code_is_set_to_ret
}

[Theory]
[InlineData(typeof(InvocationContext))]
[InlineData(typeof(BindingContext))]
[InlineData(typeof(ParseResult))]
[InlineData(typeof(CancellationToken))]
Expand Down
3 changes: 0 additions & 3 deletions src/System.CommandLine.DragonFruit/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

using System.Collections.Generic;
using System.CommandLine.Binding;
using System.CommandLine.Help;
using System.CommandLine.Invocation;
using System.CommandLine.NamingConventionBinder;
using System.CommandLine.Utility;
using System.IO;
Expand Down Expand Up @@ -251,7 +249,6 @@ public static IEnumerable<Option> BuildOptions(this MethodInfo method)

var omittedTypes = new[]
{
typeof(InvocationContext),
typeof(BindingContext),
typeof(ParseResult),
typeof(CommandLineConfiguration),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Invocation;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
Expand Down Expand Up @@ -118,24 +117,20 @@ int Execute(int first, int second)
[Fact]
public async Task Can_generate_handler_with_well_know_parameters_types()
{
InvocationContext? boundInvocationContext = null;
ParseResult? boundParseResult = null;

void Execute(
InvocationContext invocationContext,
ParseResult parseResult)
{
boundInvocationContext = invocationContext;
boundParseResult = parseResult;
}

var command = new Command("command");

command.SetHandler<Action<InvocationContext, ParseResult>>(Execute);
command.SetHandler<Action<ParseResult>>(Execute);

await command.Parse("command").InvokeAsync();

boundInvocationContext.Should().NotBeNull();
boundParseResult.Should().NotBeNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ private class GeneratedHandler_{handlerCount} : {CliActionType}
}

builder.Append($@"
public override int Invoke(global::System.CommandLine.Invocation.InvocationContext context) => InvokeAsync(context, global::System.Threading.CancellationToken.None).GetAwaiter().GetResult();");
public override int Invoke(global::System.CommandLine.ParseResult context) => InvokeAsync(context, global::System.Threading.CancellationToken.None).GetAwaiter().GetResult();");

builder.Append($@"
public override async global::System.Threading.Tasks.Task<int> InvokeAsync(global::System.CommandLine.Invocation.InvocationContext context, global::System.Threading.CancellationToken cancellationToken)
public override async global::System.Threading.Tasks.Task<int> InvokeAsync(global::System.CommandLine.ParseResult context, global::System.Threading.CancellationToken cancellationToken)
{{");
builder.Append($@"
{invocation.InvokeContents()}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ArgumentParameter(string localName, INamedTypeSymbol type, ITypeSymbol va
}

public override string GetValueFromContext()
=> $"context.ParseResult.GetValue({LocalName})";
=> $"context.GetValue({LocalName})";

public override int GetHashCode()
=> base.GetHashCode();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public OptionParameter(string localName, INamedTypeSymbol type, ITypeSymbol valu
}

public override string GetValueFromContext()
=> $"context.ParseResult.GetValue({LocalName})";
=> $"context.GetValue({LocalName})";

public override int GetHashCode()
=> base.GetHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ParseResultParameter(ITypeSymbol parseResultType)
}

public override string GetValueFromContext()
=> "context.ParseResult";
=> "context";

public override int GetHashCode()
=> base.GetHashCode();
Expand Down
8 changes: 0 additions & 8 deletions src/System.CommandLine.Generator/WellKnownTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ namespace System.CommandLine.Generator
internal class WellKnownTypes
{
public INamedTypeSymbol ParseResult { get; }
public INamedTypeSymbol InvocationContext { get; }
public IEqualityComparer<ISymbol?> Comparer { get; }

public WellKnownTypes(Compilation compilation, IEqualityComparer<ISymbol?> comparer)
{
ParseResult = GetType("System.CommandLine.ParseResult");
InvocationContext = GetType("System.CommandLine.Invocation.InvocationContext");

INamedTypeSymbol GetType(string typeName)
=> compilation.GetTypeByMetadataName(typeName)
Expand All @@ -29,12 +27,6 @@ INamedTypeSymbol GetType(string typeName)

internal bool TryGet(ISymbol symbol, out Parameter? parameter)
{
if (Comparer.Equals(InvocationContext, symbol))
{
parameter = new InvocationContextParameter(InvocationContext);
return true;
}

if (Comparer.Equals(ParseResult, symbol))
{
parameter = new ParseResultParameter(ParseResult);
Expand Down
Loading