Skip to content

Commit c728207

Browse files
committed
R: code cleanup and a few micro-optimizations
1 parent 447f763 commit c728207

File tree

7 files changed

+33
-37
lines changed

7 files changed

+33
-37
lines changed

src/System.CommandLine/Argument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public IArgumentArity Arity
5353
return ArgumentArity.Default(
5454
ArgumentType,
5555
this,
56-
Parents.FirstOrDefault());
56+
Parents);
5757
}
5858

5959
return _arity;

src/System.CommandLine/ArgumentArity.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System.Collections;
55
using System.CommandLine.Binding;
6+
using System.CommandLine.Collections;
67
using System.CommandLine.Parsing;
8+
using System.Linq;
79

810
namespace System.CommandLine
911
{
@@ -107,8 +109,20 @@ public ArgumentArity(int minimumNumberOfValues, int maximumNumberOfValues)
107109
/// </summary>
108110
public static IArgumentArity OneOrMore => new ArgumentArity(1, MaximumArity);
109111

110-
internal static IArgumentArity Default(Type type, Argument argument, ISymbol parent)
112+
internal static IArgumentArity Default(Type type, Argument argument, ISymbolSet parents)
111113
{
114+
if (type == typeof(bool))
115+
{
116+
return ZeroOrOne;
117+
}
118+
119+
if (type == typeof(void))
120+
{
121+
return Zero;
122+
}
123+
124+
var parent = parents.FirstOrDefault();
125+
112126
if (typeof(IEnumerable).IsAssignableFrom(type) &&
113127
type != typeof(string))
114128
{
@@ -117,23 +131,13 @@ internal static IArgumentArity Default(Type type, Argument argument, ISymbol par
117131
: OneOrMore;
118132
}
119133

120-
if (type == typeof(bool))
121-
{
122-
return ZeroOrOne;
123-
}
124-
125134
if (parent is ICommand &&
126135
(argument.HasDefaultValue ||
127136
type.IsNullable()))
128137
{
129138
return ZeroOrOne;
130139
}
131140

132-
if (type == typeof(void))
133-
{
134-
return Zero;
135-
}
136-
137141
return ExactlyOne;
138142
}
139143
}

src/System.CommandLine/Binding/ArgumentConverter.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,7 @@ public static ArgumentConversionResult ConvertStrings(
154154
.GetInterfaces()
155155
.FirstOrDefault(IsEnumerable);
156156

157-
if (enumerableInterface is {})
158-
{
159-
return enumerableInterface.GenericTypeArguments[0];
160-
}
161-
162-
return null;
157+
return enumerableInterface?.GenericTypeArguments[0];
163158
}
164159

165160
internal static bool IsEnumerable(this Type type)
@@ -227,7 +222,7 @@ private static bool TryFindConstructorWithSingleParameterOfType(
227222
Type parameterType,
228223
[NotNullWhen(true)] out ConstructorInfo? ctor)
229224
{
230-
var (x, y) = type.GetConstructors()
225+
var (x, _) = type.GetConstructors()
231226
.Select(c => (ctor: c, parameters: c.GetParameters()))
232227
.SingleOrDefault(tuple => tuple.ctor.IsPublic &&
233228
tuple.parameters.Length == 1 &&

src/System.CommandLine/CommandLineConfiguration.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace System.CommandLine
1616
/// </summary>
1717
public class CommandLineConfiguration
1818
{
19-
private readonly IReadOnlyCollection<InvocationMiddleware> _middlewarePipeline;
20-
private readonly Func<BindingContext, IHelpBuilder> _helpBuilderFactory;
2119
private readonly SymbolSet _symbols = new SymbolSet();
2220

2321
/// <summary>
@@ -56,15 +54,15 @@ public CommandLineConfiguration(
5654

5755
if (argumentDelimiters is null)
5856
{
59-
ArgumentDelimitersInternal = new []
57+
ArgumentDelimiters = new []
6058
{
6159
':',
6260
'='
6361
};
6462
}
6563
else
6664
{
67-
ArgumentDelimitersInternal = argumentDelimiters.Distinct().ToArray();
65+
ArgumentDelimiters = argumentDelimiters.Distinct().ToArray();
6866
}
6967

7068
foreach (var symbol in symbols)
@@ -119,8 +117,8 @@ public CommandLineConfiguration(
119117
EnableDirectives = enableDirectives;
120118
ValidationMessages = validationMessages ?? ValidationMessages.Instance;
121119
ResponseFileHandling = responseFileHandling;
122-
_middlewarePipeline = middlewarePipeline ?? new List<InvocationMiddleware>();
123-
_helpBuilderFactory = helpBuilderFactory ?? (context => new HelpBuilder(context.Console));
120+
Middleware = middlewarePipeline ?? new List<InvocationMiddleware>();
121+
HelpBuilderFactory = helpBuilderFactory ?? (context => new HelpBuilder(context.Console));
124122
}
125123

126124
private void AddGlobalOptionsToChildren(Command parentCommand)
@@ -150,10 +148,8 @@ private void AddGlobalOptionsToChildren(Command parentCommand)
150148
/// <summary>
151149
/// Represents all of the argument delimiters.
152150
/// </summary>
153-
public IReadOnlyList<char> ArgumentDelimiters => ArgumentDelimitersInternal;
151+
public IReadOnlyList<char> ArgumentDelimiters { get; }
154152

155-
internal IReadOnlyList<char> ArgumentDelimitersInternal { get; }
156-
157153
/// <summary>
158154
/// Gets whether directives are enabled.
159155
/// </summary>
@@ -172,9 +168,9 @@ private void AddGlobalOptionsToChildren(Command parentCommand)
172168
/// </summary>
173169
public ValidationMessages ValidationMessages { get; }
174170

175-
internal Func<BindingContext, IHelpBuilder> HelpBuilderFactory => _helpBuilderFactory;
171+
internal Func<BindingContext, IHelpBuilder> HelpBuilderFactory { get; }
176172

177-
internal IReadOnlyCollection<InvocationMiddleware> Middleware => _middlewarePipeline;
173+
internal IReadOnlyCollection<InvocationMiddleware> Middleware { get; }
178174

179175
/// <summary>
180176
/// Gets the root command.

src/System.CommandLine/EnumerableExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5-
using System.Linq;
65

76
namespace System.CommandLine
87
{

src/System.CommandLine/Parsing/ParseResultExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public static bool HasOption(
190190
throw new ArgumentNullException(nameof(parseResult));
191191
}
192192

193-
return parseResult.CommandResult.Children.Any(s => Equals(s.Symbol, option));
193+
return parseResult.FindResultFor(option) is { };
194194
}
195195

196196
public static bool HasOption(

src/System.CommandLine/Parsing/StringExtensions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal static TokenizeResult Tokenize(
6868
var foundEndOfDirectives = !configuration.EnableDirectives;
6969
var argList = NormalizeRootCommand(configuration, args);
7070

71-
var argumentDelimiters = configuration.ArgumentDelimitersInternal.ToArray();
71+
var argumentDelimiters = configuration.ArgumentDelimiters.ToArray();
7272

7373
var knownTokens = configuration.RootCommand.ValidTokens();
7474

@@ -417,11 +417,9 @@ internal static bool TrySplitIntoSubtokens(
417417
out string? first,
418418
out string? rest)
419419
{
420-
var delimitersArray = delimiters;
421-
422420
for (var j = 0; j < delimiters.Length; j++)
423421
{
424-
var i = arg.IndexOfAny(delimitersArray);
422+
var i = arg.IndexOfAny(delimiters);
425423

426424
if (i >= 0)
427425
{
@@ -513,8 +511,12 @@ private static IEnumerable<string> ExpandResponseFile(
513511
string filePath,
514512
ResponseFileHandling responseFileHandling)
515513
{
516-
foreach (var line in File.ReadAllLines(filePath))
514+
var lines = File.ReadAllLines(filePath);
515+
516+
for (var i = 0; i < lines.Length; i++)
517517
{
518+
var line = lines[i];
519+
518520
foreach (var p in SplitLine(line))
519521
{
520522
if (p.GetResponseFileReference() is { } path)

0 commit comments

Comments
 (0)