Skip to content

Allow Suggestions to be replaced #955

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

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ public void Setup()
new Option("--fruit")
{
Argument = new Argument<string>()
.WithSuggestions("apple", "banana", "cherry")
{
Suggestions = {"apple", "banana", "cherry" }
}
},
new Option("--vegetable")
{
Argument = new Argument<string>()
.WithSuggestions("asparagus", "broccoli", "carrot")
{
Suggestions = {"asparagus", "broccoli", "carrot" }
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void Setup_FromSymbol()
{
Argument = new Argument
{
Arity = ArgumentArity.ExactlyOne
}
.WithSuggestions(GenerateSuggestionsArray(TestSuggestionsCount))
Arity = ArgumentArity.ExactlyOne,
Suggestions = { GenerateSuggestionsArray(TestSuggestionsCount) }
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void Help_describes_default_values_for_complex_root_command_scenario()
Argument = new Argument<FileAccess>("the-root-option-arg", () => FileAccess.Read)
{
Description = "the-root-option-arg-description",
},
}
},
new Option(aliases: new string[] {"--the-root-option-required-enum-arg", "-trorea"}) {
Description = "the-root-option-description",
Expand Down
5 changes: 2 additions & 3 deletions src/System.CommandLine.Tests/SuggestDirectiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.IO;
using System.CommandLine.Parsing;
using System.Threading.Tasks;
Expand All @@ -23,10 +22,10 @@ public class SuggestDirectiveTests
public SuggestDirectiveTests()
{
_fruitOption = new Option<string>("--fruit")
.WithSuggestions("apple", "banana", "cherry");
.AddSuggestions("apple", "banana", "cherry");

_vegetableOption = new Option<string>("--vegetable")
.WithSuggestions("asparagus", "broccoli", "carrot");
.AddSuggestions("asparagus", "broccoli", "carrot");

_eatCommand = new Command("eat")
{
Expand Down
76 changes: 57 additions & 19 deletions src/System.CommandLine.Tests/SuggestionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void Option_Suggest_returns_argument_suggestions_if_configured()
{
Argument = new Argument
{
Arity = ArgumentArity.ExactlyOne
Arity = ArgumentArity.ExactlyOne,
Suggestions = { "one", "two", "three" }
}
.WithSuggestions("one", "two", "three")
};

var suggestions = option.GetSuggestions();
Expand Down Expand Up @@ -102,9 +102,9 @@ public void Command_Suggest_returns_available_subcommands_and_option_aliases_and
new Option("--option", "option"),
new Argument
{
Arity = ArgumentArity.OneOrMore
Arity = ArgumentArity.OneOrMore,
Suggestions = { "command-argument" }
}
.WithSuggestions("command-argument")
};

var suggestions = command.GetSuggestions();
Expand Down Expand Up @@ -505,9 +505,9 @@ public void Suggestions_can_be_provided_in_the_absence_of_validation()
{
Argument = new Argument
{
Arity = ArgumentArity.ExactlyOne
Arity = ArgumentArity.ExactlyOne,
Suggestions = { "vegetable", "mineral", "animal" }
}
.WithSuggestions("vegetable", "mineral", "animal")
}
};

Expand All @@ -529,14 +529,9 @@ public void Command_argument_suggestions_can_be_provided_using_a_delegate()
{
new Argument
{
Arity = ArgumentArity.ExactlyOne
Arity = ArgumentArity.ExactlyOne,
Suggestions = { _ => new[] { "vegetable", "mineral", "animal" } }
}
.WithSuggestionSource(_ => new[]
{
"vegetable",
"mineral",
"animal"
})
}
};

Expand All @@ -551,13 +546,13 @@ public void Option_argument_suggestions_can_be_provided_using_a_delegate()
{
var command = new Command("the-command")
{
new Option<string>("-x")
.WithSuggestionSource(_ => new[]
new Option<string>("-x")
{
Argument = new Argument<string>()
{
"vegetable",
"mineral",
"animal"
})
Suggestions = { _ => new[] { "vegetable", "mineral", "animal" } }
}
}
};

var parseResult = command.Parse("the-command -x m");
Expand Down Expand Up @@ -997,6 +992,49 @@ public void When_there_are_multiple_arguments_then_suggestions_are_only_offered_
{
Assert.True(false, "Test testname is not written yet.");
}

[Fact]
public void Enum_suggestions_can_be_configured_with_list_clear()
{
var argument = new Argument<DayOfWeek?>();
argument.Suggestions.Clear();
argument.Suggestions.Add(new[] { "mon", "tues", "wed", "thur", "fri", "sat", "sun" });
var command = new Command("the-command")
{
argument
};

var suggestions = command.Parse("the-command s")
.GetSuggestions();

suggestions.Should().BeEquivalentTo("sat", "sun","tues");
}

[Fact]
public void Enum_suggestions_can_be_configured_without_list_clear()
{
var command = new Command("the-command")
{
new Argument<DayOfWeek?>()
{
Suggestions = { "mon", "tues", "wed", "thur", "fri", "sat", "sun" }
}
};

var suggestions = command.Parse("the-command s")
.GetSuggestions();

suggestions
.Should()
.BeEquivalentTo(
"sat",
nameof(DayOfWeek.Saturday),
"sun", nameof(DayOfWeek.Sunday),
"tues",
nameof(DayOfWeek.Tuesday),
nameof(DayOfWeek.Thursday),
nameof(DayOfWeek.Wednesday));
}
}
}
}
61 changes: 20 additions & 41 deletions src/System.CommandLine/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace System.CommandLine
public class Argument : Symbol, IArgument
{
private Func<ArgumentResult, object?>? _defaultValueFactory;
private readonly List<string> _suggestions = new List<string>();
private readonly List<ISuggestionSource> _suggestionSources = new List<ISuggestionSource>();
private IArgumentArity? _arity;
private TryConvertArgument? _convertArguments;
private Type _argumentType = typeof(void);
Expand Down Expand Up @@ -109,6 +107,24 @@ bool DefaultConvert(SymbolResult symbol, out object value)
set => _convertArguments = value;
}


