Skip to content

Commit 82e8198

Browse files
authored
More XML docs and cleanup (#1348)
* wip * XML comments and cleanup
1 parent bc91166 commit 82e8198

23 files changed

+64
-70
lines changed

src/System.CommandLine/Binding/ArgumentConversionResult.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ private protected ArgumentConversionResult(IArgument argument)
1414

1515
internal string? ErrorMessage { get; set; }
1616

17-
internal static FailedArgumentConversionResult Failure(IArgument argument, string error) => new FailedArgumentConversionResult(argument, error);
17+
internal static FailedArgumentConversionResult Failure(IArgument argument, string error) => new(argument, error);
1818

19-
public static SuccessfulArgumentConversionResult Success(IArgument argument, object? value) => new SuccessfulArgumentConversionResult(argument, value);
19+
public static SuccessfulArgumentConversionResult Success(IArgument argument, object? value) => new(argument, value);
2020

21-
internal static NoArgumentConversionResult None(IArgument argument) => new NoArgumentConversionResult(argument);
21+
internal static NoArgumentConversionResult None(IArgument argument) => new(argument);
2222
}
2323
}

src/System.CommandLine/Binding/ArgumentConversionResultSet.cs

-17
This file was deleted.

src/System.CommandLine/Binding/Binder.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ internal static bool CanBeBoundFromScalarValue(this Type type)
6868
{
6969
return type.GetGenericTypeDefinition() switch
7070
{
71-
Type enumerable when enumerable == typeof(IEnumerable<>) => GetEmptyEnumerable(itemType),
72-
Type list when list == typeof(List<>) => GetEmptyList(itemType),
73-
Type array when array == typeof(IList<>) ||
74-
array == typeof(ICollection<>) => CreateEmptyArray(itemType),
71+
{ } enumerable when enumerable == typeof(IEnumerable<>) => GetEmptyEnumerable(itemType),
72+
{ } list when list == typeof(List<>) => GetEmptyList(itemType),
73+
{ } array when array == typeof(IList<>) ||
74+
array == typeof(ICollection<>) => CreateEmptyArray(itemType),
7575
_ => null
7676
};
7777
}
7878
}
7979

8080
return type switch
8181
{
82-
Type nonGeneric
82+
{ } nonGeneric
8383
when nonGeneric == typeof(IList) ||
8484
nonGeneric == typeof(ICollection) ||
8585
nonGeneric == typeof(IEnumerable)

src/System.CommandLine/Binding/ConstructorDescriptor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal object Invoke(IReadOnlyCollection<object?> parameters)
3232
return _constructorInfo.Invoke(parameters.ToArray());
3333
}
3434

35+
/// <inheritdoc />
3536
public override string ToString() =>
3637
$"{Parent} ({string.Join(", ", ParameterDescriptors)})";
3738
}

src/System.CommandLine/Binding/HandlerDescriptor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public abstract class HandlerDescriptor : IMethodDescriptor
2121

2222
protected abstract IEnumerable<ParameterDescriptor> InitializeParameterDescriptors();
2323

24+
/// <inheritdoc />
2425
public override string ToString() =>
2526
$"{Parent} ({string.Join(", ", ParameterDescriptors)})";
2627

src/System.CommandLine/Binding/ModelBinder.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ internal ModelBinder(IValueDescriptor valueDescriptor)
2424
public bool EnforceExplicitBinding { get; set; }
2525

2626
internal Dictionary<IValueDescriptor, IValueSource> ConstructorArgumentBindingSources { get; } =
27-
new Dictionary<IValueDescriptor, IValueSource>();
27+
new();
2828

2929
internal Dictionary<IValueDescriptor, IValueSource> MemberBindingSources { get; } =
30-
new Dictionary<IValueDescriptor, IValueSource>();
30+
new();
3131

