diff --git a/README.md b/README.md index 5a5753b..64a1ffc 100644 --- a/README.md +++ b/README.md @@ -164,8 +164,8 @@ way out. A user flicking a toggle back and forth produces one write, not one per ``` They appear in `--help` in registration order, between the tray switches and `--for`. A value handler -that throws `FormatException` or `ArgumentException` turns its message into the usage error, and the -tool exits 2 without starting. +that throws `FormatException`, `ArgumentException` or `OverflowException` (what `int.Parse` throws for a +number out of range) turns its message into the usage error, and the tool exits 2 without starting. ### Adding rows to `--status` diff --git a/TrayApp.Test/TrayAppBuilderTests.cs b/TrayApp.Test/TrayAppBuilderTests.cs index 44607aa..13ea000 100644 --- a/TrayApp.Test/TrayAppBuilderTests.cs +++ b/TrayApp.Test/TrayAppBuilderTests.cs @@ -303,6 +303,21 @@ public void TryApplyOptions_WhenAHandlerRejectsTheValue_ReportsItAsAUsageError() Assert.AreEqual("'lots' is not a number.", error); } + [TestMethod] + public void TryApplyOptions_WhenAHandlerParsesAnOutOfRangeNumber_ReportsItAsAUsageError() + { + // int.Parse throws OverflowException, not FormatException, for a number too big for an int, and + // that used to escape as an unhandled exception (#69). + TrayAppDefinition app = TrayAppBuilder.Create("demo") + .Option(["--count"], "n", "How many.", value => _ = int.Parse(value, System.Globalization.CultureInfo.InvariantCulture)) + .Build(); + + Assert.IsTrue(CommandLineParser.TryParse(["--count", "99999999999"], app.Options, out CommandLineOptions options, out _)); + Assert.IsFalse(TrayAppBuilder.TryApplyOptions(options, out string error)); + string expected = Assert.ThrowsExactly(() => int.Parse("99999999999", System.Globalization.CultureInfo.InvariantCulture)).Message; + Assert.AreEqual(expected, error); + } + [TestMethod] public async Task RunAsync_WithHelp_PrintsUsageAndSucceeds() { diff --git a/TrayApp/TrayAppBuilder.cs b/TrayApp/TrayAppBuilder.cs index 73cf5ca..70a876c 100644 --- a/TrayApp/TrayAppBuilder.cs +++ b/TrayApp/TrayAppBuilder.cs @@ -559,7 +559,7 @@ internal static bool TryApplyOptions(CommandLineOptions options, out string erro { match.Option.Apply(match.Value); } - catch (Exception ex) when (ex is FormatException or ArgumentException) + catch (Exception ex) when (ex is FormatException or ArgumentException or OverflowException) { error = ex.Message; return false;