private List<ISuggestionSource>? _suggestions = null;
public List<ISuggestionSource> Suggestions
{
get
{
if (_suggestions is null)
{
_suggestions = new List<ISuggestionSource>()
{
SuggestionSource.ForType(ArgumentType)
};
}

return _suggestions;
}
}

public Type ArgumentType
{
get => _argumentType;
Expand Down Expand Up @@ -158,36 +174,6 @@ public void SetDefaultValueFactory(Func<ArgumentResult, object?> getDefaultValue

internal static Argument None => new Argument { Arity = ArgumentArity.Zero };

public void AddSuggestions(IReadOnlyCollection<string> suggestions)
{
if (suggestions is null)
{
throw new ArgumentNullException(nameof(suggestions));
}

_suggestions.AddRange(suggestions);
}

public void AddSuggestionSource(ISuggestionSource suggest)
{
if (suggest is null)
{
throw new ArgumentNullException(nameof(suggest));
}

_suggestionSources.Add(suggest);
}

public void AddSuggestionSource(Suggest suggest)
{
if (suggest is null)
{
throw new ArgumentNullException(nameof(suggest));
}

AddSuggestionSource(new AnonymousSuggestionSource(suggest));
}

internal void AddAllowedValues(IEnumerable<string> values)
{
if (AllowedValues is null)
Expand All @@ -200,17 +186,10 @@ internal void AddAllowedValues(IEnumerable<string> values)

public override IEnumerable<string?> GetSuggestions(string? textToMatch = null)
{
var fixedSuggestions = _suggestions;

var dynamicSuggestions = _suggestionSources
var dynamicSuggestions = Suggestions
.SelectMany(source => source.GetSuggestions(textToMatch));

var typeSuggestions = SuggestionSource.ForType(ArgumentType)
.GetSuggestions(textToMatch);

return fixedSuggestions
.Concat(dynamicSuggestions)
.Concat(typeSuggestions)
return dynamicSuggestions
.Distinct()
.OrderBy(c => c, StringComparer.OrdinalIgnoreCase)
.Containing(textToMatch);
Expand Down
23 changes: 2 additions & 21 deletions src/System.CommandLine/ArgumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

using System.Collections.Generic;
using System.CommandLine.Parsing;
using System.CommandLine.Suggestions;
using System.Linq;
using System.IO;
using System.CommandLine.Suggestions;

namespace System.CommandLine
{
Expand All @@ -17,30 +17,11 @@ public static TArgument FromAmong<TArgument>(
where TArgument : Argument
{
argument.AddAllowedValues(values);
argument.AddSuggestions(values);
argument.Suggestions.Add(values);

return argument;
}

public static TArgument WithSuggestions<TArgument>(
this TArgument argument,
params string[] suggestions)
where TArgument : Argument
{
argument.AddSuggestions(suggestions);

return argument;
}

public static TArgument WithSuggestionSource<TArgument>(
this TArgument argument,
Suggest suggest)
where TArgument : Argument
{
argument.AddSuggestionSource(suggest);

return argument;
}
public static Argument<FileInfo> ExistingOnly(this Argument<FileInfo> argument)
{
argument.AddValidator(symbol =>
Expand Down
16 changes: 8 additions & 8 deletions src/System.CommandLine/OptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// 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;
Expand All @@ -17,27 +17,27 @@ public static TOption FromAmong<TOption>(
where TOption : Option
{
option.Argument.AddAllowedValues(values);
option.Argument.AddSuggestions(values);
option.Argument.Suggestions.Add(values);

return option;
}

public static TOption WithSuggestions<TOption>(
public static TOption AddSuggestions<TOption>(
this TOption option,
params string[] suggestions)
params string[] values)
where TOption : Option
{
option.Argument.AddSuggestions(suggestions);
option.Argument.Suggestions.Add(values);

return option;
}

public static TOption WithSuggestionSource<TOption>(
public static TOption AddSuggestion<TOption>(
this TOption option,
Suggest suggest)
where TOption : Option
where TOption : Option
{
option.Argument.AddSuggestionSource(suggest);
option.Argument.Suggestions.Add(suggest);

return option;
}
Expand Down
Loading