Skip to content
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

Small cleanup #832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CommandLine/Core/SpecificationProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private SpecificationProperty(Specification specification, PropertyInfo property

public static SpecificationProperty Create(Specification specification, PropertyInfo property, Maybe<object> value)
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw new ArgumentNullException(nameof(value));

return new SpecificationProperty(specification, property, value);
}
Expand Down
3 changes: 1 addition & 2 deletions src/CommandLine/Core/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public Name(string text)

public override bool Equals(object obj)
{
var other = obj as Name;
if (other != null)
if (obj is Name other)
{
return Equals(other);
}
Expand Down
5 changes: 2 additions & 3 deletions src/CommandLine/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public abstract class TokenError : Error, IEquatable<TokenError>
protected internal TokenError(ErrorType tag, string token)
: base(tag)
{
if (token == null) throw new ArgumentNullException("token");
if (token == null) throw new ArgumentNullException(nameof(token));

this.token = token;
}
Expand All @@ -209,8 +209,7 @@ public string Token
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as TokenError;
if (other != null)
if (obj is TokenError other)
{
return Equals(other);
}
Expand Down
9 changes: 1 addition & 8 deletions src/CommandLine/Infrastructure/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ static class ReflectionHelper
/// </param>
public static void SetAttributeOverride(IEnumerable<Attribute> overrides)
{
if (overrides != null)
{
_overrides = overrides.ToDictionary(attr => attr.GetType(), attr => attr);
}
else
{
_overrides = null;
}
_overrides = overrides?.ToDictionary(attr => attr.GetType(), attr => attr);
}

public static Maybe<TAttribute> GetAttribute<TAttribute>()
Expand Down
9 changes: 4 additions & 5 deletions src/CommandLine/NameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public sealed class NameInfo : IEquatable<NameInfo>

internal NameInfo(string shortName, string longName)
{
if (shortName == null) throw new ArgumentNullException("shortName");
if (longName == null) throw new ArgumentNullException("longName");
if (shortName == null) throw new ArgumentNullException(nameof(shortName));
if (longName == null) throw new ArgumentNullException(nameof(longName));

this.longName = longName;
this.shortName = shortName;
Expand Down Expand Up @@ -65,8 +65,7 @@ public string NameText
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as NameInfo;
if (other != null)
if (obj is NameInfo other)
{
return Equals(other);
}
Expand Down Expand Up @@ -98,4 +97,4 @@ public bool Equals(NameInfo other)
return ShortName.Equals(other.ShortName) && LongName.Equals(other.LongName);
}
}
}
}
4 changes: 2 additions & 2 deletions src/CommandLine/NullInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace CommandLine
{
/// <summary>
/// Models a null result when constructing a <see cref="ParserResult{T}"/> in a faling verbs scenario.
/// Models a null result when constructing a <see cref="ParserResult{T}"/> in a failing verbs scenario.
/// </summary>
public sealed class NullInstance
{
internal NullInstance() { }
}
}
}
6 changes: 3 additions & 3 deletions src/CommandLine/OptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public sealed class OptionAttribute : BaseAttribute

