Skip to content

Commit 9eae53a

Browse files
authored
Expose List properties, remove Add* methods (#1987)
* expose Argument.Validators, remove AddValidator helper method * expose Option.Validators, remove AddValidator helper method * expose Command.Validators, remove AddValidator helper method * Argument.Completions should return a List rather than ICollection, remove Argument<T>.AddCompletions * expose Option.Completions, remove AddCompletions helper methods * address code review feedback: rename Completions to CompletionSources
1 parent 4a82c9e commit 9eae53a

File tree

17 files changed

+105
-187
lines changed

17 files changed

+105
-187
lines changed

src/Common/OptionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Option CreateOption(string name, Type valueType, string descriptio
1919
var optionType = typeof(Option<>).MakeGenericType(valueType);
2020

2121
#if NET6_0_OR_GREATER
22-
var ctor = (ConstructorInfo)optionType.GetMemberWithSameMetadataDefinitionAs(_ctor);
22+
var ctor = (ConstructorInfo)optionType.GetMemberWithSameMetadataDefinitionAs(_ctor);
2323
#else
2424
var ctor = optionType.GetConstructor(new[] { typeof(string), typeof(string) });
2525
#endif

src/System.CommandLine.ApiCompatibility.Tests/ApiCompatibilityApprovalTests.System_CommandLine_api_is_not_changed.approved.txt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
System.CommandLine
22
public abstract class Argument : Symbol, System.CommandLine.Binding.IValueDescriptor
33
public ArgumentArity Arity { get; set; }
4-
public System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> Completions { get; }
4+
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
55
public System.Boolean HasDefaultValue { get; }
66
public System.String HelpName { get; set; }
7+
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.ArgumentResult>> Validators { get; }
78
public System.Type ValueType { get; }
89
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
910
public System.Object GetDefaultValue()
@@ -23,10 +24,6 @@ System.CommandLine
2324
public Argument<T> AcceptLegalFileNamesOnly()
2425
public Argument<T> AcceptLegalFilePathsOnly()
2526
public Argument<T> AcceptOnlyFromAmong(System.String[] values)
26-
public Argument<T> AddCompletions(System.String[] completions)
27-
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
28-
public Argument<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
29-
public Argument<T> AddValidator(System.Action<System.CommandLine.Parsing.ArgumentResult> validate)
3027
public System.Void SetDefaultValue(T value)
3128
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
3229
public System.Void SetDefaultValueFactory(Func<System.CommandLine.Parsing.ArgumentResult,T> defaultValueFactory)
@@ -55,14 +52,14 @@ System.CommandLine
5552
public System.Collections.Generic.IReadOnlyList<Option> Options { get; }
5653
public System.Collections.Generic.IReadOnlyList<Command> Subcommands { get; }
5754
public System.Boolean TreatUnmatchedTokensAsErrors { get; set; }
55+
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.CommandResult>> Validators { get; }
5856
public System.Void Add(Option option)
5957
public System.Void Add(Argument argument)
6058
public System.Void Add(Command command)
6159
public System.Void AddArgument(Argument argument)
6260
public System.Void AddCommand(Command command)
6361
public System.Void AddGlobalOption(Option option)
6462
public System.Void AddOption(Option option)
65-
public System.Void AddValidator(System.Action<System.CommandLine.Parsing.CommandResult> validate)
6663
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
6764
public System.Collections.Generic.IEnumerator<Symbol> GetEnumerator()
6865
public static class CommandExtensions
@@ -111,8 +108,8 @@ System.CommandLine
111108
.ctor()
112109
.ctor(System.String message, System.Exception innerException)
113110
public static class CompletionSourceExtensions
114-
public static System.Void Add(this System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
115-
public static System.Void Add(this System.Collections.Generic.ICollection<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.String[] completions)
111+
public static System.Void Add(this System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
112+
public static System.Void Add(this System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> completionSources, System.String[] completions)
116113
public static class ConsoleExtensions
117114
public static System.Void Write(this IConsole console, System.String value)
118115
public static System.Void WriteLine(this IConsole console, System.String value)
@@ -192,7 +189,9 @@ System.CommandLine
192189
public System.Boolean AllowMultipleArgumentsPerToken { get; set; }
193190
public System.String ArgumentHelpName { get; set; }
194191
public ArgumentArity Arity { get; set; }
192+
public System.Collections.Generic.List<System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>>> CompletionSources { get; }
195193
public System.Boolean IsRequired { get; set; }
194+
public System.Collections.Generic.List<System.Action<System.CommandLine.Parsing.OptionResult>> Validators { get; }
196195
public System.Type ValueType { get; }
197196
public System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem> GetCompletions(System.CommandLine.Completions.CompletionContext context)
198197
public ParseResult Parse(System.String commandLine)
@@ -207,10 +206,6 @@ System.CommandLine
207206
public Option<T> AcceptLegalFileNamesOnly()
208207
public Option<T> AcceptLegalFilePathsOnly()
209208
public Option<T> AcceptOnlyFromAmong(System.String[] values)
210-
public Option<T> AddCompletions(System.String[] completions)
211-
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.String>> completionsDelegate)
212-
public Option<T> AddCompletions(System.Func<System.CommandLine.Completions.CompletionContext,System.Collections.Generic.IEnumerable<System.CommandLine.Completions.CompletionItem>> completionsDelegate)
213-
public Option<T> AddValidator(System.Action<System.CommandLine.Parsing.OptionResult> validate)
214209
public System.Void SetDefaultValue(T value)
215210
public System.Void SetDefaultValueFactory(Func<T> defaultValueFactory)
216211
public static class OptionValidation

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_Directives_Suggest.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ public void Setup()
2222
{
2323
_nullConsole = new NullConsole();
2424

25+
Option<string> fruitOption = new("--fruit");
26+
fruitOption.CompletionSources.Add("apple", "banana", "cherry");
27+
28+
Option<string> vegetableOption = new("--vegetable");
29+
vegetableOption.CompletionSources.Add("asparagus", "broccoli", "carrot");
30+
2531
var eatCommand = new Command("eat")
2632
{
27-
new Option<string>("--fruit").AddCompletions("apple", "banana", "cherry"),
28-
new Option<string>("--vegetable").AddCompletions("asparagus", "broccoli", "carrot")
33+
fruitOption,
34+
vegetableOption
2935
};
3036

3137
_testParser = new CommandLineBuilder(eatCommand)

src/System.CommandLine.Benchmarks/CommandLine/Perf_Suggestions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace System.CommandLine.Benchmarks.CommandLine
1515
[BenchmarkCategory(Categories.CommandLine)]
1616
public class Perf_Suggestions
1717
{
18-
private Symbol _testSymbol;
18+
private Option _testSymbol;
1919
private ParseResult _testParseResult;
2020

2121
/// <remarks>
@@ -37,8 +37,8 @@ private IEnumerable<Option> GenerateOptionsArray(int count)
3737
[GlobalSetup(Target = nameof(SuggestionsFromSymbol))]
3838
public void Setup_FromSymbol()
3939
{
40-
_testSymbol = new Option<string>("--hello")
41-
.AddCompletions(GenerateSuggestionsArray(TestSuggestionsCount));
40+
_testSymbol = new Option<string>("--hello");
41+
_testSymbol.CompletionSources.Add(GenerateSuggestionsArray(TestSuggestionsCount));
4242
}
4343

4444
[Benchmark]

src/System.CommandLine.Tests/ArgumentTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,6 @@ public void Argument_of_T_fluent_APIs_return_Argument_of_T()
789789
{
790790
Argument<string> argument = new Argument<string>("--path")
791791
.AcceptOnlyFromAmong("text")
792-
.AddCompletions("test")
793-
.AddCompletions(ctx => Array.Empty<string>())
794-
.AddCompletions(ctx => Array.Empty<CompletionItem>())
795-
.AddValidator(_ => { })
796792
.AcceptLegalFileNamesOnly()
797793
.AcceptLegalFilePathsOnly();
798794

src/System.CommandLine.Tests/CompletionTests.cs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public CompletionTests(ITestOutputHelper output)
2525
[Fact]
2626
public void Option_GetCompletions_returns_argument_completions_if_configured()
2727
{
28-
var option = new Option<string>("--hello")
29-
.AddCompletions("one", "two", "three");
28+
var option = new Option<string>("--hello");
29+
option.CompletionSources.Add("one", "two", "three");
3030

3131
var completions = option.GetCompletions(CompletionContext.Empty);
3232

@@ -122,7 +122,7 @@ public void Command_GetCompletions_returns_available_subcommands_and_option_alia
122122
new Argument<string[]>
123123
{
124124
Arity = ArgumentArity.OneOrMore,
125-
Completions = { "command-argument" }
125+
CompletionSources = { "command-argument" }
126126
}
127127
};
128128

@@ -211,17 +211,19 @@ public void When_an_option_has_a_default_value_it_will_still_be_suggested()
211211
public void Command_GetCompletions_can_access_ParseResult()
212212
{
213213
var originOption = new Option<string>("--origin");
214+
var cloneOption = new Option<string>("--clone");
215+
216+
cloneOption.CompletionSources.Add(ctx =>
217+
{
218+
var opt1Value = ctx.ParseResult.GetValue(originOption);
219+
return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty<string>();
220+
});
214221

215222
var parser = new Parser(
216223
new RootCommand
217224
{
218225
originOption,
219-
new Option<string>("--clone")
220-
.AddCompletions(ctx =>
221-
{
222-
var opt1Value = ctx.ParseResult.GetValue(originOption);
223-
return !string.IsNullOrWhiteSpace(opt1Value) ? new[] { opt1Value } : Array.Empty<string>();
224-
})
226+
cloneOption
225227
});
226228

227229
var result = parser.Parse("--origin test --clone ");
@@ -579,10 +581,12 @@ public void Option_GetCompletions_can_be_based_on_the_proximate_option_and_parti
579581
[Fact]
580582
public void Completions_can_be_provided_in_the_absence_of_validation()
581583
{
584+
Option<string> option = new ("-t");
585+
option.CompletionSources.Add("vegetable", "mineral", "animal");
586+
582587
var command = new Command("the-command")
583588
{
584-
new Option<string>("-t")
585-
.AddCompletions("vegetable", "mineral", "animal")
589+
option
586590
};
587591

588592
command.Parse("the-command -t m")
@@ -607,7 +611,7 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
607611
{
608612
new Argument<string>
609613
{
610-
Completions = { _ => new[] { "vegetable", "mineral", "animal" } }
614+
CompletionSources = { _ => new[] { "vegetable", "mineral", "animal" } }
611615
}
612616
}
613617
};
@@ -622,10 +626,12 @@ public void Command_argument_completions_can_be_provided_using_a_delegate()
622626
[Fact]
623627
public void Option_argument_completions_can_be_provided_using_a_delegate()
624628
{
629+
var option = new Option<string>("-x");
630+
option.CompletionSources.Add(_ => new[] { "vegetable", "mineral", "animal" });
631+
625632
var command = new Command("the-command")
626633
{
627-
new Option<string>("-x")
628-
.AddCompletions(_ => new [] { "vegetable", "mineral", "animal" })
634+
option
629635
};
630636

631637
var parseResult = command.Parse("the-command -x m");
@@ -851,8 +857,8 @@ public void It_can_provide_completions_within_quotes(string commandLine, int pos
851857
"\"nuget:Microsoft.DotNet.Interactive\""
852858
};
853859

854-
var argument = new Argument<string>()
855-
.AddCompletions(expectedSuggestions);
860+
var argument = new Argument<string>();
861+
argument.CompletionSources.Add(expectedSuggestions);
856862

857863
var r = new Command("#r")
858864
{
@@ -873,8 +879,8 @@ public void It_can_provide_completions_within_quotes(string commandLine, int pos
873879
public void Default_completions_can_be_cleared_and_replaced()
874880
{
875881
var argument = new Argument<DayOfWeek>();
876-
argument.Completions.Clear();
877-
argument.Completions.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
882+
argument.CompletionSources.Clear();
883+
argument.CompletionSources.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
878884
var command = new Command("the-command")
879885
{
880886
argument
@@ -895,7 +901,7 @@ public void Default_completions_can_be_appended_to()
895901
{
896902
new Argument<DayOfWeek>
897903
{
898-
Completions = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }
904+
CompletionSources = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }
899905
}
900906
};
901907

src/System.CommandLine.Tests/OptionTests.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public void Option_T_default_value_factory_can_be_set_after_instantiation()
286286
public void Option_T_default_value_is_validated()
287287
{
288288
var option = new Option<int>("-x", () => 123);
289-
option.AddValidator(symbol =>
289+
option.Validators.Add(symbol =>
290290
symbol.ErrorMessage = symbol.Tokens
291291
.Select(t => t.Value)
292292
.Where(v => v == "123")
@@ -374,10 +374,6 @@ public void Option_of_T_fluent_APIs_return_Option_of_T()
374374
{
375375
Option<string> option = new Option<string>("--path")
376376
.AcceptOnlyFromAmong("text")
377-
.AddCompletions("test")
378-
.AddCompletions(ctx => Array.Empty<string>())
379-
.AddCompletions(ctx => Array.Empty<CompletionItem>())
380-
.AddValidator(_ => { })
381377
.AcceptLegalFileNamesOnly()
382378
.AcceptLegalFilePathsOnly();
383379

0 commit comments

Comments
 (0)