8000 update suggestion API (#962) · jonsequitur/command-line-api@8ac2601 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8ac2601

Browse files
authored
update suggestion API (dotnet#962)
1 parent 6b85295 commit 8ac2601

File tree

7 files changed

+30
-11
lines changed

7 files changed

+30
-11
lines changed

src/System.CommandLine.Tests/SuggestDirectiveTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public SuggestDirectiveTests()
2525
.AddSuggestions("apple", "banana", "cherry");
2626

2727
_vegetableOption = new Option<string>("--vegetable")
28-
.AddSuggestions("asparagus", "broccoli", "carrot");
28+
.AddSuggestions(_ => new[] { "asparagus", "broccoli", "carrot" });
2929

3030
_eatCommand = new Command("eat")
3131
{

src/System.CommandLine/Argument.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Argument : Symbol, IArgument
1515
private IArgumentArity? _arity;
1616
private TryConvertArgument? _convertArguments;
1717
private Type _argumentType = typeof(string);
18+
private List<ISuggestionSource>? _suggestions = null;
1819

1920
public Argument()
2021
{
@@ -102,15 +103,13 @@ bool DefaultConvert(SymbolResult symbol, out object value)
102103
set => _convertArguments = value;
103104
}
104105

105-
106-
private List<ISuggestionSource>? _suggestions = null;
107106
public List<ISuggestionSource> Suggestions
108107
{
109108
get
110109
{
111110
if (_suggestions is null)
112111
{
113-
_suggestions = new List<ISuggestionSource>()
112+
_suggestions = new List<ISuggestionSource>
114113
{
115114
SuggestionSource.ForType(ArgumentType)
116115
};

src/System.CommandLine/ArgumentExtensions.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,34 @@
33

44
using System.Collections.Generic;
55
using System.CommandLine.Parsing;
6+
using System.CommandLine.Suggestions;
67
using System.Linq;
78
using System.IO;
8-
using System.CommandLine.Suggestions;
99

1010
namespace System.CommandLine
1111
{
1212
public static class ArgumentExtensions
1313
{
14+
public static TArgument AddSuggestions<TArgument>(
15+
this TArgument argument,
16+
params string[] values)
17+
where TArgument : Argument
18+
{
19+
argument.Suggestions.Add(values);
20+
21+
return argument;
22+
}
23+
24+
public static TArgument AddSuggestions<TArgument>(
25+
this TArgument argument,
26+
SuggestDelegate suggest)
27+
where TArgument : Argument
28+
{
29+
argument.Suggestions.Add(suggest);
30+
31+
return argument;
32+
}
33+
1434
public static TArgument FromAmong<TArgument>(
1535
this TArgument argument,
1636
params string[] values)

src/System.CommandLine/OptionExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public static TOption AddSuggestions<TOption>(
3232
return option;
3333
}
3434

35-
public static TOption AddSuggestion<TOption>(
35+
public static TOption AddSuggestions<TOption>(
3636
this TOption option,
37-
Suggest suggest)
37+
SuggestDelegate suggest)
3838
where TOption : Option
3939
{
4040
option.Argument.Suggestions.Add(suggest);

src/System.CommandLine/SuggestionSourceExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class SuggestionSourceExtensions
1010
{
1111
public static void Add(
1212
this List<ISuggestionSource> suggestionSources,
13-
Suggest suggest)
13+
SuggestDelegate suggest)
1414
{
1515
if (suggestionSources is null)
1616
{

src/System.CommandLine/Suggestions/AnonymousSuggestionSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace System.CommandLine.Suggestions
77
{
88
internal class AnonymousSuggestionSource : ISuggestionSource
99
{
10-
private readonly Suggest _suggest;
10+
private readonly SuggestDelegate _suggest;
1111

12-
public AnonymousSuggestionSource(Suggest suggest)
12+
public AnonymousSuggestionSource(SuggestDelegate suggest)
1313
{
1414
_suggest = suggest ?? throw new ArgumentNullException(nameof(suggest));
1515
}

src/System.CommandLine/Suggestions/Suggest.cs renamed to src/System.CommandLine/Suggestions/SuggestDelegate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
namespace System.CommandLine.Suggestions
77
{
8-
public delegate IEnumerable<string> Suggest(string? textToMatch);
8+
public delegate IEnumerable<string> SuggestDelegate(string? textToMatch);
99
}

0 commit comments

Comments
 (0)
0