3232
// Consider deprecating in favor or BindingConfiguration/BindingContext attach validatation. Then make internal.
3333
// Or at least rename to "ConfigureBinding" or similar
@@ -133,7 +133,7 @@ private bool ShortCutTheBinding()
133133
{
134134
return (false, null, false);
135135
}
136-
if (!(newInstance is null))
136+
if (newInstance is not null)
137137
{
138138
nonDefaultsUsed = UpdateInstanceInternalNotifyIfNonDefaultsUsed(newInstance, bindingContext);
139139
}
@@ -333,7 +333,7 @@ protected IValueDescriptor FindModelPropertyDescriptor(Type propertyType, string
333333

334334
private ConstructorInfo FindConstructorOrThrow(ParameterInfo parameter, string message)
335335
{
336-
if (!(parameter.Member is ConstructorInfo constructor))
336+
if (parameter.Member is not ConstructorInfo constructor)
337337
{
338338
throw new ArgumentException(paramName: nameof(parameter),
339339
message: message);

src/System.CommandLine/Binding/ModelDescriptor.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class ModelDescriptor
1515
| BindingFlags.Public
1616
| BindingFlags.Instance;
1717

18-
private static readonly ConcurrentDictionary<Type, ModelDescriptor> _modelDescriptors = new ConcurrentDictionary<Type, ModelDescriptor>();
18+
private static readonly ConcurrentDictionary<Type, ModelDescriptor> _modelDescriptors = new();
1919

2020
private List<PropertyDescriptor>? _propertyDescriptors;
2121
private List<ConstructorDescriptor>? _constructorDescriptors;
@@ -41,6 +41,7 @@ protected ModelDescriptor(Type modelType)
4141

4242
public Type ModelType { get; }
4343

44+
/// <inheritdoc />
4445
public override string ToString() => $"{ModelType.Name}";
4546

4647
public static ModelDescriptor FromType<T>() =>

src/System.CommandLine/Binding/PropertyDescriptor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public void SetValue(object? instance, object? value)
3434
_propertyInfo.SetValue(instance, value);
3535
}
3636

37+
/// <inheritdoc />
3738
public override string ToString() => $"{ValueType.Name} {Path}";
3839
}
3940
}

