Skip to content

Commit b188553

Browse files
committed
Do not print the stack trace for ArgumentException #112
1 parent 97a1881 commit b188553

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal static partial class ConsoleApp
7777
catch (Exception ex)
7878
{
7979
Environment.ExitCode = 1;
80-
if (ex is ValidationException)
80+
if (ex is ValidationException or ArgumentParseFailedException)
8181
{
8282
LogError(ex.Message);
8383
}

sandbox/GeneratorSandbox/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66

7-
args = ["show", "--aaa", "a", "--value", "10.2"];
7+
args = ["show", "--aaa", "a", "value", "10.2"];
88

99
var app = ConsoleApp.Create();
1010
app.Add<Test>();
@@ -13,4 +13,5 @@
1313
public class Test
1414
{
1515
public void Show(string aaa, [Range(0, 1)] double value) => ConsoleApp.Log($"{value}");
16-
}
16+
}
17+

src/ConsoleAppFramework.Abstractions/ConsoleApp.Abstractions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ public abstract class ConsoleAppFilter(ConsoleAppFilter next)
1818
public sealed class ConsoleAppFilterAttribute<T> : Attribute
1919
where T : ConsoleAppFilter
2020
{
21+
}
22+
23+
public sealed class ArgumentParseFailedException(string message) : Exception(message)
24+
{
2125
}

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ internal sealed class ConsoleAppFilterAttribute<T> : Attribute
110110
{
111111
}
112112
113+
internal sealed class ArgumentParseFailedException(string message) : Exception(message)
114+
{
115+
}
116+
113117
#endif
114118
115119
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
@@ -179,17 +183,17 @@ public static Task RunAsync(string[] args)
179183
180184
static void ThrowArgumentParseFailed(string argumentName, string value)
181185
{
182-
throw new ArgumentException($"Argument '{argumentName}' failed to parse, provided value: {value}");
186+
throw new ArgumentParseFailedException($"Argument '{argumentName}' failed to parse, provided value: {value}");
183187
}
184188
185189
static void ThrowRequiredArgumentNotParsed(string name)
186190
{
187-
throw new ArgumentException($"Required argument '{name}' was not specified.");
191+
throw new ArgumentParseFailedException($"Required argument '{name}' was not specified.");
188192
}
189193
190194
static void ThrowArgumentNameNotFound(string argumentName)
191195
{
192-
throw new ArgumentException($"Argument '{argumentName}' is not recognized.");
196+
throw new ArgumentParseFailedException($"Argument '{argumentName}' is not recognized.");
193197
}
194198
195199
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -364,7 +368,7 @@ static async Task RunWithFilterAsync(string commandName, string[] args, ConsoleA
364368
}
365369
366370
Environment.ExitCode = 1;
367-
if (ex is ValidationException)
371+
if (ex is ValidationException or ArgumentParseFailedException)
368372
{
369373
LogError(ex.Message);
370374
}

src/ConsoleAppFramework/Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public void EmitRun(SourceBuilder sb, CommandWithId commandWithId, bool isRunAsy
313313

314314
sb.AppendLine("Environment.ExitCode = 1;");
315315

316-
using (sb.BeginBlock("if (ex is ValidationException)"))
316+
using (sb.BeginBlock("if (ex is ValidationException or ArgumentParseFailedException)"))
317317
{
318318
sb.AppendLine("LogError(ex.Message);");
319319
}

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ public void SyncRunShouldFailed()
2626
verifier.Error("ConsoleApp.Run(args, (int x) => { Console.Write((x)); });", "--x").Should().Contain("Argument 'x' failed to parse");
2727
}
2828

29+
[Fact]
30+
public void MissingArgument()
31+
{
32+
verifier.Error("ConsoleApp.Run(args, (int x, int y) => { Console.Write((x + y)); });", "--x 10 y 20").Should().Contain("Argument 'y' is not recognized.");
33+
34+
Environment.ExitCode.Should().Be(1);
35+
Environment.ExitCode = 0;
36+
}
37+
2938
[Fact]
3039
public void ValidateOne()
3140
{

0 commit comments

Comments
 (0)