Skip to content

Commit f160e87

Browse files
committed
cleanup: run code formatters and cleanups
1 parent 21a73b3 commit f160e87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+167
-225
lines changed

src/CommandLineUtils/Abstractions/IValueParser{T}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace McMaster.Extensions.CommandLineUtils.Abstractions
88
/// <summary>
99
/// A parser that can convert string into <typeparamref name="T" />.
1010
/// </summary>
11-
public interface IValueParser<T> : IValueParser
11+
public interface IValueParser<out T> : IValueParser
1212
{
1313
/// <summary>
1414
/// Parses the raw string value.

src/CommandLineUtils/Abstractions/ValueParserProvider.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,12 @@ public IValueParser GetParser(Type type)
7575
public IValueParser<T>? GetParser<T>()
7676
{
7777
var parser = GetParserImpl<T>();
78-
if (parser == null)
78+
return parser switch
7979
{
80-
return null;
81-
}
82-
83-
if (parser is IValueParser<T> retVal)
84-
{
85-
return retVal;
86-
}
87-
88-
return new GenericParserAdapter<T>(parser);
80+
null => null,
81+
IValueParser<T> retVal => retVal,
82+
_ => new GenericParserAdapter<T>(parser)
83+
};
8984
}
9085

9186
internal IValueParser? GetParserImpl<T>()
@@ -191,7 +186,7 @@ private void SafeAdd(IValueParser parser, bool andReplace = false)
191186
throw new ArgumentNullException(nameof(parser));
192187
}
193188

194-
Type targetType = parser.TargetType;
189+
var targetType = parser.TargetType;
195190

196191
if (targetType == null)
197192
{

src/CommandLineUtils/Attributes/AllowedValuesAttribute.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.ObjectModel;
66
using System.ComponentModel.DataAnnotations;
7+
using System.Linq;
78

89
namespace McMaster.Extensions.CommandLineUtils
910
{
@@ -29,7 +30,7 @@ public AllowedValuesAttribute(params string[] allowedValues)
2930
{
3031
}
3132

32-
internal ReadOnlyCollection<string> AllowedValues => Array.AsReadOnly(this._allowedValues);
33+
internal ReadOnlyCollection<string> AllowedValues => Array.AsReadOnly(_allowedValues);
3334

3435
/// <summary>
3536
/// Initializes an instance of <see cref="AllowedValuesAttribute"/>.
@@ -73,15 +74,9 @@ public bool IgnoreCase
7374
/// <inheritdoc />
7475
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
7576
{
76-
if (value is string str)
77+
if (value is string str && _allowedValues.Any(t => str.Equals(t, Comparer)))
7778
{
78-
for (var i = 0; i < _allowedValues.Length; i++)
79-
{
80-
if (str.Equals(_allowedValues[i], Comparer))
81-
{
82-
return ValidationResult.Success;
83-
}
84-
}
79+
return ValidationResult.Success;
8580
}
8681

8782
return new ValidationResult(FormatErrorMessage(value as string));

src/CommandLineUtils/Attributes/FilePathExistsAttributeBase.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,12 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
5555

5656
private static string GetDefaultErrorMessage(FilePathType filePathType)
5757
{
58-
if (filePathType == FilePathType.File)
58+
return filePathType switch
5959
{
60-
return "The file '{0}' does not exist.";
61-
}
62-
63-
if (filePathType == FilePathType.Directory)
64-
{
65-
return "The directory '{0}' does not exist.";
66-
}
67-
68-
return "The file path '{0}' does not exist.";
60+
FilePathType.File => "The file '{0}' does not exist.",
61+
FilePathType.Directory => "The directory '{0}' does not exist.",
62+
_ => "The file path '{0}' does not exist."
63+
};
6964
}
7065
}
7166
}

src/CommandLineUtils/Attributes/FilePathNotExistsAttributeBase.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,12 @@ protected override ValidationResult IsValid(object value, ValidationContext vali
6060

6161
private static string GetDefaultErrorMessage(FilePathType filePathType)
6262
{
63-
if (filePathType == FilePathType.File)
63+
return filePathType switch
6464
{
65-
return "The file '{0}' already exists.";
66-
}
67-
68-
if (filePathType == FilePathType.Directory)
69-
{
70-
return "The directory '{0}' already exists.";
71-
}
72-
73-
return "The file path '{0}' already exists.";
65+
FilePathType.File => "The file '{0}' already exists.",
66+
FilePathType.Directory => "The directory '{0}' already exists.",
67+
_ => "The file path '{0}' already exists."
68+
};
7469
}
7570
}
7671
}

