Skip to content

Commit ef7f2e8

Browse files
committed
remove IDirectiveCollection
1 parent 4c7669a commit ef7f2e8

File tree

8 files changed

+54
-56
lines changed

8 files changed

+54
-56
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@
9292
public static class ConsoleExtensions
9393
public static System.Void Write(this IConsole console, System.String value)
9494
public static System.Void WriteLine(this IConsole console, System.String value)
95+
public class DirectiveCollection, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.String,System.Collections.Generic.IEnumerable<System.String>>>, System.Collections.IEnumerable
96+
.ctor()
97+
public System.Boolean Contains(System.String name)
98+
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<System.String,System.Collections.Generic.IEnumerable<System.String>>> GetEnumerator()
99+
public System.Boolean TryGetValues(System.String name, ref System.Collections.Generic.IReadOnlyList<System.String> values)
95100
public static class Handler
96101
public static System.Void SetHandler(this Command command, System.Action handle)
97102
public static System.Void SetHandler(this Command command, System.Func<System.Threading.Tasks.Task> handle)
@@ -133,9 +138,6 @@
133138
public System.String Name { get; set; }
134139
public System.Void AddAlias(System.String alias)
135140
public System.Boolean HasAlias(System.String alias)
136-
public interface IDirectiveCollection : System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<System.String,System.Collections.Generic.IEnumerable<System.String>>>, System.Collections.IEnumerable
137-
public System.Boolean Contains(System.String name)
138-
public System.Boolean TryGetValues(System.String name, ref System.Collections.Generic.IReadOnlyList<System.String> values)
139141
public class LocalizationResources
140142
public static LocalizationResources Instance { get; }
141143
public System.String ArgumentConversionCannotParse(System.String value, System.Type expectedType)
@@ -455,7 +457,7 @@ System.CommandLine.Parsing
455457
public ParseResult Parse(System.Collections.Generic.IReadOnlyList<System.String> arguments, System.String rawInput = null)
456458
public class ParseResult
457459
public CommandResult CommandResult { get; }
458-
public System.CommandLine.IDirectiveCollection Directives { get; }
460+
public System.CommandLine.DirectiveCollection Directives { get; }
459461
public System.Collections.Generic.IReadOnlyList<ParseError> Errors { get; }
460462
public Parser Parser { get; }
461463
public CommandResult RootCommandResult { get; }

src/System.CommandLine.Benchmarks/CommandLine/Perf_Parser_ParseResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public IEnumerable<object> GenerateTestParseResults()
4444

4545
[Benchmark]
4646
[ArgumentsSource(nameof(GenerateTestInputs))]
47-
public IDirectiveCollection ParseResult_Directives(string input)
47+
public DirectiveCollection ParseResult_Directives(string input)
4848
=> _testParser.Parse(input).Directives;
4949

5050
[Benchmark]

src/System.CommandLine/Builder/CommandLineBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public CommandLineBuilder(Command? rootCommand = null)
3333
/// <summary>
3434
/// Determines whether the parser recognizes command line directives.
3535
/// </summary>
36-
/// <seealso cref="IDirectiveCollection"/>
36+
/// <seealso cref="DirectiveCollection"/>
3737
public bool EnableDirectives { get; set; } = true;
3838

