Skip to content

Commit 4797088

Browse files
committed
ICommandLineValidatable back as it was, but now with support for CommandLineValidationException
1 parent 686ba42 commit 4797088

File tree

4 files changed

+43
-33
lines changed

4 files changed

+43
-33
lines changed

Src/CommandLineParser.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,18 @@ 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-
try
502-
{
503-
if (ret is ICommandLineProcessed v)
504-
v.Process();
505-
}
506-
catch (CommandLineValidationException exc) when (exc.GenerateHelpFunc == null)
501+
if (ret is ICommandLineValidatable v)
507502
{
508-
throw new CommandLineValidationException(exc.ColoredMessage, getHelpGenerator(type, helpProcessor));
503+
try
504+
{
505+
var error = v.Validate();
506+
if (error != null)
507+
throw new CommandLineValidationException(error, getHelpGenerator(type, helpProcessor));
508+
}
509+
catch (CommandLineValidationException exc) when (exc.GenerateHelpFunc == null)
510+
{
511+
throw new CommandLineValidationException(exc.ColoredMessage, getHelpGenerator(type, helpProcessor));
512+
}
509513
}
510514

511515
return ret;

Src/ICommandLineProcessed.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

Src/ICommandLineValidatable.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using RT.Util.Consoles;
2+
3+
namespace RT.CommandLine;
4+
5+
/// <summary>
6+
/// Contains methods to validate and post-process a class representing command-line options as populated by <see
7+
/// cref="CommandLineParser"/>.</summary>
8+
public interface ICommandLineValidatable
9+
{
10+
/// <summary>
11+
/// When implemented in a class, returns an error message if the contents of the class are invalid, otherwise returns
12+
/// null.</summary>
13+
/// <remarks>
14+
/// <para>
15+
/// When <see cref="CommandLineParser"/> invokes this method, all parsed commands and options have already been
16+
/// populated. This method can thus alter the class in application-specific ways, perform further parsing, or
17+
/// further validate the options for constraints such as mutual exclusivity.</para>
18+
/// <para>
19+
/// To report a validation error, this method can either return the text of the error message, or throw a <see
20+
/// cref="CommandLineValidationException"/>. The message is passed to the user in the same way as other parse
21+
/// errors (for example, see <see cref="CommandLineParser.ParseOrWriteUsageToConsole"/> which prints it to the
22+
/// console).</para></remarks>
23+
ConsoleColoredString Validate();
24+
}

Tests/Test1.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace RT.CommandLine.Tests;
44

55
#pragma warning disable CS0649 // Field is never assigned to and will always have its default value
66

7-
class Test1Cmd : ICommandLineProcessed
7+
class Test1Cmd : ICommandLineValidatable
88
{
99
[IsPositional, IsMandatory]
1010
public string Base;
@@ -14,17 +14,18 @@ class Test1Cmd : ICommandLineProcessed
1414

1515
public static int ValidateCalled = 0;
1616

17-
public void Process()
17+
public ConsoleColoredString Validate()
1818
{
1919
ValidateCalled++;
20+
return null;
2021
}
2122
}
2223

2324
[CommandGroup]
24-
abstract class Test1SubcommandBase : ICommandLineProcessed
25+
abstract class Test1SubcommandBase : ICommandLineValidatable
2526
{
2627
public static int ValidateCalled = 0;
27-
public abstract void Process();
28+
public abstract ConsoleColoredString Validate();
2829
}
2930

3031
[CommandName("sub1")]
@@ -33,14 +34,15 @@ sealed class Test1Subcommand1 : Test1SubcommandBase
3334
[IsPositional, IsMandatory]
3435
public string ItemName;
3536

36-
public override void Process()
37+
public override ConsoleColoredString Validate()
3738
{
3839
ValidateCalled++;
40+
return null;
3941
}
4042
}
4143

4244
[CommandName("sub2")]
4345
sealed class Test1Subcommand2 : Test1SubcommandBase
4446
{
45-
public override void Process() { }
47+
public override ConsoleColoredString Validate() { return null; }
4648
}

0 commit comments

Comments
 (0)