From 2557b0c9b7f2173831f779e5aa491ac9e7b828c2 Mon Sep 17 00:00:00 2001 From: Jon Sequeira Date: Tue, 5 Jan 2021 08:28:29 -0800 Subject: [PATCH] fix #1135 --- .../SuggestionTests.cs | 2 +- src/System.CommandLine/Argument.cs | 6 ++-- .../Builder/CommandLineBuilderExtensions.cs | 1 - .../SuggestionSourceExtensions.cs | 5 ++- .../SuggestionSourceList.cs | 32 +++++++++++++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 src/System.CommandLine/SuggestionSourceList.cs diff --git a/src/System.CommandLine.Tests/SuggestionTests.cs b/src/System.CommandLine.Tests/SuggestionTests.cs index ef982a1899..895a2a36da 100644 --- a/src/System.CommandLine.Tests/SuggestionTests.cs +++ b/src/System.CommandLine.Tests/SuggestionTests.cs @@ -987,7 +987,7 @@ public void Enum_suggestions_can_be_configured_without_list_clear() { var command = new Command("the-command") { - new Argument() + new Argument { Suggestions = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" } } diff --git a/src/System.CommandLine/Argument.cs b/src/System.CommandLine/Argument.cs index deadaf161f..aad1f9f2a9 100644 --- a/src/System.CommandLine/Argument.cs +++ b/src/System.CommandLine/Argument.cs @@ -19,7 +19,7 @@ public class Argument : Symbol, IArgument private IArgumentArity? _arity; private TryConvertArgument? _convertArguments; private Type _argumentType = typeof(string); - private List? _suggestions = null; + private SuggestionSourceList? _suggestions = null; /// /// Initializes a new instance of the Argument class. @@ -93,13 +93,13 @@ internal TryConvertArgument? ConvertArguments /// /// Gets the list of suggestion sources for the argument. /// - public List Suggestions + public SuggestionSourceList Suggestions { get { if (_suggestions is null) { - _suggestions = new List + _suggestions = new SuggestionSourceList { SuggestionSource.ForType(ArgumentType) }; diff --git a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs index 4aebaf3a5a..4e193ea527 100644 --- a/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs +++ b/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs @@ -10,7 +10,6 @@ using System.IO; using System.Linq; using System.Reflection; -using System.Text; using System.Threading; using System.Threading.Tasks; using static System.Environment; diff --git a/src/System.CommandLine/SuggestionSourceExtensions.cs b/src/System.CommandLine/SuggestionSourceExtensions.cs index 431db3460f..11c56c1dc7 100644 --- a/src/System.CommandLine/SuggestionSourceExtensions.cs +++ b/src/System.CommandLine/SuggestionSourceExtensions.cs @@ -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.Collections.Generic; using System.CommandLine.Suggestions; namespace System.CommandLine @@ -9,7 +8,7 @@ namespace System.CommandLine public static class SuggestionSourceExtensions { public static void Add( - this List suggestionSources, + this SuggestionSourceList suggestionSources, SuggestDelegate suggest) { if (suggestionSources is null) @@ -26,7 +25,7 @@ public static void Add( } public static void Add( - this List suggestionSources, + this SuggestionSourceList suggestionSources, params string[] suggestions) { if (suggestionSources is null) diff --git a/src/System.CommandLine/SuggestionSourceList.cs b/src/System.CommandLine/SuggestionSourceList.cs new file mode 100644 index 0000000000..610b36eb27 --- /dev/null +++ b/src/System.CommandLine/SuggestionSourceList.cs @@ -0,0 +1,32 @@ +// // 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.Collections; +using System.Collections.Generic; +using System.CommandLine.Suggestions; + +namespace System.CommandLine +{ + public class SuggestionSourceList : IReadOnlyList + { + private readonly List _sources = new List(); + + public void Add(ISuggestionSource source) + { + _sources.Add(source); + } + + public IEnumerator GetEnumerator() => _sources.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + public void Clear() + { + _sources.Clear(); + } + + public int Count => _sources.Count; + + public ISuggestionSource this[int index] => _sources[index]; + } +} \ No newline at end of file