3939
/// <summary>

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public static CommandLineBuilder CancelOnProcessTermination(this CommandLineBuil
102102
/// <param name="builder">A command line builder.</param>
103103
/// <param name="value">If set to <see langword="true"/>, then directives are enabled. Otherwise, they are parsed like any other token.</param>
104104
/// <returns>The same instance of <see cref="CommandLineBuilder"/>.</returns>
105-
/// <seealso cref="IDirectiveCollection"/>
105+
/// <seealso cref="DirectiveCollection"/>
106106
public static CommandLineBuilder EnableDirectives(
107107
this CommandLineBuilder builder,
108108
bool value = true)
@@ -279,11 +279,11 @@ public static CommandLineBuilder UseEnvironmentVariableDirective(
279279
{
280280
builder.AddMiddleware((context, next) =>
281281
{
282-
if (context.ParseResult.Directives.TryGetValues("env", out var directives))
282+
if (context.ParseResult.Directives.TryGetValues("env", out var keyValuePairs))
283283
{
284-
for (var i = 0; i < directives.Count; i++)
284+
for (var i = 0; i < keyValuePairs.Count; i++)
285285
{
286-
var envDirective = directives[i];
286+
var envDirective = keyValuePairs[i];
287287
var components = envDirective.Split(new[] { '=' }, count: 2);
288288
var variable = components.Length > 0 ? components[0].Trim() : string.Empty;
289289
if (string.IsNullOrEmpty(variable) || components.Length < 2)

src/System.CommandLine/DirectiveCollection.cs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88

99
namespace System.CommandLine
1010
{
11-
internal class DirectiveCollection : IDirectiveCollection
11+
/// <summary>
12+
/// A collection of directives parsed from a command line.
13+
/// </summary>
14+
/// <remarks>A directive is specified on the command line using square brackets, containing no spaces and preceding other tokens unless they are also directives. In the following example, two directives are present, <c>directive-one</c> and <c>directive-two</c>:
15+
/// <code> > myapp [directive-one] [directive-two:value] arg1 arg2</code>
16+
/// The second has a value specified as well, <c>value</c>. Directive values can be read by calling using <see cref="TryGetValues"/>.
17+
/// </remarks>
18+
public class DirectiveCollection : IEnumerable<KeyValuePair<string, IEnumerable<string>>>
1219
{
13-
private readonly Dictionary<string, List<string>> _directives = new();
20+
private Dictionary<string, List<string>>? _directives;
1421

15-
public void Add(string name, string? value)
22+
internal void Add(string name, string? value)
1623
{
24+
_directives ??= new();
25+
1726
if (!_directives.TryGetValue(name, out var values))
1827
{
1928
values = new List<string>();
@@ -27,14 +36,26 @@ public void Add(string name, string? value)
2736
}
2837
}
2938

39+
/// <summary>
40+
/// Gets a value determining whether a directive with the specified name was parsed.
41+
/// </summary>
42+
/// <param name="name">The name of the directive.</param>
43+
/// <returns><see langword="true"/> if a directive with the specified name was parsed; otherwise, <see langword="false"/>.</returns>
3044
public bool Contains(string name)
3145
{
32-
return _directives.ContainsKey(name);
46+
return _directives is not null && _directives.ContainsKey(name);
3347
}
3448

35-
public bool TryGetValues(string name, [NotNullWhen(true)] out IReadOnlyList<string>? values)
49+
/// <summary>
50+
/// Gets the values specified for a given directive. A return value indicates whether the specified directive name was present.
51+
/// </summary>
52+
/// <param name="name">The name of the directive.</param>
53+
/// <param name="values">The values provided for the specified directive.</param>
54+
/// <returns><see langword="true"/> if a directive with the specified name was parsed; otherwise, <see langword="false"/>.</returns>
55+
public bool TryGetValues(string name, [NotNullWhen(true)] out IReadOnlyList<string>? values)
3656
{
37-
if (_directives.TryGetValue(name, out var v))
57+
if (_directives is not null &&
58+
_directives.TryGetValue(name, out var v))
3859
{
3960
values = v;
4061
return true;
@@ -46,10 +67,18 @@ public bool TryGetValues(string name, [NotNullWhen(true)] out IReadOnlyList<str
4667
}
4768
}
4869

49-
public IEnumerator<KeyValuePair<string, IEnumerable<string>>> GetEnumerator() =>
50-
_directives
51-
.Select(pair => new KeyValuePair<string, IEnumerable<string>>(pair.Key, pair.Value))
52-
.GetEnumerator();
70+
/// <inheritdoc />
71+
public IEnumerator<KeyValuePair<string, IEnumerable<string>>> GetEnumerator()
72+
{
73+
if (_directives is null)
74+
{
75+
return Enumerable.Empty<KeyValuePair<string, IEnumerable<string>>>().GetEnumerator();
76+
}
77+
78+
return _directives
79+
.Select(pair => new KeyValuePair<string, IEnumerable<string>>(pair.Key, pair.Value))
80+
.GetEnumerator();
81+
}
5382

5483
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
5584
}

src/System.CommandLine/IDirectiveCollection.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/System.CommandLine/Parsing/ParseResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal ParseResult(
2424
Parser parser,
2525
RootCommandResult rootCommandResult,
2626
CommandResult commandResult,
27-
IDirectiveCollection directives,
27+
DirectiveCollection directives,
2828
TokenizeResult tokenizeResult,
2929
IReadOnlyList<Token>? unparsedTokens,
3030
IReadOnlyList<Token>? unmatchedTokens,
@@ -102,7 +102,7 @@ internal ParseResult(
102102
/// Gets the directives found while parsing command line input.
103103
/// </summary>
104104
/// <remarks>If <see cref="CommandLineConfiguration.EnableDirectives"/> is set to <see langword="false"/>, then this collection will be empty.</remarks>
105-
public IDirectiveCollection Directives { get; }
105+
public DirectiveCollection Directives { get; }
106106

107107
/// <summary>
108108
/// Gets the tokens identified while parsing command line input.

src/System.CommandLine/Parsing/TokenType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum TokenType
4141
/// <summary>
4242
/// A directive token.
4343
/// </summary>
44-
/// <see cref="IDirectiveCollection"/>
44+
/// <see cref="DirectiveCollection"/>
4545
Directive
4646
}
4747
}

0 commit comments

Comments
 (0)