Skip to content

Commit 0ba58e0

Browse files
committed
Remove some unnecessary exception constructors by using optional argument
1 parent 4797088 commit 0ba58e0

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

Src/Exceptions.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Reflection;
1+
using System.Reflection;
22
using RT.Util;
33
using RT.Util.Consoles;
44
using RT.Util.ExtensionMethods;
@@ -86,20 +86,18 @@ public sealed class CommandLineHelpRequestedException(Func<int, ConsoleColoredSt
8686
/// Specifies that the command-line parser encountered a command or option that was not recognised (there was no <see
8787
/// cref="OptionAttribute"/> or <see cref="CommandNameAttribute"/> attribute with a matching option or command name).</summary>
8888
[Serializable]
89-
public sealed class UnrecognizedCommandOrOptionException(string commandOrOptionName, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
89+
public sealed class UnrecognizedCommandOrOptionException(string commandOrOptionName, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
9090
: CommandLineParseException("The specified command or option, {0}, is not recognized.".ToConsoleColoredString().Fmt(commandOrOptionName.Color(ConsoleColor.White)), helpGenerator, inner)
9191
{
9292
/// <summary>The unrecognized command name or option name.</summary>
9393
public string CommandOrOptionName { get; private set; } = commandOrOptionName;
94-
/// <summary>Constructor.</summary>
95-
public UnrecognizedCommandOrOptionException(string commandOrOptionName, Func<int, ConsoleColoredString> helpGenerator) : this(commandOrOptionName, helpGenerator, null) { }
9694
}
9795

9896
/// <summary>
9997
/// Specifies that the command-line parser encountered a command or option that is not allowed in conjunction with a
10098
/// previously-encountered command or option.</summary>
10199
[Serializable]
102-
public sealed class IncompatibleCommandOrOptionException(string earlier, string later, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
100+
public sealed class IncompatibleCommandOrOptionException(string earlier, string later, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
103101
: CommandLineParseException("The command or option, {0}, cannot be used in conjunction with {1}. Please specify only one of the two.".ToConsoleColoredString().Fmt(later.Color(ConsoleColor.White), earlier.Color(ConsoleColor.White)), helpGenerator, inner)
104102
{
105103
/// <summary>
@@ -108,15 +106,13 @@ public sealed class IncompatibleCommandOrOptionException(string earlier, string
108106
public string EarlierCommandOrOption { get; private set; } = earlier;
109107
/// <summary>The later option or command, which conflicts with the <see cref="EarlierCommandOrOption"/>.</summary>
110108
public string LaterCommandOrOption { get; private set; } = later;
111-
/// <summary>Constructor.</summary>
112-
public IncompatibleCommandOrOptionException(string earlier, string later, Func<int, ConsoleColoredString> helpGenerator) : this(earlier, later, helpGenerator, null) { }
113109
}
114110

115111
/// <summary>
116112
/// Specifies that the command-line parser encountered the end of the command line when it expected additional mandatory
117113
/// options.</summary>
118114
[Serializable]
119-
public sealed class MissingParameterException(FieldInfo paramField, FieldInfo beforeField, bool isOption, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
115+
public sealed class MissingParameterException(FieldInfo paramField, FieldInfo beforeField, bool isOption, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
120116
: CommandLineParseException(getMessage(paramField, beforeField, isOption), helpGenerator, inner)
121117
{
122118
/// <summary>Contains the field pertaining to the parameter that was missing.</summary>
@@ -129,9 +125,6 @@ public sealed class MissingParameterException(FieldInfo paramField, FieldInfo be
129125
/// Specifies whether the missing parameter was a missing option (true) or a missing positional parameter (false).</summary>
130126
public bool IsOption { get; private set; } = isOption;
131127

132-
/// <summary>Constructor.</summary>
133-
public MissingParameterException(FieldInfo paramField, FieldInfo beforeField, bool isOption, Func<int, ConsoleColoredString> helpGenerator) : this(paramField, beforeField, isOption, helpGenerator, null) { }
134-
135128
private static ConsoleColoredString getMessage(FieldInfo field, FieldInfo beforeField, bool isOption)
136129
{
137130
if (beforeField == null)
@@ -147,39 +140,33 @@ private static ConsoleColoredString getMessage(FieldInfo field, FieldInfo before
147140
/// Specifies that the command-line parser encountered additional command-line arguments when it expected the end of the
148141
/// command line.</summary>
149142
[Serializable]
150-
public sealed class UnexpectedArgumentException(string[] unexpectedArgs, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
143+
public sealed class UnexpectedArgumentException(string[] unexpectedArgs, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
151144
: CommandLineParseException("Unexpected parameter: {0}".ToConsoleColoredString().Fmt(unexpectedArgs.Select(prm => prm.Length > 50 ? $"{prm.Substring(0, 47)}..." : prm).FirstOrDefault().Color(CmdLineColor.UnexpectedArgument)), helpGenerator, inner)
152145
{
153146
/// <summary>Contains the first unexpected argument and all of the subsequent arguments.</summary>
154147
public string[] UnexpectedParameters { get; private set; } = unexpectedArgs;
155-
/// <summary>Constructor.</summary>
156-
public UnexpectedArgumentException(string[] unexpectedArgs, Func<int, ConsoleColoredString> helpGenerator) : this(unexpectedArgs, helpGenerator, null) { }
157148
}
158149

159150
/// <summary>
160151
/// Specifies that the command-line parser encountered the end of the command line when it expected an argument to an
161152
/// option.</summary>
162153
[Serializable]
163-
public sealed class IncompleteOptionException(string optionName, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
154+
public sealed class IncompleteOptionException(string optionName, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
164155
: CommandLineParseException("The {0} option must be followed by an additional parameter.".ToConsoleColoredString().Fmt(optionName.Color(ConsoleColor.White)), helpGenerator, inner)
165156
{
166157
/// <summary>The name of the option that was missing an argument.</summary>
167158
public string OptionName { get; private set; } = optionName;
168-
/// <summary>Constructor.</summary>
169-
public IncompleteOptionException(string optionName, Func<int, ConsoleColoredString> helpGenerator) : this(optionName, helpGenerator, null) { }
170159
}
171160

172161
/// <summary>
173162
/// Specifies that a parameter that expected a numerical value was passed a string by the user that doesn’t parse as a
174163
/// number.</summary>
175164
[Serializable]
176-
public sealed class InvalidNumericParameterException(string fieldName, Func<int, ConsoleColoredString> helpGenerator, Exception inner)
165+
public sealed class InvalidNumericParameterException(string fieldName, Func<int, ConsoleColoredString> helpGenerator, Exception inner = null)
177166
: CommandLineParseException("The {0} option expects a number. The specified parameter does not constitute a valid number.".ToConsoleColoredString().Fmt("<".Color(CmdLineColor.FieldBrackets) + fieldName.Color(CmdLineColor.Field) + ">".Color(CmdLineColor.FieldBrackets)), helpGenerator, inner)
178167
{
179168
/// <summary>Contains the name of the field pertaining to the parameter that was passed an invalid value.</summary>
180169
public string FieldName { get; private set; } = fieldName;
181-
/// <summary>Constructor.</summary>
182-
public InvalidNumericParameterException(string fieldName, Func<int, ConsoleColoredString> helpGenerator) : this(fieldName, helpGenerator, null) { }
183170
}
184171

185172
/// <summary>Indicates that the arguments specified by the user on the command-line do not pass the custom validation check.</summary>

0 commit comments

Comments
 (0)