private OptionAttribute(string shortName, string longName) : base()
{
if (shortName == null) throw new ArgumentNullException("shortName");
if (longName == null) throw new ArgumentNullException("longName");
if (shortName == null) throw new ArgumentNullException(nameof(shortName));
if (longName == null) throw new ArgumentNullException(nameof(longName));

this.shortName = shortName;
this.longName = longName;
Expand Down Expand Up @@ -91,7 +91,7 @@ public string SetName
get { return setName; }
set
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw new ArgumentNullException(nameof(value));

setName = value;
}
Expand Down
16 changes: 8 additions & 8 deletions src/CommandLine/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Parser()
/// aspects and behaviors of the parser.</param>
public Parser(Action<ParserSettings> configuration)
{
if (configuration == null) throw new ArgumentNullException("configuration");
if (configuration == null) throw new ArgumentNullException(nameof(configuration));

settings = new ParserSettings();
configuration(settings);
Expand Down Expand Up @@ -85,7 +85,7 @@ public ParserSettings Settings
/// <exception cref="System.ArgumentNullException">Thrown if one or more arguments are null.</exception>
public ParserResult<T> ParseArguments<T>(IEnumerable<string> args)
{
if (args == null) throw new ArgumentNullException("args");
if (args == null) throw new ArgumentNullException(nameof(args));

var factory = typeof(T).IsMutable()
? Maybe.Just<Func<T>>(Activator.CreateInstance<T>)
Expand Down Expand Up @@ -118,9 +118,9 @@ public ParserResult<T> ParseArguments<T>(IEnumerable<string> args)
/// <exception cref="System.ArgumentNullException">Thrown if one or more arguments are null.</exception>
public ParserResult<T> ParseArguments<T>(Func<T> factory, IEnumerable<string> args)
{
if (factory == null) throw new ArgumentNullException("factory");
if (!typeof(T).IsMutable()) throw new ArgumentException("factory");
if (args == null) throw new ArgumentNullException("args");
if (factory == null) throw new ArgumentNullException(nameof(factory));
if (!typeof(T).IsMutable()) throw new ArgumentException(nameof(factory));
if (args == null) throw new ArgumentNullException(nameof(args));

return MakeParserResult(
InstanceBuilder.Build(
Expand Down Expand Up @@ -151,9 +151,9 @@ public ParserResult<T> ParseArguments<T>(Func<T> factory, IEnumerable<string> ar
/// <remarks>All types must expose a parameterless constructor. It's strongly recommended to use a generic overload.</remarks>
public ParserResult<object> ParseArguments(IEnumerable<string> args, params Type[] types)
{
if (args == null) throw new ArgumentNullException("args");
if (types == null) throw new ArgumentNullException("types");
if (types.Length == 0) throw new ArgumentOutOfRangeException("types");
if (args == null) throw new ArgumentNullException(nameof(args));
if (types == null) throw new ArgumentNullException(nameof(types));
if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types));

return MakeParserResult(
InstanceChooser.Choose(
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLine/Text/CopyrightInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public CopyrightInfo(string author, params int[] years)
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when parameter <paramref name="copyrightYears"/> is not supplied.</exception>
public CopyrightInfo(bool isSymbolUpper, string author, params int[] copyrightYears)
{
if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException("author");
if (copyrightYears.Length == 0) throw new ArgumentOutOfRangeException("copyrightYears");
if (string.IsNullOrWhiteSpace(author)) throw new ArgumentException(nameof(author));
if (copyrightYears.Length == 0) throw new ArgumentOutOfRangeException(nameof(copyrightYears));

const int ExtraLength = 10;
this.isSymbolUpper = isSymbolUpper;
Expand Down
9 changes: 4 additions & 5 deletions src/CommandLine/Text/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public sealed class Example : IEquatable<Example>
/// <param name="sample">A sample instance.</param>
public Example(string helpText, IEnumerable<UnParserSettings> formatStyles, object sample)
{
if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", "helpText");
if (formatStyles == null) throw new ArgumentNullException("formatStyles");
if (sample == null) throw new ArgumentNullException("sample");
if (string.IsNullOrEmpty(helpText)) throw new ArgumentException("helpText can't be null or empty", nameof(helpText));
if (formatStyles == null) throw new ArgumentNullException(nameof(formatStyles));
if (sample == null) throw new ArgumentNullException(nameof(sample));

this.helpText = helpText;
this.formatStyles = formatStyles;
Expand Down Expand Up @@ -84,8 +84,7 @@ public object Sample
/// <returns><value>true</value> if the specified <see cref="System.Object"/> is equal to the current <see cref="System.Object"/>; otherwise, <value>false</value>.</returns>
public override bool Equals(object obj)
{
var other = obj as Example;
if (other != null)
if (obj is Example other)
{
return Equals(other);
}
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLine/Text/HeadingInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public override string ToString()
/// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="writer"/> is null.</exception>
public void WriteMessage(string message, TextWriter writer)
{
if (string.IsNullOrWhiteSpace("message")) throw new ArgumentException("message");
if (writer == null) throw new ArgumentNullException("writer");
if (string.IsNullOrWhiteSpace(message)) throw new ArgumentException(nameof(message));
if (writer == null) throw new ArgumentNullException(nameof(writer));

writer.WriteLine(
new StringBuilder(programName.Length + message.Length + 2)
Expand Down
30 changes: 15 additions & 15 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public HelpText(string heading, string copyright)
/// <exception cref="System.ArgumentNullException">Thrown when one or more parameters are null or empty strings.</exception>
public HelpText(SentenceBuilder sentenceBuilder, string heading, string copyright)
{
if (sentenceBuilder == null) throw new ArgumentNullException("sentenceBuilder");
if (heading == null) throw new ArgumentNullException("heading");
if (copyright == null) throw new ArgumentNullException("copyright");
if (sentenceBuilder == null) throw new ArgumentNullException(nameof(sentenceBuilder));
if (heading == null) throw new ArgumentNullException(nameof(heading));
if (copyright == null) throw new ArgumentNullException(nameof(copyright));

preOptionsHelp = new StringBuilder(BuilderCapacity);
postOptionsHelp = new StringBuilder(BuilderCapacity);
Expand Down Expand Up @@ -211,7 +211,7 @@ public string Heading
get { return heading; }
set
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw new ArgumentNullException(nameof(value));

heading = value;
}
Expand All @@ -226,7 +226,7 @@ public string Copyright
get { return copyright; }
set
{
if (value == null) throw new ArgumentNullException("value");
if (value == null) throw new ArgumentNullException(nameof(value));

copyright = value;
}
Expand Down Expand Up @@ -419,7 +419,7 @@ public static HelpText AutoBuild<T>(ParserResult<T> parserResult, int maxDisplay
public static HelpText AutoBuild<T>(ParserResult<T> parserResult, Func<HelpText, HelpText> onError, int maxDisplayWidth = DefaultMaximumLength)
{
if (parserResult.Tag != ParserResultType.NotParsed)
throw new ArgumentException("Excepting NotParsed<T> type.", "parserResult");
throw new ArgumentException("Excepting NotParsed<T> type.", nameof(parserResult));

var errors = ((NotParsed<T>)parserResult).Errors;

Expand Down Expand Up @@ -455,8 +455,8 @@ public static HelpText AutoBuild<T>(ParserResult<T> parserResult, Func<HelpText,
/// <param name="current">The <see cref="CommandLine.Text.HelpText"/> instance.</param>
public static HelpText DefaultParsingErrorsHandler<T>(ParserResult<T> parserResult, HelpText current)
{
if (parserResult == null) throw new ArgumentNullException("parserResult");
if (current == null) throw new ArgumentNullException("current");
if (parserResult == null) throw new ArgumentNullException(nameof(parserResult));
if (current == null) throw new ArgumentNullException(nameof(current));

if (((NotParsed<T>)parserResult).Errors.OnlyMeaningfulOnes().Empty())
return current;
Expand Down Expand Up @@ -558,7 +558,7 @@ public HelpText AddPostOptionsText(string text)
/// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="result"/> is null.</exception>
public HelpText AddOptions<T>(ParserResult<T> result)
{
if (result == null) throw new ArgumentNullException("result");
if (result == null) throw new ArgumentNullException(nameof(result));

return AddOptionsImpl(
GetSpecificationsFromType(result.TypeInfo.Current),
Expand All @@ -575,8 +575,8 @@ public HelpText AddOptions<T>(ParserResult<T> result)
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="types"/> array is empty.</exception>
public HelpText AddVerbs(params Type[] types)
{
if (types == null) throw new ArgumentNullException("types");
if (types.Length == 0) throw new ArgumentOutOfRangeException("types");
if (types == null) throw new ArgumentNullException(nameof(types));
if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types));

return AddOptionsImpl(
AdaptVerbsToSpecifications(types),
Expand All @@ -593,7 +593,7 @@ public HelpText AddVerbs(params Type[] types)
/// <exception cref="System.ArgumentNullException">Thrown when parameter <paramref name="result"/> is null.</exception>
public HelpText AddOptions<T>(int maximumLength, ParserResult<T> result)
{
if (result == null) throw new ArgumentNullException("result");
if (result == null) throw new ArgumentNullException(nameof(result));

return AddOptionsImpl(
GetSpecificationsFromType(result.TypeInfo.Current),
Expand All @@ -611,8 +611,8 @@ public HelpText AddOptions<T>(int maximumLength, ParserResult<T> result)
/// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="types"/> array is empty.</exception>
public HelpText AddVerbs(int maximumLength, params Type[] types)
{
if (types == null) throw new ArgumentNullException("types");
if (types.Length == 0) throw new ArgumentOutOfRangeException("types");
if (types == null) throw new ArgumentNullException(nameof(types));
if (types.Length == 0) throw new ArgumentOutOfRangeException(nameof(types));

return AddOptionsImpl(
AdaptVerbsToSpecifications(types),
Expand Down Expand Up @@ -654,7 +654,7 @@ public HelpText AddVerbs(int maximumLength, params Type[] types)
Func<IEnumerable<MutuallyExclusiveSetError>, string> formatMutuallyExclusiveSetErrors,
int indent)
{
if (parserResult == null) throw new ArgumentNullException("parserResult");
if (parserResult == null) throw new ArgumentNullException(nameof(parserResult));

var meaningfulErrors =
((NotParsed<T>)parserResult).Errors.OnlyMeaningfulOnes();
Expand Down
6 changes: 3 additions & 3 deletions src/CommandLine/Text/SentenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public override Func<string> OptionGroupWord
case ErrorType.UnknownOptionError:
return "Option '".JoinTo(((UnknownOptionError)error).Token, "' is unknown.");
case ErrorType.MissingRequiredOptionError:
var errMisssing = ((MissingRequiredOptionError)error);
return errMisssing.NameInfo.Equals(NameInfo.EmptyName)
var errMissing = ((MissingRequiredOptionError)error);
return errMissing.NameInfo.Equals(NameInfo.EmptyName)
? "A required value not bound to option name is missing."
: "Required option '".JoinTo(errMisssing.NameInfo.NameText, "' is missing.");
: "Required option '".JoinTo(errMissing.NameInfo.NameText, "' is missing.");
case ErrorType.BadFormatConversionError:
var badFormat = ((BadFormatConversionError)error);
return badFormat.NameInfo.Equals(NameInfo.EmptyName)
Expand Down
4 changes: 2 additions & 2 deletions tests/CommandLine.Tests/CultureInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ static class CultureInfoExtensions
{
public static CultureHandlers MakeCultureHandlers(this CultureInfo newCulture)
{
var currentCulutre = Thread.CurrentThread.CurrentCulture;
var currentCulture = Thread.CurrentThread.CurrentCulture;

Action changer = () => Thread.CurrentThread.CurrentCulture = newCulture;

Action resetter = () => Thread.CurrentThread.CurrentCulture = currentCulutre;
Action resetter = () => Thread.CurrentThread.CurrentCulture = currentCulture;

return new CultureHandlers { ChangeCulture = changer, ResetCulture = resetter };
}
Expand Down