Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
expose Option.Completions, remove AddCompletions helper methods
  • Loading branch information
adamsitnik committed Dec 9, 2022
commit ab6c6cba2bcfe854cfe4f1634d33f1377319e17b
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ System.CommandLine
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
public System.String ArgumentHelpName { get; set; }
public ArgumentArity Arity { get; set; }
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> Completions { get; }
public System.Boolean IsRequired { get; set; }
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.OptionResult>> Validators { get; }
public System.Type ValueType { get; }
Expand All @@ -209,9 +210,6 @@ System.CommandLine
public Option<T> AcceptLegalFileNamesOnly()
public Option<T> AcceptLegalFilePathsOnly()
public Option<T> AcceptOnlyFromAmong(System.String[] values)
public Option<T> AddCompletions(System.String[] completions)
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
public System.Void SetDefaultValue(T value)
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
public static class OptionValidation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@ public void Setup()
{
_nullConsole = new NullConsole();

Option<string> fruitOption = new("--fruit");
fruitOption.Completions.Add("apple", "banana", "cherry");

Option<string> vegetableOption = new("--vegetable");
vegetableOption.Completions.Add("asparagus", "broccoli", "carrot");

var eatCommand = new Command("eat")
{
new Option<string>("--fruit").AddCompletions("apple", "banana", "cherry"),
new Option<string>("--vegetable").AddCompletions("asparagus", "broccoli", "carrot")
fruitOption,
vegetableOption
};

_testParser = new CommandLineBuilder(eatCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace System.CommandLine.Benchmarks.CommandLine
[BenchmarkCategory(Categories.CommandLine)]
public class Perf_Suggestions
{
private Symbol _testSymbol;
private Option _testSymbol;
private ParseResult _testParseResult;

/// <remarks>
Expand All @@ -36,8 +36,8 @@ private IEnumerable<Option> GenerateOptionsArray(int count)
[GlobalSetup(Target = nameof(SuggestionsFromSymbol))]
public void Setup_FromSymbol()
{
_testSymbol = new Option<string>("--hello")
.AddCompletions(GenerateSuggestionsArray(TestSuggestionsCount));
_testSymbol = new Option<string>("--hello");
_testSymbol.Completions.Add(GenerateSuggestionsArray(TestSuggestionsCount));
}

[Benchmark]
Expand Down
30 changes: 18 additions & 12 deletions src/System.CommandLine.Tests/CompletionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public CompletionTests(ITestOutputHelper output)
[Fact]
public void Option_GetCompletions_returns_argument_completions_if_configured()
{
var option = new Option<string>("--hello")
.AddCompletions("one", "two", "three");
var option = new Option<string>("--hello");
option.Completions.Add("one", "two", "three");

var completions = option.GetCompletions();

Expand Down Expand Up @@ -210,17 +210,19 @@ public void When_an_option_has_a_default_value_it_will_still_be_suggested()
public void Command_GetCompletions_can_access_ParseResult()
{
var originOption = new Option<string>("--origin");
var cloneOption = new Option<string>("--clone");

cloneOption.Completions.Add(ctx =>
{
var opt1Value = ctx.ParseResult.GetValue(originOption);
return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty<string>();
});

var parser = new Parser(
new RootCommand
{
originOption,
new Option<string>("--clone")
.AddCompletions(ctx =>
{
var opt1Value = ctx.ParseResult.GetValue(originOption);
return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty<string>();
})
cloneOption
});

var result = parser.Parse("--origin test --clone ");
Expand Down Expand Up @@ -578,10 +580,12 @@ public void Option_GetCompletions_can_be_based_on_the_proximate_option_and_parti
[Fact]
public void Completions_can_be_provided_in_the_absence_of_validation()
{
Option<string> option = new ("-t");
option.Completions.Add("vegetable", "mineral", "animal");

var command = new Command("the-command")
{
new Option<string>("-t")
.AddCompletions("vegetable", "mineral", "animal")
option
};

command.Parse("the-command -t m")
Expand Down Expand Up @@ -621,10 +625,12 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
[Fact]
public void Option_argument_completions_can_be_provided_using_a_delegate()
{
var option = new Option<string>("-x");
option.Completions.Add(_ => new[] { "vegetable", "mineral", "animal" });

var command = new Command("the-command")
{
new Option<string>("-x")
.AddCompletions(_ => new [] { "vegetable", "mineral", "animal" })
option
};

var parseResult = command.Parse("the-command -x m");
Expand Down
3 changes: 0 additions & 3 deletions src/System.CommandLine.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,6 @@ public void Option_of_T_fluent_APIs_return_Option_of_T()
{
Option<string> option = new Option<string>("--path")
.AcceptOnlyFromAmong("text")
.AddCompletions("test")
.AddCompletions(ctx => Array.Empty<string>())
.AddCompletions(ctx => Array.Empty<CompletionItem>())
.AcceptLegalFileNamesOnly()
.AcceptLegalFilePathsOnly();

Expand Down
8 changes: 4 additions & 4 deletions src/System.CommandLine.Tests/SuggestDirectiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class SuggestDirectiveTests

public SuggestDirectiveTests()
{
_fruitOption = new Option<string>("--fruit")
.AddCompletions("apple", "banana", "cherry");
_fruitOption = new Option<string>("--fruit");
_fruitOption.Completions.Add("apple", "banana", "cherry");

_vegetableOption = new Option<string>("--vegetable")
.AddCompletions(_ => new[] { "asparagus", "broccoli", "carrot" });
_vegetableOption = new Option<string>("--vegetable");
_vegetableOption.Completions.Add(_ => new[] { "asparagus", "broccoli", "carrot" });

_eatCommand = new Command("eat")
{
Expand Down
5 changes: 5 additions & 0 deletions src/System.CommandLine/Option.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public override string Name

internal bool HasValidators => _validators is not null && _validators.Count > 0;

/// <summary>
/// Gets the list of completion sources for the option.
/// </summary>
public List<Func<CompletionContext, IEnumerable<CompletionItem>>> Completions => Argument.Completions;

/// <summary>
/// Indicates whether a given alias exists on the option, regardless of its prefix.
/// </summary>
Expand Down
33 changes: 0 additions & 33 deletions src/System.CommandLine/Option{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,6 @@ public Option<T> AcceptOnlyFromAmong(params string[] values)
return this;
}

/// <summary>
/// Adds completions for the option.
/// </summary>
/// <param name="completions">The completions to add.</param>
/// <returns>The configured option.</returns>
public Option<T> AddCompletions(params string[] completions)
{
_argument.Completions.Add(completions);
return this;
}

/// <summary>
/// Adds completions for the option.
/// </summary>
/// <param name="completionsDelegate">A function that will be called to provide completions.</param>
/// <returns>The configured option.</returns>
public Option<T> AddCompletions(Func<CompletionContext, IEnumerable<string>> completionsDelegate)
{
_argument.Completions.Add(completionsDelegate);
return this;
}

/// <summary>
/// Adds completions for the option.
/// </summary>
/// <param name="completionsDelegate">A function that will be called to provide completions.</param>
/// <returns>The configured option.</returns>
public Option<T> AddCompletions(Func<CompletionContext, IEnumerable<CompletionItem>> completionsDelegate)
{
_argument.Completions.Add(completionsDelegate);
return this;
}

/// <summary>
/// Configures the option to accept only values representing legal file paths.
/// </summary>
Expand Down