Skip to content

Commit 19e2c95

Browse files
committed
Fix issue #409 to avoid IOException break in Debug mode in WPF app. (#589)
1 parent 3c4e726 commit 19e2c95

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

src/CommandLine/ParserSettings.cs

+17-6
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,29 @@ public ParserSettings()
3636
autoHelp = true;
3737
autoVersion = true;
3838
parsingCulture = CultureInfo.InvariantCulture;
39+
maximumDisplayWidth = GetWindowWidth();
40+
}
41+
42+
private int GetWindowWidth()
43+
{
44+
45+
#if !NET40
46+
if (Console.IsOutputRedirected) return DefaultMaximumLength;
47+
#endif
48+
var width = 1;
3949
try
4050
{
41-
maximumDisplayWidth = Console.WindowWidth;
42-
if (maximumDisplayWidth < 1)
51+
width = Console.WindowWidth;
52+
if (width < 1)
4353
{
44-
maximumDisplayWidth = DefaultMaximumLength;
54+
width = DefaultMaximumLength;
4555
}
46-
}
47-
catch (IOException)
56+
}
57+
catch (Exception e) when (e is IOException || e is PlatformNotSupportedException || e is ArgumentOutOfRangeException)
4858
{
49-
maximumDisplayWidth = DefaultMaximumLength;
59+
width = DefaultMaximumLength;
5060
}
61+
return width;
5162
}
5263

5364
/// <summary>

tests/CommandLine.Tests/Unit/ParserTests.cs

+12
Original file line numberDiff line numberDiff line change
@@ -892,5 +892,17 @@ public void Parse_default_verb_with_empty_name()
892892
Assert.True(args.TestValue);
893893
});
894894
}
895+
//Fix Issue #409 for WPF
896+
[Fact]
897+
public void When_HelpWriter_is_null_it_should_not_fire_exception()
898+
{
899+
// Arrange
900+
901+
//Act
902+
var sut = new Parser(config => config.HelpWriter = null);
903+
sut.ParseArguments<Simple_Options>(new[] {"--dummy"});
904+
//Assert
905+
sut.Settings.MaximumDisplayWidth.Should().Be(80);
906+
}
895907
}
896908
}

0 commit comments

Comments
 (0)