src/System.CommandLine/Binding/SpecificSymbolValueSource.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public bool TryGetValue(IValueDescriptor valueDescriptor,
2323
{
2424
case IOption option:
2525
var optionResult = bindingContext?.ParseResult.FindResultFor(option);
26-
if (!(optionResult is null))
26+
if (optionResult is not null)
2727
{
2828
boundValue = optionResult.GetValueOrDefault();
2929
return true;
3030
}
3131
break;
3232
case IArgument argument:
3333
var argumentResult = bindingContext?.ParseResult.FindResultFor(argument);
34-
if (!(argumentResult is null))
34+
if (argumentResult is not null)
3535
{
3636
boundValue = argumentResult.GetValueOrDefault();
3737
return true;

src/System.CommandLine/Builder/CommandLineBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace System.CommandLine.Builder
1212
{
1313
public class CommandLineBuilder : CommandBuilder
1414
{
15-
private readonly List<(InvocationMiddleware middleware, int order)> _middlewareList = new List<(InvocationMiddleware middleware, int order)>();
15+
private readonly List<(InvocationMiddleware middleware, int order)> _middlewareList = new();
1616

1717
public CommandLineBuilder(Command? rootCommand = null)
1818
: base(rootCommand ?? new RootCommand())

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public static CommandLineBuilder UseExceptionHandler(
324324

325325
void Default(Exception exception, InvocationContext context)
326326
{
327-
if (!(exception is OperationCanceledException))
327+
if (exception is not OperationCanceledException)
328328
{
329329
context.Console.ResetTerminalForegroundColor();
330330
context.Console.SetTerminalForegroundRed();

src/System.CommandLine/Collections/AliasedSet.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ namespace System.CommandLine.Collections
1414
public abstract class AliasedSet<T> : IReadOnlyList<T>
1515
where T : class
1616
{
17-
private protected readonly Dictionary<string, T> ItemsByAlias = new Dictionary<string, T>();
17+
private protected readonly Dictionary<string, T> ItemsByAlias = new();
1818

19-
private protected List<T> Items { get; } = new List<T>();
19+
private protected List<T> Items { get; } = new();
2020

21-
private protected HashSet<T> DirtyItems { get; } = new HashSet<T>();
21+
private protected HashSet<T> DirtyItems { get; } = new();
2222

2323
/// <inheritdoc/>
2424
public int Count => Items.Count;

src/System.CommandLine/CommandLineConfiguration.cs

+23-16
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace System.CommandLine
1717
/// </summary>
1818
public class CommandLineConfiguration
1919
{
20-
private readonly SymbolSet _symbols = new SymbolSet();
20+
private readonly SymbolSet _symbols = new();
21+
private Func<BindingContext, IHelpBuilder>? _helpBuilderFactory;
2122

2223
/// <summary>
2324
/// Initializes a new instance of the CommandLineConfiguration class.
@@ -60,17 +61,18 @@ public CommandLineConfiguration(
6061
else
6162
{
6263
// Reuse existing auto-generated root command, if one is present, to prevent repeated mutations
63-
RootCommand? parentRootCommand =
64+
RootCommand? parentRootCommand =
6465
symbols.SelectMany(s => s.Parents)
65-
.OfType<RootCommand>()
66-
.FirstOrDefault();
66+
.OfType<RootCommand>()
67+
.FirstOrDefault();
6768

6869
if (parentRootCommand is null)
6970
{
7071
parentRootCommand = new RootCommand();
7172

72-
foreach (var symbol in symbols)
73+
for (var i = 0; i < symbols.Count; i++)
7374
{
75+
var symbol = symbols[i];
7476
parentRootCommand.Add(symbol);
7577
}
7678
}
@@ -87,19 +89,13 @@ public CommandLineConfiguration(
8789
Resources = resources ?? Resources.Instance;
8890
ResponseFileHandling = responseFileHandling;
8991
Middleware = middlewarePipeline ?? new List<InvocationMiddleware>();
90-
HelpBuilderFactory = helpBuilderFactory ?? (context =>
91-
{
92-
int maxWidth = int.MaxValue;
93-
if (context.Console is SystemConsole systemConsole)
94-
{
95-
maxWidth = systemConsole.GetWindowWidth();
96-
}
97-
return new HelpBuilder(context.Console, maxWidth);
98-
});
92+
93+
_helpBuilderFactory = helpBuilderFactory;
94+
9995
if (configureHelp != null)
10096
{
10197
var factory = HelpBuilderFactory;
102-
HelpBuilderFactory = context =>
98+
_helpBuilderFactory = context =>
10399
{
104100
IHelpBuilder helpBuilder = factory(context);
105101
configureHelp(helpBuilder);
@@ -108,6 +104,17 @@ public CommandLineConfiguration(
108104
}
109105
}
110106

107+
private static IHelpBuilder DefaultHelpBuilderFactory(BindingContext context)
108+
{
109+
int maxWidth = int.MaxValue;
110+
if (context.Console is SystemConsole systemConsole)
111+
{
112+
maxWidth = systemConsole.GetWindowWidth();
113+
}
114+
115+
return new HelpBuilder(context.Console, maxWidth);
116+
}
117+
111118
private void AddGlobalOptionsToChildren(Command parentCommand)
112119
{
113120
for (var childIndex = 0; childIndex < parentCommand.Children.Count; childIndex++)
@@ -151,7 +158,7 @@ private void AddGlobalOptionsToChildren(Command parentCommand)
151158
/// </summary>
152159
public Resources Resources { get; }
153160

154-
internal Func<BindingContext, IHelpBuilder> HelpBuilderFactory { get; }
161+
internal Func<BindingContext, IHelpBuilder> HelpBuilderFactory => _helpBuilderFactory ??= DefaultHelpBuilderFactory;
155162

156163
internal IReadOnlyCollection<InvocationMiddleware> Middleware { get; }
157164

src/System.CommandLine/DirectiveCollection.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.CommandLine
1010
{
1111
internal class DirectiveCollection : IDirectiveCollection
1212
{
13-
private readonly Dictionary<string, List<string>> _directives = new Dictionary<string, List<string>>();
13+
private readonly Dictionary<string, List<string>> _directives = new();
1414

1515
public void Add(string name, string? value)
1616
{

src/System.CommandLine/Help/HelpBuilder.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ public class HelpBuilder : IHelpBuilder
1313
{
1414
private const string Indent = " ";
1515

16-
private Dictionary<ISymbol, Customization> Customizations { get; }
17-
= new Dictionary<ISymbol, Customization>();
16+
private Dictionary<ISymbol, Customization> Customizations { get; } = new();
1817

1918
protected IConsole Console { get; }
2019
public int MaxWidth { get; }

src/System.CommandLine/Invocation/HelpResult.cs renamed to src/System.CommandLine/Help/HelpResult.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) .NET Foundation and contributors. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
namespace System.CommandLine.Invocation
4+
using System.CommandLine.Invocation;
5+
6+
namespace System.CommandLine.Help
57
{
68
public class HelpResult : IInvocationResult
79
{

src/System.CommandLine/IO/TestConsole.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public TestConsole()
2626

2727
internal class StandardStreamWriter : TextWriter, IStandardStreamWriter
2828
{
29-
private readonly StringBuilder _stringBuilder = new StringBuilder();
29+
private readonly StringBuilder _stringBuilder = new();
3030

3131
public override void Write(char value)
3232
{

src/System.CommandLine/Parsing/ParseResultVisitor.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected override void Stop(SyntaxNode node)
167167
var argumentResults = new List<ArgumentResult>();
168168
foreach (var result in _rootCommandResult.AllArgumentResults)
169169
{
170-
if (!(result.Parent is OptionResult))
170+
if (result.Parent is not OptionResult)
171171
{
172172
argumentResults.Add(result);
173173
}
@@ -300,8 +300,7 @@ private void ValidateCommandResult()
300300

301301
private void ValidateCommandHandler()
302302
{
303-
if (!(_innermostCommandResult!.Command is Command cmd) ||
304-
cmd.Handler != null)
303+
if (_innermostCommandResult!.Command is not Command { Handler: null } cmd)
305304
{
306305
return;
307306
}

src/System.CommandLine/Parsing/SymbolResult.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace System.CommandLine.Parsing
1010
/// </summary>
1111
public abstract class SymbolResult
1212
{
13-
private protected readonly List<Token> _tokens = new List<Token>();
13+
private protected readonly List<Token> _tokens = new();
1414
private Resources? _resources;
15-
private readonly Dictionary<IArgument, ArgumentResult> _defaultArgumentValues = new Dictionary<IArgument, ArgumentResult>();
15+
private readonly Dictionary<IArgument, ArgumentResult> _defaultArgumentValues = new();
1616

1717
private protected SymbolResult(
1818
ISymbol symbol,
@@ -34,7 +34,7 @@ private protected SymbolResult(
3434
/// <summary>
3535
/// Child symbol results in the parse tree.
3636
/// </summary>
37-
public SymbolResultSet Children { get; } = new SymbolResultSet();
37+
public SymbolResultSet Children { get; } = new();
3838

3939
/// <summary>
4040
/// The parent symbol result in the parse tree.
@@ -111,7 +111,7 @@ internal ArgumentResult GetOrCreateDefaultArgumentResult(Argument argument) =>
111111
_defaultArgumentValues.GetOrAdd(
112112
argument,
113113
arg => new ArgumentResult(
114-
argument,
114+
arg,
115115
this));
116116

117117
internal virtual bool UseDefaultValueFor(IArgument argument) => false;

src/System.CommandLine/Resources.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Resources
1616
/// <summary>
1717
/// Gets a global instance of the <see cref="Resources"/> class.
1818
/// </summary>
19-
public static Resources Instance { get; } = new Resources();
19+
public static Resources Instance { get; } = new();
2020

2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="Resources"/> class.

src/System.CommandLine/StringBuilderPool.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ internal class StringBuilderPool
2424
/// Gets the default instance of <see cref="StringBuilderPool"/>.
2525
/// </summary>
2626
/// <value>The default instance of <see cref="StringBuilderPool"/>.</value>
27-
public static StringBuilderPool Default { get; } = new StringBuilderPool();
27+
public static StringBuilderPool Default { get; } = new();
2828

2929
/// <summary>
3030
/// The pool of <see cref="WeakReference{T}"/> of reusable instances of <see cref="StringBuilder"/>.

src/System.CommandLine/SuggestionSourceList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace System.CommandLine
99
{
1010
public class SuggestionSourceList : IReadOnlyList<ISuggestionSource>
1111
{
12-
private readonly List<ISuggestionSource> _sources = new List<ISuggestionSource>();
12+
private readonly List<ISuggestionSource> _sources = new();
1313

1414
public void Add(ISuggestionSource source)
1515
{

src/System.CommandLine/Symbol.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace System.CommandLine
1313
public abstract class Symbol : ISymbol
1414
{
1515
private string? _name;
16-
private readonly SymbolSet _parents = new SymbolSet();
16+
private readonly SymbolSet _parents = new();
1717

1818
private protected Symbol()
1919
{
@@ -60,7 +60,7 @@ private protected void AddArgumentInner(Argument argument)
6060
/// <summary>
6161
/// Gets the child symbols.
6262
/// </summary>
63-
public SymbolSet Children { get; } = new SymbolSet();
63+
public SymbolSet Children { get; } = new();
6464

6565
/// <summary>
6666
/// Gets or sets a value indicating whether the symbol is hidden.

0 commit comments

Comments
 (0)