Skip to content

Commit 04cef79

Browse files
committed
Change ICommandLineValidatable so that Validate no longer returns a ConsoleColoredString; instead, parse failures are expected to throw CommandLineValidationException.
1 parent b7f9e49 commit 04cef79

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

Src/CommandLineParser.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,12 +498,15 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
498498
if (missingMandatories.Count > 0)
499499
throw new MissingParameterException(missingMandatories[0], swallowingField, !missingMandatories[0].IsDefined<IsPositionalAttribute>(), getHelpGenerator(type, helpProcessor));
500500

501-
ConsoleColoredString error = null;
502-
if (typeof(ICommandLineValidatable).IsAssignableFrom(type))
503-
error = ((ICommandLineValidatable) ret).Validate();
504-
505-
if (error != null)
506-
throw new CommandLineValidationException(error, getHelpGenerator(type, helpProcessor));
501+
try
502+
{
503+
if (ret is ICommandLineValidatable v)
504+
v.Validate();
505+
}
506+
catch (CommandLineValidationException exc) when (exc.GenerateHelpFunc == null)
507+
{
508+
throw new CommandLineValidationException(exc.Message, getHelpGenerator(type, helpProcessor));
509+
}
507510

508511
return ret;
509512
}

Src/Exceptions.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,14 @@ public sealed class InvalidNumericParameterException(string fieldName, Func<int,
181181
public InvalidNumericParameterException(string fieldName, Func<int, ConsoleColoredString> helpGenerator) : this(fieldName, helpGenerator, null) { }
182182
}
183183

184-
/// <summary>Specifies that the arguments specified by the user on the command-line do not pass the custom validation checks.</summary>
184+
/// <summary>
185+
/// Indicates that the arguments specified by the user on the command-line do not pass the custom validation check.</summary>
186+
/// <param name="message">
187+
/// Provide a helpful, descriptive message for the user to determine how to provide a valid command-line parameter.</param>
188+
/// <param name="helpGenerator">
189+
/// Used internally to generate the help screen; omit if throwing from a validation check.</param>
185190
[Serializable]
186-
public sealed class CommandLineValidationException(ConsoleColoredString message, Func<int, ConsoleColoredString> helpGenerator)
191+
public sealed class CommandLineValidationException(ConsoleColoredString message, Func<int, ConsoleColoredString> helpGenerator = null)
187192
: CommandLineParseException(message, helpGenerator)
188193
{
189194
}

Src/ICommandLineValidatable.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
using RT.Util.Consoles;
1+
using RT.Util.Consoles;
22

33
namespace RT.CommandLine;
44

55
/// <summary>
66
/// Contains methods to validate a set of parameters passed by the user on the command-line and parsed by <see
77
/// cref="CommandLineParser"/>.</summary>
8+
/// <remarks>
9+
/// If an input doesn’t parse correctly, throw <see cref="CommandLineValidationException"/> with a helpful, descriptive
10+
/// message that is displayed to the user.</remarks>
811
public interface ICommandLineValidatable
912
{
1013
/// <summary>
1114
/// When overridden in a derived class, returns an error message if the contents of the class are invalid, otherwise
1215
/// returns null.</summary>
13-
ConsoleColoredString Validate();
16+
void Validate();
1417
}

Tests/Test1.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ class Test1Cmd : ICommandLineValidatable
1414

1515
public static int ValidateCalled = 0;
1616

17-
public ConsoleColoredString Validate()
17+
public void Validate()
1818
{
1919
ValidateCalled++;
20-
return null;
2120
}
2221
}
2322

2423
[CommandGroup]
2524
abstract class Test1SubcommandBase : ICommandLineValidatable
2625
{
2726
public static int ValidateCalled = 0;
28-
public abstract ConsoleColoredString Validate();
27+
public abstract void Validate();
2928
}
3029

3130
[CommandName("sub1")]
@@ -34,15 +33,14 @@ sealed class Test1Subcommand1 : Test1SubcommandBase
3433
[IsPositional, IsMandatory]
3534
public string ItemName;
3635

37-
public override ConsoleColoredString Validate()
36+
public override void Validate()
3837
{
3938
ValidateCalled++;
40-
return null;
4139
}
4240
}
4341

4442
[CommandName("sub2")]
4543
sealed class Test1Subcommand2 : Test1SubcommandBase
4644
{
47-
public override ConsoleColoredString Validate() { return null; }
45+
public override void Validate() { }
4846
}

0 commit comments

Comments
 (0)