src/CommandLineUtils/Attributes/OptionAttribute.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace McMaster.Extensions.CommandLineUtils
1111
/// Represents one or many command line option that is identified by flag proceeded by '-' or '--'.
1212
/// Options are not positional. Compare to <see cref="ArgumentAttribute"/>.
1313
/// </summary>
14-
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
14+
[AttributeUsage(AttributeTargets.Property)]
1515
public sealed class OptionAttribute : OptionAttributeBase
1616
{
1717
/// <summary>
@@ -88,10 +88,7 @@ internal CommandOption Configure(CommandLineApplication app, PropertyInfo prop)
8888

8989
Configure(option);
9090

91-
if (option.Description == null)
92-
{
93-
option.Description = prop.Name;
94-
}
91+
option.Description ??= prop.Name;
9592

9693
app.Options.Add(option);
9794
return option;

src/CommandLineUtils/Attributes/SubcommandAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace McMaster.Extensions.CommandLineUtils
1111
/// <summary>
1212
/// Represents a subcommand.
1313
/// </summary>
14-
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
14+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
1515
public sealed class SubcommandAttribute : Attribute
1616
{
1717
/// <summary>

src/CommandLineUtils/Attributes/SuppressDefaultHelpOptionAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace McMaster.Extensions.CommandLineUtils
99
/// <summary>
1010
/// Suppress <see cref="DefaultHelpOptionConvention"/>.
1111
/// </summary>
12-
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = true)]
12+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly)]
1313
public sealed class SuppressDefaultHelpOptionAttribute : Attribute
1414
{
1515
}

src/CommandLineUtils/CommandArgument{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public CommandArgument(IValueParser<T> valueParser)
4444
void IInternalCommandParamOfT.Parse(CultureInfo culture)
4545
{
4646
_parsedValues.Clear();
47-
for (int i = 0; i < Values.Count; i++)
47+
foreach (var t in Values)
4848
{
49-
_parsedValues.Add(_valueParser.Parse(Name, Values[i], culture));
49+
_parsedValues.Add(_valueParser.Parse(Name, t, culture));
5050
}
5151
}
5252
}

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 20 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ static CommandLineApplication()
5151
private readonly HashSet<string> _names = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
5252
private string? _primaryCommandName;
5353
internal CommandLineContext _context;
54-
private ParserConfig _parserConfig;
54+
private readonly ParserConfig _parserConfig;
5555
private IHelpTextGenerator _helpTextGenerator;
5656
private CommandOption? _optionHelp;
5757
private readonly Lazy<IServiceProvider> _services;
@@ -224,11 +224,9 @@ public CommandOption? OptionHelp
224224
{
225225
return _optionHelp;
226226
}
227-
if (Parent?.OptionHelp?.Inherited == true)
228-
{
229-
return Parent.OptionHelp;
230-
}
231-
return null;
227+
return Parent?.OptionHelp?.Inherited == true
228+
? Parent.OptionHelp
229+
: null;
232230
}
233231
internal set => _optionHelp = value;
234232
}
@@ -245,7 +243,7 @@ public CommandOption? OptionHelp
245243
public List<CommandArgument> Arguments { get; private set; }
246244

247245
/// <summary>
248-
/// When initialized when <see cref="UnrecognizedArgumentHandling"/> is <see cref="UnrecognizedArgumentHandling.StopParsingAndCollect" />,
246+
/// When initialized when <see cref="UnrecognizedArgumentHandling"/> is <see cref="McMaster.Extensions.CommandLineUtils.UnrecognizedArgumentHandling.StopParsingAndCollect" />,
249247
/// this will contain any unrecognized arguments.
250248
/// </summary>
251249
public List<string> RemainingArguments { get; private set; }
@@ -290,7 +288,7 @@ public UnrecognizedArgumentHandling UnrecognizedArgumentHandling
290288
/// A response file contains additional arguments that will be treated as if they were passed in on the command line.
291289
/// </para>
292290
/// <para>
293-
/// Defaults to <see cref="ResponseFileHandling.Disabled" />.
291+
/// Defaults to <see cref="McMaster.Extensions.CommandLineUtils.ResponseFileHandling.Disabled" />.
294292
/// </para>
295293
/// <para>
296294
/// Nested response false are not supported.
@@ -359,19 +357,16 @@ public char[] OptionNameValueSeparators
359357
get => _parserConfig.OptionNameValueSeparators ?? Parent?.OptionNameValueSeparators ?? new[] { ' ', ':', '=' };
360358
set
361359
{
362-
if (value is null)
363-
{
364-
throw new ArgumentNullException(nameof(value));
365-
}
366-
else if (value.Length == 0)
360+
if (value.Length == 0)
367361
{
368362
throw new ArgumentException(Strings.IsEmptyArray, nameof(value));
369363
}
364+
370365
_parserConfig.OptionNameValueSeparators = value;
371366
}
372367
}
373368

