Skip to content
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

Remove CompletionDelegate, CompletionSourceList and DirectiveCollection #1946

Merged
merged 6 commits into from
Nov 4, 2022
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public IEnumerable<object> GenerateTestParseResults()

[Benchmark]
[ArgumentsSource(nameof(GenerateTestInputs))]
public DirectiveCollection ParseResult_Directives(string input)
public IReadOnlyDictionary<string, IReadOnlyList<string>> ParseResult_Directives(string input)
=> _testParser.Parse(input).Directives;

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static IConfigurationBuilder AddCommandLineDirectives(
if (name is null)
throw new ArgumentNullException(nameof(name));

if (!commandline.Directives.TryGetValues(name, out var directives))
if (!commandline.Directives.TryGetValue(name, out var directives))
return config;

var kvpSeparator = new[] { '=' };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static CommandLineBuilder UseAnsiTerminalWhenAvailable(
internal static bool PreferVirtualTerminal(
this BindingContext context)
{
if (context.ParseResult.Directives.TryGetValues(
if (context.ParseResult.Directives.TryGetValue(
"enable-vt",
out var trueOrFalse))
{
Expand All @@ -45,7 +45,7 @@ internal static bool PreferVirtualTerminal(

public static OutputMode OutputMode(this BindingContext context)
{
if (context.ParseResult.Directives.TryGetValues(
if (context.ParseResult.Directives.TryGetValue(
"output",
out var modeString) &&
Enum.TryParse<OutputMode>(
Expand Down
14 changes: 7 additions & 7 deletions src/System.CommandLine.Tests/DirectiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Raw_tokens_still_hold_directives()

var result = option.Parse("[parse] -y");

result.Directives.Contains("parse").Should().BeTrue();
result.Directives.ContainsKey("parse").Should().BeTrue();
result.Tokens.Should().Contain(t => t.Value == "[parse]");
}

Expand All @@ -39,7 +39,7 @@ public void Directives_should_parse_into_the_directives_collection()

var result = option.Parse("[parse] -y");

result.Directives.Contains("parse").Should().BeTrue();
result.Directives.ContainsKey("parse").Should().BeTrue();
}

[Fact]
Expand All @@ -49,8 +49,8 @@ public void Multiple_directives_are_allowed()

var result = option.Parse("[parse] [suggest] -y");

result.Directives.Contains("parse").Should().BeTrue();
result.Directives.Contains("suggest").Should().BeTrue();
result.Directives.ContainsKey("parse").Should().BeTrue();
result.Directives.ContainsKey("suggest").Should().BeTrue();
}

[Fact]
Expand All @@ -76,7 +76,7 @@ public void Directives_can_have_a_value_which_is_everything_after_the_first_colo

var result = option.Parse($"{directive} -y");

result.Directives.TryGetValues(expectedKey, out var values).Should().BeTrue();
result.Directives.TryGetValue(expectedKey, out var values).Should().BeTrue();
values.Should().BeEquivalentTo(expectedValue);
}

Expand All @@ -87,7 +87,7 @@ public void Directives_without_a_value_specified_have_a_value_of_empty_string()

var result = option.Parse("[parse] -y");

result.Directives.TryGetValues("parse", out var values).Should().BeTrue();
result.Directives.TryGetValue("parse", out var values).Should().BeTrue();
values.Should().BeEmpty();
}

Expand Down Expand Up @@ -124,7 +124,7 @@ public void When_a_directive_is_specified_more_than_once_then_its_values_are_agg

var result = option.Parse("[directive:one] [directive:two] -a");

result.Directives.TryGetValues("directive", out var values).Should().BeTrue();
result.Directives.TryGetValue("directive", out var values).Should().BeTrue();
values.Should().BeEquivalentTo("one", "two");
}

Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/UseHelpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void Help_sections_can_be_replaced()

console.Out.ToString().Should().Be($"one{NewLine}{NewLine}two{NewLine}{NewLine}three{NewLine}{NewLine}{NewLine}");

IEnumerable<HelpSectionDelegate> CustomLayout(HelpContext _)
IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.Output.WriteLine("one");
yield return ctx => ctx.Output.WriteLine("two");
Expand All @@ -327,7 +327,7 @@ public void Help_sections_can_be_supplemented()

output.Should().Be(expected);

IEnumerable<HelpSectionDelegate> CustomLayout(HelpContext _)
IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.Output.WriteLine("first");

Expand Down Expand Up @@ -358,7 +358,7 @@ public void Layout_can_be_composed_dynamically_based_on_context()
.CustomizeLayout(c =>
c.Command == commandWithTypicalHelp
? HelpBuilder.Default.GetLayout()
: new HelpSectionDelegate[]
: new Action<HelpContext>[]
{
c => c.Output.WriteLine("Custom layout!")
}
Expand Down Expand Up @@ -417,7 +417,7 @@ public void Help_customized_sections_can_be_wrapped()
string result = console.Out.ToString();
result.Should().Be($" 123 123{NewLine} 456 456{NewLine} 78 789{NewLine} 0{NewLine}{NewLine}{NewLine}");

IEnumerable<HelpSectionDelegate> CustomLayout(HelpContext _)
IEnumerable<Action<HelpContext>> CustomLayout(HelpContext _)
{
yield return ctx => ctx.HelpBuilder.WriteColumns(new[] { new TwoColumnHelpRow("12345678", "1234567890") }, ctx);
}
Expand Down
18 changes: 9 additions & 9 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public abstract class Argument : Symbol, IValueDescriptor
private Func<ArgumentResult, object?>? _defaultValueFactory;
private ArgumentArity _arity;
private TryConvertArgument? _convertArguments;
private CompletionSourceList? _completions = null;
private List<ValidateSymbolResult<ArgumentResult>>? _validators = null;
private List<ICompletionSource>? _completions = null;
private List<Action<ArgumentResult>>? _validators = null;

/// <summary>
/// Initializes a new instance of the Argument class.
Expand Down Expand Up @@ -72,10 +72,10 @@ internal TryConvertArgument? ConvertArguments
}

/// <summary>
/// Gets the list of completion sources for the argument.
/// Gets the collection of completion sources for the argument.
/// </summary>
public CompletionSourceList Completions =>
_completions ??= new CompletionSourceList
public ICollection<ICompletionSource> Completions =>
_completions ??= new ()
{
CompletionSource.ForType(ValueType)
};
Expand Down Expand Up @@ -104,14 +104,14 @@ private protected override string DefaultName
}
}

internal List<ValidateSymbolResult<ArgumentResult>> Validators => _validators ??= new ();
internal List<Action<ArgumentResult>> Validators => _validators ??= new ();

/// <summary>
/// Adds a custom <see cref="ValidateSymbolResult{ArgumentResult}"/> to the argument. Validators can be used
/// Adds a custom validator to the argument. Validators can be used
/// to provide custom errors based on user input.
/// </summary>
/// <param name="validate">The delegate to validate the parsed argument.</param>
public void AddValidator(ValidateSymbolResult<ArgumentResult> validate) => Validators.Add(validate);
/// <param name="validate">The action to validate the parsed argument.</param>
public void AddValidator(Action<ArgumentResult> validate) => Validators.Add(validate);

/// <summary>
/// Gets the default value for the argument.
Expand Down
6 changes: 3 additions & 3 deletions src/System.CommandLine/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static TArgument AddCompletions<TArgument>(
/// </summary>
/// <typeparam name="TArgument">The type of the argument.</typeparam>
/// <param name="argument">The argument for which to add completions.</param>
/// <param name="complete">A <see cref="CompletionDelegate"/> that will be called to provide completions.</param>
/// <param name="complete">A function that will be called to provide completions.</param>
/// <returns>The option being extended.</returns>
public static TArgument AddCompletions<TArgument>(
this TArgument argument,
Expand All @@ -52,11 +52,11 @@ public static TArgument AddCompletions<TArgument>(
/// </summary>
/// <typeparam name="TArgument">The type of the argument.</typeparam>
/// <param name="argument">The argument for which to add completions.</param>
/// <param name="complete">A <see cref="CompletionDelegate"/> that will be called to provide completions.</param>
/// <param name="complete">A function that will be called to provide completions.</param>
/// <returns>The configured argument.</returns>
public static TArgument AddCompletions<TArgument>(
this TArgument argument,
CompletionDelegate complete)
Func<CompletionContext, IEnumerable<CompletionItem>> complete)
where TArgument : Argument
{
argument.Completions.Add(complete);
Expand Down
4 changes: 2 additions & 2 deletions src/System.CommandLine/Argument{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Argument(Func<T> getDefaultValue) : this()
/// <exception cref="ArgumentNullException">Thrown when <paramref name="parse"/> is null.</exception>
public Argument(
string? name,
ParseArgument<T> parse,
Func<ArgumentResult, T> parse,
bool isDefault = false,
string? description = null) : this(name, description)
{
Expand Down Expand Up @@ -108,7 +108,7 @@ public Argument(
/// </summary>
/// <param name="parse">A custom argument parser.</param>
/// <param name="isDefault"><see langword="true"/> to use the <paramref name="parse"/> result as default value.</param>
public Argument(ParseArgument<T> parse, bool isDefault = false) : this(null!, parse, isDefault)
public Argument(Func<ArgumentResult, T> parse, bool isDefault = false) : this(null!, parse, isDefault)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public static CommandLineBuilder UseEnvironmentVariableDirective(
{
builder.AddMiddleware((context, next) =>
{
if (context.ParseResult.Directives.TryGetValues("env", out var keyValuePairs))
if (context.ParseResult.Directives.TryGetValue("env", out var keyValuePairs))
{
for (var i = 0; i < keyValuePairs.Count; i++)
{
Expand Down Expand Up @@ -498,7 +498,7 @@ public static CommandLineBuilder UseParseDirective(
{
builder.AddMiddleware(async (context, next) =>
{
if (context.ParseResult.Directives.Contains("parse"))
if (context.ParseResult.Directives.ContainsKey("parse"))
{
context.InvocationResult = new ParseDirectiveResult(errorExitCode);
}
Expand Down Expand Up @@ -547,7 +547,7 @@ public static CommandLineBuilder UseSuggestDirective(
{
builder.AddMiddleware(async (context, next) =>
{
if (context.ParseResult.Directives.TryGetValues("suggest", out var values))
if (context.ParseResult.Directives.TryGetValue("suggest", out var values))
{
int position;

Expand Down
10 changes: 5 additions & 5 deletions src/System.CommandLine/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Command : IdentifierSymbol, IEnumerable<Symbol>
private List<Argument>? _arguments;
private List<Option>? _options;
private List<Command>? _subcommands;
private List<ValidateSymbolResult<CommandResult>>? _validators;
private List<Action<CommandResult>>? _validators;

/// <summary>
/// Initializes a new instance of the Command class.
Expand Down Expand Up @@ -68,8 +68,8 @@ public IEnumerable<Symbol> Children
/// </summary>
public IReadOnlyList<Command> Subcommands => _subcommands is not null ? _subcommands : Array.Empty<Command>();

internal IReadOnlyList<ValidateSymbolResult<CommandResult>> Validators
=> _validators is not null ? _validators : Array.Empty<ValidateSymbolResult<CommandResult>>();
internal IReadOnlyList<Action<CommandResult>> Validators
=> _validators is not null ? _validators : Array.Empty<Action<CommandResult>>();

internal bool HasValidators => _validators is not null; // initialized by Add method, so when it's not null the Count is always > 0

Expand Down Expand Up @@ -140,8 +140,8 @@ public void AddGlobalOption(Option option)
/// Adds a custom validator to the command. Validators can be used
/// to create custom validation logic.
/// </summary>
/// <param name="validate">The delegate to validate the symbols during parsing.</param>
public void AddValidator(ValidateSymbolResult<CommandResult> validate) => (_validators ??= new()).Add(validate);
/// <param name="validate">The action to validate the symbols during parsing.</param>
public void AddValidator(Action<CommandResult> validate) => (_validators ??= new()).Add(validate);

/// <summary>
/// Gets or sets a value that indicates whether unmatched tokens should be treated as errors. For example,
Expand Down
10 changes: 5 additions & 5 deletions src/System.CommandLine/CompletionSourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class CompletionSourceExtensions
/// <param name="completionSources">The list of completion sources to add to.</param>
/// <param name="complete">The delegate to be called when calculating completions.</param>
public static void Add(
this CompletionSourceList completionSources,
this ICollection<ICompletionSource> completionSources,
Func<CompletionContext, IEnumerable<string>> complete)
{
if (completionSources is null)
Expand All @@ -38,10 +38,10 @@ public static void Add(
/// Adds a completion source using a delegate.
/// </summary>
/// <param name="completionSources">The list of completion sources to add to.</param>
/// <param name="complete">The delegate to be called when calculating completions.</param>
/// <param name="complete">The function to be called when calculating completions.</param>
public static void Add(
this CompletionSourceList completionSources,
CompletionDelegate complete)
this ICollection<ICompletionSource> completionSources,
Func<CompletionContext, IEnumerable<CompletionItem>> complete)
{
if (completionSources is null)
{
Expand All @@ -62,7 +62,7 @@ public static void Add(
/// <param name="completionSources">The list of completion sources to add to.</param>
/// <param name="completions">A list of strings to be suggested for command line completions.</param>
public static void Add(
this CompletionSourceList completionSources,
this ICollection<ICompletionSource> completionSources,
params string[] completions)
{
if (completionSources is null)
Expand Down
45 changes: 0 additions & 45 deletions src/System.CommandLine/CompletionSourceList.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace System.CommandLine.Completions
{
internal class AnonymousCompletionSource : ICompletionSource
{
private readonly CompletionDelegate _complete;
private readonly Func<CompletionContext, IEnumerable<CompletionItem>> _complete;

public AnonymousCompletionSource(CompletionDelegate complete)
public AnonymousCompletionSource(Func<CompletionContext, IEnumerable<CompletionItem>> complete)
{
_complete = complete ?? throw new ArgumentNullException(nameof(complete));
}
Expand Down
13 changes: 0 additions & 13 deletions src/System.CommandLine/Completions/CompletionDelegate.cs

This file was deleted.

Loading