374-
internal bool OptionNameAndValueCanBeSpaceSeparated => Array.IndexOf(this.OptionNameValueSeparators, ' ') >= 0;
369+
internal bool OptionNameAndValueCanBeSpaceSeparated => Array.IndexOf(OptionNameValueSeparators, ' ') >= 0;
375370

376371
/// <summary>
377372
/// Gets the default value parser provider.
@@ -920,14 +915,9 @@ public CommandOption VersionOption(string template,
920915
string? shortFormVersion,
921916
string? longFormVersion = null)
922917
{
923-
if (longFormVersion == null)
924-
{
925-
return VersionOption(template, () => shortFormVersion);
926-
}
927-
else
928-
{
929-
return VersionOption(template, () => shortFormVersion, () => longFormVersion);
930-
}
918+
return longFormVersion == null
919+
? VersionOption(template, () => shortFormVersion)
920+
: VersionOption(template, () => shortFormVersion, () => longFormVersion);
931921
}
932922

933923
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
@@ -980,7 +970,7 @@ public virtual void ShowHint()
980970
/// <param name="usePager">Use a console pager to display help text, if possible.</param>
981971
public void ShowHelp(bool usePager)
982972
{
983-
CommandLineApplication? cmd = this;
973+
var cmd = this;
984974
while (cmd != null)
985975
{
986976
cmd.IsShowingInformation = true;
@@ -1015,7 +1005,7 @@ public virtual string GetHelpText()
10151005
/// </summary>
10161006
public void ShowVersion()
10171007
{
1018-
CommandLineApplication? cmd = this;
1008+
var cmd = this;
10191009
while (cmd != null)
10201010
{
10211011
cmd.IsShowingInformation = true;
@@ -1077,12 +1067,7 @@ public void ShowRootCommandFullNameAndVersion()
10771067

10781068
internal bool MatchesName(string name)
10791069
{
1080-
if (string.Equals(name, Name, StringComparison.OrdinalIgnoreCase))
1081-
{
1082-
return true;
1083-
}
1084-
1085-
return _names.Contains(name);
1070+
return string.Equals(name, Name, StringComparison.OrdinalIgnoreCase) || _names.Contains(name);
10861071
}
10871072

10881073
private sealed class Builder : IConventionBuilder
@@ -1113,17 +1098,7 @@ IConventionBuilder IConventionBuilder.AddConvention(IConvention convention)
11131098
/// <summary>
11141099
/// Gets a builder that can be used to apply conventions to
11151100
/// </summary>
1116-
public IConventionBuilder Conventions
1117-
{
1118-
get
1119-
{
1120-
if (_builder == null)
1121-
{
1122-
_builder = new Builder(this);
1123-
}
1124-
return _builder;
1125-
}
1126-
}
1101+
public IConventionBuilder Conventions => _builder ??= new Builder(this);
11271102

11281103
private protected virtual ConventionContext CreateConventionContext() => new ConventionContext(this, null);
11291104

@@ -1192,7 +1167,7 @@ public ServiceProvider(CommandLineApplication parent)
11921167
return _parent;
11931168
}
11941169

1195-
// prefer this type before AdditionalServces because it is common for service containers to automatically
1170+
// prefer this type before AdditionalServices because it is common for service containers to automatically
11961171
// create IEnumerable<T> to allow registration of multiple services
11971172
if (serviceType == typeof(IEnumerable<CommandOption>))
11981173
{
@@ -1219,13 +1194,10 @@ public ServiceProvider(CommandLineApplication parent)
12191194
return accessor.GetModel();
12201195
}
12211196

1222-
if (_parent.AdditionalServices != null)
1197+
var retVal = _parent.AdditionalServices?.GetService(serviceType);
1198+
if (retVal != null)
12231199
{
1224-
var retVal = _parent.AdditionalServices.GetService(serviceType);
1225-
if (retVal != null)
1226-
{
1227-
return retVal;
1228-
}
1200+
return retVal;
12291201
}
12301202

12311203
// Resolve this after AdditionalServices to support overriding IConsole from a custom service container

0 commit comments

Comments
 (0)